From 4c2b97385955b0af47497f92fb528638072131ba Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 26 Mar 2015 20:51:30 -0400 Subject: [PATCH] Convert to new DevStack plugin model for functional testing Switch over to the new plugin style as documented here: http://docs.openstack.org/developer/devstack/plugins.html Makes it very easy to setup a CI job. Added a new directory for functional job and the tests are run from the post_test_hook.sh. There's even a "magnum bay-list" in the shell script to show that we are actually making calls to the magnum API endpoint and things are working fine. Note that this is just the minimal plumbing needed and there's a lot more effort needed to come up with proper test cases for testing magnum functionality. Closes-Bug: #1439621 Change-Id: I88d45015338ef55713b32de196f9071c8fc2031b --- contrib/devstack/prepare_devstack.sh | 14 - devstack/README.rst | 23 ++ {contrib/devstack => devstack}/lib/magnum | 1 + .../70-magnum.sh => devstack/plugin.sh | 19 +- .../localrc.example => devstack/settings | 29 +- magnum/tests/contrib/post_test_hook.sh | 51 +++ magnum/tests/functional/__init__.py | 0 magnum/tests/functional/test_magnum.py | 28 ++ tools/colorizer.py | 332 ++++++++++++++++++ 9 files changed, 466 insertions(+), 31 deletions(-) delete mode 100755 contrib/devstack/prepare_devstack.sh create mode 100644 devstack/README.rst rename {contrib/devstack => devstack}/lib/magnum (99%) rename contrib/devstack/extras.d/70-magnum.sh => devstack/plugin.sh (70%) mode change 100644 => 100755 rename contrib/devstack/localrc.example => devstack/settings (75%) create mode 100755 magnum/tests/contrib/post_test_hook.sh create mode 100644 magnum/tests/functional/__init__.py create mode 100644 magnum/tests/functional/test_magnum.py create mode 100755 tools/colorizer.py diff --git a/contrib/devstack/prepare_devstack.sh b/contrib/devstack/prepare_devstack.sh deleted file mode 100755 index 0a699497b8..0000000000 --- a/contrib/devstack/prepare_devstack.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -set -eux - -MAGNUM_DIR=$(readlink -f $(dirname $0)/../..) -INSTALL_DIR=${INSTALL_DIR:-/opt/stack} - -cp ${MAGNUM_DIR}/contrib/devstack/lib/magnum ${INSTALL_DIR}/devstack/lib -cp ${MAGNUM_DIR}/contrib/devstack/extras.d/70-magnum.sh ${INSTALL_DIR}/devstack/extras.d - -# Add magnum specific requirements to global requirements -git clone https://git.openstack.org/openstack/requirements ${INSTALL_DIR}/requirements || true -echo "python-kubernetes>=0.2" >> ${INSTALL_DIR}/requirements/global-requirements.txt -echo "docker-py>=0.5.1" >> ${INSTALL_DIR}/requirements/global-requirements.txt diff --git a/devstack/README.rst b/devstack/README.rst new file mode 100644 index 0000000000..46f1d86715 --- /dev/null +++ b/devstack/README.rst @@ -0,0 +1,23 @@ +==================== +Devstack Integration +==================== + +This directory contains the files necessary to integrate Magnum with devstack. + +Refer the quickstart guide for more information on using devstack and magnum. + +Running devstack with magnum for the first time may take a long time as it +needs to download an atomic fedora 21 qcow image. If you already have this image +you can copy it to /opt/stack/devstack/files/fedora-21-atomic.qcow2 to save you +this time. + +To install magnum into devstack, Add this repo as an external repository: :: + + > cat local.conf + [[local|localrc]] + enable_plugin magnum https://github.com/openstack/magnum + +Run devstack as normal: :: + + cd /opt/stack/devstack + ./stack.sh diff --git a/contrib/devstack/lib/magnum b/devstack/lib/magnum similarity index 99% rename from contrib/devstack/lib/magnum rename to devstack/lib/magnum index 6c9ca3662c..21a90040ae 100644 --- a/contrib/devstack/lib/magnum +++ b/devstack/lib/magnum @@ -196,6 +196,7 @@ function init_magnum { # install_magnumclient() - Collect source and prepare function install_magnumclient { if use_library_from_git "python-magnumclient"; then + ERROR_ON_CLONE=False git_clone_by_name "python-magnumclient" setup_dev_lib "python-magnumclient" fi diff --git a/contrib/devstack/extras.d/70-magnum.sh b/devstack/plugin.sh old mode 100644 new mode 100755 similarity index 70% rename from contrib/devstack/extras.d/70-magnum.sh rename to devstack/plugin.sh index c63742d498..0e6b8b8520 --- a/contrib/devstack/extras.d/70-magnum.sh +++ b/devstack/plugin.sh @@ -1,9 +1,19 @@ # magnum.sh - Devstack extras script to install magnum +# Save trace setting +XTRACE=$(set +o | grep xtrace) +set -o xtrace + +echo_summary "magnum's plugin.sh was called..." +source $DEST/magnum/devstack/lib/magnum +(set -o posix; set) + if is_service_enabled m-api m-cond; then - if [[ "$1" == "source" ]]; then - # Initial source - source $TOP_DIR/lib/magnum + if [[ "$1" == "stack" && "$2" == "pre-install" ]]; then + echo_summary "Before Installing magnum" + mkdir -p $SCREEN_LOGDIR + echo "python-kubernetes>=0.2" >> ${REQUIREMENTS_DIR}/global-requirements.txt + echo "docker-py>=0.5.1" >> ${REQUIREMENTS_DIR}/global-requirements.txt elif [[ "$1" == "stack" && "$2" == "install" ]]; then echo_summary "Installing magnum" install_magnum @@ -43,3 +53,6 @@ if is_service_enabled m-api m-cond; then cleanup_magnum fi fi + +# Restore xtrace +$XTRACE \ No newline at end of file diff --git a/contrib/devstack/localrc.example b/devstack/settings similarity index 75% rename from contrib/devstack/localrc.example rename to devstack/settings index 82ee459e2f..e67e647b81 100644 --- a/contrib/devstack/localrc.example +++ b/devstack/settings @@ -1,7 +1,17 @@ -# Modify to your environment -FLOATING_RANGE=192.168.1.224/27 -PUBLIC_NETWORK_GATEWAY=192.168.1.225 -PUBLIC_INTERFACE=em1 +# Devstack settings + +## Modify to your environment +# FLOATING_RANGE=192.168.1.224/27 +# PUBLIC_NETWORK_GATEWAY=192.168.1.225 +# PUBLIC_INTERFACE=em1 +# FIXED_RANGE=10.0.0.0/24 + +# Neutron settings +Q_USE_SECGROUP=True +ENABLE_TENANT_VLANS=True +TENANT_VLAN_RANGE= +PHYSICAL_NETWORK=public +OVS_PHYSICAL_BRIDGE=br-ex # Credentials ADMIN_PASSWORD=password @@ -26,15 +36,6 @@ enable_service magnum enable_service m-api enable_service m-cond -FIXED_RANGE=10.0.0.0/24 - -Q_USE_SECGROUP=True -ENABLE_TENANT_VLANS=True -TENANT_VLAN_RANGE= - -PHYSICAL_NETWORK=public -OVS_PHYSICAL_BRIDGE=br-ex - # Log all output to files LOGFILE=$HOME/devstack.log -SCREEN_LOGDIR=$HOME/logs +SCREEN_LOGDIR=$HOME/logs \ No newline at end of file diff --git a/magnum/tests/contrib/post_test_hook.sh b/magnum/tests/contrib/post_test_hook.sh new file mode 100755 index 0000000000..4874f1b5cb --- /dev/null +++ b/magnum/tests/contrib/post_test_hook.sh @@ -0,0 +1,51 @@ +#!/bin/bash -x +# +# 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. + +# Sleep some time until all services are starting +sleep 5 + +if ! function_exists echo_summary; then + function echo_summary { + echo $@ + } +fi + +# Save trace setting +XTRACE=$(set +o | grep xtrace) +set -o xtrace + +echo_summary "magnum's post_test_hook.sh was called..." +(set -o posix; set) + +sudo pip install -r test-requirements.txt + +# Try a command line as a sanity check +source ../devstack/accrc/admin/admin + +echo_summary "Running bay-list" +magnum --debug bay-list + +sudo OS_STDOUT_CAPTURE=-1 OS_STDERR_CAPTURE=-1 OS_TEST_TIMEOUT=500 OS_TEST_LOCK_PATH=${TMPDIR:-'/tmp'} \ + python -m subunit.run discover -t ./ ./magnum/tests/functional | subunit-2to1 | tools/colorizer.py +RETVAL=$? + +# Restore xtrace +$XTRACE + +# Save the logs +sudo mv ../logs/* /opt/stack/logs/ + +exit $RETVAL \ No newline at end of file diff --git a/magnum/tests/functional/__init__.py b/magnum/tests/functional/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/magnum/tests/functional/test_magnum.py b/magnum/tests/functional/test_magnum.py new file mode 100644 index 0000000000..8173a921e7 --- /dev/null +++ b/magnum/tests/functional/test_magnum.py @@ -0,0 +1,28 @@ +# -*- 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. + +""" +test_magnum +---------------------------------- + +Tests for `magnum` module. +""" + +from magnum.tests import base + + +class TestMagnum(base.TestCase): + + def test_something(self): + pass diff --git a/tools/colorizer.py b/tools/colorizer.py new file mode 100755 index 0000000000..ee8f914d71 --- /dev/null +++ b/tools/colorizer.py @@ -0,0 +1,332 @@ +#!/usr/bin/env python + +# Copyright (c) 2013, Nebula, Inc. +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. +# +# Colorizer Code is borrowed from Twisted: +# Copyright (c) 2001-2010 Twisted Matrix Laboratories. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Display a subunit stream through a colorized unittest test runner.""" + +import heapq +import sys +import unittest + +import subunit +import testtools + + +class _AnsiColorizer(object): + """ + A colorizer is an object that loosely wraps around a stream, allowing + callers to write text to the stream in a particular color. + + Colorizer classes must implement C{supported()} and C{write(text, color)}. + """ + _colors = dict(black=30, red=31, green=32, yellow=33, + blue=34, magenta=35, cyan=36, white=37) + + def __init__(self, stream): + self.stream = stream + + def supported(cls, stream=sys.stdout): + """ + A class method that returns True if the current platform supports + coloring terminal output using this method. Returns False otherwise. + """ + if not stream.isatty(): + return False # auto color only on TTYs + try: + import curses + except ImportError: + return False + else: + try: + try: + return curses.tigetnum("colors") > 2 + except curses.error: + curses.setupterm() + return curses.tigetnum("colors") > 2 + except Exception: + # guess false in case of error + return False + supported = classmethod(supported) + + def write(self, text, color): + """ + Write the given text to the stream in the given color. + + @param text: Text to be written to the stream. + + @param color: A string label for a color. e.g. 'red', 'white'. + """ + color = self._colors[color] + self.stream.write('\x1b[%s;1m%s\x1b[0m' % (color, text)) + + +class _Win32Colorizer(object): + """ + See _AnsiColorizer docstring. + """ + def __init__(self, stream): + import win32console + red, green, blue, bold = (win32console.FOREGROUND_RED, + win32console.FOREGROUND_GREEN, + win32console.FOREGROUND_BLUE, + win32console.FOREGROUND_INTENSITY) + self.stream = stream + self.screenBuffer = win32console.GetStdHandle( + win32console.STD_OUT_HANDLE) + self._colors = {'normal': red | green | blue, + 'red': red | bold, + 'green': green | bold, + 'blue': blue | bold, + 'yellow': red | green | bold, + 'magenta': red | blue | bold, + 'cyan': green | blue | bold, + 'white': red | green | blue | bold} + + def supported(cls, stream=sys.stdout): + try: + import win32console + screenBuffer = win32console.GetStdHandle( + win32console.STD_OUT_HANDLE) + except ImportError: + return False + import pywintypes + try: + screenBuffer.SetConsoleTextAttribute( + win32console.FOREGROUND_RED | + win32console.FOREGROUND_GREEN | + win32console.FOREGROUND_BLUE) + except pywintypes.error: + return False + else: + return True + supported = classmethod(supported) + + def write(self, text, color): + color = self._colors[color] + self.screenBuffer.SetConsoleTextAttribute(color) + self.stream.write(text) + self.screenBuffer.SetConsoleTextAttribute(self._colors['normal']) + + +class _NullColorizer(object): + """ + See _AnsiColorizer docstring. + """ + def __init__(self, stream): + self.stream = stream + + def supported(cls, stream=sys.stdout): + return True + supported = classmethod(supported) + + def write(self, text, color): + self.stream.write(text) + + +def get_elapsed_time_color(elapsed_time): + if elapsed_time > 1.0: + return 'red' + elif elapsed_time > 0.25: + return 'yellow' + else: + return 'green' + + +class EC2ApiTestResult(testtools.TestResult): + def __init__(self, stream, descriptions, verbosity): + super(EC2ApiTestResult, self).__init__() + self.stream = stream + self.showAll = verbosity > 1 + self.num_slow_tests = 10 + self.slow_tests = [] # this is a fixed-sized heap + self.colorizer = None + # NOTE(vish): reset stdout for the terminal check + stdout = sys.stdout + sys.stdout = sys.__stdout__ + for colorizer in [_Win32Colorizer, _AnsiColorizer, _NullColorizer]: + if colorizer.supported(): + self.colorizer = colorizer(self.stream) + break + sys.stdout = stdout + self.start_time = None + self.last_time = {} + self.results = {} + self.last_written = None + + def _writeElapsedTime(self, elapsed): + color = get_elapsed_time_color(elapsed) + self.colorizer.write(" %.2f" % elapsed, color) + + def _addResult(self, test, *args): + try: + name = test.id() + except AttributeError: + name = 'Unknown.unknown' + test_class, test_name = name.rsplit('.', 1) + + elapsed = (self._now() - self.start_time).total_seconds() + item = (elapsed, test_class, test_name) + if len(self.slow_tests) >= self.num_slow_tests: + heapq.heappushpop(self.slow_tests, item) + else: + heapq.heappush(self.slow_tests, item) + + self.results.setdefault(test_class, []) + self.results[test_class].append((test_name, elapsed) + args) + self.last_time[test_class] = self._now() + self.writeTests() + + def _writeResult(self, test_name, elapsed, long_result, color, + short_result, success): + if self.showAll: + self.stream.write(' %s' % str(test_name).ljust(66)) + self.colorizer.write(long_result, color) + if success: + self._writeElapsedTime(elapsed) + self.stream.writeln() + else: + self.colorizer.write(short_result, color) + + def addSuccess(self, test): + super(EC2ApiTestResult, self).addSuccess(test) + self._addResult(test, 'OK', 'green', '.', True) + + def addFailure(self, test, err): + if test.id() == 'process-returncode': + return + super(EC2ApiTestResult, self).addFailure(test, err) + self._addResult(test, 'FAIL', 'red', 'F', False) + + def addError(self, test, err): + super(EC2ApiTestResult, self).addFailure(test, err) + self._addResult(test, 'ERROR', 'red', 'E', False) + + def addSkip(self, test, reason=None, details=None): + super(EC2ApiTestResult, self).addSkip(test, reason, details) + self._addResult(test, 'SKIP', 'blue', 'S', True) + + def startTest(self, test): + self.start_time = self._now() + super(EC2ApiTestResult, self).startTest(test) + + def writeTestCase(self, cls): + if not self.results.get(cls): + return + if cls != self.last_written: + self.colorizer.write(cls, 'white') + self.stream.writeln() + for result in self.results[cls]: + self._writeResult(*result) + del self.results[cls] + self.stream.flush() + self.last_written = cls + + def writeTests(self): + time = self.last_time.get(self.last_written, self._now()) + if not self.last_written or (self._now() - time).total_seconds() > 2.0: + diff = 3.0 + while diff > 2.0: + classes = self.results.keys() + oldest = min(classes, key=lambda x: self.last_time[x]) + diff = (self._now() - self.last_time[oldest]).total_seconds() + self.writeTestCase(oldest) + else: + self.writeTestCase(self.last_written) + + def done(self): + self.stopTestRun() + + def stopTestRun(self): + for cls in list(self.results.iterkeys()): + self.writeTestCase(cls) + self.stream.writeln() + self.writeSlowTests() + + def writeSlowTests(self): + # Pare out 'fast' tests + slow_tests = [item for item in self.slow_tests + if get_elapsed_time_color(item[0]) != 'green'] + if slow_tests: + slow_total_time = sum(item[0] for item in slow_tests) + slow = ("Slowest %i tests took %.2f secs:" + % (len(slow_tests), slow_total_time)) + self.colorizer.write(slow, 'yellow') + self.stream.writeln() + last_cls = None + # sort by name + for elapsed, cls, name in sorted(slow_tests, + key=lambda x: x[1] + x[2]): + if cls != last_cls: + self.colorizer.write(cls, 'white') + self.stream.writeln() + last_cls = cls + self.stream.write(' %s' % str(name).ljust(68)) + self._writeElapsedTime(elapsed) + self.stream.writeln() + + def printErrors(self): + if self.showAll: + self.stream.writeln() + self.printErrorList('ERROR', self.errors) + self.printErrorList('FAIL', self.failures) + + def printErrorList(self, flavor, errors): + for test, err in errors: + self.colorizer.write("=" * 70, 'red') + self.stream.writeln() + self.colorizer.write(flavor, 'red') + self.stream.writeln(": %s" % test.id()) + self.colorizer.write("-" * 70, 'red') + self.stream.writeln() + self.stream.writeln("%s" % err) + + +test = subunit.ProtocolTestCase(sys.stdin, passthrough=None) + +if sys.version_info[0:2] <= (2, 6): + runner = unittest.TextTestRunner(verbosity=2) +else: + runner = unittest.TextTestRunner(verbosity=2, resultclass=EC2ApiTestResult) + +if runner.run(test).wasSuccessful(): + exit_code = 0 +else: + exit_code = 1 +sys.exit(exit_code)