def test_main_running_sigterm(mocker, context, event_loop, running): """Test that sending SIGTERM causes the main loop to stop after the next call to async_main.""" run_tasks_cancelled = event_loop.create_future() class MockRunTasks: @staticmethod def cancel(): run_tasks_cancelled.set_result(True) async def async_main(internal_context, _): # scriptworker reads context from a file, so we have to modify the context given here instead of the variable # from the fixture if running: internal_context.running_tasks = MockRunTasks() # Send SIGTERM to ourselves so that we stop os.kill(os.getpid(), signal.SIGTERM) _, tmp = tempfile.mkstemp() try: with open(tmp, "w") as fh: json.dump(context.config, fh) mocker.patch.object(worker, 'async_main', new=async_main) mocker.patch.object(sys, 'argv', new=['x', tmp]) worker.main(event_loop=event_loop) finally: os.remove(tmp) if running: event_loop.run_until_complete(run_tasks_cancelled) assert run_tasks_cancelled.result()
def test_main_running_sigusr1(mocker, context, event_loop, running): """Test that sending SIGUSR1 causes the main loop to stop after the next call to async_main without cancelling the task.""" run_tasks_cancelled = event_loop.create_future() class MockRunTasks: @staticmethod def cancel(): run_tasks_cancelled.set_result(True) async def async_main(internal_context, _): # scriptworker reads context from a file, so we have to modify the # context given here instead of the variable from the fixture if running: internal_context.running_tasks = MockRunTasks() # Send SIGUSR1 to ourselves so that we stop os.kill(os.getpid(), signal.SIGUSR1) _, tmp = tempfile.mkstemp() try: with open(tmp, "w") as fh: json.dump(context.config, fh) mocker.patch.object(worker, 'async_main', new=async_main) mocker.patch.object(sys, 'argv', new=['x', tmp]) worker.main(event_loop=event_loop) finally: os.remove(tmp) assert not run_tasks_cancelled.done()
def test_main(mocker, context, event_loop): config = dict(context.config) config['poll_interval'] = 1 creds = {'fake_creds': True} config['credentials'] = deepcopy(creds) async def foo(arg, credentials): # arg.credentials will be a dict copy of a frozendict. assert credentials == dict(creds) raise ScriptWorkerException("foo") try: _, tmp = tempfile.mkstemp() with open(tmp, "w") as fh: json.dump(config, fh) del (config['credentials']) mocker.patch.object(worker, 'async_main', new=foo) mocker.patch.object(sys, 'argv', new=['x', tmp]) with pytest.raises(ScriptWorkerException): worker.main(event_loop=event_loop) finally: os.remove(tmp)
def test_main(mocker, context, event_loop): config = dict(context.config) config["poll_interval"] = 1 creds = {"fake_creds": True} config["credentials"] = deepcopy(creds) async def foo(arg, credentials): # arg.credentials will be a dict copy of a immutabledict. assert credentials == dict(creds) raise ScriptWorkerException("foo") try: _, tmp = tempfile.mkstemp() with open(tmp, "w") as fh: json.dump(config, fh) del config["credentials"] mocker.patch.object(worker, "async_main", new=foo) mocker.patch.object(sys, "argv", new=["x", tmp]) with pytest.raises(ScriptWorkerException): worker.main(event_loop=event_loop) finally: os.remove(tmp)
def test_main(mocker, context, event_loop): config = dict(context.config) config['poll_interval'] = 1 creds = {'fake_creds': True} config['credentials'] = deepcopy(creds) async def foo(arg, credentials): # arg.credentials will be a dict copy of a frozendict. assert credentials == dict(creds) raise ScriptWorkerException("foo") try: _, tmp = tempfile.mkstemp() with open(tmp, "w") as fh: json.dump(config, fh) del(config['credentials']) mocker.patch.object(worker, 'async_main', new=foo) mocker.patch.object(sys, 'argv', new=['x', tmp]) with pytest.raises(ScriptWorkerException): worker.main(event_loop=event_loop) finally: os.remove(tmp)
def test_main(self, mocker, event_loop): path = os.path.join(os.path.dirname(__file__), "data", "good.json") config, creds = create_config(path) loop = mock.MagicMock() exceptions = [RuntimeError, ScriptWorkerException] def run_forever(): exc = exceptions.pop(0) raise exc("foo") def foo(arg): assert arg.config == config assert arg.credentials == creds loop.run_forever = run_forever mocker.patch.object(sys, 'argv', new=[__file__, path]) mocker.patch.object(worker, 'async_main', new=foo) with mock.patch.object(asyncio, 'get_event_loop') as p: p.return_value = loop with pytest.raises(ScriptWorkerException): worker.main()
def test_main(mocker, context, event_loop): cot_path = os.path.join(os.path.dirname(__file__), "data", "cot_config.json") cot_schema_path = os.path.join(os.path.dirname(__file__), "data", "cot_config_schema.json") config = dict(context.config) config['poll_interval'] = 1 config['credential_update_interval'] = 1 config['cot_config_path'] = cot_path config['cot_config_schema_path'] = cot_schema_path creds = {'fake_creds': True} config['credentials'] = deepcopy(creds) loop = mock.MagicMock() exceptions = [RuntimeError, ScriptWorkerException] def run_forever(): exc = exceptions.pop(0) raise exc("foo") def foo(arg): # arg.config will be a frozendict. assert arg.config == frozendict(config) # arg.credentials will be a dict copy of a frozendict. assert arg.credentials == dict(creds) loop.run_forever = run_forever try: _, tmp = tempfile.mkstemp() with open(tmp, "w") as fh: json.dump(config, fh) del(config['credentials']) mocker.patch.object(worker, 'async_main', new=foo) mocker.patch.object(sys, 'argv', new=['x', tmp]) with mock.patch.object(asyncio, 'get_event_loop') as p: p.return_value = loop with pytest.raises(ScriptWorkerException): worker.main() finally: os.remove(tmp)
def test_main_sigterm(mocker, context, event_loop): """Test that sending SIGTERM causes the main loop to stop after the next call to async_main.""" config = dict(context.config) config['poll_interval'] = 1 creds = {'fake_creds': True} config['credentials'] = deepcopy(creds) async def async_main(*args): # Send SIGTERM to ourselves so that we stop os.kill(os.getpid(), signal.SIGTERM) return True try: _, tmp = tempfile.mkstemp() with open(tmp, "w") as fh: json.dump(config, fh) del (config['credentials']) mocker.patch.object(worker, 'async_main', new=async_main) mocker.patch.object(sys, 'argv', new=['x', tmp]) worker.main(event_loop=event_loop) finally: os.remove(tmp)
def test_main_bad_json(self, event_loop): path = os.path.join(os.path.dirname(__file__), "data", "bad.json") with pytest.raises(SystemExit): with mock.patch('sys.argv', new=[__file__, path]): worker.main()
def test_main_too_many_args(self, event_loop): with pytest.raises(SystemExit): with mock.patch('sys.argv', new=[1, 2, 3, 4]): worker.main()