From 896f044d19d4cd69f8ae6b3f300930d99dd4c02d Mon Sep 17 00:00:00 2001 From: Ivan Kolodyazhny Date: Mon, 11 Jan 2016 14:57:50 +0200 Subject: [PATCH] Introduce functional tests for python-brick-cinderclient-ext This patch adds new tox environment to run functional tests: $ tox -e functional It also adds 'post_test_hook.sh' script to run functional tests on gates. All unit tests moved to 'unit' directory. Change-Id: Iac2f4c6d9d77e96d2478aa00632aad33fa6a22d8 --- .testr.conf | 4 +- .../tests/functional/__init__.py | 0 .../tests/functional/hooks/post_test_hook.sh | 53 ++++++++++++++ .../tests/functional/test_brick_client.py | 73 +++++++++++++++++++ brick_cinderclient_ext/tests/unit/__init__.py | 0 .../tests/{ => unit}/test_brick_client.py | 0 tox.ini | 23 ++++-- 7 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 brick_cinderclient_ext/tests/functional/__init__.py create mode 100755 brick_cinderclient_ext/tests/functional/hooks/post_test_hook.sh create mode 100644 brick_cinderclient_ext/tests/functional/test_brick_client.py create mode 100644 brick_cinderclient_ext/tests/unit/__init__.py rename brick_cinderclient_ext/tests/{ => unit}/test_brick_client.py (100%) diff --git a/.testr.conf b/.testr.conf index 6d83b3c..a4c4b65 100644 --- a/.testr.conf +++ b/.testr.conf @@ -2,6 +2,8 @@ test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \ OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \ OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \ - ${PYTHON:-python} -m subunit.run discover -t ./ . $LISTOPT $IDOPTION + ${PYTHON:-python} -m subunit.run discover \ + -t ./ ${OS_TEST_PATH:-./brick_cinderclient_ext/tests/unit} \ + $LISTOPT $IDOPTION test_id_option=--load-list $IDFILE test_list_option=--list diff --git a/brick_cinderclient_ext/tests/functional/__init__.py b/brick_cinderclient_ext/tests/functional/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/brick_cinderclient_ext/tests/functional/hooks/post_test_hook.sh b/brick_cinderclient_ext/tests/functional/hooks/post_test_hook.sh new file mode 100755 index 0000000..2178589 --- /dev/null +++ b/brick_cinderclient_ext/tests/functional/hooks/post_test_hook.sh @@ -0,0 +1,53 @@ +#!/bin/bash -xe + +# 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. + +# This script is executed inside post_test_hook function in devstack gate. + +# Default gate uses /opt/stack/new... but some of us may install differently +STACK_DIR=$BASE/new/devstack + +function generate_testr_results { + if [ -f .testrepository/0 ]; then + sudo .tox/functional/bin/testr last --subunit > $WORKSPACE/testrepository.subunit + sudo mv $WORKSPACE/testrepository.subunit $BASE/logs/testrepository.subunit + sudo /usr/os-testr-env/bin/subunit2html $BASE/logs/testrepository.subunit $BASE/logs/testr_results.html + sudo gzip -9 $BASE/logs/testrepository.subunit + sudo gzip -9 $BASE/logs/testr_results.html + sudo chown jenkins:jenkins $BASE/logs/testrepository.subunit.gz $BASE/logs/testr_results.html.gz + sudo chmod a+r $BASE/logs/testrepository.subunit.gz $BASE/logs/testr_results.html.gz + fi +} + +export BRICK_CINDERCLIENT_EXT_DIR="$BASE/new/python-brick-cinderclient-ext" + +sudo chown -R jenkins:stack $BRICK_CINDERCLIENT_EXT_DIR + +# Get admin credentials +cd $STACK_DIR +source openrc admin admin + +# Go to the cinderclient dir +cd $BRICK_CINDERCLIENT_EXT_DIR + +# Run tests +echo "Running cinderclient functional test suite" +set +e +# Preserve env for OS_ credentials +sudo -E -H -u jenkins tox -efunctional +EXIT_CODE=$? +set -e + +# Collect and parse result +generate_testr_results +exit $EXIT_CODE diff --git a/brick_cinderclient_ext/tests/functional/test_brick_client.py b/brick_cinderclient_ext/tests/functional/test_brick_client.py new file mode 100644 index 0000000..4c971ee --- /dev/null +++ b/brick_cinderclient_ext/tests/functional/test_brick_client.py @@ -0,0 +1,73 @@ +# -*- 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. + +import os + +from cinderclient import client as c_client +from oslotest import base +import six + +from brick_cinderclient_ext import client + + +_CREDS_FILE = 'functional_creds.conf' + + +def credentials(): + """Retrieves credentials to run functional tests + + Credentials are either read from the environment or from a config file + ('functional_creds.conf'). Environment variables override those from the + config file. + + The 'functional_creds.conf' file is the clean and new way to use (by + default tox 2.0 does not pass environment variables). + """ + + username = os.environ.get('OS_USERNAME') + password = os.environ.get('OS_PASSWORD') + tenant_name = os.environ.get('OS_TENANT_NAME') + auth_url = os.environ.get('OS_AUTH_URL') + + config = six.moves.configparser.RawConfigParser() + if config.read(_CREDS_FILE): + username = username or config.get('admin', 'user') + password = password or config.get('admin', 'pass') + tenant_name = tenant_name or config.get('admin', 'tenant_name') + auth_url = auth_url or config.get('auth', 'uri') + + return { + 'username': username, + 'password': password, + 'tenant_name': tenant_name, + 'uri': auth_url + } + + +class BrickClientTests(base.BaseTestCase): + def setUp(self): + super(BrickClientTests, self).setUp() + creds = credentials() + self.cinder_client = c_client.Client(2, + creds['username'], + creds['password'], + creds['tenant_name'], + creds['uri']) + + self.client = client.Client(self.cinder_client) + + def test_get_connector(self): + connector = self.client.get_connector() + for prop in ['ip', 'host', 'multipath', 'platform', 'os_type']: + self.assertIn(prop, connector) diff --git a/brick_cinderclient_ext/tests/unit/__init__.py b/brick_cinderclient_ext/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/brick_cinderclient_ext/tests/test_brick_client.py b/brick_cinderclient_ext/tests/unit/test_brick_client.py similarity index 100% rename from brick_cinderclient_ext/tests/test_brick_client.py rename to brick_cinderclient_ext/tests/unit/test_brick_client.py diff --git a/tox.ini b/tox.ini index 53a7b34..9342278 100644 --- a/tox.ini +++ b/tox.ini @@ -4,14 +4,16 @@ envlist = py34-constraints,py27-constraints,pypy-constraints,pep8-constraints skipsdist = True [testenv] -usedevelop = True -install_command = - constraints: {[testenv:common-constraints]install_command} - pip install -U {opts} {packages} -setenv = - VIRTUAL_ENV={envdir} -deps = -r{toxinidir}/test-requirements.txt -commands = python setup.py test --slowest --testr-args='{posargs}' +install_command = pip install -U {opts} {packages} +setenv = VIRTUAL_ENV={envdir} +passenv = *_proxy *_PROXY + +deps = -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt +commands = + find . -type f -name "*.pyc" -delete + python setup.py testr --testr-args='{posargs}' +whitelist_externals = find [testenv:common-constraints] install_command = pip install -c{env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt} {opts} {packages} @@ -26,6 +28,11 @@ commands = flake8 {posargs} [testenv:venv] commands = {posargs} +[testenv:functional] +setenv = + OS_TEST_PATH=./brick_cinderclient_ext/tests/functional +passenv = OS_* + [testenv:venv-constraints] install_command = {[testenv:common-constraints]install_command} commands = {posargs}