Merge "Services test for Keystone version 2 api services"
diff --git a/patrole_tempest_plugin/tests/api/compute/rbac_base.py b/patrole_tempest_plugin/tests/api/compute/rbac_base.py
index ca24204..6fd8f30 100644
--- a/patrole_tempest_plugin/tests/api/compute/rbac_base.py
+++ b/patrole_tempest_plugin/tests/api/compute/rbac_base.py
@@ -11,6 +11,9 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest.lib.common.utils import data_utils
+from tempest.lib.common.utils import test_utils
+
 from tempest.api.compute import base as compute_base
 from tempest import config
 
@@ -56,3 +59,34 @@
         super(BaseV2ComputeAdminRbacTest, cls).setup_clients()
         cls.admin_client = cls.os_admin.agents_client
         cls.auth_provider = cls.os.auth_provider
+
+    @classmethod
+    def resource_setup(cls):
+        super(BaseV2ComputeAdminRbacTest, cls).resource_setup()
+        cls.flavors = []
+
+    @classmethod
+    def resource_cleanup(cls):
+        cls.clear_flavors()
+        super(BaseV2ComputeAdminRbacTest, cls).resource_cleanup()
+
+    @classmethod
+    def clear_flavors(cls):
+        for flavor in cls.flavors:
+            test_utils.call_and_ignore_notfound_exc(
+                cls.flavors_client.delete_flavor, flavor['id'])
+
+    @classmethod
+    def _create_flavor(cls, **kwargs):
+        flavor_kwargs = {
+            "name": data_utils.rand_name('flavor'),
+            "ram": data_utils.rand_int_id(1, 10),
+            "vcpus": data_utils.rand_int_id(1, 10),
+            "disk": data_utils.rand_int_id(1, 10),
+            "id": data_utils.rand_uuid(),
+        }
+        if kwargs:
+            flavor_kwargs.update(kwargs)
+        flavor = cls.flavors_client.create_flavor(**flavor_kwargs)['flavor']
+        cls.flavors.append(flavor)
+        return flavor
diff --git a/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py
new file mode 100644
index 0000000..32ec91a
--- /dev/null
+++ b/patrole_tempest_plugin/tests/api/compute/test_flavor_access_rbac.py
@@ -0,0 +1,93 @@
+#    Copyright 2017 AT&T Corporation.
+#    All Rights Reserved.
+#
+#    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 oslo_log import log
+
+from tempest import config
+from tempest.lib.common.utils import test_utils
+from tempest.lib import decorators
+from tempest.lib import exceptions
+
+from patrole_tempest_plugin import rbac_exceptions
+from patrole_tempest_plugin import rbac_rule_validation
+from patrole_tempest_plugin.rbac_utils import rbac_utils
+from patrole_tempest_plugin.tests.api.compute import rbac_base
+
+CONF = config.CONF
+LOG = log.getLogger(__name__)
+
+
+class FlavorAccessAdminRbacTest(rbac_base.BaseV2ComputeAdminRbacTest):
+
+    @classmethod
+    def setup_clients(cls):
+        super(FlavorAccessAdminRbacTest, cls).setup_clients()
+        cls.client = cls.flavors_client
+
+    @classmethod
+    def skip_checks(cls):
+        super(FlavorAccessAdminRbacTest, cls).skip_checks()
+        if not CONF.compute_feature_enabled.api_extensions:
+            raise cls.skipException(
+                '%s skipped as no compute extensions enabled' % cls.__name__)
+
+    @classmethod
+    def resource_setup(cls):
+        super(FlavorAccessAdminRbacTest, cls).resource_setup()
+        cls.flavor_id = cls._create_flavor(is_public=False)['id']
+        cls.tenant_id = cls.auth_provider.credentials.tenant_id
+
+    def tearDown(self):
+        rbac_utils.switch_role(self, switchToRbacRole=False)
+        super(FlavorAccessAdminRbacTest, self).tearDown()
+
+    @decorators.idempotent_id('a2bd3740-765d-4c95-ac98-9e027378c75e')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:os-flavor-access")
+    def test_list_flavor_access(self):
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        try:
+            self.client.list_flavor_access(self.flavor_id)
+        except exceptions.NotFound as e:
+            LOG.info("NotFound exception caught. Exception is thrown when "
+                     "role doesn't have access to the endpoint."
+                     "This is irregular and should be fixed.")
+            raise rbac_exceptions.RbacActionFailed(e)
+
+    @decorators.idempotent_id('39cb5c8f-9990-436f-9282-fc76a41d9bac')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:os-flavor-access:add_tenant_access")
+    def test_add_flavor_access(self):
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        self.client.add_flavor_access(
+            flavor_id=self.flavor_id, tenant_id=self.tenant_id)
+        self.addCleanup(self.client.remove_flavor_access,
+                        flavor_id=self.flavor_id, tenant_id=self.tenant_id)
+
+    @decorators.idempotent_id('61b8621f-52e4-473a-8d07-e228af8853d1')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:os-flavor-access:remove_tenant_access")
+    def test_remove_flavor_access(self):
+        self.client.add_flavor_access(
+            flavor_id=self.flavor_id, tenant_id=self.tenant_id)
+        self.addCleanup(test_utils.call_and_ignore_notfound_exc,
+                        self.client.remove_flavor_access,
+                        flavor_id=self.flavor_id, tenant_id=self.tenant_id)
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        self.client.remove_flavor_access(
+            flavor_id=self.flavor_id, tenant_id=self.tenant_id)
diff --git a/patrole_tempest_plugin/tests/api/compute/test_access_ips_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_hosts_rbac.py
similarity index 62%
rename from patrole_tempest_plugin/tests/api/compute/test_access_ips_rbac.py
rename to patrole_tempest_plugin/tests/api/compute/test_hosts_rbac.py
index ffeebc9..d74a78e 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_access_ips_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_hosts_rbac.py
@@ -1,5 +1,5 @@
-# Copyright 2017 AT&T Corporation.
-# All Rights Reserved.
+#    Copyright 2017 AT&T Corporation.
+#    All Rights Reserved.
 #
 #    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
@@ -23,23 +23,28 @@
 CONF = config.CONF
 
 
-class AccessIpsRbacTest(rbac_base.BaseV2ComputeRbacTest):
+class HostsAdminRbacTest(rbac_base.BaseV2ComputeAdminRbacTest):
 
-    def tearDown(self):
-        rbac_utils.switch_role(self, switchToRbacRole=True)
-        super(AccessIpsRbacTest, self).tearDown()
+    @classmethod
+    def setup_clients(cls):
+        super(HostsAdminRbacTest, cls).setup_clients()
+        cls.client = cls.os.hosts_client
 
     @classmethod
     def skip_checks(cls):
-        super(AccessIpsRbacTest, cls).skip_checks()
+        super(HostsAdminRbacTest, cls).skip_checks()
         if not CONF.compute_feature_enabled.api_extensions:
             raise cls.skipException(
                 '%s skipped as no compute extensions enabled' % cls.__name__)
 
-    @rbac_rule_validation.action(service="nova",
-                                 rule="os_compute_api:os-access-ips")
-    @decorators.idempotent_id('f5811ed1-95d4-4085-a69e-87e6bd958738')
-    def test_access_ip(self):
+    def tearDown(self):
+        rbac_utils.switch_role(self, switchToRbacRole=False)
+        super(HostsAdminRbacTest, self).tearDown()
+
+    @decorators.idempotent_id('035b7935-2fae-4218-8d37-27fa83097494')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:os-hosts")
+    def test_list_hosts(self):
         rbac_utils.switch_role(self, switchToRbacRole=True)
-        ipv4 = '127.0.0.1'
-        self.create_test_server(accessIPv4=ipv4)
+        self.client.list_hosts()['hosts']
diff --git a/patrole_tempest_plugin/tests/api/compute/test_ips_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_ips_rbac.py
new file mode 100644
index 0000000..cbe66f6
--- /dev/null
+++ b/patrole_tempest_plugin/tests/api/compute/test_ips_rbac.py
@@ -0,0 +1,74 @@
+#    Copyright 2017 AT&T Corporation.
+#    All Rights Reserved.
+#
+#    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 import config
+from tempest.lib import decorators
+
+from patrole_tempest_plugin import rbac_rule_validation
+from patrole_tempest_plugin.rbac_utils import rbac_utils
+from patrole_tempest_plugin.tests.api.compute import rbac_base
+
+CONF = config.CONF
+
+
+class IpsRbacTest(rbac_base.BaseV2ComputeRbacTest):
+
+    @classmethod
+    def setup_clients(cls):
+        super(IpsRbacTest, cls).setup_clients()
+        cls.client = cls.servers_client
+
+    @classmethod
+    def skip_checks(cls):
+        super(IpsRbacTest, cls).skip_checks()
+        if not CONF.compute_feature_enabled.api_extensions:
+            raise cls.skipException(
+                '%s skipped as no compute extensions enabled' % cls.__name__)
+        if not CONF.service_available.neutron:
+            raise cls.skipException(
+                '%s skipped as Neutron is required' % cls.__name__)
+
+    @classmethod
+    def setup_credentials(cls):
+        cls.prepare_instance_network()
+        super(IpsRbacTest, cls).setup_credentials()
+
+    @classmethod
+    def resource_setup(cls):
+        super(IpsRbacTest, cls).resource_setup()
+        cls.server = cls.create_test_server(wait_until='ACTIVE')
+
+    def tearDown(self):
+        rbac_utils.switch_role(self, switchToRbacRole=False)
+        super(IpsRbacTest, self).tearDown()
+
+    @decorators.idempotent_id('6886d360-0d86-4760-b1a3-882d81fbebcc')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:ips:index")
+    def test_list_addresses(self):
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        self.client.list_addresses(self.server['id'])['addresses']
+
+    @decorators.idempotent_id('fa43e7e5-0db9-48eb-9c6b-c11eb766b8e4')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:ips:show")
+    def test_list_addresses_by_network(self):
+        addresses = self.client.list_addresses(self.server['id'])['addresses']
+        address = next(iter(addresses))
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        self.client.list_addresses_by_network(
+            self.server['id'], address)[address]