Skip to content

Commit 3624017

Browse files
committed
Test load/reload of invalid/valid config.
1 parent 2ddc420 commit 3624017

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

faucet/config_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ def watcher_parser(config_file, logname, prom_client):
252252
def _parse_dps_for_watchers(conf, logname, meta_dp_state=None):
253253
all_dps_list = []
254254
faucet_conf_hashes = {}
255+
256+
if not isinstance(conf, dict):
257+
raise InvalidConfigError('Gauge config not valid')
258+
255259
faucet_config_files = conf.get('faucet_configs', [])
256260
for faucet_config_file in faucet_config_files:
257261
conf_hashes, dp_list, _ = dp_parser(faucet_config_file, logname)

tests/unit/gauge/test_gauge.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,8 @@ def setUp(self):
860860

861861
def tearDown(self):
862862
valve_util.close_logger(self.ryu_app.logger)
863-
# shutil.rmtree(self.tmpdir)
863+
valve_util.close_logger(self.ryu_app.exc_logger)
864+
shutil.rmtree(self.tmpdir)
864865

865866
@staticmethod
866867
def _fake_dp():
@@ -922,8 +923,7 @@ def test_gauge_config(self):
922923
os.environ['FAUCET_CONFIG'] = os.path.join(self.tmpdir, 'faucet.yaml')
923924
self._write_config(os.environ['FAUCET_CONFIG'], faucet_conf1)
924925
os.environ['GAUGE_CONFIG'] = os.path.join(self.tmpdir, 'gauge.yaml')
925-
self._write_config(os.environ['GAUGE_CONFIG'],
926-
"""
926+
gauge_conf = """
927927
faucet_configs:
928928
- '%s'
929929
watchers:
@@ -946,7 +946,8 @@ def test_gauge_config(self):
946946
type: 'prometheus'
947947
prometheus_addr: '0.0.0.0'
948948
prometheus_port: 0
949-
""" % os.environ['FAUCET_CONFIG'])
949+
""" % os.environ['FAUCET_CONFIG']
950+
self._write_config(os.environ['GAUGE_CONFIG'], gauge_conf)
950951
self.ryu_app = gauge.Gauge(
951952
dpset={},
952953
reg=CollectorRegistry())
@@ -956,11 +957,25 @@ def test_gauge_config(self):
956957
self.ryu_app.reload_config(None)
957958
self.assertTrue(self.ryu_app.watchers)
958959
self.assertFalse(self.ryu_app._config_files_changed())
960+
# Load a new FAUCET config.
959961
self._write_config(os.environ['FAUCET_CONFIG'], faucet_conf2)
960962
self.assertTrue(self.ryu_app._config_files_changed())
961963
self.ryu_app.reload_config(None)
962964
self.assertTrue(self.ryu_app.watchers)
963965
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())
964979

965980

966981
if __name__ == "__main__":

0 commit comments

Comments
 (0)