Remove singleton from RbacUtils constructor
Currently, the RbacUtils class in rbac_utils is a singleton,
which means the constructor is only called once. The problem with
that is when we instantiate RbacUtils in each rbac_base class,
we have to then also call switch_role(toggle_rbac_role=False).
We could do that inside the constructor to simplify the code,
but only if RbacUtils constructor stops being a singleton -- or
else admin credentials are not guaranteed during set up across
all test classes.
In addition, setting "rbac_utils = RbacUtils" at the end of rbac_utils
is pointless and only makes the code harder to read. This patch
removes that line of code and refactors the imports for rbac_utils where
necessary.
Change-Id: I778ae19b4bd0b71ab77984ae57dd96fd829a1fc4
Closes-Bug: #1688079
diff --git a/patrole_tempest_plugin/rbac_utils.py b/patrole_tempest_plugin/rbac_utils.py
index 4cddb8d..fe2d99f 100644
--- a/patrole_tempest_plugin/rbac_utils.py
+++ b/patrole_tempest_plugin/rbac_utils.py
@@ -18,7 +18,6 @@
from oslo_log import log as logging
import oslo_utils.uuidutils as uuid_utils
-import six
from tempest.common import credentials_factory as credentials
from tempest import config
@@ -29,19 +28,11 @@
LOG = logging.getLogger(__name__)
-class Singleton(type):
- _instances = {}
-
- def __call__(cls, *args, **kwargs):
- if cls not in cls._instances:
- cls._instances[cls] = super(Singleton, cls).__call__(*args,
- **kwargs)
- return cls._instances[cls]
-
-
-@six.add_metaclass(Singleton)
class RbacUtils(object):
+ def __init__(self, test_obj):
+ self.switch_role(test_obj, toggle_rbac_role=False)
+
# References the last value of `toggle_rbac_role` that was passed to
# `switch_role`. Used for ensuring that `switch_role` is correctly used
# in a test file, so that false positives are prevented. The key used
@@ -70,7 +61,7 @@
if not self.admin_role_id or not self.rbac_role_id:
self._get_roles()
- rbac_utils._validate_switch_role(self, test_obj, toggle_rbac_role)
+ self._validate_switch_role(test_obj, toggle_rbac_role)
if toggle_rbac_role:
self._add_role_to_user(self.rbac_role_id)
@@ -171,5 +162,3 @@
self.admin_role_id = admin_role_id
self.rbac_role_id = rbac_role_id
-
-rbac_utils = RbacUtils