Merge "Moving "os.environ" things to envutils.py module"

This commit is contained in:
Jenkins 2015-12-16 12:12:12 +00:00 committed by Gerrit Code Review
commit 8b2f471c43
4 changed files with 73 additions and 38 deletions

View File

@ -87,34 +87,8 @@ class DeploymentCommands(object):
"""
if fromenv:
required_env_vars = ["OS_USERNAME", "OS_PASSWORD", "OS_AUTH_URL",
"OS_TENANT_NAME"]
unavailable_vars = [v for v in required_env_vars
if v not in os.environ]
if unavailable_vars:
print("The following environment variables are required but "
"not set: %s" % " ".join(unavailable_vars))
return(1)
config = {
"type": "ExistingCloud",
"auth_url": os.environ["OS_AUTH_URL"],
"endpoint": os.environ.get("OS_ENDPOINT"),
"admin": {
"username": os.environ["OS_USERNAME"],
"password": os.environ["OS_PASSWORD"],
"tenant_name": os.environ["OS_TENANT_NAME"]
},
"https_cacert": os.environ.get("OS_CACERT", ""),
"https_insecure": False
}
region_name = os.environ.get("OS_REGION_NAME")
if region_name and region_name != "None":
config["region_name"] = region_name
https_insecure = os.environ.get("OS_INSECURE")
if https_insecure and https_insecure.lower() == "true":
config["https_insecure"] = True
config = {"type": "ExistingCloud"}
config.update(envutils.get_creds_from_env_vars())
else:
if not filename:
print("Either --filename or --fromenv is required")

View File

@ -16,6 +16,7 @@
import os
import decorator
from oslo_utils import strutils
from rally.common import fileutils
from rally.common.i18n import _
@ -80,3 +81,29 @@ def with_default_deployment(cli_arg_name="uuid"):
with_default_task_id = default_from_global("task_id", ENV_TASK, "uuid")
with_default_verification_id = default_from_global(
"verification_uuid", ENV_VERIFICATION, "uuid")
def get_creds_from_env_vars():
required_env_vars = ["OS_AUTH_URL", "OS_USERNAME",
"OS_PASSWORD", "OS_TENANT_NAME"]
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": os.environ["OS_TENANT_NAME"]
},
"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"))
}
return creds

View File

@ -14,10 +14,10 @@
# under the License.
import abc
import os
from oslo_config import cfg
from rally.cli import envutils
from rally.common.i18n import _
from rally.common import logging
from rally.common import objects
@ -650,17 +650,17 @@ class Clients(object):
@classmethod
def create_from_env(cls):
https_insecure = os.environ.get("OS_INSECURE")
creds = envutils.get_creds_from_env_vars()
return cls(
objects.Credential(
os.environ["OS_AUTH_URL"],
os.environ["OS_USERNAME"],
os.environ["OS_PASSWORD"],
os.environ.get("OS_TENANT_NAME"),
region_name=os.environ.get("OS_REGION_NAME"),
https_cacert=os.environ.get("OS_CACERT", ""),
https_insecure=(True if https_insecure and
https_insecure.lower() == "true" else False)
creds["auth_url"],
creds["admin"]["username"],
creds["admin"]["password"],
creds["admin"]["tenant_name"],
endpoint=creds["endpoint"],
region_name=creds["region_name"],
https_cacert=creds["https_cacert"],
https_insecure=creds["https_insecure"]
))
def clear(self):

View File

@ -103,3 +103,37 @@ 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": "fake_endpoint",
"OS_INSECURE": "True",
"OS_CACERT": "fake_cacert"})
def test_get_creds_from_env_vars(self):
expected_creds = {
"auth_url": "fake_auth_url",
"admin": {
"username": "fake_username",
"password": "fake_password",
"tenant_name": "fake_tenant_name"
},
"endpoint": "fake_endpoint",
"region_name": "fake_region_name",
"https_cacert": "fake_cacert",
"https_insecure": True
}
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",
"OS_CACERT": "fake_cacert"})
def test_get_creds_from_env_vars_when_required_vars_missing(self):
self.assertRaises(exceptions.ValidationError,
envutils.get_creds_from_env_vars)