diff --git a/murano/tests/functional/api/base.py b/murano/tests/functional/api/base.py index 4823a0d47..55ccbeffa 100644 --- a/murano/tests/functional/api/base.py +++ b/murano/tests/functional/api/base.py @@ -44,9 +44,8 @@ class MuranoClient(rest_client.RestClient): return resp, json.loads(body) def create_environment(self, name): - post_body = '{"name": "%s"}' % name - - resp, body = self.post('v1/environments', post_body) + body = {'name': name} + resp, body = self.post('v1/environments', json.dumps(body)) return resp, json.loads(body) @@ -110,7 +109,7 @@ class MuranoClient(rest_client.RestClient): resp, body = self.post(url.format(environment_id, session_id), post_body) - return resp, json.loads(body) + return resp def create_service(self, environment_id, session_id, post_body): post_body = json.dumps(post_body) @@ -224,6 +223,94 @@ class MuranoClient(rest_client.RestClient): return resp, json.loads(body) + def get_env_templates_list(self): + """Check the environment templates deployed by the user.""" + resp, body = self.get('v1/templates') + + return resp, json.loads(body) + + def create_env_template(self, env_template_name): + """Check the creation of an environment template.""" + body = {'name': env_template_name} + resp, body = self.post('v1/templates', json.dumps(body)) + + return resp, json.loads(body) + + def create_env_template_with_apps(self, env_template_name): + """Check the creation of an environment template.""" + body = {'name': env_template_name} + body['services'] = [self._get_demo_app()] + resp, body = self.post('v1/templates', json.dumps(body)) + return resp, json.loads(body) + + def create_app_in_env_template(self, env_template_name): + """Check the creation of an environment template.""" + resp, body = self.post('v1/templates/{0}/services'. + format(env_template_name), + json.dumps(self._get_demo_app())) + return resp, json.loads(body) + + def get_apps_in_env_template(self, env_template_name): + """Check getting information about applications + in an environment template. + """ + resp, body = self.get('v1/templates/{0}/services'. + format(env_template_name)) + return resp, json.loads(body) + + def get_app_in_env_template(self, env_template_name, app_name): + """Check getting information about an application + in an environment template. + """ + resp, body = self.get('v1/templates/{0}/services/{1}'. + format(env_template_name, app_name)) + return resp, json.loads(body) + + def delete_app_in_env_template(self, env_template_name): + """Delete an application in an environment template.""" + resp, body = self.delete('v1/templates/{0}/services/{1}'. + format(env_template_name, 'ID')) + return resp + + def delete_env_template(self, env_template_id): + """Check the deletion of an environment template.""" + resp, body = self.delete('v1/templates/{0}'.format(env_template_id)) + return resp + + def get_env_template(self, env_template_id): + """Check getting information of an environment template.""" + resp, body = self.get('v1/templates/{0}'.format(env_template_id)) + + return resp, json.loads(body) + + def create_env_from_template(self, env_template_id, env_name): + """Check creating an environment from a template.""" + body = {'name': env_name} + resp, body = self.post('v1/templates/{0}/create-environment'. + format(env_template_id), + json.dumps(body)) + return resp, json.loads(body) + + def _get_demo_app(self): + return { + "instance": { + "assignFloatingIp": "true", + "keyname": "mykeyname", + "image": "cloud-fedora-v3", + "flavor": "m1.medium", + "?": { + "type": "io.murano.resources.LinuxMuranoInstance", + "id": "ef984a74-29a4-45c0-b1dc-2ab9f075732e" + } + }, + "name": "orion", + "port": "8080", + "?": { + "type": "io.murano.apps.apache.Tomcat", + "id": "ID" + } + } + class TestCase(test.BaseTestCase): @classmethod @@ -241,6 +328,7 @@ class TestCase(test.BaseTestCase): super(TestCase, self).setUp() self.environments = [] + self.env_templates = [] def tearDown(self): super(TestCase, self).tearDown() @@ -251,12 +339,24 @@ class TestCase(test.BaseTestCase): except exceptions.NotFound: pass + for env_template in self.env_templates: + try: + self.client.delete_env_template(env_template['id']) + except exceptions.NotFound: + pass + def create_environment(self, name): environment = self.client.create_environment(name)[1] self.environments.append(environment) return environment + def create_env_template(self, name): + env_template = self.client.create_env_template(name)[1] + self.env_templates.append(env_template) + + return env_template + def create_demo_service(self, environment_id, session_id, client=None): if not client: client = self.client diff --git a/murano/tests/functional/api/v1/test_env_templates.py b/murano/tests/functional/api/v1/test_env_templates.py new file mode 100644 index 000000000..243a5b5d2 --- /dev/null +++ b/murano/tests/functional/api/v1/test_env_templates.py @@ -0,0 +1,189 @@ +# Copyright (c) 2015 Telefonica I+D. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from tempest.test import attr +from tempest_lib import exceptions + +from murano.tests.functional.api import base + + +class TestEnvTemplate(base.TestCase): + + @attr(type='smoke') + def test_list_env_templates(self): + """Check getting the list of environment templates.""" + resp, body = self.client.get_env_templates_list() + + self.assertIn('templates', body) + self.assertEqual(resp.status, 200) + + @attr(type='smoke') + def test_create_and_delete_env_template(self): + """It checks the creation and deletion of an enviroment template.""" + env_templates_list_start = self.client.get_env_templates_list()[1] + + resp, env_template = self.client.create_env_template('test_env_temp') + self.env_templates.append(env_template) + + self.assertEqual(resp.status, 200) + self.assertEqual('test_env_temp', env_template['name']) + + env_templates_list = self.client.get_env_templates_list()[1] + + self.assertEqual(len(env_templates_list_start['templates']) + 1, + len(env_templates_list['templates'])) + + self.client.delete_env_template(env_template['id']) + + env_templates_list = self.client.get_env_templates_list()[1] + + self.assertEqual(len(env_templates_list_start['templates']), + len(env_templates_list['templates'])) + + self.env_templates.pop(self.env_templates.index(env_template)) + + @attr(type='smoke') + def test_get_env_template(self): + """Check getting information about an environment template.""" + resp, env_template = self.client.create_env_template('test_env_temp') + + resp, env_obtained_template =\ + self.client.get_env_template(env_template['id']) + + self.assertEqual(resp.status, 200) + self.assertEqual(env_obtained_template['name'], 'test_env_temp') + self.client.delete_env_template(env_template['id']) + + @attr(type='smoke') + def test_create_env_template_with_apps(self): + """Check the creation of an environment template with applications.""" + resp, env_template = \ + self.client.create_env_template_with_apps('test_env_temp') + self.assertEqual(resp.status, 200) + resp, apps_template = \ + self.client.get_apps_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + self.assertEqual(len(apps_template), 1) + self.client.delete_env_template(env_template['id']) + + @attr(type='smoke') + def test_create_app_in_env_template(self): + """Check the creationg of applications in an environment template.""" + resp, env_template = self.client.create_env_template('test_env_temp') + resp, apps = self.client.get_apps_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + self.assertEqual(len(apps), 0) + + resp, apps = self.client.create_app_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + resp, apps = self.client.get_apps_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + self.assertEqual(len(apps), 1) + + self.client.delete_env_template(env_template['id']) + + @attr(type='smoke') + def test_delete_app_in_env_template(self): + """Check the deletion of applications in an environmente template.""" + resp, env_template = self.client.create_env_template('test_env_temp') + + resp, apps = self.client.create_app_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + resp, apps = self.client.get_apps_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + self.assertEqual(len(apps), 1) + resp = self.client.delete_app_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + resp, apps = self.client.get_apps_in_env_template(env_template['id']) + self.assertEqual(resp.status, 200) + self.assertEqual(len(apps), 0) + + self.client.delete_env_template(env_template['id']) + + @attr(type='smoke') + def test_create_env_from_template(self): + """Check the creation of an environment from a template.""" + resp, env_template = \ + self.client.create_env_template_with_apps('test_env_temp') + self.assertEqual(resp.status, 200) + + resp, env = self.client.create_env_from_template(env_template['id'], + "env") + self.assertEqual(resp.status, 200) + + self.client.delete_env_template(env_template['id']) + self.client.delete_environment(env['environment_id']) + + @attr(type='negative') + def test_delete_environment_with_wrong_env_id(self): + """Check the deletion of an wrong environment template request.""" + self.assertRaises(exceptions.NotFound, + self.client.delete_env_template, + None) + + @attr(type='negative') + def test_create_environment_with_wrong_payload(self): + """Check the deletion of an wrong environment template request.""" + self.assertRaises(exceptions.BadRequest, + self.client.create_env_template, + '-+3') + + @attr(type='negative') + def test_double_delete_env_template(self): + """Check the deletion of an wrong environment template request.""" + resp, env_template = self.client.create_env_template('test_env_temp') + + self.client.delete_env_template(env_template['id']) + + self.assertRaises(exceptions.NotFound, + self.client.delete_env_template, + env_template['id']) + + @attr(type='negative') + def test_get_deleted_env_template(self): + """Check the deletion of an wrong environment template request.""" + resp, env_template = self.client.create_env_template('test_env_temp') + + self.client.delete_env_template(env_template['id']) + + self.assertRaises(exceptions.NotFound, + self.client.get_env_template, + env_template['id']) + + +class TestEnvTemplatesTenantIsolation(base.NegativeTestCase): + + @attr(type='negative') + def test_get_env_template_from_another_tenant(self): + """It tests getting information from an environment + template from another user. + """ + env_template = self.create_env_template('test_env_temp') + + self.assertRaises(exceptions.Unauthorized, + self.alt_client.get_env_template, env_template['id']) + + self.client.delete_env_template(env_template['id']) + + @attr(type='negative') + def test_delete_env_template_from_another_tenant(self): + """It tests deleting information from an environment + template from another user. + """ + env_template = self.create_env_template('test_env_temp') + + self.assertRaises(exceptions.Unauthorized, + self.alt_client.delete_env_template, + env_template['id']) + self.client.delete_env_template(env_template['id']) diff --git a/murano/utils.py b/murano/utils.py index f73d75f0e..d276ff749 100644 --- a/murano/utils.py +++ b/murano/utils.py @@ -52,14 +52,14 @@ def verify_env_template(func): unit = db_session.get_session() template = unit.query(models.EnvironmentTemplate).get(env_template_id) if template is None: - LOG.info(_("Environment Template with id '{0}'" - " not found").format(env_template_id)) + LOG.info(_LI("Environment Template with id '{0}' not found"). + format(env_template_id)) raise exc.HTTPNotFound() if hasattr(request, 'context'): if template.tenant_id != request.context.tenant: - LOG.info(_('User is not authorized to access' - ' this tenant resources')) + LOG.info(_LI('User is not authorized to access ' + 'this tenant resources')) raise exc.HTTPUnauthorized() return func(self, request, env_template_id, *args, **kwargs)