コード例 #1
0
    def test_status_wildcard(self):
        """Test status using a wildcard. It should first call api.get_jobs, and then do a
    getTasksWithoutConfigs on each job."""
        mock_context = FakeAuroraCommandContext()
        mock_api = mock_context.get_api('west')
        mock_api.check_status.return_value = self.create_status_response()
        mock_api.get_jobs.return_value = self.create_getjobs_response()
        with contextlib.nested(
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.cli.context.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            cmd = AuroraCommandLine()
            cmd.execute(['job', 'status', '*'])

        # Wildcard should have expanded to two jobs, so there should be two calls
        # to check_status.
        assert mock_api.check_status.call_count == 2

        assert mock_api.check_status.call_args_list[0][0][0].cluster == 'west'
        assert mock_api.check_status.call_args_list[0][0][0].role == 'RoleA'
        assert mock_api.check_status.call_args_list[0][0][0].env == 'test'
        assert mock_api.check_status.call_args_list[0][0][0].name == 'hithere'

        assert mock_api.check_status.call_args_list[1][0][0].cluster == 'west'
        assert mock_api.check_status.call_args_list[1][0][0].role == 'bozo'
        assert mock_api.check_status.call_args_list[1][0][0].env == 'test'
        assert mock_api.check_status.call_args_list[1][0][0].name == 'hello'
コード例 #2
0
 def test_successful_status_output_no_metadata(self):
     """Test the status command more deeply: in a request with a fully specified
 job, it should end up doing a query using getTasksWithoutConfigs."""
     mock_context = FakeAuroraCommandContext()
     mock_context.add_expected_status_query_result(
         self.create_status_null_metadata())
     with contextlib.nested(
             patch('apache.aurora.client.cli.jobs.Job.create_context',
                   return_value=mock_context),
             patch('apache.aurora.client.factory.CLUSTERS',
                   new=self.TEST_CLUSTERS)):
         cmd = AuroraCommandLine()
         cmd.execute(['job', 'status', 'west/bozo/test/hello'])
         actual = re.sub("\\d\\d:\\d\\d:\\d\\d", "##:##:##",
                         '\n'.join(mock_context.get_out()))
         expected = textwrap.dedent("""\
       Active tasks (3):
       \tTask:
       \t  cpus: 2, ram: 2 MB, disk: 2 MB
       \t  events:
       \t   1970-11-23 ##:##:## RUNNING: Hi there
       \tTask:
       \t  cpus: 2, ram: 2 MB, disk: 2 MB
       \t  events:
       \t   1970-11-23 ##:##:## RUNNING: Hi there
       \tTask:
       \t  cpus: 2, ram: 2 MB, disk: 2 MB
       \t  events:
       \t   1970-11-23 ##:##:## RUNNING: Hi there
       Inactive tasks (0):
       """)
         assert actual == expected
コード例 #3
0
    def test_kill_job_with_invalid_instances_strict(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('threading._Event.wait'),
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            api = mock_context.get_api('west')
            self.setup_get_tasks_status_calls(api.scheduler_proxy)
            api.kill_job.return_value = self.get_kill_job_response()
            mock_context.add_expected_status_query_result(
                self.create_status_call_result(
                    self.create_mock_task(ScheduleStatus.KILLED)))
            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'kill',
                    '--config=%s' % fp.name, '--no-batching', '--strict',
                    'west/bozo/test/hello/0,2,4-6,11-20'
                ])

            # Now check that the right API calls got made.
            assert api.kill_job.call_count == 0
コード例 #4
0
    def test_empty_plugins_in_create_job(self):
        """Installs a plugin that doesn't implement any of the plugin methods.
    Prior to AURORA-362, this would cause the client to crash with an empty
    argument list.
    """
        mock_context = FakeAuroraCommandContext()
        with patch('apache.aurora.client.cli.jobs.Job.create_context',
                   return_value=mock_context):
            mock_query = self.create_mock_query()
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.INIT))
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.RUNNING))
            api = mock_context.get_api('west')
            api.create_job.return_value = self.get_createjob_response()

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.register_plugin(EmptyPlugin())
                cmd.execute([
                    'job', 'create', '--wait-until=RUNNING',
                    'west/bozo/test/hello', fp.name
                ])
            self.assert_create_job_called(api)
            self.assert_scheduler_called(api, mock_query, 1)
コード例 #5
0
    def test_create_job_with_failed_hook(self):
        GlobalCommandHookRegistry.reset()
        command_hook = HookForTesting(False)
        GlobalCommandHookRegistry.register_command_hook(command_hook)
        mock_context = FakeAuroraCommandContext()
        with patch("apache.aurora.client.cli.jobs.Job.create_context",
                   return_value=mock_context):
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.INIT))
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.RUNNING))
            api = mock_context.get_api("west")
            api.create_job.return_value = self.get_createjob_response()

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                result = cmd.execute([
                    "job", "create", "--wait-until=RUNNING",
                    "west/bozo/test/hello", fp.name
                ])

                assert result == 1
                assert api.create_job.call_count == 0
                assert command_hook.ran_pre
                assert not command_hook.ran_post
コード例 #6
0
    def generic_test_successful_hook(self, command_hook):
        mock_context = FakeAuroraCommandContext()
        with patch("apache.aurora.client.cli.jobs.Job.create_context",
                   return_value=mock_context):
            mock_query = self.create_mock_query()
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.INIT))
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.RUNNING))
            api = mock_context.get_api("west")
            api.create_job.return_value = self.get_createjob_response()

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    "job", "create", "--wait-until=RUNNING",
                    "west/bozo/test/hello", fp.name
                ])

            self.assert_create_job_called(api)
            self.assert_scheduler_called(api, mock_query, 1)
            assert command_hook.ran_pre
            assert command_hook.ran_post
コード例 #7
0
    def test_kill_job_with_instances_batched_maxerrors_output(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            api = mock_context.get_api('west')
            status_result = self.create_status_call_result()
            mock_context.add_expected_status_query_result(status_result)
            api.kill_job.return_value = self.create_error_response()

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'kill', '--max-total-failures=1',
                    '--config=%s' % fp.name, 'west/bozo/test/hello/0,2,4-13'
                ])

            assert mock_context.get_out() == []
            print(mock_context.get_err())
            assert mock_context.get_err() == [
                'Kill of shards [0, 2, 4, 5, 6] failed with error; see log for details',
                'Kill of shards [7, 8, 9, 10, 11] failed with error; see log for details',
                'Exceeded maximum number of errors while killing instances'
            ]
コード例 #8
0
    def test_kill_job_with_instances_batched_maxerrors(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            api = mock_context.get_api('west')
            status_result = self.create_status_call_result()
            mock_context.add_expected_status_query_result(status_result)
            api.kill_job.return_value = self.create_error_response()

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'kill', '--max-total-failures=1',
                    '--config=%s' % fp.name, 'west/bozo/test/hello/0,2,4-13'
                ])

            # Now check that the right API calls got made. We should have aborted after the second batch.
            assert api.kill_job.call_count == 2
            assert api.scheduler_proxy.getTasksWithoutConfigs.call_count == 0
コード例 #9
0
    def test_killall_job_output(self):
        """Test kill output."""
        mock_context = FakeAuroraCommandContext()
        mock_scheduler_proxy = Mock()
        with contextlib.nested(
                patch('threading._Event.wait'),
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):

            api = mock_context.get_api('west')
            mock_scheduler_proxy.getTasksWithoutConfigs.return_value = self.create_status_call_result(
            )
            api.kill_job.return_value = self.get_kill_job_response()
            mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response(
            )
            mock_context.add_expected_status_query_result(
                self.create_status_call_result(
                    self.create_mock_task(ScheduleStatus.KILLED)))
            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'killall', '--no-batching',
                    '--config=%s' % fp.name, 'west/bozo/test/hello'
                ])
            assert mock_context.get_out() == ['job killall succeeded']
            assert mock_context.get_err() == []
コード例 #10
0
    def test_kill_job_with_instances_batched_output(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('threading._Event.wait'),
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            api = mock_context.get_api('west')
            status_result = self.create_status_call_result()
            mock_context.add_expected_status_query_result(status_result)
            api.kill_job.return_value = self.get_kill_job_response()
            mock_context.add_expected_status_query_result(
                self.create_status_call_result(
                    self.create_mock_task(ScheduleStatus.KILLED)))

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'kill',
                    '--config=%s' % fp.name, 'west/bozo/test/hello/0,2,4-6'
                ])

        assert mock_context.get_out() == [
            'Successfully killed shards [0, 2, 4, 5, 6]', 'job kill succeeded'
        ]
        assert mock_context.get_err() == []
コード例 #11
0
    def test_killall_job(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        mock_scheduler_proxy = Mock()
        with contextlib.nested(
                patch('threading._Event.wait'),
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):

            api = mock_context.get_api('west')
            mock_scheduler_proxy.getTasksWithoutConfigs.return_value = self.create_status_call_result(
            )
            api.kill_job.return_value = self.get_kill_job_response()
            mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response(
            )
            mock_context.add_expected_status_query_result(
                self.create_status_call_result(
                    self.create_mock_task(ScheduleStatus.KILLED)))
            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'killall', '--no-batching',
                    '--config=%s' % fp.name, 'west/bozo/test/hello'
                ])

            # Now check that the right API calls got made.
            assert api.kill_job.call_count == 1
            api.kill_job.assert_called_with(
                AuroraJobKey.from_path('west/bozo/test/hello'), None)
            self.assert_scheduler_called(api, self.get_expected_task_query(),
                                         2)
コード例 #12
0
    def test_kill_job_with_empty_instances_batched(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('threading._Event.wait'),
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            api = mock_context.get_api('west')
            # set up an empty instance list in the getTasksWithoutConfigs response
            status_response = self.create_simple_success_response()
            schedule_status = Mock(spec=ScheduleStatusResult)
            status_response.result.scheduleStatusResult = schedule_status
            schedule_status.tasks = []
            mock_context.add_expected_status_query_result(status_response)
            api.kill_job.return_value = self.get_kill_job_response()
            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'kill',
                    '--config=%s' % fp.name, 'west/bozo/test/hello/0,2,4-13'
                ])

            # Now check that the right API calls got made.
            assert api.kill_job.call_count == 0
コード例 #13
0
    def test_kill_job_with_instances_batched_large(self):
        """Test kill client-side API logic."""
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('threading._Event.wait'),
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            api = mock_context.get_api('west')
            status_result = self.create_status_call_result()
            mock_context.add_expected_status_query_result(status_result)
            api.kill_job.return_value = self.get_kill_job_response()
            mock_context.add_expected_status_query_result(
                self.create_status_call_result(
                    self.create_mock_task(ScheduleStatus.KILLED)))

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute([
                    'job', 'kill',
                    '--config=%s' % fp.name, 'west/bozo/test/hello/0,2,4-13'
                ])

            # Now check that the right API calls got made.
            assert api.kill_job.call_count == 3
            api.kill_job.assert_called_with(
                AuroraJobKey.from_path('west/bozo/test/hello'), [12, 13])
            # Expect total 5 calls (3 from JobMonitor).
            self.assert_scheduler_called(
                api, self.get_expected_task_query([12, 13]), 5)
コード例 #14
0
    def test_start_update_command_line_succeeds(self):
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('apache.aurora.client.cli.update.Update.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            mock_api = mock_context.get_api('west')
            mock_api.start_job_update.return_value = self.create_simple_success_response(
            )
            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                result = cmd.execute(
                    ['update', 'start', self.TEST_JOBSPEC, fp.name])
                assert result == EXIT_OK

            assert mock_api.start_job_update.call_count == 1
            args, kwargs = mock_api.start_job_update.call_args
            assert isinstance(args[0], AuroraConfig)
            assert args[1] is None
            assert mock_context.get_out() == [
                "Scheduler-driven update of job west/bozo/test/hello has started."
            ]
            assert mock_context.get_err() == []
コード例 #15
0
ファイル: test_open.py プロジェクト: bhuvan/incubator-aurora
 def test_open_noparam(self):
     mock_context = FakeAuroraCommandContext()
     with patch('apache.aurora.client.cli.jobs.Job.create_context',
                return_value=mock_context):
         cmd = AuroraCommandLine()
         self.assertRaises(SystemExit, cmd.execute, (['job', 'open']))
         assert mock_context.showed_urls == []
コード例 #16
0
  def test_simple_successful_create_job_with_bindings(self):
    """Run a test of the "create" command against a mocked-out API:
    Verifies that the creation command sends the right API RPCs, and performs the correct
    tests on the result."""

    mock_context = FakeAuroraCommandContext()
    with contextlib.nested(
        patch('threading._Event.wait'),
        patch('apache.aurora.client.cli.jobs.Job.create_context', return_value=mock_context)):
      mock_query = self.create_mock_query()
      mock_context.add_expected_status_query_result(
        self.create_mock_status_query_result(ScheduleStatus.PENDING))
      mock_context.add_expected_status_query_result(
        self.create_mock_status_query_result(ScheduleStatus.RUNNING))
      api = mock_context.get_api('west')
      api.create_job.return_value = self.get_createjob_response()
      mock_context.enable_reveal_errors()

      # This is the real test: invoke create as if it had been called by the command line.
      with temporary_file() as fp:
        fp.write(self.get_unbound_test_config())
        fp.flush()
        cmd = AuroraCommandLine()
        cmd.execute(['job', 'create', '--wait-until=RUNNING', '--bind', 'cluster_binding=west',
            '--bind', 'instances_binding=20', 'west/bozo/test/hello',
            fp.name])

      # Now check that the right API calls got made.
      # Check that create_job was called exactly once, with an AuroraConfig parameter.
      self.assert_create_job_called(api)
      self.assert_scheduler_called(api, mock_query, 2)
コード例 #17
0
    def test_skip_hooks_in_create(self):
        """Run a test of create, with a hook that should forbid running create, but
    with a user who's covered by one of the hook skip exception rules - so it
    should succeed.
    """

        GlobalCommandHookRegistry.reset()
        command_hook = HookForTesting(True)
        GlobalCommandHookRegistry.register_command_hook(command_hook)
        command_hook_two = SecondHookForTesting(False)
        GlobalCommandHookRegistry.register_command_hook(command_hook_two)
        mock_response = Mock()
        mock_context = FakeAuroraCommandContext()

        with contextlib.nested(
                patch("apache.aurora.client.cli.jobs.Job.create_context",
                      return_value=mock_context),
                patch("requests.get", return_value=mock_response),
                patch("getpass.getuser", return_value="bozo")):
            mock_response.json.return_value = {
                "a": {
                    "users": ["bozo", "clown"],
                    "commands": {
                        "job": ["killall", "create"]
                    },
                    "hooks": ["test_hook", "second"]
                },
                "b": {
                    "commands": {
                        "user": ["kick"]
                    },
                }
            }

            mock_query = self.create_mock_query()
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.INIT))
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.RUNNING))
            api = mock_context.get_api("west")
            api.create_job.return_value = self.get_createjob_response()
            GlobalCommandHookRegistry.setup("http://foo.bar")

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                result = cmd.execute([
                    "job", "create", "--skip-hooks=second",
                    "--wait-until=RUNNING", "west/bozo/test/hello", fp.name
                ])
                assert result == 0
                self.assert_create_job_called(api)
                self.assert_scheduler_called(api, mock_query, 1)
                assert command_hook.ran_pre
                assert command_hook.ran_post
コード例 #18
0
ファイル: test_open.py プロジェクト: bhuvan/incubator-aurora
 def test_open_job(self):
     mock_context = FakeAuroraCommandContext()
     with patch('apache.aurora.client.cli.jobs.Job.create_context',
                return_value=mock_context):
         cmd = AuroraCommandLine()
         result = cmd.execute(['job', 'open', 'west/bozo/devel/foo'])
         assert mock_context.showed_urls == [
             'http://something_or_other/scheduler/bozo/devel/foo'
         ]
         assert result == EXIT_OK
コード例 #19
0
 def test_list_configs_nojobs(self):
     mock_context = FakeAuroraCommandContext()
     with patch('apache.aurora.client.cli.config.ConfigNoun.create_context',
                return_value=mock_context):
         with temporary_file() as fp:
             fp.write(self.get_config_with_no_jobs())
             fp.flush()
             cmd = AuroraCommandLine()
             cmd.execute(['config', 'list', fp.name])
             assert mock_context.out == ["jobs=[]"]
             assert mock_context.err == []
コード例 #20
0
    def test_status_job_not_found(self):
        """Regression test: there was a crasher bug when metadata was None."""

        mock_context = FakeAuroraCommandContext()
        mock_api = mock_context.get_api('west')
        mock_api.check_status.return_value = self.create_empty_status()
        with contextlib.nested(
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context)):
            cmd = AuroraCommandLine()
            result = cmd.execute(['job', 'status', 'west/bozo/test/hello'])
            assert result == EXIT_INVALID_PARAMETER
            assert mock_context.get_err() == ["No matching jobs found"]
コード例 #21
0
    def test_successful_status_shallow_nometadata(self):
        """Regression test: there was a crasher bug when metadata was None."""

        mock_context = FakeAuroraCommandContext()
        mock_api = mock_context.get_api('west')
        mock_api.check_status.return_value = self.create_status_null_metadata()
        with contextlib.nested(
                patch('apache.aurora.client.cli.jobs.Job.create_context',
                      return_value=mock_context)):
            cmd = AuroraCommandLine()
            cmd.execute(['job', 'status', 'west/bozo/test/hello'])
            mock_api.check_status.assert_called_with(
                AuroraJobKey('west', 'bozo', 'test', 'hello'))
コード例 #22
0
 def test_simple_successful_cancel_update(self):
     """Run a test of the "kill" command against a mocked-out API:
 Verifies that the kill command sends the right API RPCs, and performs the correct
 tests on the result."""
     mock_context = FakeAuroraCommandContext()
     mock_api = mock_context.get_api('west')
     mock_api.cancel_update.return_value = self.create_simple_success_response(
     )
     with patch('apache.aurora.client.cli.jobs.Job.create_context',
                return_value=mock_context):
         cmd = AuroraCommandLine()
         cmd.execute(['job', 'cancel-update', 'west/bozo/test/hello'])
         self.assert_cancel_update_called(mock_api)
コード例 #23
0
 def test_successful_status_shallow(self):
     """Test the status command at the shallowest level: calling status should end up invoking
 the local APIs get_status method."""
     mock_context = FakeAuroraCommandContext()
     mock_api = mock_context.get_api('west')
     mock_api.check_status.return_value = self.create_status_response()
     with contextlib.nested(
             patch('apache.aurora.client.cli.jobs.Job.create_context',
                   return_value=mock_context)):
         cmd = AuroraCommandLine()
         cmd.execute(['job', 'status', 'west/bozo/test/hello'])
         mock_api.check_status.assert_called_with(
             AuroraJobKey('west', 'bozo', 'test', 'hello'))
コード例 #24
0
    def test_cannot_skip_hooks_in_create(self):
        """This time, the hook shouldn't be skippable, because we use a username
    who isn't allowed by the hook exception rule.
    """
        GlobalCommandHookRegistry.reset()
        command_hook = HookForTesting(True)
        GlobalCommandHookRegistry.register_command_hook(command_hook)
        command_hook_two = SecondHookForTesting(False)
        GlobalCommandHookRegistry.register_command_hook(command_hook_two)
        mock_response = Mock()
        mock_context = FakeAuroraCommandContext()

        with contextlib.nested(
                patch("apache.aurora.client.cli.jobs.Job.create_context",
                      return_value=mock_context),
                patch("requests.get", return_value=mock_response),
                patch("getpass.getuser", return_value="beezus")):
            mock_response.json.return_value = {
                "a": {
                    "users": ["bozo", "clown"],
                    "commands": {
                        "job": ["killall", "create"]
                    },
                    "hooks": ["test_hook", "second"]
                },
                "b": {
                    "commands": {
                        "user": ["kick"]
                    },
                }
            }

            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.INIT))
            mock_context.add_expected_status_query_result(
                self.create_mock_status_query_result(ScheduleStatus.RUNNING))
            api = mock_context.get_api("west")
            api.create_job.return_value = self.get_createjob_response()
            GlobalCommandHookRegistry.setup("http://foo.bar")

            with temporary_file() as fp:
                fp.write(self.get_valid_config())
                fp.flush()
                cmd = AuroraCommandLine()
                result = cmd.execute([
                    "job", "create", "--skip-hooks=second",
                    "--wait-until=RUNNING", "west/bozo/test/hello", fp.name
                ])
                # Check that it returns the right error code, and that create_job didn't get called.
                assert result == EXIT_PERMISSION_VIOLATION
                assert api.create_job.call_count == 0
コード例 #25
0
 def test_unsuccessful_status_shallow(self):
     """Test the status command at the shallowest level: calling status should end up invoking
 the local APIs get_status method."""
     # Calls api.check_status, which calls scheduler_proxy.getJobs
     mock_context = FakeAuroraCommandContext()
     mock_api = mock_context.get_api('west')
     mock_api.check_status.return_value = self.create_failed_status_response(
     )
     with contextlib.nested(
             patch('apache.aurora.client.cli.jobs.Job.create_context',
                   return_value=mock_context)):
         cmd = AuroraCommandLine()
         result = cmd.execute(['job', 'status', 'west/bozo/test/hello'])
         assert result == EXIT_INVALID_PARAMETER
コード例 #26
0
 def test_list_configs_invalid(self):
     mock_context = FakeAuroraCommandContext()
     with patch('apache.aurora.client.cli.config.ConfigNoun.create_context',
                return_value=mock_context):
         with temporary_file() as fp:
             fp.write(self.get_invalid_config("blather=..."))
             fp.flush()
             cmd = AuroraCommandLine()
             result = cmd.execute(['config', 'list', fp.name])
             assert result == EXIT_COMMAND_FAILURE
             assert mock_context.out == []
             assert any(
                 line.startswith(
                     "Error loading configuration file: invalid syntax")
                 for line in mock_context.err)
コード例 #27
0
 def test_json_skip_rules(self):
     """Load up a set of skips, specified in JSON, and then check a bunch of different
 cases to see that the skip rules work correctly.
 """
     mock_response = Mock()
     mock_context = FakeAuroraCommandContext()
     with patch("requests.get", return_value=mock_response):
         mock_response.json.return_value = {
             "a": {
                 "users": ["bozo", "clown"],
                 "commands": {
                     "job": ["killall"]
                 },
                 "hooks": ["test_hook", "second"]
             },
             "b": {
                 "commands": {
                     "user": ["kick"]
                 },
             }
         }
         GlobalCommandHookRegistry.reset()
         GlobalCommandHookRegistry.setup("http://foo.bar")
         command_hook_one = HookForTesting(False)
         GlobalCommandHookRegistry.register_command_hook(command_hook_one)
         command_hook_two = SecondHookForTesting(True)
         GlobalCommandHookRegistry.register_command_hook(command_hook_two)
         assert len(GlobalCommandHookRegistry.SKIP_HOOK_RULES) == 2
         # Should not be allowed: no skip rule permitting skipping hooks on job kill
         self.assert_skip_forbidden(mock_context, "all", "beezus", "job",
                                    "kill", ["a", "b", "c"])
         # Should not be allowed: there are hooks on "job killall", and beezus doesn't satisfy
         # their exception rules
         self.assert_skip_forbidden(mock_context, "all", "beezus", "job",
                                    "kill", ["a", "b", "c"])
         # Should be allowed: there's a rule allowing bozo to skip test_hook on job killall.
         self.assert_skip_allowed(mock_context, "test_hook", "bozo", "job",
                                  "killall", ["a", "b", "c"])
         # Should be allowed: since there's only one hook on killall, this is equivalent to
         # the previous.
         self.assert_skip_allowed(mock_context, "all", "bozo", "job",
                                  "killall", ["a", "b", "c"])
         # Should be allowed: there's a rule allowing anyone to skip any hook on "user kick".
         self.assert_skip_allowed(mock_context, "test_hook,something_else",
                                  "nobody", "user", "kick", ["a", "b", "c"])
         # should be allowed: there are no hooks in place for "user kick", so all is acceptable.
         self.assert_skip_allowed(mock_context, "all", "nobody", "user",
                                  "kick", ["a", "b", "c"])
コード例 #28
0
 def test_unknown_error(self):
   mock_context = FakeAuroraCommandContext(reveal=False)
   with contextlib.nested(
       patch('time.sleep'),
       patch('apache.aurora.client.cli.jobs.Job.create_context',
           side_effect=Exception("Argh"))):
     api = mock_context.get_api('west')
     api.create_job.return_value = self.get_createjob_response()
     with temporary_file() as fp:
       fp.write(self.get_valid_config())
       fp.flush()
       cmd = AuroraCommandLine()
       result = cmd.execute(['job', 'create', '--wait-until=RUNNING', 'west/bozo/test/hello',
           fp.name])
       assert result == EXIT_UNKNOWN_ERROR
       assert api.create_job.call_count == 0
コード例 #29
0
ファイル: test_quota.py プロジェクト: bhuvan/incubator-aurora
    def _get_quota(self, include_consumption, command_args):
        mock_context = FakeAuroraCommandContext()
        if include_consumption:
            self.setup_mock_quota_call_with_consumption(mock_context)
        else:
            self.setup_mock_quota_call_no_consumption(mock_context)

        with contextlib.nested(
                patch('apache.aurora.client.cli.quota.Quota.create_context',
                      return_value=mock_context),
                patch('apache.aurora.client.factory.CLUSTERS',
                      new=self.TEST_CLUSTERS)):
            cmd = AuroraCommandLine()
            cmd.execute(command_args)
            out = '\n'.join(mock_context.get_out())
            return out
コード例 #30
0
    def test_schedule_cron_failed_invalid_config(self):
        mock_context = FakeAuroraCommandContext()
        with patch('apache.aurora.client.cli.cron.CronNoun.create_context',
                   return_value=mock_context):
            with temporary_file() as fp:
                fp.write(self.get_invalid_config('invalid_clause=oops'))
                fp.flush()
                cmd = AuroraCommandLine()
                result = cmd.execute(
                    ['cron', 'schedule', 'west/bozo/test/hello', fp.name])
                assert result == EXIT_INVALID_CONFIGURATION

            # Now check that the right API calls got made.
            # Check that create_job was not called.
            api = mock_context.get_api('west')
            assert api.schedule_cron.call_count == 0