From fedc060158d407bb58c2e45850cac5da92c6e6e4 Mon Sep 17 00:00:00 2001 From: Ilya Shakhat Date: Tue, 18 Dec 2018 14:38:58 +0100 Subject: [PATCH] Specify auth parameters in devstack tests Single-node devstack does not have SSH key for stack user. In all tests we will use custom os-faults key and connect to localhost as `stack` user. Tests are also executed as `stack` user explicitly (by default they are run as zuul user, but we do not want to rely on this). Change-Id: Ibcc9a406dc850363e651d4023874147db45214f7 --- .zuul.yaml | 2 + os_faults/ansible/executor.py | 9 ++- .../tests/devstack/os-faults-universal.yaml | 3 + os_faults/tests/devstack/playbooks/post.yml | 23 +++++++ os_faults/tests/devstack/playbooks/pre.yml | 5 ++ .../devstack/playbooks/run_on_devstack.yml | 69 ++++++++++++------- os_faults/tests/devstack/test_lib.py | 48 +++++++++++++ .../tests/devstack/test_os_inject_fault.py | 4 +- tox.ini | 5 ++ 9 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 os_faults/tests/devstack/playbooks/post.yml create mode 100644 os_faults/tests/devstack/playbooks/pre.yml create mode 100644 os_faults/tests/devstack/test_lib.py diff --git a/.zuul.yaml b/.zuul.yaml index b33e60f..d95a914 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -18,7 +18,9 @@ description: | Run os-faults integration tests on DevStack under Python 3 timeout: 2400 + pre-run: os_faults/tests/devstack/playbooks/pre.yml run: os_faults/tests/devstack/playbooks/run_on_devstack.yml + post-run: os_faults/tests/devstack/playbooks/post.yml required-projects: - openstack/os-faults vars: diff --git a/os_faults/ansible/executor.py b/os_faults/ansible/executor.py index 389273f..cc33be6 100644 --- a/os_faults/ansible/executor.py +++ b/os_faults/ansible/executor.py @@ -163,8 +163,9 @@ class AnsibleRunner(object): playbook_file_name = os.path.join(temp_dir, 'playbook') with open(inventory_file_name, 'w') as fd: - print(yaml.safe_dump(full_inventory, default_flow_style=False), - file=fd) + cnt = yaml.safe_dump(full_inventory, default_flow_style=False) + print(cnt, file=fd) + LOG.debug('Inventory:\n%s', cnt) play = { 'hosts': 'all', @@ -173,7 +174,9 @@ class AnsibleRunner(object): } with open(playbook_file_name, 'w') as fd: - print(yaml.safe_dump([play], default_flow_style=False), file=fd) + cnt = yaml.safe_dump([play], default_flow_style=False) + print(cnt, file=fd) + LOG.debug('Playbook:\n%s', cnt) cmd = ('%(ansible)s --module-path %(module_path)s ' '-i %(inventory)s %(playbook)s' % diff --git a/os_faults/tests/devstack/os-faults-universal.yaml b/os_faults/tests/devstack/os-faults-universal.yaml index 97b1209..a17876d 100644 --- a/os_faults/tests/devstack/os-faults-universal.yaml +++ b/os_faults/tests/devstack/os-faults-universal.yaml @@ -4,6 +4,9 @@ node_discover: driver: node_list args: - ip: localhost + auth: + username: stack + private_key_file: os_faults_key services: memcached: args: diff --git a/os_faults/tests/devstack/playbooks/post.yml b/os_faults/tests/devstack/playbooks/post.yml new file mode 100644 index 0000000..464bfd9 --- /dev/null +++ b/os_faults/tests/devstack/playbooks/post.yml @@ -0,0 +1,23 @@ +--- + +- hosts: all + vars: + - test_results_path: "/opt/stack/os-faults/.tox/{{tox_envlist}}/log/pytest_results.html" + tasks: + - name: Check whether test results file exists + stat: + path: "{{ test_results_path }}" + register: test_results + + - name: Copy job results + become: yes + synchronize: + src: "{{ test_results_path }}" + dest: "{{ zuul.executor.log_root }}" + mode: pull + copy_links: true + verify_host: true + rsync_opts: + - "--include=/**" + - "--include=*/" + when: test_results.stat.exists diff --git a/os_faults/tests/devstack/playbooks/pre.yml b/os_faults/tests/devstack/playbooks/pre.yml new file mode 100644 index 0000000..0f88af9 --- /dev/null +++ b/os_faults/tests/devstack/playbooks/pre.yml @@ -0,0 +1,5 @@ +--- + +- hosts: all + roles: + - orchestrate-devstack diff --git a/os_faults/tests/devstack/playbooks/run_on_devstack.yml b/os_faults/tests/devstack/playbooks/run_on_devstack.yml index 295b05b..6d280bb 100644 --- a/os_faults/tests/devstack/playbooks/run_on_devstack.yml +++ b/os_faults/tests/devstack/playbooks/run_on_devstack.yml @@ -1,30 +1,53 @@ --- - hosts: all - roles: - - orchestrate-devstack - - -- hosts: all + vars: + - stack_home: "/opt/stack" + - os_faults_home: "/opt/stack/os-faults" tasks: - - name: Run integration tests + + - name: Create folder for SSH keys (as stack user) + file: + path: "{{ stack_home }}/.ssh" + state: directory + mode: 0755 + become: yes + become_user: stack + + - name: Generate SSH key pair + command: "ssh-keygen -t rsa -f {{ os_faults_home }}/os_faults_key -N ''" + become: yes + become_user: stack + + - name: List os-faults folder + command: "ls -al {{ os_faults_home }}" + become: yes + become_user: stack + + - name: Add os-faults key into authorized_keys for stack user + shell: | + cat {{ os_faults_home}}/os_faults_key.pub >> {{ stack_home }}/.ssh/authorized_keys + become: yes + become_user: stack + + - name: Check SSH connection using os-faults key + command: "ssh -vvv -i os_faults_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=60 -o IdentitiesOnly=yes stack@localhost" + args: + chdir: "{{ os_faults_home }}" + become: yes + become_user: stack + + - name: Run tests command: "tox -e {{tox_envlist}}" args: - chdir: "/opt/stack/os-faults" - - -- hosts: all - vars: - - test_results_path: "/opt/stack/os-faults/.tox/{{tox_envlist}}/log/pytest_results.html" - tasks: - - name: Copy job results + chdir: "{{ os_faults_home }}" become: yes - synchronize: - src: "{{ test_results_path }}" - dest: "{{ zuul.executor.log_root }}" - mode: pull - copy_links: true - verify_host: true - rsync_opts: - - "--include=/**" - - "--include=*/" + become_user: stack + + - name: Check connectivity using command-line tool + shell: | + cd /opt/stack/os-faults + . .tox/devstack/bin/activate + os-inject-fault -c /opt/stack/os-faults/os_faults/tests/devstack/os-faults-universal.yaml -vd + become: yes + become_user: stack diff --git a/os_faults/tests/devstack/test_lib.py b/os_faults/tests/devstack/test_lib.py new file mode 100644 index 0000000..3ba962a --- /dev/null +++ b/os_faults/tests/devstack/test_lib.py @@ -0,0 +1,48 @@ +# 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 logging + +from oslotest import base + +import os_faults + + +LOG = logging.getLogger(__name__) + + +class TestOSFaultsLibrary(base.BaseTestCase): + def test_connection_stack_user(self): + cloud_config = { + 'cloud_management': { + 'driver': 'universal' + }, + 'node_discover': { + 'driver': 'node_list', + 'args': [ + { + 'ip': 'localhost', + 'auth': { + 'username': 'stack', + 'private_key_file': 'os_faults_key' + } + } + ] + } + } + + LOG.info('# Create connection to the cloud') + cloud_management = os_faults.connect(cloud_config) + self.assertIsNotNone(cloud_management) + + LOG.info('# Verify connection to the cloud') + cloud_management.verify() diff --git a/os_faults/tests/devstack/test_os_inject_fault.py b/os_faults/tests/devstack/test_os_inject_fault.py index 7ca5611..52a42a0 100644 --- a/os_faults/tests/devstack/test_os_inject_fault.py +++ b/os_faults/tests/devstack/test_os_inject_fault.py @@ -28,7 +28,7 @@ class TestOSInjectFaultUniversalDriver(test.TestCase): 'os-faults-universal.yaml') def test_connect(self): - cmd = 'os-inject-fault -c %s -v' % self.CONFIG_FILE + cmd = 'os-inject-fault -dc %s -v' % self.CONFIG_FILE command_stdout, command_stderr = processutils.execute( *shlex.split(cmd)) @@ -37,7 +37,7 @@ class TestOSInjectFaultUniversalDriver(test.TestCase): self.assertTrue(success) def test_restart_etcd(self): - cmd = 'os-inject-fault -c %s restart etcd service' % self.CONFIG_FILE + cmd = 'os-inject-fault -dc %s restart etcd service' % self.CONFIG_FILE command_stdout, command_stderr = processutils.execute( *shlex.split(cmd)) diff --git a/tox.ini b/tox.ini index 6a826c6..2df0fd3 100644 --- a/tox.ini +++ b/tox.ini @@ -58,9 +58,14 @@ install_command = {[testenv:common-constraints]install_command} commands = {[testenv:cover]commands} [testenv:devstack] +# to execute the tests: +# 1) create SSH key in os-faults folder: ssh-keygen -t rsa -f os_faults_key -N '' +# 2) copy public key into authorized_keys of user stack: cat os_faults_key.pub >> ~/.ssh/authorized_keys +# 3) run tests normally: tox -e devstack basepython = python3 setenv = {[testenv]setenv} OS_TEST_PATH=./os_faults/tests/devstack + OS_DEBUG=True deps = {[testenv]deps} extras = commands =