diff --git a/brian_polling_manager/cli.py b/brian_polling_manager/cli.py index e9015a357db3a3362763571069fef1850697b281..a3a19129bac3148062e0ef9e7369bd40c02b3443 100644 --- a/brian_polling_manager/cli.py +++ b/brian_polling_manager/cli.py @@ -1,6 +1,7 @@ import json import logging import os +from typing import Union import click import jsonschema @@ -131,10 +132,13 @@ class State(object): return state['last'] if state else -1 @last.setter - def last(self, new_last: float): - state = {'last': new_last} - with open(self.filenames['state'], 'w') as f: - f.write(json.dumps(state)) + def last(self, new_last: Union[float, None]): + if not new_last or new_last < 0: + os.unlink(self.filenames['state']) + else: + state = {'last': new_last} + with open(self.filenames['state'], 'w') as f: + f.write(json.dumps(state)) @property def interfaces(self) -> list: @@ -178,8 +182,6 @@ def _validate_config(ctx, param, value): return config - - @click.command() @click.option( '--config', @@ -195,7 +197,7 @@ def main(config, force): state = State(config['statedir']) last = inventory.last_update_timestamp(config['inventory']) - if force or last != state.last: + if force or not last or last != state.last: state.last = last state.interfaces = inventory.load_interfaces(config['inventory']) diff --git a/test/conftest.py b/test/conftest.py index 16af34ebc188039e288471410610ddd7a4d83bb7..ea94ef8e3d16ea45a570671607994f04636a94cb 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,5 +1,6 @@ import json import os +import random import re import tempfile @@ -87,7 +88,7 @@ def mocked_sensu(): responses.add( method=responses.GET, url=re.compile(r'.*sensu.+/api/core/v2/namespaces/[^\/]+/checks$'), - body=json.dumps(list(saved_sensu_checks.items())) + json=list(saved_sensu_checks.values()) ) def new_check_callback(request): @@ -138,6 +139,7 @@ def mocked_sensu(): yield saved_sensu_checks + @pytest.fixture def mocked_inventory(): @@ -147,3 +149,9 @@ def mocked_inventory(): url=re.compile(r'.*inventory.+/poller/interfaces.*'), body=_load_test_data('interfaces.json')) + bogus_version = {'latch': {'timestamp': 10000 * random.random()}} + # mocked api for returning all checks + responses.add( + method=responses.GET, + url=re.compile(r'.*inventory.+/version.*'), + body=json.dumps(bogus_version)) diff --git a/test/test_e2e.py b/test/test_e2e.py new file mode 100644 index 0000000000000000000000000000000000000000..61d48375e9cd69f2a74ab9e99fbfc6f27c61f4b9 --- /dev/null +++ b/test/test_e2e.py @@ -0,0 +1,22 @@ +import json +import tempfile + +from click.testing import CliRunner +import responses + +from brian_polling_manager import cli + + +@responses.activate +def test_run_flashtest(config, mocked_sensu, mocked_inventory): + + with tempfile.NamedTemporaryFile(mode='w') as f: + f.write(json.dumps(config)) + f.flush() + + runner = CliRunner() + result = runner.invoke( + cli.main, + ['--config', f.name, '--force'] + ) + assert result.exit_code == 0