diff --git a/README.rst b/README.rst index 470c2596..cc14721e 100755 --- a/README.rst +++ b/README.rst @@ -260,6 +260,12 @@ the *fullstack* test case:: $ tox -e fullstack +Also you can run *fullstack* test using credentials from openrc config file, +this requires you source openrc file in your DevStack or production environment. +In DevStack, you can using command "source openrc admin" in your devstack directory. +For production environment, please refer "Create OpenStack client environment scripts" +in OpenStack install guide. + Generating Documentation ------------------------ diff --git a/kuryr_libnetwork/controllers.py b/kuryr_libnetwork/controllers.py index 0eef35c9..711b8b55 100755 --- a/kuryr_libnetwork/controllers.py +++ b/kuryr_libnetwork/controllers.py @@ -10,7 +10,6 @@ # License for the specific language governing permissions and limitations # under the License. -import os_client_config from collections import defaultdict import flask @@ -22,7 +21,6 @@ import six import time from neutronclient.common import exceptions as n_exceptions -from neutronclient.v2_0 import client from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log @@ -48,27 +46,6 @@ SUBNET_POOLS_V4 = [cfg.CONF.neutron.default_subnetpool_v4] SUBNET_POOLS_V6 = [cfg.CONF.neutron.default_subnetpool_v6] -def _get_cloud_config_auth_data(cloud='devstack-admin'): - """Retrieves Keystone auth data to run functional tests - - Credentials are either read via os-client-config from the environment - or from a config file ('clouds.yaml'). Environment variables override - those from the config file. - - devstack produces a clouds.yaml with two named clouds - one named - 'devstack' which has user privs and one named 'devstack-admin' which - has admin privs. This function will default to getting the devstack-admin - cloud as that is the current expected behavior. - """ - cloud_config = os_client_config.OpenStackConfig().get_one_cloud(cloud) - return cloud_config.get_auth(), cloud_config.get_session() - - -def get_neutron_client_from_creds(): - auth_plugin, session = _get_cloud_config_auth_data() - return client.Client(session=session, auth=auth_plugin) - - def get_neutron_client(): """Creates the Neutron client for communicating with Neutron.""" return lib_utils.get_neutron_client() diff --git a/kuryr_libnetwork/tests/fullstack/kuryr_base.py b/kuryr_libnetwork/tests/fullstack/kuryr_base.py index 91eac317..3e3b61f5 100644 --- a/kuryr_libnetwork/tests/fullstack/kuryr_base.py +++ b/kuryr_libnetwork/tests/fullstack/kuryr_base.py @@ -11,10 +11,66 @@ # under the License. import docker +import os +from keystoneauth1 import identity +from keystoneauth1 import session as ks +from kuryr.lib._i18n import _LW +from neutronclient.v2_0 import client +import os_client_config +from oslo_log import log from oslotest import base -from kuryr_libnetwork import controllers + +LOG = log.getLogger(__name__) + + +def get_neutron_client_from_env(): + # We should catch KeyError exception with the purpose of + # source or configure openrc file. + auth_url = os.environ['OS_AUTH_URL'] + username = os.environ['OS_USERNAME'] + password = os.environ['OS_PASSWORD'] + project_name = os.environ['OS_PROJECT_NAME'] + + # Either project(user)_domain_name or project(user)_domain_id + # would be acceptable. + project_domain_name = os.environ.get("OS_PROJECT_DOMAIN_NAME") + project_domain_id = os.environ.get("OS_PROJECT_DOMAIN_ID") + user_domain_name = os.environ.get("OS_USER_DOMAIN_NAME") + user_domain_id = os.environ.get("OS_USER_DOMAIN_ID") + + auth = identity.Password(auth_url=auth_url, + username=username, + password=password, + project_name=project_name, + project_domain_id=project_domain_id, + project_domain_name=project_domain_name, + user_domain_id=user_domain_id, + user_domain_name=user_domain_name) + session = ks.Session(auth=auth) + return client.Client(session=session) + + +def _get_cloud_config_auth_data(cloud='devstack-admin'): + """Retrieves Keystone auth data to run functional tests + + Credentials are either read via os-client-config from the environment + or from a config file ('clouds.yaml'). Environment variables override + those from the config file. + + devstack produces a clouds.yaml with two named clouds - one named + 'devstack' which has user privs and one named 'devstack-admin' which + has admin privs. This function will default to getting the devstack-admin + cloud as that is the current expected behavior. + """ + cloud_config = os_client_config.OpenStackConfig().get_one_cloud(cloud) + return cloud_config.get_auth(), cloud_config.get_session() + + +def get_neutron_client_from_creds(): + auth_plugin, session = _get_cloud_config_auth_data() + return client.Client(session=session, auth=auth_plugin) class KuryrBaseTest(base.BaseTestCase): @@ -28,4 +84,14 @@ class KuryrBaseTest(base.BaseTestCase): super(KuryrBaseTest, self).setUp() self.docker_client = docker.Client( base_url='tcp://0.0.0.0:2375') - self.neutron_client = controllers.get_neutron_client_from_creds() + try: + self.neutron_client = get_neutron_client_from_env() + except Exception as e: + # We may missing or didn't source configured openrc file. + message = _LW('Missing environment variable %s in your local. ' + 'Please add it and also check other missing ' + 'environment variables. After that please source ' + 'the openrc file. ' + 'Trying credentials from DevStack cloud.yaml ...') + LOG.warning(message, e.args[0]) + self.neutron_client = get_neutron_client_from_creds() diff --git a/tox.ini b/tox.ini index b012e4f6..682fa5d7 100644 --- a/tox.ini +++ b/tox.ini @@ -22,6 +22,7 @@ commands = find . -type f -name "*.py[c|o]" -delete [testenv:fullstack] basepython = python2.7 setenv = OS_TEST_PATH=./kuryr_libnetwork/tests/fullstack +passenv = OS_* [testenv:debug] commands = oslo_debug_helper {posargs}