def test_get_api_caches_hook_enabled_apis_separately(): with CLUSTERS.patch([TEST_CLUSTER]): context = AuroraCommandContext() hooked_api = context.get_api(TEST_CLUSTER.name) unhooked_api = context.get_api(TEST_CLUSTER.name, enable_hooks=False) assert hooked_api != unhooked_api assert hooked_api in context.apis.values() assert hooked_api not in context.unhooked_apis.values() assert unhooked_api in context.unhooked_apis.values() assert unhooked_api not in context.apis.values()
def test_get_api_caches_hook_enabled_apis_separately(): with CLUSTERS.patch([TEST_CLUSTER]): context = AuroraCommandContext() hooked_api = context.get_api(TEST_CLUSTER.name) unhooked_api = context.get_api(TEST_CLUSTER.name, enable_hooks=False) assert hooked_api != unhooked_api assert hooked_api in context.apis.values() assert hooked_api not in context.unhooked_apis.values() assert unhooked_api in context.unhooked_apis.values() assert unhooked_api not in context.apis.values()
def test_handles_api_auth_error(): context = AuroraCommandContext() mock_scheduler_proxy = mock.create_autospec(spec=SchedulerProxyApiSpec, instance=True) mock_scheduler_proxy.killTasks.side_effect = SchedulerProxy.AuthError() mock_api = AuroraClientAPI(TEST_CLUSTER, "user-agent") mock_api._scheduler_proxy = mock_scheduler_proxy context.apis = {TEST_CLUSTER.name: mock_api} api = context.get_api(TEST_CLUSTER.name, clusters={TEST_CLUSTER.name: TEST_CLUSTER}) with pytest.raises(Context.CommandError) as e: api.kill_job(AuroraJobKey(TEST_CLUSTER.name, "role", "env", "job")) assert e.value.code == EXIT_AUTH_ERROR assert mock_scheduler_proxy.killTasks.call_count == 1
def test_get_api_forwards_hooks_disabled(): with patch('apache.aurora.client.cli.context.make_client' ) as mock_make_client: cluster = 'some-cluster' AuroraCommandContext().get_api(cluster, enable_hooks=False) assert mock_make_client.mock_calls == [ call(cluster, AURORA_V2_USER_AGENT_NAME, False) ]
def test_get_api_defaults_to_hooks_enabled(): with patch('apache.aurora.client.cli.context.make_client' ) as mock_make_client: cluster = 'some-cluster' AuroraCommandContext().get_api(cluster) assert mock_make_client.mock_calls == [ call(cluster, AURORA_V2_USER_AGENT_NAME, True) ]
def test_handles_api_auth_error(): context = AuroraCommandContext() mock_scheduler_proxy = mock.create_autospec(spec=SchedulerProxyApiSpec, instance=True) mock_scheduler_proxy.killTasks.side_effect = SchedulerProxy.AuthError() mock_api = AuroraClientAPI(TEST_CLUSTER, 'user-agent') mock_api._scheduler_proxy = mock_scheduler_proxy context.apis = {TEST_CLUSTER.name: mock_api} api = context.get_api(TEST_CLUSTER.name, clusters={TEST_CLUSTER.name: TEST_CLUSTER}) with pytest.raises(Context.CommandError) as e: api.kill_job(AuroraJobKey(TEST_CLUSTER.name, 'role', 'env', 'job')) assert e.value.code == EXIT_AUTH_ERROR assert mock_scheduler_proxy.killTasks.call_count == 1
def test_inspect_job(self): (mock_api, mock_scheduler_proxy) = self.create_mock_api() AuroraCommandContext.enable_reveal_errors() mock_transcript = [] def mock_print_out(msg, indent=0): indent_str = " " * indent mock_transcript.append("%s%s" % (indent_str, msg)) with contextlib.nested( patch('threading._Event.wait'), patch('apache.aurora.client.api.SchedulerProxy', return_value=mock_scheduler_proxy), patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS), patch('apache.aurora.client.cli.context.AuroraCommandContext.print_out', side_effect=mock_print_out), patch('apache.aurora.client.cli.context.get_config', return_value=self.get_mock_config())): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() cmd = AuroraCommandLine() result = cmd.execute(['job', 'inspect', '--reveal-errors', 'west/bozo/test/hello', fp.name]) # inspect command should run without errors, and return 0. assert result == 0 # The command output for the mock should look right. print(mock_transcript) assert mock_transcript == [ "Job level information", " name: 'the_job'", " role: 'bozo'", " contact: '*****@*****.**'", " cluster: 'west'", " instances: '3'", " cron:", " schedule: '* * * * *'", " policy: '0'", " service: False", " production: False", "", "Task level information", " name: 'task'", "", "Process 'process':", " cmdline:", " ls -la", ""]
def test_inspect_job(self): (mock_api, mock_scheduler_proxy) = self.create_mock_api() AuroraCommandContext.enable_reveal_errors() mock_transcript = [] def mock_print_out(msg, indent=0): indent_str = " " * indent mock_transcript.append("%s%s" % (indent_str, msg)) with contextlib.nested( patch('threading._Event.wait'), patch('apache.aurora.client.api.SchedulerProxy', return_value=mock_scheduler_proxy), patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS), patch( 'apache.aurora.client.cli.context.AuroraCommandContext.print_out', side_effect=mock_print_out), patch('apache.aurora.client.cli.context.get_config', return_value=self.get_mock_config())): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() cmd = AuroraCommandLine() result = cmd.execute([ 'job', 'inspect', '--reveal-errors', 'west/bozo/test/hello', fp.name ]) # inspect command should run without errors, and return 0. assert result == 0 # The command output for the mock should look right. print(mock_transcript) assert mock_transcript == [ "Job level information", " name: 'the_job'", " role: 'bozo'", " contact: '*****@*****.**'", " cluster: 'west'", " instances: '3'", " cron:", " schedule: '* * * * *'", " policy: '0'", " service: False", " production: False", "", "Task level information", " name: 'task'", "", "Process 'process':", " cmdline:", " ls -la", "" ]
def test_get_api_caches_hook_enabled_apis_separately(): with patch('apache.aurora.client.cli.context.make_client' ) as mock_make_client: # return a new Mock instance for each call of the two calls we're expecting. mock_make_client.side_effect = [Mock(), Mock()] cluster = 'some-cluster' context = AuroraCommandContext() hooked_api = context.get_api(cluster) unhooked_api = context.get_api(cluster, False) assert mock_make_client.mock_calls == [ call(cluster, AURORA_V2_USER_AGENT_NAME, True), call(cluster, AURORA_V2_USER_AGENT_NAME, False) ] assert hooked_api != unhooked_api assert hooked_api in context.apis.values() assert hooked_api not in context.unhooked_apis.values() assert unhooked_api in context.unhooked_apis.values() assert unhooked_api not in context.apis.values()
def create_context(cls): return AuroraCommandContext()
def arg_type_jobkey(key): return AuroraCommandContext.parse_partial_jobkey(key)
def test_get_api_forwards_hooks_disabled(): with CLUSTERS.patch([TEST_CLUSTER]): api = AuroraCommandContext().get_api(TEST_CLUSTER.name, enable_hooks=False) assert isinstance(api, AuroraClientAPI) assert api._cluster == TEST_CLUSTER
def test_get_api_defaults_to_hooks_enabled(): with CLUSTERS.patch([TEST_CLUSTER]): api = AuroraCommandContext().get_api(TEST_CLUSTER.name) assert isinstance(api, HookedAuroraClientAPI) assert api._cluster == TEST_CLUSTER
def arg_type_jobkey(key): return AuroraCommandContext.parse_partial_jobkey(key)