Generate accounts.yaml

Generate accounts.yaml file when create_accounts_file
is specified and test_accounts is not.

Story: 2001693
Task: 8685

Change-Id: I7ed8390b29a430d97e1846f8f868f5b6af8eb02b
This commit is contained in:
Martin Kopec 2018-05-28 09:30:07 +00:00
parent 4912283b03
commit 243ab6cb47
5 changed files with 134 additions and 2 deletions

View File

@ -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)

View File

@ -42,6 +42,7 @@ import logging
import os import os
import sys import sys
import accounts
from clients import ClientManager from clients import ClientManager
import constants as C import constants as C
from constants import LOG from constants import LOG
@ -222,6 +223,9 @@ def get_arg_parser():
help='Run without admin creds') help='Run without admin creds')
parser.add_argument('--test-accounts', default=None, metavar='PATH', parser.add_argument('--test-accounts', default=None, metavar='PATH',
help='Use accounts from accounts.yaml') 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, parser.add_argument('--image-disk-format', default=C.DEFAULT_IMAGE_FORMAT,
help="""a format of an image to be uploaded to glance. help="""a format of an image to be uploaded to glance.
Default is '%s'""" % C.DEFAULT_IMAGE_FORMAT) Default is '%s'""" % C.DEFAULT_IMAGE_FORMAT)
@ -396,6 +400,14 @@ def config_tempest(**kwargs):
services.set_supported_api_versions() services.set_supported_api_versions()
services.set_service_extensions() 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 # remove all unwanted values if were specified
if remove != {}: if remove != {}:
LOG.info("Removing configuration: %s", str(remove)) LOG.info("Removing configuration: %s", str(remove))
@ -408,12 +420,13 @@ def main():
args = parse_arguments() args = parse_arguments()
cloud_creds = get_cloud_creds(args) cloud_creds = get_cloud_creds(args)
config_tempest( config_tempest(
create=args.create,
cloud_creds=cloud_creds, cloud_creds=cloud_creds,
create=args.create,
create_accounts_file=args.create_accounts_file,
debug=args.debug, debug=args.debug,
deployer_input=args.deployer_input, deployer_input=args.deployer_input,
image_path=args.image,
image_disk_format=args.image_disk_format, image_disk_format=args.image_disk_format,
image_path=args.image,
network_id=args.network_id, network_id=args.network_id,
non_admin=args.non_admin, non_admin=args.non_admin,
os_cloud=args.os_cloud, os_cloud=args.os_cloud,

View File

@ -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))

View File

@ -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.

View File

@ -7,3 +7,4 @@ tempest>=14.0.0 # Apache-2.0
requests>=2.10.0,!=2.12.2 # Apache-2.0 requests>=2.10.0,!=2.12.2 # Apache-2.0
os-client-config>=1.26.0 # Apache-2.0 os-client-config>=1.26.0 # Apache-2.0
oslo.config>=3.23.0 # Apache-2.0 oslo.config>=3.23.0 # Apache-2.0
PyYAML>=3.12 # MIT