Merge "Add floating IP test cases for RBAC."
diff --git a/patrole_tempest_plugin/rbac_auth.py b/patrole_tempest_plugin/rbac_auth.py
index 1afc7ae..40a46a7 100644
--- a/patrole_tempest_plugin/rbac_auth.py
+++ b/patrole_tempest_plugin/rbac_auth.py
@@ -15,15 +15,15 @@
 
 from oslo_log import log as logging
 
-from patrole_tempest_plugin import rbac_role_converter
+from patrole_tempest_plugin import rbac_policy_parser
 
 LOG = logging.getLogger(__name__)
 
 
 class RbacAuthority(object):
     def __init__(self, tenant_id, service=None):
-        self.converter = rbac_role_converter.RbacPolicyConverter(tenant_id,
-                                                                 service)
+        self.converter = rbac_policy_parser.RbacPolicyParser(tenant_id,
+                                                             service)
 
     def get_permission(self, rule_name, role):
         try:
diff --git a/patrole_tempest_plugin/rbac_role_converter.py b/patrole_tempest_plugin/rbac_policy_parser.py
similarity index 97%
rename from patrole_tempest_plugin/rbac_role_converter.py
rename to patrole_tempest_plugin/rbac_policy_parser.py
index bc6e006..860a53d 100644
--- a/patrole_tempest_plugin/rbac_role_converter.py
+++ b/patrole_tempest_plugin/rbac_policy_parser.py
@@ -19,15 +19,13 @@
 from oslo_log import log as logging
 from oslo_policy import generator
 from oslo_policy import policy
-from tempest import config
 
 from patrole_tempest_plugin import rbac_exceptions
 
-CONF = config.CONF
 LOG = logging.getLogger(__name__)
 
 
-class RbacPolicyConverter(object):
+class RbacPolicyParser(object):
     """A class for parsing policy rules into lists of allowed roles.
 
     RBAC testing requires that each rule in a policy file be broken up into
@@ -38,7 +36,7 @@
     """
 
     def __init__(self, tenant_id, service, path=None):
-        """Initialization of Policy Converter.
+        """Initialization of Rbac Policy Parser.
 
         Parses a policy file to create a dictionary, mapping policy actions to
         roles. If a policy file does not exist, checks whether the policy file
@@ -161,5 +159,5 @@
             LOG.debug("{0} not found in policy file.".format(apply_rule))
             return False
         except Exception as e:
-            LOG.debug("Exception: {0} for rule: {1}.".format(e, rule))
+            LOG.debug("Exception: {0} for rule: {1}.".format(e, apply_rule))
             return False
diff --git a/patrole_tempest_plugin/tests/api/compute/test_config_drive_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_config_drive_rbac.py
new file mode 100644
index 0000000..ed4edfd
--- /dev/null
+++ b/patrole_tempest_plugin/tests/api/compute/test_config_drive_rbac.py
@@ -0,0 +1,53 @@
+#    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.lib import decorators
+from tempest import test
+
+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
+
+
+class ConfigDriveRbacTest(rbac_base.BaseV2ComputeRbacTest):
+
+    @classmethod
+    def setup_clients(cls):
+        super(ConfigDriveRbacTest, cls).setup_clients()
+        cls.client = cls.servers_client
+
+    @classmethod
+    def skip_checks(cls):
+        super(ConfigDriveRbacTest, cls).skip_checks()
+        if not test.is_extension_enabled('os-config-drive', 'compute'):
+            msg = "%s skipped as os-config-drive extension not enabled." \
+                  % cls.__name__
+            raise cls.skipException(msg)
+
+    def tearDown(self):
+        rbac_utils.switch_role(self, switchToRbacRole=False)
+        super(ConfigDriveRbacTest, self).tearDown()
+
+    @decorators.idempotent_id('55c62ef7-b72b-4970-acc6-05b0a4316e5d')
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:os-config-drive")
+    def test_create_test_server_with_config_drive(self):
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        # NOTE(felipemonteiro): This policy action is always enforced,
+        # regardless whether the config_drive flag is set to true or false.
+        # However, it has been explicitly set to true below, in case that this
+        # behavior ever changes in the future.
+        self.create_test_server(config_drive=True)
diff --git a/patrole_tempest_plugin/tests/api/compute/test_hypervisor_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_hypervisor_rbac.py
index e495b7d..e50bb9e 100644
--- a/patrole_tempest_plugin/tests/api/compute/test_hypervisor_rbac.py
+++ b/patrole_tempest_plugin/tests/api/compute/test_hypervisor_rbac.py
@@ -13,15 +13,13 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-from tempest import config
 from tempest.lib import decorators
+from tempest import test
 
 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 HypervisorAdminRbacTest(rbac_base.BaseV2ComputeAdminRbacTest):
 
@@ -33,9 +31,10 @@
     @classmethod
     def skip_checks(cls):
         super(HypervisorAdminRbacTest, 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 test.is_extension_enabled('os-hypervisors', 'compute'):
+            msg = "%s skipped as os-hypervisors extension not enabled." \
+                  % cls.__name__
+            raise cls.skipException(msg)
 
     def tearDown(self):
         rbac_utils.switch_role(self, switchToRbacRole=False)
@@ -46,4 +45,5 @@
         service="nova",
         rule="os_compute_api:os-hypervisors")
     def test_list_hypervisors(self):
+        rbac_utils.switch_role(self, switchToRbacRole=True)
         self.client.list_hypervisors()['hypervisors']
diff --git a/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py b/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py
new file mode 100644
index 0000000..9e3b259
--- /dev/null
+++ b/patrole_tempest_plugin/tests/api/compute/test_server_actions_rbac.py
@@ -0,0 +1,81 @@
+#    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.common import waiters
+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 ServerActionsRbacTest(rbac_base.BaseV2ComputeRbacTest):
+
+    def tearDown(self):
+        rbac_utils.switch_role(self, switchToRbacRole=False)
+        super(ServerActionsRbacTest, self).tearDown()
+
+    @classmethod
+    def setup_clients(cls):
+        super(ServerActionsRbacTest, cls).setup_clients()
+        cls.client = cls.servers_client
+
+    @classmethod
+    def skip_checks(cls):
+        super(ServerActionsRbacTest, 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.compute_feature_enabled.interface_attach:
+            raise cls.skipException(
+                '%s skipped as interface attachment is not available'
+                % cls.__name__)
+
+    @classmethod
+    def resource_setup(cls):
+        cls.set_validation_resources()
+        super(ServerActionsRbacTest, cls).resource_setup()
+        cls.server_id = cls.create_test_server(wait_until='ACTIVE',
+                                               validatable=True)['id']
+
+    def _test_start_server(self):
+        self.client.start_server(self.server_id)
+        waiters.wait_for_server_status(self.client, self.server_id,
+                                       'ACTIVE')
+
+    def _test_stop_server(self):
+        self.client.stop_server(self.server_id)
+        waiters.wait_for_server_status(self.client, self.server_id,
+                                       'SHUTOFF')
+
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:servers:stop")
+    @decorators.idempotent_id('ab4a17d2-166f-4a6d-9944-f17baa576cf2')
+    def test_stop_server(self):
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        self._test_stop_server()
+
+    @rbac_rule_validation.action(
+        service="nova",
+        rule="os_compute_api:servers:start")
+    @decorators.idempotent_id('8876bfa9-4d10-406e-a335-a57e451abb12')
+    def test_start_server(self):
+        self._test_stop_server()
+        rbac_utils.switch_role(self, switchToRbacRole=True)
+        self._test_start_server()
diff --git a/tests/test_rbac_role_converter.py b/tests/test_rbac_policy_parser.py
similarity index 93%
rename from tests/test_rbac_role_converter.py
rename to tests/test_rbac_policy_parser.py
index 09fa081..cc0fc4f 100644
--- a/tests/test_rbac_role_converter.py
+++ b/tests/test_rbac_policy_parser.py
@@ -19,7 +19,7 @@
 from tempest import config
 from tempest.tests import base
 
-from patrole_tempest_plugin import rbac_role_converter
+from patrole_tempest_plugin import rbac_policy_parser
 
 CONF = config.CONF
 
@@ -43,12 +43,12 @@
                                                'resources',
                                                'tenant_rbac_policy.json')
 
-    @mock.patch.object(rbac_role_converter, 'LOG', autospec=True)
+    @mock.patch.object(rbac_policy_parser, 'LOG', autospec=True)
     def test_custom_policy(self, m_log):
         default_roles = ['zero', 'one', 'two', 'three', 'four',
                          'five', 'six', 'seven', 'eight', 'nine']
 
-        converter = rbac_role_converter.RbacPolicyConverter(
+        converter = rbac_policy_parser.RbacPolicyParser(
             None, "test", self.custom_policy_file)
 
         expected = {
@@ -76,7 +76,7 @@
                 self.assertFalse(converter.allowed(rule, role))
 
     def test_admin_policy_file_with_admin_role(self):
-        converter = rbac_role_converter.RbacPolicyConverter(
+        converter = rbac_policy_parser.RbacPolicyParser(
             None, "test", self.admin_policy_file)
 
         role = 'admin'
@@ -94,7 +94,7 @@
             self.assertFalse(allowed)
 
     def test_admin_policy_file_with_member_role(self):
-        converter = rbac_role_converter.RbacPolicyConverter(
+        converter = rbac_policy_parser.RbacPolicyParser(
             None, "test", self.admin_policy_file)
 
         role = 'Member'
@@ -113,7 +113,7 @@
             self.assertFalse(allowed)
 
     def test_admin_policy_file_with_context_is_admin(self):
-        converter = rbac_role_converter.RbacPolicyConverter(
+        converter = rbac_policy_parser.RbacPolicyParser(
             None, "test", self.alt_admin_policy_file)
 
         role = 'fake_admin'
@@ -147,7 +147,7 @@
         network:tenant_id pass.
         """
         test_tenant_id = mock.sentinel.tenant_id
-        converter = rbac_role_converter.RbacPolicyConverter(
+        converter = rbac_policy_parser.RbacPolicyParser(
             test_tenant_id, "test", self.tenant_policy_file)
 
         # Check whether Member role can perform expected actions.