コード例 #1
0
 def setUp(self):
   self._command = UpdateCommand()
   self._mock_options = mock_verb_options(self._command)
   self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [])
   self._mock_options.force = True
   self._fake_context = FakeAuroraCommandContext()
   self._fake_context.set_options(self._mock_options)
   self._mock_api = self._fake_context.get_api("test")
コード例 #2
0
ファイル: test_update.py プロジェクト: wfarr/incubator-aurora
class TestJobUpdateCommand(AuroraClientCommandTest):
    def setUp(self):
        self._command = UpdateCommand()
        self._mock_options = mock_verb_options(self._command)
        self._mock_options.instance_spec = TaskInstanceKey(
            self.TEST_JOBKEY, [])
        self._mock_options.force = True
        self._fake_context = FakeAuroraCommandContext()
        self._fake_context.set_options(self._mock_options)
        self._mock_api = self._fake_context.get_api("test")

    @classmethod
    def create_mock_config(cls, is_cron=False):
        mock_config = create_autospec(spec=AuroraConfig,
                                      spec_set=True,
                                      instance=True)
        mock_raw_config = Mock()
        mock_raw_config.has_cron_schedule.return_value = is_cron
        mock_config.raw = Mock(return_value=mock_raw_config)
        return mock_config

    def test_update_with_lock(self):
        mock_config = self.create_mock_config()
        self._fake_context.get_job_config = Mock(return_value=mock_config)
        self._mock_api.update_job.return_value = self.create_blank_response(
            ResponseCode.LOCK_ERROR, "Error.")

        with pytest.raises(Context.CommandError):
            self._command.execute(self._fake_context)

        self._mock_api.update_job.assert_called_once_with(
            mock_config, self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)
        self.assert_lock_message(self._fake_context)

    def test_update_print_error_once(self):
        mock_config = self.create_mock_config()
        self._fake_context.get_job_config = Mock(return_value=mock_config)
        error = "Error printed once."
        self._mock_api.update_job.return_value = self.create_blank_response(
            ResponseCode.INVALID_REQUEST, error)

        with pytest.raises(Context.CommandErrorLogged):
            self._command.execute(self._fake_context)

        self._mock_api.update_job.assert_called_once_with(
            mock_config, self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)
        assert self._fake_context.get_err() == [
            "Update failed due to error:",
            "\t%s" % error
        ]

    def test_update_no_active_instance_check(self):
        self._mock_options.instance_spec = TaskInstanceKey(
            self.TEST_JOBKEY, [1])
        self._mock_options.strict = True

        mock_config = self.create_mock_config()
        self._fake_context.get_job_config = Mock(return_value=mock_config)
        self._mock_api.update_job.return_value = self.create_simple_success_response(
        )

        self._command.execute(self._fake_context)

        self._mock_api.update_job.assert_called_once_with(
            mock_config, self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)
コード例 #3
0
class TestJobUpdateCommand(AuroraClientCommandTest):
    def setUp(self):
        self._command = UpdateCommand()
        self._mock_options = mock_verb_options(self._command)
        self._mock_options.instance_spec = TaskInstanceKey(
            self.TEST_JOBKEY, [])
        self._mock_options.force = True
        self._fake_context = FakeAuroraCommandContext()
        self._fake_context.set_options(self._mock_options)
        self._mock_api = self._fake_context.get_api("test")

    @classmethod
    def get_job_config(self, is_service=True, is_cron=False):
        return AuroraConfig(job=Job(
            cluster='west',
            role='bozo',
            environment='test',
            name='the_job',
            service=is_service,
            cron_schedule='* * * * *' if is_cron else Empty,
            task=Task(name='task',
                      processes=[Process(cmdline='ls -la', name='process')],
                      resources=Resources(
                          cpu=1.0, ram=1024 * MB, disk=1024 * MB)),
            contact='*****@*****.**',
            instances=3,
        ))

    def test_update_with_lock(self):
        config = self.get_job_config()
        self._fake_context.get_job_config = Mock(return_value=config)
        self._mock_api.update_job.return_value = self.create_blank_response(
            ResponseCode.LOCK_ERROR, "Error.")

        with pytest.raises(Context.CommandError):
            self._command.execute(self._fake_context)

        self._mock_api.update_job.assert_called_once_with(
            config, self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)
        self.assert_lock_message(self._fake_context)

    def test_update_print_error_once(self):
        config = self.get_job_config()
        self._fake_context.get_job_config = Mock(return_value=config)
        error = "Error printed once."
        self._mock_api.update_job.return_value = self.create_blank_response(
            ResponseCode.INVALID_REQUEST, error)

        with pytest.raises(Context.CommandErrorLogged):
            self._command.execute(self._fake_context)

        self._mock_api.update_job.assert_called_once_with(
            config, self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)
        assert self._fake_context.get_err() == [
            "Update failed due to error:",
            "\t%s" % error
        ]

    def test_update_no_active_instance_check(self):
        self._mock_options.instance_spec = TaskInstanceKey(
            self.TEST_JOBKEY, [1])
        self._mock_options.strict = True

        config = self.get_job_config()
        self._fake_context.get_job_config = Mock(return_value=config)
        self._mock_api.update_job.return_value = self.create_simple_success_response(
        )

        self._command.execute(self._fake_context)

        assert self._mock_api.update_job.mock_calls == [
            call(config, self._mock_options.healthcheck_interval_seconds,
                 self._mock_options.instance_spec.instance)
        ]

    def test_update_non_service(self):
        self._fake_context.get_job_config = Mock(
            return_value=self.get_job_config(is_service=False))

        # Command failure is the only expectation here, as the request was invalid.
        with pytest.raises(Context.CommandError):
            self._command.execute(self._fake_context)

    def test_update_cron(self):
        config = self.get_job_config(is_cron=True)
        self._fake_context.get_job_config = Mock(return_value=config)
        self._mock_api.update_job.return_value = self.create_simple_success_response(
        )

        self._command.execute(self._fake_context)
        assert self._mock_api.update_job.mock_calls == [
            call(config, self._mock_options.healthcheck_interval_seconds,
                 self._mock_options.instance_spec.instance)
        ]
コード例 #4
0
ファイル: test_update.py プロジェクト: KancerEzeroglu/aurora
class TestJobUpdateCommand(AuroraClientCommandTest):

  def setUp(self):
    self._command = UpdateCommand()
    self._mock_options = mock_verb_options(self._command)
    self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [])
    self._mock_options.force = True
    self._fake_context = FakeAuroraCommandContext()
    self._fake_context.set_options(self._mock_options)
    self._mock_api = self._fake_context.get_api("test")

  @classmethod
  def get_job_config(self, is_service=True, is_cron=False):
    return AuroraConfig(job=Job(
      cluster='west',
      role='bozo',
      environment='test',
      name='the_job',
      service=is_service,
      cron_schedule='* * * * *' if is_cron else Empty,
      task=Task(
        name='task',
        processes=[Process(cmdline='ls -la', name='process')],
        resources=Resources(cpu=1.0, ram=1024 * MB, disk=1024 * MB)
      ),
      contact='*****@*****.**',
      instances=3,
    ))

  def test_update_with_lock(self):
    config = self.get_job_config()
    self._fake_context.get_job_config = Mock(return_value=config)
    self._mock_api.update_job.return_value = self.create_blank_response(
        ResponseCode.LOCK_ERROR, "Error.")

    with pytest.raises(Context.CommandError):
      self._command.execute(self._fake_context)

    self._mock_api.update_job.assert_called_once_with(
      config,
      self._mock_options.healthcheck_interval_seconds,
      self._mock_options.instance_spec.instance)
    self.assert_lock_message(self._fake_context)

  def test_update_print_error_once(self):
    config = self.get_job_config()
    self._fake_context.get_job_config = Mock(return_value=config)
    error = "Error printed once."
    self._mock_api.update_job.return_value = self.create_blank_response(
        ResponseCode.INVALID_REQUEST,
        error)

    with pytest.raises(Context.CommandErrorLogged):
      self._command.execute(self._fake_context)

    self._mock_api.update_job.assert_called_once_with(
      config,
      self._mock_options.healthcheck_interval_seconds,
      self._mock_options.instance_spec.instance)
    assert self._fake_context.get_err() == [
        CLIENT_UPDATER_DEPRECATION,
        "Update failed due to error:", "\t%s" % error
    ]

  def test_update_no_active_instance_check(self):
    self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1])
    self._mock_options.strict = True

    config = self.get_job_config()
    self._fake_context.get_job_config = Mock(return_value=config)
    self._mock_api.update_job.return_value = self.create_simple_success_response()

    self._command.execute(self._fake_context)

    assert self._mock_api.update_job.mock_calls == [
        call(
            config,
            self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)]

  def test_update_non_service(self):
    self._fake_context.get_job_config = Mock(return_value=self.get_job_config(is_service=False))

    # Command failure is the only expectation here, as the request was invalid.
    with pytest.raises(Context.CommandError):
      self._command.execute(self._fake_context)

  def test_update_cron(self):
    config = self.get_job_config(is_cron=True)
    self._fake_context.get_job_config = Mock(return_value=config)
    self._mock_api.update_job.return_value = self.create_simple_success_response()

    self._command.execute(self._fake_context)
    assert self._mock_api.update_job.mock_calls == [
        call(
            config,
            self._mock_options.healthcheck_interval_seconds,
            self._mock_options.instance_spec.instance)]
コード例 #5
0
class TestJobUpdateCommand(AuroraClientCommandTest):

  def setUp(self):
    self._command = UpdateCommand()
    self._mock_options = mock_verb_options(self._command)
    self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [])
    self._mock_options.force = True
    self._fake_context = FakeAuroraCommandContext()
    self._fake_context.set_options(self._mock_options)
    self._mock_api = self._fake_context.get_api("test")

  @classmethod
  def create_mock_config(cls, is_cron=False):
    mock_config = create_autospec(spec=AuroraConfig, spec_set=True, instance=True)
    mock_raw_config = Mock()
    mock_raw_config.has_cron_schedule.return_value = is_cron
    mock_config.raw = Mock(return_value=mock_raw_config)
    return mock_config

  def test_update_with_lock(self):
    mock_config = self.create_mock_config()
    self._fake_context.get_job_config = Mock(return_value=mock_config)
    self._mock_api.update_job.return_value = self.create_blank_response(
        ResponseCode.LOCK_ERROR, "Error.")

    with pytest.raises(Context.CommandError):
      self._command.execute(self._fake_context)

    self._mock_api.update_job.assert_called_once_with(
      mock_config,
      self._mock_options.healthcheck_interval_seconds,
      self._mock_options.instance_spec.instance)
    self.assert_lock_message(self._fake_context)

  def test_update_print_error_once(self):
    mock_config = self.create_mock_config()
    self._fake_context.get_job_config = Mock(return_value=mock_config)
    error = "Error printed once."
    self._mock_api.update_job.return_value = self.create_blank_response(
        ResponseCode.INVALID_REQUEST,
        error)

    with pytest.raises(Context.CommandErrorLogged):
      self._command.execute(self._fake_context)

    self._mock_api.update_job.assert_called_once_with(
      mock_config,
      self._mock_options.healthcheck_interval_seconds,
      self._mock_options.instance_spec.instance)
    assert self._fake_context.get_err() == ["Update failed due to error:", "\t%s" % error]

  def test_update_no_active_instance_check(self):
    self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1])
    self._mock_options.strict = True

    mock_config = self.create_mock_config()
    self._fake_context.get_job_config = Mock(return_value=mock_config)
    self._mock_api.update_job.return_value = self.create_simple_success_response()

    self._command.execute(self._fake_context)

    self._mock_api.update_job.assert_called_once_with(
      mock_config,
      self._mock_options.healthcheck_interval_seconds,
      self._mock_options.instance_spec.instance)