예제 #1
0
    def test_start(self):
        self.client.put.return_value = self.mock_response(
            '/v2/apps/app_id', HTTPStatus.CREATED, None, 'v2', 'apps',
            'PUT_{id}_response.json')
        mock_summary = self.mock_response('/v2/apps/app_id/summary',
                                          HTTPStatus.OK, None, 'v2', 'apps',
                                          'GET_{id}_summary_response.json')
        mock_instances_stopped = self.mock_response(
            '/v2/apps/app_id/instances', HTTPStatus.BAD_REQUEST, None, 'v2',
            'apps', 'GET_{id}_instances_stopped_response.json')
        mock_instances_started = self.mock_response(
            '/v2/apps/app_id/instances', HTTPStatus.OK, None, 'v2', 'apps',
            'GET_{id}_instances_response.json')
        self.client.get.side_effect = [
            mock_summary,
            InvalidStatusCode(HTTPStatus.BAD_REQUEST, dict(code=220001)),
            mock_instances_started
        ]

        application = self.client.v2.apps.start('app_id')
        self.client.put.assert_called_with(self.client.put.return_value.url,
                                           json=dict(state='STARTED'))
        self.client.get.assert_has_calls([
            call(mock_summary.url),
            call(mock_instances_stopped.url),
            call(mock_instances_started.url)
        ],
                                         any_order=False)
        self.assertIsNotNone(application)
예제 #2
0
    def test_scale_paas_app_handles_unexpected_errors(self,
                                                      mock_get_statsd_client,
                                                      mock_paas_client, _,
                                                      caplog):
        caplog.set_level(logging.INFO)
        app_name = 'app-name-1'
        app_guid = '11111-11111-11111111-1111'
        cf_info = {'name': app_name, 'instances': 4, 'guid': app_guid}
        app = self._get_mock_app(app_name, cf_info)
        app.get_desired_instance_count = Mock(return_value=6)

        mock_paas_client.return_value.update.side_effect = InvalidStatusCode(
            status_code=HTTPStatus.BAD_REQUEST,
            body={'description': 'something bad'})

        autoscaler = Autoscaler()
        autoscaler.scale(app)
        mock_paas_client.return_value.update.assert_called_once_with(
            app_guid, 6)
        assert caplog.record_tuples == [
            ('root', logging.INFO, 'Scaling app-name-1 from 4 to 6'),
            ('root', logging.ERROR,
             'Failed to scale app-name-1: BAD_REQUEST : {"description": "something bad"}'
             )
        ]
예제 #3
0
    def test_scale_paas_app_handles_deployments(self, mock_get_statsd_client,
                                                mock_paas_client, _, caplog):
        caplog.set_level(logging.INFO)
        app_name = 'app-name-1'
        app_guid = '11111-11111-11111111-1111'
        cf_info = {'name': app_name, 'instances': 4, 'guid': app_guid}
        app = self._get_mock_app(app_name, cf_info)
        app.get_desired_instance_count = Mock(return_value=6)

        mock_paas_client.return_value.update.side_effect = InvalidStatusCode(
            status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
            body={
                "description":
                "Cannot scale this process while a deployment is in flight.",
                "error_code": "CF-ScaleDisabledDuringDeployment",
                "code": 390016
            })

        autoscaler = Autoscaler()
        autoscaler.scale(app)
        mock_paas_client.return_value.update.assert_called_once_with(
            app_guid, 6)
        assert caplog.record_tuples == [
            ('root', logging.INFO, 'Scaling app-name-1 from 4 to 6'),
            ('root', logging.INFO, 'Cannot scale during deployment app-name-1')
        ]
예제 #4
0
 def _check_response(response: Response) -> Response:
     if int(response.status_code / 100) == 2:
         return response
     else:
         try:
             body = response.json()
         except ValueError as _:
             body = response.text
         raise InvalidStatusCode(HTTPStatus(response.status_code), body)
예제 #5
0
 def _check_response(response):
     if int(response.status_code / 100) == 2:
         return response
     else:
         try:
             body = response.json()
         except Exception as _:
             body = response.text
         raise InvalidStatusCode(response.status_code, body)
 def _check_response(response: Response) -> Response:
     if int(response.status_code / 100) == 2:
         return response
     else:
         try:
             body = response.json()
         except ValueError:
             body = response.text
         raise InvalidStatusCode(HTTPStatus(response.status_code), body, response.headers.get("x-vcap-request-id"))
예제 #7
0
 def test_stop(self):
     self.client.put.return_value = mock_response(
         '/v2/apps/app_id',
         CREATED,
         None,
         'v2', 'apps', 'PUT_{id}_response.json')
     self.client.get.side_effect = [InvalidStatusCode(BAD_REQUEST, dict(code=220001))]
     application = self.client.v2.apps.stop('app_id')
     self.client.put.assert_called_with(self.client.put.return_value.url,
                                        json=dict(state='STOPPED'))
     self.client.get.assert_called_with('%s/v2/apps/app_id/instances' % TARGET_ENDPOINT)
     self.assertIsNotNone(application)
예제 #8
0
def resolve_id(argument, get_by_name, domain_name, allow_search_by_name):
    if is_guid(argument):
        return argument
    elif allow_search_by_name:
        result = get_by_name(argument)
        if result is not None:
            return result['metadata']['guid']
        else:
            raise InvalidStatusCode(
                NOT_FOUND, '%s with name %s' % (domain_name, argument))
    else:
        raise ValueError('id: %s: does not allow search by name' % domain_name)
예제 #9
0
 def test_stop(self):
     self.client.put.return_value = self.mock_response(
         "/v2/apps/app_id", HTTPStatus.CREATED, None, "v2", "apps",
         "PUT_{id}_response.json")
     self.client.get.side_effect = [
         InvalidStatusCode(HTTPStatus.BAD_REQUEST, dict(code=220001))
     ]
     application = self.client.v2.apps.stop("app_id")
     self.client.put.assert_called_with(self.client.put.return_value.url,
                                        json=dict(state="STOPPED"))
     self.client.get.assert_called_with("%s/v2/apps/app_id/instances" %
                                        self.TARGET_ENDPOINT)
     self.assertIsNotNone(application)
예제 #10
0
 def resolve_id(self, argument, get_by_name):
     if CommandDomain.is_guid(argument):
         return argument
     elif self.allow_retrieve_by_name:
         result = get_by_name(argument)
         if result is not None:
             return result['metadata']['guid']
         else:
             raise InvalidStatusCode(
                 NOT_FOUND,
                 '%s with name %s' % (self.client_domain, argument))
     else:
         raise ValueError('id: %s: does not allow search by name' %
                          self.client_domain)
예제 #11
0
 def execute(client, arguments):
     if self.is_guid(arguments.id[0]):
         self._get_client_domain(client)._remove(arguments.id[0])
     elif self.allow_retrieve_by_name:
         entity = self.find_by_name(client, arguments.id[0])
         if entity is None:
             raise InvalidStatusCode(
                 NOT_FOUND, '%s with name %s' %
                 (self.client_domain, arguments.id[0]))
         else:
             self._get_client_domain(client)._remove(self.id(entity))
     else:
         raise ValueError('id: %s: does not allow search by name' %
                          self.client_domain)
 def execute(client: CloudFoundryClient, arguments: Namespace):
     if self.is_guid(arguments.id[0]):
         self._get_client_domain(client)._remove(arguments.id[0])
     elif self.allow_retrieve_by_name:
         entity = self.find_by_name(client, arguments.id[0])
         if entity is None:
             raise InvalidStatusCode(
                 HTTPStatus.NOT_FOUND, "%s with name %s" %
                 (self.client_domain, arguments.id[0]))
         else:
             self._get_client_domain(client)._remove(self.id(entity))
     else:
         raise ValueError("id: %s: does not allow search by name" %
                          self.client_domain)
예제 #13
0
def resolve_id(argument: str, get_by_name: Callable[[str], JsonObject],
               domain_name: str, allow_search_by_name: bool) -> str:
    if is_guid(argument):
        return argument
    elif allow_search_by_name:
        result = get_by_name(argument)
        if result is not None:
            return result['metadata']['guid']
        else:
            raise InvalidStatusCode(
                HTTPStatus.NOT_FOUND,
                '%s with name %s' % (domain_name, argument))
    else:
        raise ValueError('id: %s: does not allow search by name' % domain_name)
 def resolve_id(self, argument: str,
                get_by_name: Callable[[str], JsonObject]) -> str:
     if CommandDomain.is_guid(argument):
         return argument
     elif self.allow_retrieve_by_name:
         result = get_by_name(argument)
         if result is not None:
             if self.api_version == "v2":
                 return result["metadata"]["guid"]
             elif self.api_version == "v3":
                 return result["guid"]
         else:
             raise InvalidStatusCode(
                 HTTPStatus.NOT_FOUND,
                 "%s with name %s" % (self.client_domain, argument))
     else:
         raise ValueError("id: %s: does not allow search by name" %
                          self.client_domain)