diff --git a/config_tempest/accounts.py b/config_tempest/accounts.py new file mode 100644 index 00000000..24e997cd --- /dev/null +++ b/config_tempest/accounts.py @@ -0,0 +1,58 @@ +# Copyright 2018 Red Hat, Inc. +# All Rights Reserved. +# +# 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 yaml + + +def create_accounts_file(create, accounts_path, conf): + if create: + section = 'auth' + prefix = 'admin_' + else: + section = 'identity' + prefix = '' + write_accounts_file(accounts_path, + conf.get(section, prefix + 'username'), + conf.get(section, prefix + 'password'), + conf.get(section, prefix + 'project_name')) + conf.set("auth", "test_accounts_file", os.path.abspath(accounts_path)) + + +def write_accounts_file(path, username, password, project_name): + """Creates a minimal accounts.yaml file. + + Dumps provided credentials in a yaml format. + + :type path: string + :type username: string + :type password: string + :type project_name: string + """ + comments = "# A minimal accounts.yaml file\n" \ + "# Will likely not work with swift, since additional\n" \ + "# roles are required. For more documentation see:\n" \ + "# https://git.openstack.org/cgit/openstack/tempest/tree/etc/" \ + "accounts.yaml.sample\n\n" + accounts = [] + accounts.append({ + 'username': username, + 'project_name': project_name, + 'password': password + }) + with open(path, 'w') as outfile: + for line in comments: + outfile.write(line) + yaml.safe_dump(accounts, outfile, default_flow_style=False) diff --git a/config_tempest/main.py b/config_tempest/main.py index 1df908d3..08c6edb0 100755 --- a/config_tempest/main.py +++ b/config_tempest/main.py @@ -42,6 +42,7 @@ import logging import os import sys +import accounts from clients import ClientManager import constants as C from constants import LOG @@ -222,6 +223,9 @@ def get_arg_parser(): help='Run without admin creds') parser.add_argument('--test-accounts', default=None, metavar='PATH', help='Use accounts from accounts.yaml') + parser.add_argument('--create-accounts-file', default=None, + metavar='PATH', help="""Generate test accounts file + in the specified path.""") parser.add_argument('--image-disk-format', default=C.DEFAULT_IMAGE_FORMAT, help="""a format of an image to be uploaded to glance. Default is '%s'""" % C.DEFAULT_IMAGE_FORMAT) @@ -396,6 +400,14 @@ def config_tempest(**kwargs): services.set_supported_api_versions() services.set_service_extensions() + if kwargs.get('test_accounts') is None: + accounts_path = kwargs.get('create_accounts_file') + if accounts_path is not None: + LOG.info("Creating an accounts.yaml file in: %s", accounts_path) + accounts.create_accounts_file(kwargs.get('create', False), + accounts_path, + conf) + # remove all unwanted values if were specified if remove != {}: LOG.info("Removing configuration: %s", str(remove)) @@ -408,12 +420,13 @@ def main(): args = parse_arguments() cloud_creds = get_cloud_creds(args) config_tempest( - create=args.create, cloud_creds=cloud_creds, + create=args.create, + create_accounts_file=args.create_accounts_file, debug=args.debug, deployer_input=args.deployer_input, - image_path=args.image, image_disk_format=args.image_disk_format, + image_path=args.image, network_id=args.network_id, non_admin=args.non_admin, os_cloud=args.os_cloud, diff --git a/config_tempest/tests/test_accounts.py b/config_tempest/tests/test_accounts.py new file mode 100644 index 00000000..a3f23f13 --- /dev/null +++ b/config_tempest/tests/test_accounts.py @@ -0,0 +1,47 @@ +# Copyright 2018 Red Hat, Inc. +# All Rights Reserved. +# +# 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 mock +import os + +from config_tempest import accounts +from config_tempest.tests.base import BaseConfigTempestTest + + +class TestAccounts(BaseConfigTempestTest): + """Accounts test class + + Tests for create_accounts_file and write_accounts_file methods. + """ + + def setUp(self): + super(TestAccounts, self).setUp() + self.conf = self._get_conf("v2.0", "v3") + + @mock.patch('config_tempest.accounts.write_accounts_file') + def test_create_accounts_file(self, mock_write): + path = "./etc/accounts.yaml" + # credentials under auth section + accounts.create_accounts_file(True, path, self.conf) + mock_write.assert_called_with(path, "admin", "adminPass", + "adminTenant") + self.assertEqual(self.conf.get("auth", "test_accounts_file"), + os.path.abspath(path)) + + # credentials under identity section + accounts.create_accounts_file(False, path, self.conf) + mock_write.assert_called_with(path, "demo", "secret", "demo") + self.assertEqual(self.conf.get("auth", "test_accounts_file"), + os.path.abspath(path)) diff --git a/releasenotes/notes/Generate-accounts.yaml-6d929b3e78298579.yaml b/releasenotes/notes/Generate-accounts.yaml-6d929b3e78298579.yaml new file mode 100644 index 00000000..e72d46bf --- /dev/null +++ b/releasenotes/notes/Generate-accounts.yaml-6d929b3e78298579.yaml @@ -0,0 +1,13 @@ +--- +features: + - | + Because of integration of the tool with refstack-client a new parameter, + --create-accounts-file, is added. The parameter defines a path where + a minimal accounts.yaml file will be created. + The accounts.yaml file is important in refstack testing, because the tests + are run with non admin credentials. Therefor to make it easier for refstack + users, if a user doesn't specify a path to an existing accounts.yaml file + (via test_accounts parameter), but uses a create_accounts_file + instead, the file will be created in the specified location and set to + the tempest.conf as test_accounts_file under auth section. + For this feature a new requirement is specified - PyYAML. diff --git a/requirements.txt b/requirements.txt index a711d47a..0d74e6b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ tempest>=14.0.0 # Apache-2.0 requests>=2.10.0,!=2.12.2 # Apache-2.0 os-client-config>=1.26.0 # Apache-2.0 oslo.config>=3.23.0 # Apache-2.0 +PyYAML>=3.12 # MIT