def _test_successful_create(self, request_body, mock_url): """ Tries to create a scaling group with the given request body (which should succeed) - and test the response """ config = request_body['groupConfiguration'] launch = request_body['launchConfiguration'] policies = request_body.get('scalingPolicies', []) state = GroupState('11111', '1', '', {}, {}, None, {}, False) expected_config = config.copy() expected_config.setdefault('maxEntities', MAX_ENTITIES) expected_config.setdefault('metadata', {}) return_policies = [dict(id=str(i), **p) for i, p in enumerate(policies)] rval = { 'groupConfiguration': expected_config, 'launchConfiguration': launch, 'state': state, 'scalingPolicies': return_policies, 'id': '1' } self.mock_store.create_scaling_group.return_value = defer.succeed(rval) response_body = self.assert_status_code( 201, None, 'POST', json.dumps(request_body), '/v1.0/11111/groups/1/') self.mock_store.create_scaling_group.assert_called_once_with( mock.ANY, '11111', expected_config, launch, policies or None) resp = json.loads(response_body) validate(resp, rest_schemas.create_and_manifest_response) # compare the policies separately, because they have links and may be # in a different order resp_policies = resp['group'].pop('scalingPolicies') resp_policies_links = resp['group'].pop('scalingPolicies_links') self.assertEqual(resp, { 'group': { 'groupConfiguration': expected_config, 'launchConfiguration': launch, 'id': '1', 'state': format_state_dict(state), 'links': [{"href": "/v1.0/11111/groups/1/", "rel": "self"}] } }) resp_policies.sort(key=lambda dictionary: dictionary['id']) for pol in resp_policies: self.assertEqual(pol.pop('links'), [{ "href": "/v1.0/11111/groups/1/policies/{0}/".format(pol.pop('id')), "rel": "self" }]) self.assertEqual(resp_policies, policies) self.assertEqual(resp_policies_links, [{'href': '/v1.0/11111/groups/1/policies/', 'rel': 'policies'}])
def test_format_state_dict_has_active_and_pending(self): """ :func:`otter.rest.groups.format_state_dict` transforms a :class:`GroupState` into the state dictionary that is returned by the rest API (minus extra stuff like wrapping it in an extra dictionary with the keyword 'group', etc.) When there are active servers, this dictionary includes a list of active server links and ids. """ active = { '1': {'name': 'n1', 'links': ['links1'], 'created': 't'}, '2': {'name': 'n2', 'links': ['links2'], 'created': 't'}, '3': {'name': 'n3', 'links': ['links3'], 'created': 't'}} pending = { 'j1': {'created': 't'}, 'j2': {'created': 't'}, 'j3': {'created': 't'}} translated = format_state_dict( GroupState('11111', 'one', active, pending, None, {}, True)) # sort so it can be compared translated['active'].sort(key=lambda x: x['id']) self.assertEqual(translated, { 'active': [ {'id': '1', 'links': ['links1']}, {'id': '2', 'links': ['links2']}, {'id': '3', 'links': ['links3']} ], 'activeCapacity': 3, 'pendingCapacity': 3, 'desiredCapacity': 6, 'paused': True, 'id': "one", "links": [ {"href": "/v1.0/11111/groups/one/", "rel": "self"}, ] })
def test_format_state_dict_has_active_and_pending(self): """ :func:`otter.rest.groups.format_state_dict` transforms a :class:`GroupState` into the state dictionary that is returned by the rest API (minus extra stuff like wrapping it in an extra dictionary with the keyword 'group', etc.) When there are active servers, this dictionary includes a list of active server links and ids. """ active = { '1': { 'name': 'n1', 'links': ['links1'], 'created': 't' }, '2': { 'name': 'n2', 'links': ['links2'], 'created': 't' }, '3': { 'name': 'n3', 'links': ['links3'], 'created': 't' } } pending = { 'j1': { 'created': 't' }, 'j2': { 'created': 't' }, 'j3': { 'created': 't' } } translated = format_state_dict( GroupState('11111', 'one', active, pending, None, {}, True)) # sort so it can be compared translated['active'].sort(key=lambda x: x['id']) self.assertEqual( translated, { 'active': [{ 'id': '1', 'links': ['links1'] }, { 'id': '2', 'links': ['links2'] }, { 'id': '3', 'links': ['links3'] }], 'activeCapacity': 3, 'pendingCapacity': 3, 'desiredCapacity': 6, 'paused': True, 'id': "one", "links": [ { "href": "/v1.0/11111/groups/one/", "rel": "self" }, ] })