2626
2727from prometheus_client import CollectorRegistry
2828
29- from faucet import gauge , gauge_prom , gauge_influx , gauge_pollers , watcher
29+ from faucet import gauge , gauge_prom , gauge_influx , gauge_pollers , watcher , valve_util
3030
3131
3232class QuietHandler (BaseHTTPRequestHandler ):
@@ -853,8 +853,15 @@ def test_flow_stats(self):
853853class RyuAppSmokeTest (unittest .TestCase ): # pytype: disable=module-attr
854854
855855 def setUp (self ):
856- os .environ ['GAUGE_LOG' ] = '/dev/null'
857- os .environ ['GAUGE_EXCEPTION_LOG' ] = '/dev/null'
856+ self .tmpdir = tempfile .mkdtemp ()
857+ os .environ ['GAUGE_LOG' ] = os .path .join (self .tmpdir , 'gauge.log' )
858+ os .environ ['GAUGE_EXCEPTION_LOG' ] = os .path .join (self .tmpdir , 'gauge-exception.log' )
859+ self .ryu_app = None
860+
861+ def tearDown (self ):
862+ valve_util .close_logger (self .ryu_app .logger )
863+ valve_util .close_logger (self .ryu_app .exc_logger )
864+ shutil .rmtree (self .tmpdir )
858865
859866 @staticmethod
860867 def _fake_dp ():
@@ -868,29 +875,28 @@ def _fake_event(self):
868875 event .dp = msg .datapath
869876 return event
870877
878+ def _write_config (self , config_file_name , config ):
879+ with open (config_file_name , 'w' ) as config_file :
880+ config_file .write (config )
881+
871882 def test_gauge (self ):
872883 """Test Gauge can be initialized."""
873884 os .environ ['GAUGE_CONFIG' ] = '/dev/null'
874- ryu_app = gauge .Gauge (
885+ self . ryu_app = gauge .Gauge (
875886 dpset = {},
876887 reg = CollectorRegistry ())
877- ryu_app .reload_config (None )
878- self .assertFalse (ryu_app ._config_files_changed ())
879- ryu_app ._update_watcher (None , self ._fake_event ())
880- ryu_app ._start_watchers (self ._fake_dp (), {}, time .time ())
888+ self . ryu_app .reload_config (None )
889+ self .assertFalse (self . ryu_app ._config_files_changed ())
890+ self . ryu_app ._update_watcher (None , self ._fake_event ())
891+ self . ryu_app ._start_watchers (self ._fake_dp (), {}, time .time ())
881892 for event_handler in (
882- ryu_app ._datapath_connect ,
883- ryu_app ._datapath_disconnect ):
893+ self . ryu_app ._datapath_connect ,
894+ self . ryu_app ._datapath_disconnect ):
884895 event_handler (self ._fake_event ())
885896
886897 def test_gauge_config (self ):
887898 """Test Gauge minimal config."""
888- tmpdir = tempfile .mkdtemp ()
889- os .environ ['FAUCET_CONFIG' ] = os .path .join (tmpdir , 'faucet.yaml' )
890- os .environ ['GAUGE_CONFIG' ] = os .path .join (tmpdir , 'gauge.yaml' )
891- with open (os .environ ['FAUCET_CONFIG' ], 'w' ) as faucet_config :
892- faucet_config .write (
893- """
899+ faucet_conf1 = """
894900vlans:
895901 100:
896902 description: "100"
@@ -901,11 +907,23 @@ def test_gauge_config(self):
901907 1:
902908 description: "1"
903909 native_vlan: 100
904- """ )
905- os .environ ['GAUGE_CONFIG' ] = os .path .join (tmpdir , 'gauge.yaml' )
906- with open (os .environ ['GAUGE_CONFIG' ], 'w' ) as gauge_config :
907- gauge_config .write (
908- """
910+ """
911+ faucet_conf2 = """
912+ vlans:
913+ 100:
914+ description: "200"
915+ dps:
916+ dp1:
917+ dp_id: 0x1
918+ interfaces:
919+ 2:
920+ description: "2"
921+ native_vlan: 100
922+ """
923+ os .environ ['FAUCET_CONFIG' ] = os .path .join (self .tmpdir , 'faucet.yaml' )
924+ self ._write_config (os .environ ['FAUCET_CONFIG' ], faucet_conf1 )
925+ os .environ ['GAUGE_CONFIG' ] = os .path .join (self .tmpdir , 'gauge.yaml' )
926+ gauge_conf = """
909927faucet_configs:
910928 - '%s'
911929watchers:
@@ -928,15 +946,36 @@ def test_gauge_config(self):
928946 type: 'prometheus'
929947 prometheus_addr: '0.0.0.0'
930948 prometheus_port: 0
931- """ % os .environ ['FAUCET_CONFIG' ])
932- ryu_app = gauge .Gauge (
949+ """ % os .environ ['FAUCET_CONFIG' ]
950+ self ._write_config (os .environ ['GAUGE_CONFIG' ], gauge_conf )
951+ self .ryu_app = gauge .Gauge (
933952 dpset = {},
934953 reg = CollectorRegistry ())
935- ryu_app .reload_config (None )
936- self .assertTrue (ryu_app .watchers )
937- ryu_app .reload_config (None )
938- self .assertTrue (ryu_app .watchers )
939- shutil .rmtree (tmpdir )
954+ self .ryu_app .reload_config (None )
955+ self .assertFalse (self .ryu_app ._config_files_changed ())
956+ self .assertTrue (self .ryu_app .watchers )
957+ self .ryu_app .reload_config (None )
958+ self .assertTrue (self .ryu_app .watchers )
959+ self .assertFalse (self .ryu_app ._config_files_changed ())
960+ # Load a new FAUCET config.
961+ self ._write_config (os .environ ['FAUCET_CONFIG' ], faucet_conf2 )
962+ self .assertTrue (self .ryu_app ._config_files_changed ())
963+ self .ryu_app .reload_config (None )
964+ self .assertTrue (self .ryu_app .watchers )
965+ self .assertFalse (self .ryu_app ._config_files_changed ())
966+ # Load an invalid Gauge config
967+ self ._write_config (os .environ ['GAUGE_CONFIG' ], 'invalid' )
968+ self .assertTrue (self .ryu_app ._config_files_changed ())
969+ self .ryu_app .reload_config (None )
970+ self .assertTrue (self .ryu_app .watchers )
971+ # Keep trying to load a valid version.
972+ self .assertTrue (self .ryu_app ._config_files_changed ())
973+ # Load good Gauge config back
974+ self ._write_config (os .environ ['GAUGE_CONFIG' ], gauge_conf )
975+ self .assertTrue (self .ryu_app ._config_files_changed ())
976+ self .ryu_app .reload_config (None )
977+ self .assertTrue (self .ryu_app .watchers )
978+ self .assertFalse (self .ryu_app ._config_files_changed ())
940979
941980
942981if __name__ == "__main__" :
0 commit comments