Fix the feature what creating the deployment from sysenv

We can remove the duplicated codes via creating the deployment
with the Env manager.

Change-Id: Ied2008e895505909ed0c78f16614a8d60101b811
This commit is contained in:
chenhb 2019-04-18 14:21:19 +08:00
parent a01a60bc0f
commit 04aeef7da6
6 changed files with 56 additions and 259 deletions

View File

@ -29,6 +29,7 @@ from rally.common import fileutils
from rally.common import logging
from rally.common import utils
from rally.common import yamlutils as yaml
from rally.env import env_mgr
from rally import exceptions
from rally import plugins
@ -82,8 +83,8 @@ class DeploymentCommands(object):
"""
if fromenv:
# TODO(astudenov): move this to Credential plugin
config = {"openstack": envutils.get_creds_from_env_vars()}
result = env_mgr.EnvManager.create_spec_from_sys_environ()
config = result["spec"]
else:
if not filename:
config = {}

62
rally/cli/envutils.py Normal file → Executable file
View File

@ -19,7 +19,6 @@ import decorator
from rally.common import fileutils
from rally import exceptions
from rally.utils import strutils
PATH_GLOBALS = "~/.rally/globals"
ENV_ENV = "RALLY_ENV"
@ -111,64 +110,3 @@ def with_default_verifier_id(cli_arg_name="id"):
with_default_task_id = default_from_global("task_id", ENV_TASK, "uuid")
with_default_verification_uuid = default_from_global("verification_uuid",
ENV_VERIFICATION, "uuid")
def get_creds_from_env_vars():
required_env_vars = ["OS_AUTH_URL", "OS_USERNAME", "OS_PASSWORD"]
missing_env_vars = [v for v in required_env_vars if v not in os.environ]
if missing_env_vars:
msg = ("The following environment variables are "
"required but not set: %s" % " ".join(missing_env_vars))
raise exceptions.ValidationError(message=msg)
creds = {
"auth_url": os.environ["OS_AUTH_URL"],
"admin": {
"username": os.environ["OS_USERNAME"],
"password": os.environ["OS_PASSWORD"],
"tenant_name": get_project_name_from_env()
},
"endpoint_type": get_endpoint_type_from_env(),
"endpoint": os.environ.get("OS_ENDPOINT"),
"region_name": os.environ.get("OS_REGION_NAME", ""),
"https_cacert": os.environ.get("OS_CACERT", ""),
"https_insecure": strutils.bool_from_string(
os.environ.get("OS_INSECURE")),
"profiler_hmac_key": os.environ.get("OSPROFILER_HMAC_KEY"),
"profiler_conn_str": os.environ.get("OSPROFILER_CONN_STR")
}
user_domain_name = os.environ.get("OS_USER_DOMAIN_NAME")
project_domain_name = os.environ.get("OS_PROJECT_DOMAIN_NAME")
identity_api_version = os.environ.get(
"OS_IDENTITY_API_VERSION", os.environ.get("IDENTITY_API_VERSION"))
if (identity_api_version == "3" or
(identity_api_version is None and
(user_domain_name or project_domain_name))):
# it is Keystone v3 and it has another config scheme
creds["admin"]["project_name"] = creds["admin"].pop("tenant_name")
creds["admin"]["user_domain_name"] = user_domain_name or "Default"
project_domain_name = project_domain_name or "Default"
creds["admin"]["project_domain_name"] = project_domain_name
return creds
def get_project_name_from_env():
tenant_name = os.environ.get("OS_PROJECT_NAME",
os.environ.get("OS_TENANT_NAME"))
if tenant_name is None:
raise exceptions.ValidationError("Either the OS_PROJECT_NAME or "
"OS_TENANT_NAME environment variable "
"is required, but neither is set.")
return tenant_name
def get_endpoint_type_from_env():
endpoint_type = os.environ.get("OS_ENDPOINT_TYPE",
os.environ.get("OS_INTERFACE"))
if endpoint_type and "URL" in endpoint_type:
endpoint_type = endpoint_type.replace("URL", "")
return endpoint_type

View File

@ -41,3 +41,13 @@ class GoodPlatform(platform.Platform):
def info(self):
return {"info": {"a": 1}}
@classmethod
def create_spec_from_sys_environ(cls, sys_environ):
spec = {
"auth_url": sys_environ.get("OS_AUTH_URL"),
"username": sys_environ.get("OS_USERNAME"),
"password": sys_environ.get("OS_PASSWORD")
}
return {"spec": spec, "available": True, "message": "Available"}

View File

@ -0,0 +1,39 @@
# 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.
import os
import unittest
from tests.functional import utils
class DeploymentTestCase(unittest.TestCase):
def test_create_deployment_from_env(self):
os.environ.update(
{
"OS_AUTH_URL": "http://fake",
"OS_USERNAME": "fake",
"OS_PASSWORD": "fake"
}
)
rally = utils.Rally(plugin_path="tests/functional/extra")
rally("deployment create --name fromenv --fromenv")
spec = rally("deployment config", getjson=True)
self.assertEqual({
"good@fake": {
"auth_url": "http://fake",
"username": "fake",
"password": "fake"
}
}, spec)

75
tests/unit/cli/commands/test_deployment.py Normal file → Executable file
View File

@ -52,79 +52,12 @@ class DeploymentCommandsTestCase(test.TestCase):
self.fake_api.deployment.create.assert_called_once_with(
config={}, name="fake_deploy")
@mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url",
"OS_USERNAME": "fake_username",
"OS_PASSWORD": "fake_password",
"OS_TENANT_NAME": "fake_tenant_name",
"OS_REGION_NAME": "fake_region_name",
"OS_ENDPOINT_TYPE": "fake_endpoint_typeURL",
"OS_ENDPOINT": "fake_endpoint",
"OS_INSECURE": "True",
"OS_CACERT": "fake_cacert",
"RALLY_DEPLOYMENT": "fake_deployment_id",
"OSPROFILER_HMAC_KEY": "fake_hmac_key",
"OSPROFILER_CONN_STR": "fake_conn_str"})
@mock.patch("rally.cli.commands.deployment.DeploymentCommands.list")
def test_createfromenv_keystonev2(self, mock_list):
@mock.patch("rally.env.env_mgr.EnvManager.create_spec_from_sys_environ",
return_value={"spec": {"auth_url": "http://fake"}})
def test_createfromenv(self, mock_create_spec_from_sys_environ):
self.deployment.create(self.fake_api, "from_env", True)
self.fake_api.deployment.create.assert_called_once_with(
config={
"openstack": {
"auth_url": "fake_auth_url",
"region_name": "fake_region_name",
"endpoint_type": "fake_endpoint_type",
"endpoint": "fake_endpoint",
"admin": {
"username": "fake_username",
"password": "fake_password",
"tenant_name": "fake_tenant_name"
},
"https_insecure": True,
"https_cacert": "fake_cacert",
"profiler_hmac_key": "fake_hmac_key",
"profiler_conn_str": "fake_conn_str"
}
},
name="from_env"
)
@mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url",
"OS_USERNAME": "fake_username",
"OS_PASSWORD": "fake_password",
"OS_TENANT_NAME": "fake_tenant_name",
"OS_REGION_NAME": "fake_region_name",
"OS_ENDPOINT_TYPE": "fake_endpoint_typeURL",
"OS_PROJECT_DOMAIN_NAME": "fake_pdn",
"OS_USER_DOMAIN_NAME": "fake_udn",
"OS_ENDPOINT": "fake_endpoint",
"OS_INSECURE": "True",
"OS_CACERT": "fake_cacert",
"RALLY_DEPLOYMENT": "fake_deployment_id",
"OSPROFILER_HMAC_KEY": "fake_hmac_key",
"OSPROFILER_CONN_STR": "fake_conn_str"})
@mock.patch("rally.cli.commands.deployment.DeploymentCommands.list")
def test_createfromenv_keystonev3(self, mock_list):
self.deployment.create(self.fake_api, "from_env", True)
self.fake_api.deployment.create.assert_called_once_with(
config={
"openstack": {
"auth_url": "fake_auth_url",
"region_name": "fake_region_name",
"endpoint_type": "fake_endpoint_type",
"endpoint": "fake_endpoint",
"admin": {
"username": "fake_username",
"password": "fake_password",
"user_domain_name": "fake_udn",
"project_domain_name": "fake_pdn",
"project_name": "fake_tenant_name"
},
"https_insecure": True,
"https_cacert": "fake_cacert",
"profiler_hmac_key": "fake_hmac_key",
"profiler_conn_str": "fake_conn_str"
}
},
config={"auth_url": "http://fake"},
name="from_env"
)

View File

@ -103,127 +103,3 @@ class EnvUtilsTestCase(test.TestCase):
def test_clear_env(self, mock_update_env_file, mock_path_exists):
envutils.clear_env()
self.assertEqual({}, os.environ)
@mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url",
"OS_USERNAME": "fake_username",
"OS_PASSWORD": "fake_password",
"OS_TENANT_NAME": "fake_tenant_name",
"OS_REGION_NAME": "fake_region_name",
"OS_ENDPOINT_TYPE": "fake_endpoint_typeURL",
"OS_ENDPOINT": "fake_endpoint",
"OS_INSECURE": "True",
"OSPROFILER_HMAC_KEY": "fake_hmac_key",
"OSPROFILER_CONN_STR": "fake_conn_str",
"OS_CACERT": "fake_cacert"})
def test_get_creds_from_env_vars_keystone_v2(self):
expected_creds = {
"auth_url": "fake_auth_url",
"admin": {
"username": "fake_username",
"password": "fake_password",
"tenant_name": "fake_tenant_name"
},
"endpoint_type": "fake_endpoint_type",
"endpoint": "fake_endpoint",
"region_name": "fake_region_name",
"https_cacert": "fake_cacert",
"https_insecure": True,
"profiler_hmac_key": "fake_hmac_key",
"profiler_conn_str": "fake_conn_str"
}
creds = envutils.get_creds_from_env_vars()
self.assertEqual(expected_creds, creds)
@mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url",
"OS_USERNAME": "fake_username",
"OS_PASSWORD": "fake_password",
"OS_TENANT_NAME": "fake_tenant_name",
"OS_REGION_NAME": "fake_region_name",
"OS_ENDPOINT_TYPE": "fake_endpoint_typeURL",
"OS_ENDPOINT": "fake_endpoint",
"OS_INSECURE": "True",
"OS_PROJECT_DOMAIN_NAME": "fake_pdn",
"OS_USER_DOMAIN_NAME": "fake_udn",
"OSPROFILER_HMAC_KEY": "fake_hmac_key",
"OSPROFILER_CONN_STR": "fake_conn_str",
"OS_CACERT": "fake_cacert"})
def test_get_creds_from_env_vars_keystone_v3(self):
expected_creds = {
"auth_url": "fake_auth_url",
"admin": {
"username": "fake_username",
"password": "fake_password",
"user_domain_name": "fake_udn",
"project_domain_name": "fake_pdn",
"project_name": "fake_tenant_name"
},
"endpoint_type": "fake_endpoint_type",
"endpoint": "fake_endpoint",
"region_name": "fake_region_name",
"https_cacert": "fake_cacert",
"https_insecure": True,
"profiler_hmac_key": "fake_hmac_key",
"profiler_conn_str": "fake_conn_str"
}
creds = envutils.get_creds_from_env_vars()
self.assertEqual(expected_creds, creds)
@mock.patch.dict(os.environ, {"OS_AUTH_URL": "fake_auth_url",
"OS_PASSWORD": "fake_password",
"OS_REGION_NAME": "fake_region_name",
"OS_ENDPOINT": "fake_endpoint",
"OS_INSECURE": "True",
"OSPROFILER_HMAC_KEY": "fake_hmac_key",
"OSPROFILER_CONN_STR": "fake_conn_str",
"OS_CACERT": "fake_cacert"})
def test_get_creds_from_env_vars_when_required_vars_missing(self):
if "OS_USERNAME" in os.environ:
del os.environ["OS_USERNAME"]
self.assertRaises(exceptions.ValidationError,
envutils.get_creds_from_env_vars)
@mock.patch.dict(os.environ, {"OS_TENANT_NAME": "fake_tenant_name"},
clear=True)
def test_get_project_name_from_env_when_tenant_name(self):
project_name = envutils.get_project_name_from_env()
self.assertEqual("fake_tenant_name", project_name)
@mock.patch.dict(os.environ, {"OS_PROJECT_NAME": "fake_project_name"},
clear=True)
def test_get_project_name_from_env_when_project_name(self):
project_name = envutils.get_project_name_from_env()
self.assertEqual("fake_project_name", project_name)
@mock.patch.dict(os.environ, {"OS_TENANT_NAME": "fake_tenant_name",
"OS_PROJECT_NAME": "fake_project_name"})
def test_get_project_name_from_env_when_both(self):
project_name = envutils.get_project_name_from_env()
self.assertEqual("fake_project_name", project_name)
@mock.patch.dict(os.environ, values={}, clear=True)
def test_get_project_name_from_env_when_neither(self):
self.assertRaises(exceptions.ValidationError,
envutils.get_project_name_from_env)
@mock.patch.dict(os.environ, {"OS_ENDPOINT_TYPE": "fake_endpoint_typeURL"},
clear=True)
def test_get_endpoint_type_from_env_when_endpoint_type(self):
endpoint_type = envutils.get_endpoint_type_from_env()
self.assertEqual("fake_endpoint_type", endpoint_type)
@mock.patch.dict(os.environ, {"OS_INTERFACE": "fake_interface"},
clear=True)
def test_get_endpoint_type_from_env_when_interface(self):
endpoint_type = envutils.get_endpoint_type_from_env()
self.assertEqual("fake_interface", endpoint_type)
@mock.patch.dict(os.environ, {"OS_ENDPOINT_TYPE": "fake_endpoint_typeURL",
"OS_INTERFACE": "fake_interface"})
def test_get_endpoint_type_from_env_when_both(self):
endpoint_type = envutils.get_endpoint_type_from_env()
self.assertEqual("fake_endpoint_type", endpoint_type)
@mock.patch.dict(os.environ, values={}, clear=True)
def test_get_endpoint_type_from_env_when_neither(self):
endpoint_type = envutils.get_endpoint_type_from_env()
self.assertIsNone(endpoint_type)