Create fixture to translate OS_* env variables to Keystone credentials

Change-Id: I5400a9ae4c120a55653aecbc53c6349ca7cdb5d8
This commit is contained in:
pkomarov 2019-08-20 01:19:28 +03:00 committed by Federico Ressi
parent 99f0636433
commit f0f9c3c551
4 changed files with 70 additions and 21 deletions

View File

@ -32,7 +32,11 @@ get_keystone_credentials = _credentials.get_keystone_credentials
default_keystone_credentials = _credentials.default_keystone_credentials default_keystone_credentials = _credentials.default_keystone_credentials
KeystoneCredentials = _credentials.KeystoneCredentials KeystoneCredentials = _credentials.KeystoneCredentials
KeystoneCredentialsFixture = _credentials.KeystoneCredentialsFixture KeystoneCredentialsFixture = _credentials.KeystoneCredentialsFixture
EnvironKeystoneCredentialsFixture = \
_credentials.EnvironKeystoneCredentialsFixture
InvalidKeystoneCredentials = _credentials.InvalidKeystoneCredentials InvalidKeystoneCredentials = _credentials.InvalidKeystoneCredentials
DEFAULT_KEYSTONE_CREDENTIALS_FIXTURES = \
_credentials.DEFAULT_KEYSTONE_CREDENTIALS_FIXTURES
has_service = _services.has_service has_service = _services.has_service
is_service_missing = _services.is_service_missing is_service_missing = _services.is_service_missing

View File

@ -14,6 +14,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import collections import collections
import os
import sys import sys
from oslo_log import log from oslo_log import log
@ -147,25 +148,40 @@ class KeystoneCredentialsFixture(tobiko.SharedFixture):
class EnvironKeystoneCredentialsFixture(KeystoneCredentialsFixture): class EnvironKeystoneCredentialsFixture(KeystoneCredentialsFixture):
environ = None
def __init__(self, credentials=None, environ=None):
super(EnvironKeystoneCredentialsFixture, self).__init__(
credentials=credentials)
if environ is not None:
self.environ = environ
def setup_fixture(self):
if self.environ is None:
self.environ = self.get_environ()
super(EnvironKeystoneCredentialsFixture, self).setup_fixture()
def get_environ(self):
return os.environ
def get_credentials(self): def get_credentials(self):
from tobiko import config auth_url = self.get_env('OS_AUTH_URL')
auth_url = config.get_env('OS_AUTH_URL')
if not auth_url: if not auth_url:
LOG.debug("OS_AUTH_URL environment variable not defined") LOG.debug("OS_AUTH_URL environment variable not defined")
return None return None
api_version = ( api_version = (
config.get_int_env('OS_IDENTITY_API_VERSION') or self.get_int_env('OS_IDENTITY_API_VERSION') or
api_version_from_url(auth_url)) api_version_from_url(auth_url))
username = ( username = (
config.get_env('OS_USERNAME') or self.get_env('OS_USERNAME') or
config.get_env('OS_USER_ID')) self.get_env('OS_USER_ID'))
password = config.get_env('OS_PASSWORD') password = self.get_env('OS_PASSWORD')
project_name = ( project_name = (
config.get_env('OS_PROJECT_NAME') or self.get_env('OS_PROJECT_NAME') or
config.get_env('OS_TENANT_NAME') or self.get_env('OS_TENANT_NAME') or
config.get_env('OS_PROJECT_ID') or self.get_env('OS_PROJECT_ID') or
config.get_env('OS_TENANT_ID')) self.get_env('OS_TENANT_ID'))
if api_version == 2: if api_version == 2:
return keystone_credentials( return keystone_credentials(
api_version=api_version, api_version=api_version,
@ -175,16 +191,16 @@ class EnvironKeystoneCredentialsFixture(KeystoneCredentialsFixture):
project_name=project_name) project_name=project_name)
else: else:
domain_name = ( domain_name = (
config.get_env('OS_DOMAIN_NAME') or self.get_env('OS_DOMAIN_NAME') or
config.get_env('OS_DOMAIN_ID')) self.get_env('OS_DOMAIN_ID'))
user_domain_name = ( user_domain_name = (
config.get_env('OS_USER_DOMAIN_NAME') or self.get_env('OS_USER_DOMAIN_NAME') or
config.get_env('OS_USER_DOMAIN_ID')) self.get_env('OS_USER_DOMAIN_ID'))
project_domain_name = ( project_domain_name = (
config.get_env('OS_PROJECT_DOMAIN_NAME')) self.get_env('OS_PROJECT_DOMAIN_NAME'))
project_domain_id = ( project_domain_id = (
config.get_env('OS_PROJECT_DOMAIN_ID')) self.get_env('OS_PROJECT_DOMAIN_ID'))
trust_id = config.get_env('OS_TRUST_ID') trust_id = self.get_env('OS_TRUST_ID')
return keystone_credentials( return keystone_credentials(
api_version=api_version, api_version=api_version,
auth_url=auth_url, auth_url=auth_url,
@ -197,6 +213,15 @@ class EnvironKeystoneCredentialsFixture(KeystoneCredentialsFixture):
project_domain_id=project_domain_id, project_domain_id=project_domain_id,
trust_id=trust_id) trust_id=trust_id)
def get_env(self, name):
return self.environ.get(name, None)
def get_int_env(self, name):
value = self.get_env(name=name)
if value is not None:
value = int(value)
return value
class ConfigKeystoneCredentialsFixture(KeystoneCredentialsFixture): class ConfigKeystoneCredentialsFixture(KeystoneCredentialsFixture):

View File

@ -41,9 +41,17 @@ OPTIONS = [
def register_tobiko_options(conf): def register_tobiko_options(conf):
conf.register_opts(group=cfg.OptGroup(GROUP_NAME), opts=OPTIONS) conf.register_opts(group=cfg.OptGroup(GROUP_NAME), opts=OPTIONS)
def list_options(): def list_options():
return [(GROUP_NAME, itertools.chain(OPTIONS))] return [(GROUP_NAME, itertools.chain(OPTIONS))]
def setup_tobiko_config(conf):
# pylint: disable=unused-argument
from tobiko.openstack import keystone
from tobiko.tripleo import undercloud
if undercloud.has_undercloud():
keystone.DEFAULT_KEYSTONE_CREDENTIALS_FIXTURES.append(
undercloud.OvercloudKeystoneCredentialsFixture)

View File

@ -1,9 +1,10 @@
from __future__ import absolute_import from __future__ import absolute_import
import tobiko import tobiko
from tobiko import config
from tobiko.openstack import keystone
from tobiko.shell import ssh from tobiko.shell import ssh
from tobiko.shell import sh from tobiko.shell import sh
from tobiko import config
CONF = config.CONF CONF = config.CONF
@ -35,14 +36,25 @@ def load_overcloud_rcfile():
return fetch_os_env(rcfile=CONF.tobiko.tripleo.overcloud_rcfile) return fetch_os_env(rcfile=CONF.tobiko.tripleo.overcloud_rcfile)
class UndercloudKeystoneCredentialsFixture(
keystone.EnvironKeystoneCredentialsFixture):
def get_environ(self):
return load_undercloud_rcfile()
class OvercloudKeystoneCredentialsFixture(
keystone.EnvironKeystoneCredentialsFixture):
def get_environ(self):
return load_overcloud_rcfile()
def has_undercloud(): def has_undercloud():
host_config = undercloud_host_config() host_config = undercloud_host_config()
return bool(host_config.hostname) return bool(host_config.hostname)
skip_if_missing_undercloud = tobiko.skip_unless( skip_if_missing_undercloud = tobiko.skip_unless(
'TripleO Undercloud hostname is not configured', 'TripleO undercloud hostname not configured', has_undercloud)
has_undercloud)
class UndecloudHostConfig(tobiko.SharedFixture): class UndecloudHostConfig(tobiko.SharedFixture):