def test_get_result(self, _): """Result of task with given id and environment id is returned.""" now = timeutils.utcnow() expected_environment_id = 'test_environment' expected_task_id = 'test_task' expected_result = {'test_result': 'test_result'} environment = models.Environment( id=expected_environment_id, name='test_environment', created=now, updated=now, tenant_id=self.tenant ) task = models.Task( id=expected_task_id, started=now, finished=now, result=expected_result, environment_id=expected_environment_id ) test_utils.save_models(environment, task) request = self._get( '/environments/{environment_id}/actions/{task_id}' .format(environment_id=expected_environment_id, task_id=expected_task_id), ) response = request.get_response(self.api) self.assertEqual(200, response.status_code) self.assertEqual(expected_result, response.json)
def test_delete_environment(self): """Test that environment deletion results in the correct rpc call.""" self._set_policy_rules({'delete_environment': '@'}) self.expect_policy_check('delete_environment', {'environment_id': '12345'}) fake_now = timeutils.utcnow() expected = dict(id='12345', name='my-env', version=0, networking={}, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': { 'id': '12345' } }, 'Attributes': {} }) e = models.Environment(**expected) test_utils.save_models(e) req = self._delete('/environments/12345') result = req.get_response(self.api) # Should this be expected behavior? self.assertEqual('', result.body) self.assertEqual(200, result.status_code)
def test_execute_action_with_invalid_session_version(self, mocked_function, _): """Test whether validate session function throws error.""" self._set_policy_rules( {'execute_action': '@'} ) fake_now = timeutils.utcnow() expected = dict( id='12345', name='my-env', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '12345', '_actions': { 'actionsID_action': { 'enabled': True, 'name': 'Testaction' } }} }, 'Attributes': {} } ) environment = models.Environment(**expected) test_utils.save_models(environment) req = self._post('/environments/12345/actions/actionID_action', b'{}') mocked_function.return_value = False self.assertRaises(exc.HTTPForbidden, self.controller.execute, req, '12345', 'actionsID_action', {})
def test_get_result(self, _): """Result of task with given id and environment id is returned.""" now = timeutils.utcnow() expected_environment_id = 'test_environment' expected_task_id = 'test_task' expected_result = {'test_result': 'test_result'} environment = models.Environment( id=expected_environment_id, name='test_environment', created=now, updated=now, tenant_id=self.tenant ) task = models.Task( id=expected_task_id, started=now, finished=now, result=expected_result, environment_id=expected_environment_id ) test_utils.save_models(environment, task) request = self._get( '/environments/{environment_id}/actions/{task_id}' .format(environment_id=expected_environment_id, task_id=expected_task_id), ) response = request.get_response(self.api) self.assertEqual(response.status_code, 200) self.assertEqual(response.json, expected_result)
def test_delete_environment(self): """Test that environment deletion results in the correct rpc call.""" self._set_policy_rules( {'delete_environment': '@'} ) self.expect_policy_check( 'delete_environment', {'environment_id': '12345'} ) fake_now = timeutils.utcnow() expected = dict( id='12345', name='my-env', version=0, networking={}, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '12345'} }, 'Attributes': {} } ) e = models.Environment(**expected) test_utils.save_models(e) req = self._delete('/environments/12345') result = req.get_response(self.api) # Should this be expected behavior? self.assertEqual('', result.body) self.assertEqual(200, result.status_code)
def test_update_environment(self): """Check that environment rename works.""" self._set_policy_rules( {'show_environment': '@', 'update_environment': '@'} ) self.expect_policy_check('update_environment', {'environment_id': '12345'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict( id='12345', name='my-env', version=0, description_text='', created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '12345'} }, 'Attributes': [] } ) e = models.Environment(**expected) test_utils.save_models(e) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now del expected['description'] expected['services'] = [] expected['status'] = 'ready' expected['name'] = 'renamed_env' expected['updated'] = fake_now body = { 'name': 'renamed_env' } req = self._put('/environments/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) self.expect_policy_check('show_environment', {'environment_id': '12345'}) req = self._get('/environments/12345') result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected['created'] = timeutils.isotime(expected['created'])[:-1] expected['updated'] = timeutils.isotime(expected['updated'])[:-1] expected['acquired_by'] = None self.assertEqual(expected, jsonutils.loads(result.body))
def test_update_environment_with_existing_name(self): self._set_policy_rules( {'update_environment': '@'} ) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict( id='111', name='env1', version=0, networking={}, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '111'} }, 'Attributes': [] } ) e = models.Environment(**expected) test_utils.save_models(e) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict( id='222', name='env2', version=0, networking={}, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '222'} }, 'Attributes': [] } ) e = models.Environment(**expected) test_utils.save_models(e) self.expect_policy_check('update_environment', {'environment_id': '111'}) body = { 'name': 'env2' } req = self._put('/environments/111', json.dumps(body)) result = req.get_response(self.api) self.assertEqual(409, result.status_code)
def test_update_environment(self): """Check that environment rename works.""" self._set_policy_rules({ 'show_environment': '@', 'update_environment': '@' }) self.expect_policy_check('update_environment', {'environment_id': '12345'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict(id='12345', name='my-env', version=0, description_text='', created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': { 'id': '12345' } }, 'Attributes': [] }) e = models.Environment(**expected) test_utils.save_models(e) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now del expected['description'] expected['services'] = [] expected['status'] = 'ready' expected['name'] = 'renamed_env' expected['updated'] = fake_now body = {'name': 'renamed_env'} req = self._put('/environments/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) self.expect_policy_check('show_environment', {'environment_id': '12345'}) req = self._get('/environments/12345') result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected['created'] = timeutils.isotime(expected['created'])[:-1] expected['updated'] = timeutils.isotime(expected['updated'])[:-1] expected['acquired_by'] = None self.assertEqual(expected, jsonutils.loads(result.body))
def test_update_env_template(self): """Check that environment rename works.""" self._set_policy_rules( {'show_env_template': '@', 'update_env_template': '@'} ) self.expect_policy_check('update_env_template', {'env_template_id': '12345'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict( id='12345', is_public=False, name='my-temp', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description_text='', description={ 'name': 'my-temp', '?': {'id': '12345'} } ) e = models.EnvironmentTemplate(**expected) test_utils.save_models(e) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now del expected['description'] expected['services'] = [] expected['name'] = 'renamed_temp' expected['updated'] = fake_now body = { 'name': 'renamed_temp' } req = self._put('/templates/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) self.expect_policy_check('show_env_template', {'env_template_id': '12345'}) req = self._get('/templates/12345') result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected['created'] = timeutils.isotime(expected['created'])[:-1] expected['updated'] = timeutils.isotime(expected['updated'])[:-1] self.assertEqual(expected, jsonutils.loads(result.body))
def test_update_env_template(self): """Check that environment rename works.""" self._set_policy_rules({ 'show_env_template': '@', 'update_env_template': '@' }) self.expect_policy_check('update_env_template', {'env_template_id': '12345'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict(id='12345', is_public=False, name='my-temp', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description_text='', description={ 'name': 'my-temp', '?': { 'id': '12345' } }) e = models.EnvironmentTemplate(**expected) test_utils.save_models(e) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now del expected['description'] expected['services'] = [] expected['name'] = 'renamed_temp' expected['updated'] = fake_now body = {'name': 'renamed_temp'} req = self._put('/templates/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) self.expect_policy_check('show_env_template', {'env_template_id': '12345'}) req = self._get('/templates/12345') result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected['created'] = timeutils.isotime(expected['created'])[:-1] expected['updated'] = timeutils.isotime(expected['updated'])[:-1] self.assertEqual(expected, jsonutils.loads(result.body))
def _create_fake_environment(self, env_name="my-env", env_id="123"): fake_now = timeutils.utcnow() expected = dict( id=env_id, name=env_name, version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={"Objects": {"?": {"id": "{0}".format(env_id)}}, "Attributes": {}}, ) e = models.Environment(**expected) test_utils.save_models(e)
def test_get_environment_acquired(self): """Test GET request of an environment in deploying status""" self._set_policy_rules( {'show_environment': '@'} ) self.expect_policy_check('show_environment', {'environment_id': '1234'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now env_id = '1234' self._create_fake_environment(env_id=env_id) sess_id = '321' expected = dict( id=sess_id, environment_id=env_id, version=0, state=states.SessionState.DEPLOYING, user_id=self.tenant, description={ 'Objects': { '?': {'id': '{0}'.format(env_id)} }, 'Attributes': {} } ) s = models.Session(**expected) test_utils.save_models(s) req = self._get('/environments/{0}'.format(env_id)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected = {'tenant_id': self.tenant, 'id': env_id, 'name': 'my-env', 'version': 0, 'description_text': '', 'created': timeutils.isotime(fake_now)[:-1], 'updated': timeutils.isotime(fake_now)[:-1], 'acquired_by': sess_id, 'services': [], 'status': states.EnvironmentStatus.DEPLOYING, } self.assertEqual(expected, jsonutils.loads(result.body))
def test_delete_category(self): """Check that category deleted successfully """ self._set_policy_rules({"delete_category": "@"}) self.expect_policy_check("delete_category", {"category_id": "12345"}) fake_now = timeutils.utcnow() expected = {"name": "new_category", "created": fake_now, "updated": fake_now, "id": "12345"} e = models.Category(**expected) test_utils.save_models(e) req = self._delete("/catalog/categories/12345") processed_result = req.get_response(self.api) self.assertEqual("", processed_result.body) self.assertEqual(200, processed_result.status_code)
def test_get_environment_acquired(self): """Test GET request of an environment in deploying status""" self._set_policy_rules({'show_environment': '@'}) self.expect_policy_check('show_environment', {'environment_id': '1234'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now env_id = '1234' self._create_fake_environment(env_id=env_id) sess_id = '321' expected = dict(id=sess_id, environment_id=env_id, version=0, state=states.SessionState.DEPLOYING, user_id=self.tenant, description={ 'Objects': { '?': { 'id': '{0}'.format(env_id) } }, 'Attributes': {} }) s = models.Session(**expected) test_utils.save_models(s) req = self._get('/environments/{0}'.format(env_id)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected = { 'tenant_id': self.tenant, 'id': env_id, 'name': 'my-env', 'version': 0, 'description_text': '', 'created': timeutils.isotime(fake_now)[:-1], 'updated': timeutils.isotime(fake_now)[:-1], 'acquired_by': sess_id, 'services': [], 'status': states.EnvironmentStatus.DEPLOYING, } self.assertEqual(expected, jsonutils.loads(result.body))
def _create_fake_environment(self, env_name='my-env', env_id='123'): fake_now = timeutils.utcnow() expected = dict(id=env_id, name=env_name, version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': { 'id': '{0}'.format(env_id) } }, 'Attributes': {} }) e = models.Environment(**expected) test_utils.save_models(e)
def _create_fake_environment(self, env_name='my-env', env_id='123'): fake_now = timeutils.utcnow() expected = dict( id=env_id, name=env_name, version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '{0}'.format(env_id)} }, 'Attributes': {} } ) e = models.Environment(**expected) test_utils.save_models(e)
def test_update_environment(self): """Check that environment rename works.""" self._set_policy_rules({"show_environment": "@", "update_environment": "@"}) self.expect_policy_check("update_environment", {"environment_id": "12345"}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict( id="12345", name="my-env", version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={"Objects": {"?": {"id": "12345"}}, "Attributes": []}, ) e = models.Environment(**expected) test_utils.save_models(e) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now del expected["description"] expected["services"] = [] expected["status"] = "ready" expected["name"] = "renamed_env" expected["updated"] = fake_now body = {"name": "renamed_env"} req = self._put("/environments/12345", json.dumps(body)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) self.expect_policy_check("show_environment", {"environment_id": "12345"}) req = self._get("/environments/12345") result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected["created"] = timeutils.isotime(expected["created"])[:-1] expected["updated"] = timeutils.isotime(expected["updated"])[:-1] expected["acquired_by"] = None self.assertEqual(expected, json.loads(result.body))
def test_get_result_not_found(self, _): """If task does not exists, it should be handled correctly and API should return 404. """ expected_environment_id = 'test_environment' environment = models.Environment(id=expected_environment_id, name='test_environment', tenant_id=self.tenant) test_utils.save_models(environment) request = self._get( '/environments/{environment_id}/actions/{task_id}'.format( environment_id=expected_environment_id, task_id='not_existent_task_id'), ) response = request.get_response(self.api) self.assertEqual(response.status_code, 404)
def test_delete_category(self): """Check that category deleted successfully""" self._set_policy_rules({'delete_category': '@'}) self.expect_policy_check('delete_category', {'category_id': '12345'}) fake_now = timeutils.utcnow() expected = {'name': 'new_category', 'created': fake_now, 'updated': fake_now, 'id': '12345'} e = models.Category(**expected) test_utils.save_models(e) req = self._delete('/catalog/categories/12345') processed_result = req.get_response(self.api) self.assertEqual('', processed_result.body) self.assertEqual(200, processed_result.status_code)
def test_delete_category(self): """Check that category deleted successfully """ self._set_policy_rules({'delete_category': '@'}) self.expect_policy_check('delete_category', {'category_id': '12345'}) fake_now = timeutils.utcnow() expected = {'name': 'new_category', 'created': fake_now, 'updated': fake_now, 'id': '12345'} e = models.Category(**expected) test_utils.save_models(e) req = self._delete('/catalog/categories/12345') processed_result = req.get_response(self.api) self.assertEqual('', processed_result.body) self.assertEqual(200, processed_result.status_code)
def test_get_result_not_found(self, _): """If task does not exists, it should be handled correctly and API should return 404. """ expected_environment_id = 'test_environment' environment = models.Environment( id=expected_environment_id, name='test_environment', tenant_id=self.tenant ) test_utils.save_models(environment) request = self._get( '/environments/{environment_id}/actions/{task_id}' .format(environment_id=expected_environment_id, task_id='not_existent_task_id'), ) response = request.get_response(self.api) self.assertEqual(response.status_code, 404)
def test_get_environment_acquired(self): """Test GET request of an environment in deploying status""" self._set_policy_rules({"show_environment": "@"}) self.expect_policy_check("show_environment", {"environment_id": "1234"}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now env_id = "1234" self._create_fake_environment(env_id=env_id) sess_id = "321" expected = dict( id=sess_id, environment_id=env_id, version=0, state=states.SessionState.DEPLOYING, user_id=self.tenant, description={"Objects": {"?": {"id": "{0}".format(env_id)}}, "Attributes": {}}, ) s = models.Session(**expected) test_utils.save_models(s) req = self._get("/environments/{0}".format(env_id)) result = req.get_response(self.api) self.assertEqual(200, result.status_code) expected = { "tenant_id": self.tenant, "id": env_id, "name": "my-env", "version": 0, "created": timeutils.isotime(fake_now)[:-1], "updated": timeutils.isotime(fake_now)[:-1], "acquired_by": sess_id, "services": [], "status": states.EnvironmentStatus.DEPLOYING, } self.assertEqual(expected, json.loads(result.body))
def test_update_env_template_with_inappropriate_name(self): """Check that environment rename works.""" self._set_policy_rules({ 'show_env_template': '@', 'update_env_template': '@' }) self.expect_policy_check('update_env_template', {'env_template_id': '12345'}) fake_now = timeutils.utcnow() timeutils.utcnow.override_time = fake_now expected = dict(id='12345', is_public=False, name='my-temp', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant) e = models.EnvironmentTemplate(**expected) test_utils.save_models(e) # Attempt to update the environment template with invalid name. body = {'name': ''} req = self._put('/templates/12345', jsonutils.dump_as_bytes(body)) result = req.get_response(self.api) self.assertEqual(400, result.status_code) self.assertIn(b'EnvTemplate body is incorrect', result.body) # Verify that the name was not changed. self.expect_policy_check('show_env_template', {'env_template_id': '12345'}) req = self._get('/templates/12345') result = req.get_response(self.api) self.assertEqual(200, result.status_code) self.assertEqual(expected['name'], jsonutils.loads(result.body)['name'])
def test_execute_action_with_session_in_deleting_state(self, _): """Test whether session in deleting state throws error.""" self._set_policy_rules( {'execute_action': '@'} ) fake_now = timeutils.utcnow() expected = dict( id='12345', name='my-env', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '12345', '_actions': { 'actionsID_action': { 'enabled': True, 'name': 'Testaction' } }} }, 'Attributes': {} } ) environment = models.Environment(**expected) test_utils.save_models(environment) req = self._post('/environments/12345/actions/actionID_action', b'{}') user_id = req.context.user self._create_session_with_state(environment, user_id, states.SessionState.DELETING) self.assertRaises(exc.HTTPForbidden, self.controller.execute, req, '12345', 'actionsID_action', {})
def test_execute_action(self, mock_policy_check): """Test that action execution results in the correct rpc call.""" self._set_policy_rules( {'execute_action': '@'} ) fake_now = timeutils.utcnow() expected = dict( id='12345', name='my-env', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '12345', '_actions': { 'actionsID_action': { 'enabled': True, 'name': 'Testaction' } }} }, 'Attributes': {} } ) e = models.Environment(**expected) test_utils.save_models(e) rpc_task = { 'action': { 'args': '{}', 'method': 'Testaction', 'object_id': '12345' }, 'tenant_id': self.tenant, 'model': { 'Attributes': {}, 'Objects': { 'applications': [], '?': { '_actions': { 'actionsID_action': { 'enabled': True, 'name': 'Testaction' } }, 'id': '12345' } } }, 'token': None, 'id': '12345' } req = self._post('/environments/12345/actions/actionID_action', '{}') result = self.controller.execute(req, '12345', 'actionsID_action', '{}') self.mock_engine_rpc.handle_task.assert_called_once_with(rpc_task) self.assertIn('task_id', result)
def test_execute_action(self, mock_policy_check): """Test that action execution results in the correct rpc call.""" self._set_policy_rules( {'execute_action': '@'} ) fake_now = timeutils.utcnow() expected = dict( id='12345', name='my-env', version=0, created=fake_now, updated=fake_now, tenant_id=self.tenant, description={ 'Objects': { '?': {'id': '12345', '_actions': { 'actionsID_action': { 'enabled': True, 'name': 'Testaction' } }} }, 'Attributes': {} } ) e = models.Environment(**expected) test_utils.save_models(e) rpc_task = { 'action': { 'args': '{}', 'method': 'Testaction', 'object_id': '12345' }, 'project_id': self.tenant, 'user_id': self.user, 'model': { 'Attributes': {}, 'Objects': { 'applications': [], '?': { '_actions': { 'actionsID_action': { 'enabled': True, 'name': 'Testaction' } }, 'id': '12345' } } }, 'token': None, 'id': '12345' } req = self._post('/environments/12345/actions/actionID_action', b'{}') result = self.controller.execute(req, '12345', 'actionsID_action', '{}') self.mock_engine_rpc.handle_task.assert_called_once_with(rpc_task) self.assertIn('task_id', result)