From cb0e9130f5acf3c703070d4c33cf159de17020fe Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Mon, 9 Nov 2015 11:24:53 -0500 Subject: [PATCH] Add Ansible testing infrastructure The keypair test needed adjusting so that it would work in the gating environment, and anywhere an SSH key may not be available. Change-Id: Ic9bf41c2b4041911d05217ad3748e097326bac3a --- extras/run-ansible-tests.sh | 35 +++++++++++++++++++ shade/tests/ansible/hooks/post_test_hook.sh | 26 ++++++++++++++ .../ansible/roles/keypair/tasks/main.yml | 20 +++++++++-- tox.ini | 5 +++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100755 extras/run-ansible-tests.sh create mode 100755 shade/tests/ansible/hooks/post_test_hook.sh diff --git a/extras/run-ansible-tests.sh b/extras/run-ansible-tests.sh new file mode 100755 index 000000000..bfd1ada36 --- /dev/null +++ b/extras/run-ansible-tests.sh @@ -0,0 +1,35 @@ +#!/bin/bash +############################################################################# +# run-ansible-tests.sh +# +# Script used to setup a tox environment for running Ansible. This is meant +# to be called by tox (via tox.ini). To run the Ansible tests, use: +# +# tox -e ansible +# +# USAGE: +# run-ansible-tests.sh +# +# PARAMETERS: +# Directory of the tox environment to use for testing. +############################################################################# + +ENVDIR=$1 + +if [ -d ${ENVDIR}/ansible ] +then + echo "Using existing Ansible install" +else + echo "Installing Ansible at $ENVDIR" + git clone --recursive git://github.com/ansible/ansible.git ${ENVDIR}/ansible +fi + +# We need to source the current tox environment so that Ansible will +# be setup for the correct python environment. +source $ENVDIR/bin/activate + +# Setup Ansible +source $ENVDIR/ansible/hacking/env-setup + +# Run the shade Ansible tests +ansible-playbook -vvv ./shade/tests/ansible/run.yml -e "cloud=devstack-admin" diff --git a/shade/tests/ansible/hooks/post_test_hook.sh b/shade/tests/ansible/hooks/post_test_hook.sh new file mode 100755 index 000000000..d79a9427f --- /dev/null +++ b/shade/tests/ansible/hooks/post_test_hook.sh @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +# 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. + +export SHADE_DIR="$BASE/new/shade" + +cd $SHADE_DIR +sudo chown -R jenkins:stack $SHADE_DIR + +echo "Running shade Ansible test suite" +set +e +sudo -E -H -u jenkins tox -eansible +EXIT_CODE=$? +set -e + +exit $EXIT_CODE diff --git a/shade/tests/ansible/roles/keypair/tasks/main.yml b/shade/tests/ansible/roles/keypair/tasks/main.yml index cc30d9624..53a856e2f 100644 --- a/shade/tests/ansible/roles/keypair/tasks/main.yml +++ b/shade/tests/ansible/roles/keypair/tasks/main.yml @@ -11,12 +11,18 @@ name: "{{ keypair_name }}" state: absent +- name: Generate test key file + user: + name: "{{ ansible_env.USER }}" + generate_ssh_key: yes + ssh_key_file: .ssh/shade_id_rsa + - name: Create keypair (file) os_keypair: cloud: "{{ cloud }}" name: "{{ keypair_name }}" state: present - public_key_file: "{{ ansible_env.HOME }}/.ssh/id_rsa.pub" + public_key_file: "{{ ansible_env.HOME }}/.ssh/shade_id_rsa.pub" - name: Delete keypair (file) os_keypair: @@ -29,10 +35,20 @@ cloud: "{{ cloud }}" name: "{{ keypair_name }}" state: present - public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" + public_key: "{{ lookup('file', '~/.ssh/shade_id_rsa.pub') }}" - name: Delete keypair (key) os_keypair: cloud: "{{ cloud }}" name: "{{ keypair_name }}" state: absent + +- name: Delete test key pub file + file: + name: "{{ ansible_env.HOME }}/.ssh/shade_id_rsa.pub" + state: absent + +- name: Delete test key pvt file + file: + name: "{{ ansible_env.HOME }}/.ssh/shade_id_rsa" + state: absent diff --git a/tox.ini b/tox.ini index 777e8a88c..fb2415f7e 100644 --- a/tox.ini +++ b/tox.ini @@ -30,6 +30,11 @@ commands = {posargs} [testenv:cover] commands = python setup.py testr --coverage --testr-args='{posargs}' +[testenv:ansible] +# Need to pass some env vars for the Ansible playbooks +passenv = HOME USER +commands = {toxinidir}/extras/run-ansible-tests.sh {envdir} + [flake8] # Infra does not follow hacking, nor the broken E12* things ignore = E123,E125,E129,H