def update_and_view_scaling_policy(self, path): """ Updating a scaling policy returns with a 204 no content. When viewing the policy again, it should contain the updated version. """ request_body = policy()[-1] # the one that was not created wrapper = self.successResultOf( request(root, 'PUT', path, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 204, "Update failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200) response = json.loads(wrapper.content) updated = response['policy'] self.assertIn('id', updated) self.assertIn('links', updated) self.assertIn( path, [_strip_base_url(link["href"]) for link in updated["links"]]) del updated['id'] del updated['links'] self.assertEqual(updated, request_body)
def update_and_view_scaling_policy(self, path): """ Updating a scaling policy returns with a 204 no content. When viewing the policy again, it should contain the updated version. """ request_body = _policy()[-1] # the one that was not created wrapper = yield request(root, 'PUT', path, body=json.dumps(request_body)) self.assert_response(wrapper, 204, "Update policy failed.") self.assertEqual(wrapper.content, "") # now try to view wrapper = yield request(root, 'GET', path) self.assert_response(wrapper, 200) response = json.loads(wrapper.content) updated = response['policy'] self.assertIn('id', updated) self.assertIn('links', updated) self.assertIn(path, [path_only(link["href"]) for link in updated["links"]]) del updated['id'] del updated['links'] self.assertEqual(updated, request_body)
def update_and_view_webhook(self, path): """ Updating a webhook returns with a 204 no content. When viewing the webhook again, it should contain the updated version. """ request_body = {'name': 'updated_webhook', 'metadata': {'foo': 'bar'}} wrapper = self.successResultOf( request(root, 'PUT', path, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 204, "Update failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200) response = json.loads(wrapper.content) updated = response['webhook'] self.assertIn('id', updated) self.assertIn('links', updated) for link in updated["links"]: if link['rel'] == 'self': self.assertIn(_strip_base_url(link["href"]), path) else: self.assertEqual(link['rel'], 'capability') self.assertIn('/v1.0/execute/1/', link["href"]) del updated['id'] del updated['links'] self.assertEqual(updated, {'name': 'updated_webhook', 'metadata': {'foo': 'bar'}})
def update_and_view_scaling_policy(self, path): """ Updating a scaling policy returns with a 204 no content. When viewing the policy again, it should contain the updated version. """ request_body = policy()[-1] # the one that was not created wrapper = yield request(root, 'PUT', path, body=json.dumps(request_body)) self.assert_response(wrapper, 204, "Update policy failed.") self.assertEqual(wrapper.content, "") # now try to view wrapper = yield request(root, 'GET', path) self.assert_response(wrapper, 200) response = json.loads(wrapper.content) updated = response['policy'] self.assertIn('id', updated) self.assertIn('links', updated) self.assertIn( path, [path_only(link["href"]) for link in updated["links"]]) del updated['id'] del updated['links'] self.assertEqual(updated, request_body)
def create_and_view_scaling_group(self): """ Creating a scaling group with a valid config returns with a 200 OK and a Location header pointing to the new scaling group. :return: the path to the new scaling group resource """ request_body = { "groupConfiguration": self.config, "launchConfiguration": launch_server_config()[0] } wrapper = self.successResultOf( request(root, 'POST', '/v1.0/11111/groups/', body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 201, "Create failed: {0}".format(wrapper.content)) response = json.loads(wrapper.content) for key in request_body: self.assertEqual(response["group"][key], request_body[key]) for key in ("id", "links"): self.assertTrue(key in response["group"]) headers = wrapper.response.headers.getRawHeaders('Location') self.assertTrue(headers is not None) self.assertEqual(1, len(headers)) # now make sure the Location header points to something good! path = _strip_base_url(headers[0]) wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200, path) response = json.loads(wrapper.content) self.assertEqual(response["group"]['groupConfiguration'], self.config) self.assertEqual(response["group"]['launchConfiguration'], launch_server_config()[0]) # make sure the created group has enough pending entities, and is # not paused wrapper = self.successResultOf(request(root, 'GET', path + 'state/')) self.assertEqual(wrapper.response.code, 200) response = json.loads(wrapper.content) self.assertTrue(not response["group"]['paused']) return path
def delete_and_view_scaling_policy(self, path): """ Deleting a scaling policy returns with a 204 no content. The next attempt to view the scaling policy should return a 404 not found. """ wrapper = yield request(root, 'DELETE', path) self.assert_response(wrapper, 204, "Delete policy failed.") self.assertEqual(wrapper.content, "") # now try to view wrapper = yield request(root, 'GET', path) self.assert_response(wrapper, 404, "Deleted policy still there.") # flush any logged errors self.flushLoggedErrors(NoSuchPolicyError)
def test_execute_webhook_by_hash(self): """ Executing a webhook should look up the policy by hash and attempt to execute that policy. """ self.assert_number_of_webhooks(0) first_webhooks = self.create_and_view_webhooks() wrapper = self.successResultOf(request(root, 'GET', first_webhooks[0])) webhook = json.loads(wrapper.content)['webhook'] links = {link['rel']: link['href'] for link in webhook['links']} cap_path = _strip_base_url(links['capability']) wrapper = self.successResultOf(request(root, 'POST', cap_path)) self.assertEqual(wrapper.response.code, 202)
def delete_and_view_scaling_group(self, path): """ Deleting a scaling group returns with a 204 no content. The next attempt to view the scaling group should return a 404 not found. """ wrapper = yield request(root, 'DELETE', path) self.assert_response(wrapper, 204, "Delete failed.") self.assertEqual(wrapper.content, "") # now try to view policies # TODO: view state and manifest too, once they have been implemented wrapper = yield request(root, 'GET', path + 'policies/') self.assert_response(wrapper, 404, "Deleted group still there.") # flush any logged errors self.flushLoggedErrors(NoSuchScalingGroupError)
def delete_and_view_webhook(self, path): """ Deleting a webhook returns with a 204 no content. The next attempt to view the webhook should return a 404 not found. """ wrapper = self.successResultOf(request(root, 'DELETE', path)) self.assertEqual(wrapper.response.code, 204, "Delete failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 404) # flush any logged errors self.flushLoggedErrors(NoSuchWebhookError)
def test_history_with_one_page_pagination(self): """ The history api endpoint returns the items from the audit log, and paginates them if there are ``limit`` items in the collection, with the marker being the last timestamp """ expected = { 'events': [{ 'event_type': 'event-abc', 'timestamp': 1234567890, 'policy_id': 'policy-xyz', 'scaling_group_id': 'scaling-group-uvw', 'server_id': 'server-rst', 'message': 'audit log event', }], 'events_links': [ {'href': 'http://localhost/v1.0/101010/history?limit=1', 'rel': 'self'}, {'href': 'http://localhost/v1.0/101010/history?limit=1&marker=1234567890', 'rel': 'next'}] } result = self.successResultOf( request(self.root, "GET", self.endpoint + "?limit=1")) self.assertEqual(200, result.response.code) self.assertEqual(expected, json.loads(result.content)) self.treq.get.assert_called_once_with( 'http://dummy/_search', data='{"tenant_id": 101010}', log=matches(IsInstance(self.log.__class__))) self.assertTrue(self.treq.json_content.called) self.make_auditlog_query.assert_called_once_with('101010', 'ord', limit=1)
def create_and_view_webhooks(self): """ Creating valid webhooks returns with a 200 OK, a Location header pointing to the list of all webhooks, and a response containing a list of the newly created webhook resources only. :return: a list self links to the new webhooks (not guaranteed to be in any consistent order) """ request_body = [{ 'name': 'first', 'metadata': { 'notes': 'first webhook' } }, { 'name': 'second', 'metadata': { 'notes': 'second webhook' } }] wrapper = self.successResultOf( request(root, 'POST', self.webhooks_url, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 201, "Create failed: {0}".format(wrapper.content)) response = json.loads(wrapper.content) self.assertEqual(len(request_body), len(response["webhooks"])) # this iterates over the webhooks, checks to see that each have # 'id' and 'links' keys, makes sure that there is an extra link # containing the capability URL, and then checks to see that the # rest of the responce is in the original set of webhooks to be created for webhook in response["webhooks"]: keys = webhook.keys() keys.sort() self.assertEqual(['id', 'links', 'metadata', 'name'], keys) self.assertIn( { 'metadata': webhook['metadata'], 'name': webhook['name'] }, request_body) self.assertIn('capability', [link_obj['rel'] for link_obj in webhook['links']]) headers = wrapper.response.headers.getRawHeaders('Location') self.assertTrue(headers is not None) self.assertEqual(1, len(headers)) # now make sure the Location header points to the list webhooks header self.assertEqual(_strip_base_url(headers[0]), self.webhooks_url) links = [ _strip_base_url(link["href"]) for link in webhook["links"] if link["rel"] == "self" for webhook in response["webhooks"] ] return links
def create_and_view_scaling_group(self): """ Creating a scaling group with a valid config returns with a 200 OK and a Location header pointing to the new scaling group. :return: the path to the new scaling group resource """ request_body = { "groupConfiguration": self.config, "launchConfiguration": launch_server_config()[0] } wrapper = self.successResultOf(request( root, 'POST', '/v1.0/11111/groups/', body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 201, "Create failed: {0}".format(wrapper.content)) response = json.loads(wrapper.content) for key in request_body: self.assertEqual(response["group"][key], request_body[key]) for key in ("id", "links"): self.assertTrue(key in response["group"]) headers = wrapper.response.headers.getRawHeaders('Location') self.assertTrue(headers is not None) self.assertEqual(1, len(headers)) # now make sure the Location header points to something good! path = _strip_base_url(headers[0]) wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200, path) response = json.loads(wrapper.content) self.assertEqual(response["group"]['groupConfiguration'], self.config) self.assertEqual(response["group"]['launchConfiguration'], launch_server_config()[0]) # make sure the created group has enough pending entities, and is # not paused wrapper = self.successResultOf( request(root, 'GET', path + 'state/')) self.assertEqual(wrapper.response.code, 200) response = json.loads(wrapper.content) self.assertTrue(not response["group"]['paused']) return path
def _check_creation_worked(path): # TODO: check manifest and state as well d = defer.gatherResults([ request(root, 'GET', path + 'policies/').addCallback( _check_policies_created), self.assert_state(path, self.config['minEntities'], False)]) # no matter what, just return the path return d.addCallback(lambda _: path)
def test_execute_non_existant_webhook_by_hash(self): """ Executing a webhook that doesn't exist should still return a 202. """ self.assert_number_of_webhooks(0) wrapper = self.successResultOf( request(root, 'POST', '/v1.0/execute/1/1/')) self.assertEqual(wrapper.response.code, 202)
def assert_number_of_webhooks(self, number): """ Asserts that there are ``number`` number of scaling policies """ wrapper = self.successResultOf(request(root, 'GET', self.webhooks_url)) self.assertEqual(200, wrapper.response.code) response = json.loads(wrapper.content) self.assertEqual(len(response["webhooks"]), number)
def _check_creation_worked(path): # TODO: check manifest and state as well d = defer.gatherResults([ request(root, 'GET', path + 'policies/').addCallback(_check_policies_created), self.assert_state(path, self.config['minEntities'], False) ]) # no matter what, just return the path return d.addCallback(lambda _: path)
def assert_number_of_webhooks(self, number): """ Asserts that there are ``number`` number of scaling policies """ wrapper = self.successResultOf( request(root, 'GET', self.webhooks_url)) self.assertEqual(200, wrapper.response.code) response = json.loads(wrapper.content) self.assertEqual(len(response["webhooks"]), number)
def assert_number_of_scaling_groups(self, number): """ Asserts that there are ``number`` number of scaling groups """ wrapper = self.successResultOf( request(root, 'GET', '/v1.0/11111/groups/')) self.assertEqual(200, wrapper.response.code) response = json.loads(wrapper.content) self.assertEqual(len(response["groups"]), number)
def delete_and_view_scaling_group(self, path): """ Deleting a scaling group returns with a 204 no content. The next attempt to view the scaling group should return a 404 not found. """ wrapper = self.successResultOf(request(self.root, 'DELETE', path)) self.assertEqual(wrapper.response.code, 204, "Delete failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view wrapper = self.successResultOf(request(self.root, 'GET', path)) self.assertEqual(wrapper.response.code, 404) wrapper = self.successResultOf( request(self.root, 'GET', path + 'state/')) self.assertEqual(wrapper.response.code, 404) # flush any logged errors self.flushLoggedErrors(NoSuchScalingGroupError)
def assert_number_of_scaling_groups(self, number): """ Asserts that there are ``number`` number of scaling groups """ def _check_number(wrapper): self.assert_response(wrapper, 200) response = json.loads(wrapper.content) self.assertEqual(len(response["groups"]), number) d = request(root, 'GET', '/v1.0/11111/groups/') return d.addCallback(_check_number)
def assert_number_of_scaling_policies(self, number): """ Asserts that there are ``number`` number of scaling policies """ def _check_number(wrapper): self.assert_response(wrapper, 200) response = json.loads(wrapper.content) self.assertEqual(len(response["policies"]), number) d = request(root, 'GET', self.policies_url) return d.addCallback(_check_number)
def test_update_launch_config(self): """ Editing the launch config of a scaling group with a valid launch config returns with a 204 no content. The next attempt to view the launch config should return the new launch config. """ path = yield self.create_scaling_group() launch_path = path + 'launch/' edited_launch = launch_server_config()[1] wrapper = yield request(root, 'PUT', launch_path, body=json.dumps(edited_launch)) self.assert_response(wrapper, 204, "Edit launch config failed.") self.assertEqual(wrapper.content, "") # now try to view again - the config should be the edited config wrapper = yield request(root, 'GET', launch_path) self.assert_response(wrapper, 200) self.assertEqual(json.loads(wrapper.content), {'launchConfiguration': edited_launch})
def test_history(self): """ the history api endpoint returns the items from the audit log """ data = {} result = self.successResultOf( request(self.root, "GET", self.endpoint)) body = self.assert_status_code(200) result = json.loads(body) self.assertEqual(result, data)
def test_execute_scaling_policy_failed(self): """ Executing a non-existant scaling policy should result in a 404. """ self.mock_controller.maybe_execute_scaling_policy.return_value = defer.fail( NoSuchPolicyError('11111', '1', '2')) wrapper = yield request(root, 'POST', self.policies_url + '1/execute/') self.assertEqual(wrapper.response.code, 404, "Execute did not fail as expected: {0}".format(wrapper.content)) self.flushLoggedErrors(NoSuchPolicyError)
def test_execute_scaling_policy_success(self): """ Executing a scaling policy should result in a 202. """ yield self.assert_number_of_scaling_policies(0) first_policies = yield self.create_and_view_scaling_policies() yield self.assert_number_of_scaling_policies(len(first_policies)) wrapper = yield request(root, 'POST', first_policies[0] + 'execute/') self.assertEqual(wrapper.response.code, 202, "Execute failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "{}")
def test_ru_launch_config(self): """ Editing the launch config of a scaling group with a valid launch config returns with a 204 no content. The next attempt to view the launch config should return the new launch config. """ # make sure there is a scaling group path = self.create_and_view_scaling_group() + 'launch/' edited_launch = launch_server_config()[1] wrapper = self.successResultOf( request(root, 'PUT', path, body=json.dumps(edited_launch))) self.assertEqual(wrapper.response.code, 204, "Edit failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view again - the config should be the edited config wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200) self.assertEqual(json.loads(wrapper.content), {'launchConfiguration': edited_launch})
def test_execute_scaling_policy_failed(self): """ Executing a non-existant scaling policy should result in a 404. """ self.mock_controller.maybe_execute_scaling_policy.return_value = defer.fail( NoSuchPolicyError('11111', '1', '2')) wrapper = yield request(root, 'POST', self.policies_url + '1/execute/') self.assertEqual( wrapper.response.code, 404, "Execute did not fail as expected: {0}".format(wrapper.content)) self.flushLoggedErrors(NoSuchPolicyError)
def assert_state(self, path, entities, paused): """ Assert that the state has the specified number of total entities and is or is not paused, as specified. :return: deferred that fires with None """ def _check_state(wrapper): self.assertEqual(wrapper.response.code, 200) response = json.loads(wrapper.content) self.assertEqual(response['group']['paused'], paused) self.assertEqual(response['group']['desiredCapacity'], entities) return request(root, 'GET', path + 'state/').addCallback(_check_state)
def test_ru_launch_config(self): """ Editing the launch config of a scaling group with a valid launch config returns with a 204 no content. The next attempt to view the launch config should return the new launch config. """ # make sure there is a scaling group path = self.create_and_view_scaling_group() + 'launch/' edited_launch = launch_server_config()[1] wrapper = self.successResultOf( request(root, 'PUT', path, body=json.dumps(edited_launch))) self.assertEqual(wrapper.response.code, 204, "Edit failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view again - the config should be the edited config wrapper = self.successResultOf( request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200) self.assertEqual(json.loads(wrapper.content), {'launchConfiguration': edited_launch})
def update_and_view_webhook(self, path): """ Updating a webhook returns with a 204 no content. When viewing the webhook again, it should contain the updated version. """ request_body = {'name': 'updated_webhook', 'metadata': {'foo': 'bar'}} wrapper = self.successResultOf( request(root, 'PUT', path, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 204, "Update failed: {0}".format(wrapper.content)) self.assertEqual(wrapper.content, "") # now try to view wrapper = self.successResultOf(request(root, 'GET', path)) self.assertEqual(wrapper.response.code, 200) response = json.loads(wrapper.content) updated = response['webhook'] self.assertIn('id', updated) self.assertIn('links', updated) for link in updated["links"]: if link['rel'] == 'self': self.assertIn(_strip_base_url(link["href"]), path) else: self.assertEqual(link['rel'], 'capability') self.assertIn('/v1.0/execute/1/', link["href"]) del updated['id'] del updated['links'] self.assertEqual(updated, { 'name': 'updated_webhook', 'metadata': { 'foo': 'bar' } })
def create_and_view_webhooks(self): """ Creating valid webhooks returns with a 200 OK, a Location header pointing to the list of all webhooks, and a response containing a list of the newly created webhook resources only. :return: a list self links to the new webhooks (not guaranteed to be in any consistent order) """ request_body = [ {'name': 'first', 'metadata': {'notes': 'first webhook'}}, {'name': 'second', 'metadata': {'notes': 'second webhook'}} ] wrapper = self.successResultOf(request( root, 'POST', self.webhooks_url, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 201, "Create failed: {0}".format(wrapper.content)) response = json.loads(wrapper.content) self.assertEqual(len(request_body), len(response["webhooks"])) # this iterates over the webhooks, checks to see that each have # 'id' and 'links' keys, makes sure that there is an extra link # containing the capability URL, and then checks to see that the # rest of the responce is in the original set of webhooks to be created for webhook in response["webhooks"]: keys = webhook.keys() keys.sort() self.assertEqual(['id', 'links', 'metadata', 'name'], keys) self.assertIn( {'metadata': webhook['metadata'], 'name': webhook['name']}, request_body) self.assertIn('capability', [link_obj['rel'] for link_obj in webhook['links']]) headers = wrapper.response.headers.getRawHeaders('Location') self.assertTrue(headers is not None) self.assertEqual(1, len(headers)) # now make sure the Location header points to the list webhooks header self.assertEqual(_strip_base_url(headers[0]), self.webhooks_url) links = [_strip_base_url(link["href"]) for link in webhook["links"] if link["rel"] == "self" for webhook in response["webhooks"]] return links
def create_and_view_scaling_policies(self): """ Creating valid scaling policies returns with a 200 OK, a Location header pointing to the list of all scaling policies, and a response containing a list of the newly created scaling policy resources only. :return: a list self links to the new scaling policies (not guaranteed to be in any consistent order) """ request_body = policy()[: -1] # however many of them there are minus one wrapper = self.successResultOf( request(root, 'POST', self.policies_url, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 201, "Create failed: {0}".format(wrapper.content)) response = json.loads(wrapper.content) self.assertEqual(len(request_body), len(response["policies"])) # this iterates over the response policies, checks to see that each have # 'id' and 'links' keys, and then checks to see that the rest of the # response policy is in the original set of policies to be created for pol in response["policies"]: original_pol = pol.copy() for key in ('id', 'links'): self.assertIn(key, pol) del original_pol[key] self.assertIn(original_pol, request_body) headers = wrapper.response.headers.getRawHeaders('Location') self.assertTrue(headers is not None) self.assertEqual(1, len(headers)) # now make sure the Location header points to the list policies header self.assertEqual(_strip_base_url(headers[0]), self.policies_url) links = [ _strip_base_url(link["href"]) for link in pol["links"] if link["rel"] == "self" for pol in response["policies"] ] return links
def create_and_view_scaling_policies(self): """ Creating valid scaling policies returns with a 200 OK, a Location header pointing to the list of all scaling policies, and a response containing a list of the newly created scaling policy resources only. :return: a list self links to the new scaling policies (not guaranteed to be in any consistent order) """ request_body = _policy( )[:-1] # however many of them there are minus one def _verify_create_response(wrapper): self.assert_response(wrapper, 201, "Create policies failed.") response = json.loads(wrapper.content) self.assertEqual(len(request_body), len(response["policies"])) # this iterates over the response policies, checks to see that each # have 'id' and 'links' keys, and then checks to see that the rest # of the response policy is in the original set of policies to be #created for pol in response["policies"]: original_pol = pol.copy() for key in ('id', 'links'): self.assertIn(key, pol) del original_pol[key] self.assertIn(original_pol, request_body) # now make sure the Location header points to the list policies #header location = path_only(self.get_location_header(wrapper)) self.assertEqual(location, self.policies_url) links = [ path_only(link["href"]) for link in pol["links"] if link["rel"] == "self" for pol in response["policies"] ] return links d = request(root, 'POST', self.policies_url, body=json.dumps(request_body)) return d.addCallback(_verify_create_response)
def test_update_config(self): """ Editing the config of a scaling group to one with a higher min returns 204 with no content. The state is updated to reflect existing at least the min number of pending + current """ path = yield self.create_and_view_scaling_group() config_path = path + 'config/' self.config['minEntities'] = 2 self.config['maxEntities'] = 25 self.config['metadata'] = {} self.active_pending_etc = ({}, {'1': {}, '2': {}}, 'date', {}, False) wrapper = yield request(root, 'PUT', config_path, body=json.dumps(self.config)) self.assert_response(wrapper, 204, "Edit config failed") self.assertEqual(wrapper.content, "") yield self.assert_state(path, 2, False)
def create_scaling_group(self): """ Creates a scaling group and returns the path. """ def _check_create_response(wrapper): # the body is probably verified by the unit tests - check the # header and status code self.assert_response(wrapper, 201, "Create failed.") # now make sure the Location header points to something good! return path_only(self.get_location_header(wrapper)) request_body = { "groupConfiguration": self.config, "launchConfiguration": self._launch_server_config, "scalingPolicies": self._policies } deferred = request( root, 'POST', '/v1.0/11111/groups/', body=json.dumps(request_body)) deferred.addCallback(_check_create_response) return deferred
def create_and_view_scaling_policies(self): """ Creating valid scaling policies returns with a 200 OK, a Location header pointing to the list of all scaling policies, and a response containing a list of the newly created scaling policy resources only. :return: a list self links to the new scaling policies (not guaranteed to be in any consistent order) """ request_body = policy()[:-1] # however many of them there are minus one def _verify_create_response(wrapper): self.assert_response(wrapper, 201, "Create policies failed.") response = json.loads(wrapper.content) self.assertEqual(len(request_body), len(response["policies"])) # this iterates over the response policies, checks to see that each # have 'id' and 'links' keys, and then checks to see that the rest # of the response policy is in the original set of policies to be #created for pol in response["policies"]: original_pol = pol.copy() for key in ('id', 'links'): self.assertIn(key, pol) del original_pol[key] self.assertIn(original_pol, request_body) # now make sure the Location header points to the list policies #header location = path_only(self.get_location_header(wrapper)) self.assertEqual(location, self.policies_url) links = [path_only(link["href"]) for link in pol["links"] if link["rel"] == "self" for pol in response["policies"]] return links d = request(root, 'POST', self.policies_url, body=json.dumps(request_body)) return d.addCallback(_verify_create_response)
def create_and_view_scaling_policies(self): """ Creating valid scaling policies returns with a 200 OK, a Location header pointing to the list of all scaling policies, and a response containing a list of the newly created scaling policy resources only. :return: a list self links to the new scaling policies (not guaranteed to be in any consistent order) """ request_body = policy()[:-1] # however many of them there are minus one wrapper = self.successResultOf(request( root, 'POST', self.policies_url, body=json.dumps(request_body))) self.assertEqual(wrapper.response.code, 201, "Create failed: {0}".format(wrapper.content)) response = json.loads(wrapper.content) self.assertEqual(len(request_body), len(response["policies"])) # this iterates over the response policies, checks to see that each have # 'id' and 'links' keys, and then checks to see that the rest of the # response policy is in the original set of policies to be created for pol in response["policies"]: original_pol = pol.copy() for key in ('id', 'links'): self.assertIn(key, pol) del original_pol[key] self.assertIn(original_pol, request_body) headers = wrapper.response.headers.getRawHeaders('Location') self.assertTrue(headers is not None) self.assertEqual(1, len(headers)) # now make sure the Location header points to the list policies header self.assertEqual(_strip_base_url(headers[0]), self.policies_url) links = [_strip_base_url(link["href"]) for link in pol["links"] if link["rel"] == "self" for pol in response["policies"]] return links
def create_scaling_group(self): """ Creates a scaling group and returns the path. """ def _check_create_response(wrapper): # the body is probably verified by the unit tests - check the # header and status code self.assert_response(wrapper, 201, "Create failed.") # now make sure the Location header points to something good! return path_only(self.get_location_header(wrapper)) request_body = { "groupConfiguration": self.config, "launchConfiguration": self._launch_server_config, "scalingPolicies": self._policies } deferred = request(root, 'POST', '/v1.0/11111/groups/', body=json.dumps(request_body)) deferred.addCallback(_check_create_response) return deferred
def test_list_limits_xml(self): """ the api returns a xml blob containing the absolute limits in the correct format, if the "Accept" header specifies xml """ data = ("<?xml version='1.0' encoding='UTF-8'?>\n" '<limits xmlns="http://docs.openstack.org/common/api/v1.0">' '<absolute>' '<limit name="maxGroups" value="2"/>' '<limit name="maxPoliciesPerGroup" value="3"/>' '<limit name="maxWebhooksPerPolicy" value="4"/>' '</absolute></limits>') headers = {"Accept": ["application/xml"]} response_wrapper = self.successResultOf( request(self.root, "GET", self.endpoint, headers=headers)) self.assertEqual(response_wrapper.response.headers.getRawHeaders('Content-Type'), ['application/xml']) self.assertEqual(response_wrapper.response.code, 200) self.assertEqual(response_wrapper.content, data)
def test_history(self): """ the history api endpoint returns the items from the audit log """ expected = { 'events': [{ 'event_type': 'event-abc', 'timestamp': 1234567890, 'policy_id': 'policy-xyz', 'scaling_group_id': 'scaling-group-uvw', 'server_id': 'server-rst', 'message': 'audit log event', }], 'events_links': [{'href': 'http://localhost/v1.0/101010/history', 'rel': 'self'}] } result = self.successResultOf( request(self.root, "GET", self.endpoint)) self.assertEqual(200, result.response.code) self.assertEqual(expected, json.loads(result.content)) self.make_auditlog_query.assert_called_once_with('101010', 'ord', limit=20)
def test_list_limits_xml(self): """ the api returns a xml blob containing the absolute limits in the correct format, if the "Accept" header specifies xml """ data = ("<?xml version='1.0' encoding='UTF-8'?>\n" '<limits xmlns="http://docs.openstack.org/common/api/v1.0">' '<absolute>' '<limit name="maxGroups" value="2"/>' '<limit name="maxPoliciesPerGroup" value="3"/>' '<limit name="maxWebhooksPerPolicy" value="4"/>' '</absolute></limits>') headers = {"Accept": ["application/xml"]} response_wrapper = self.successResultOf( request(self.root, "GET", self.endpoint, headers=headers)) self.assertEqual( response_wrapper.response.headers.getRawHeaders('Content-Type'), ['application/xml']) self.assertEqual(response_wrapper.response.code, 200) self.assertEqual(response_wrapper.content, data)