diff options
author | Dan Radez <dradez@redhat.com> | 2016-06-08 15:12:20 -0400 |
---|---|---|
committer | Dan Radez <dradez@redhat.com> | 2016-06-10 12:59:27 -0400 |
commit | 24171d4b3b3a94a64b34c8f70e90b4ae5a97ef92 (patch) | |
tree | 67c8d5d254f8d632fc02ffc32cbc08afac03c1f4 | |
parent | ce4770e46df1792f80d27a279d7c6cef2d9af5e0 (diff) |
converting apex ip_utils unit tests to nose
Change-Id: I54205d217807fd5499a71571ef8bed7c684fe944
Signed-off-by: Dan Radez <dradez@redhat.com>
-rw-r--r-- | build/Makefile | 10 | ||||
-rwxr-xr-x | ci/build.sh | 20 | ||||
-rwxr-xr-x | ci/test.sh | 33 | ||||
-rwxr-xr-x | tests/python-coverage.sh | 65 | ||||
-rw-r--r-- | tests/python_coverage_ip_utils.py | 25 | ||||
-rw-r--r-- | tests/test_apex_common_utils.py | 34 | ||||
-rw-r--r-- | tests/test_apex_deploy_env.py | 88 | ||||
-rw-r--r-- | tests/test_apex_ip_utils.py | 95 | ||||
-rw-r--r-- | tests/test_apex_network_environment.py | 42 | ||||
-rw-r--r-- | tests/test_apex_network_settings.py | 45 |
10 files changed, 346 insertions, 111 deletions
diff --git a/build/Makefile b/build/Makefile index 3f3d1c3a..f599f42f 100644 --- a/build/Makefile +++ b/build/Makefile @@ -19,6 +19,10 @@ export RPMODL = $(shell pwd)/noarch/opnfv-apex-$(RPMVERS)-$(shell echo ${RELEASE export RPMONO = $(shell pwd)/noarch/opnfv-apex-onos-$(RPMVERS)-$(shell echo ${RELEASE} | tr -d '_-').noarch.rpm export RPMSFC = $(shell pwd)/noarch/opnfv-apex-opendaylight-sfc-$(RPMVERS)-$(shell echo ${RELEASE} | tr -d '_-').noarch.rpm +all_networks="admin_network private_network storage_network external_network api_network" + + + .PHONY: all all: iso @@ -62,7 +66,11 @@ $(RPMCOM): .PHONY: python-tests python-tests: - cd ../tests && ./python-coverage.sh + # run nose tests + cd ../tests && PYTHONPATH=../lib/python/ nosetests-3.4 . --with-coverage --cover-package apex + # generate reports + cd ../tests && coverage3 html --include '*lib/python/*' + cd ../tests && coverage3 report --include '*lib/python/*' -m ############### diff --git a/ci/build.sh b/ci/build.sh index 1bd96d53..dd9f9fd1 100755 --- a/ci/build.sh +++ b/ci/build.sh @@ -34,7 +34,6 @@ BUILD_BASE=$(readlink -e ../build/) CACHE_DEST="" CACHE_DIR="cache" CACHE_NAME="apex-cache" -PYTHON_TESTS="TRUE" MAKE_TARGETS="images" REQUIRED_PKGS="rpm-build python-docutils" @@ -64,11 +63,6 @@ parse_cmdline() { echo "Buiding opnfv-apex RPMs" shift 1 ;; - --skip-python-tests ) - PYTHON_TESTS="FALSE" - echo "Skipping Python Tests" - shift 1 - ;; --debug ) debug="TRUE" echo "Enable debug output" @@ -151,20 +145,6 @@ if ! rpm -q python34-devel > /dev/null; then fi fi -if [ "$PYTHON_TESTS" == "TRUE" ]; then - # Make sure coverage is installed - if ! python3 -c "import coverage" &> /dev/null; then sudo easy_install-3.4 coverage; fi - - run_make python-tests - pushd ../tests/ > /dev/null - percent=$(coverage3 report --include '*lib/python/*' -m | grep TOTAL | tr -s ' ' | awk '{ print $4 }' | cut -d % -f 1) - if [[ percent -lt 80 ]]; then - echo "Python Coverage: $percent" - echo "WARNING: Does not meet 80% requirement" - fi - popd -fi - # Execute make against targets for t in $MAKE_TARGETS; do run_make $t diff --git a/ci/test.sh b/ci/test.sh new file mode 100755 index 00000000..0f2bc046 --- /dev/null +++ b/ci/test.sh @@ -0,0 +1,33 @@ +#!/bin/sh +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +set -e + +# Make sure python is installed +if ! rpm -q python34-devel > /dev/null; then + sudo yum install -y epel-release + if ! sudo yum install -y python34-devel; then + echo "Failed to install python34-devel package..." + exit 1 + fi +fi + +# Make sure coverage is installed +if ! python3 -c "import coverage" &> /dev/null; then sudo easy_install-3.4 coverage; fi + +make python-tests +pushd ../tests/ > /dev/null +percent=$(coverage3 report --include '*lib/python/*' -m | grep TOTAL | tr -s ' ' | awk '{ print $4 }' | cut -d % -f 1) +if [[ percent -lt 80 ]]; then + echo "Python Coverage: $percent" + echo "Does not meet 80% requirement" + exit 1 +fi +popd > /dev/nul diff --git a/tests/python-coverage.sh b/tests/python-coverage.sh deleted file mode 100755 index 8de6157d..00000000 --- a/tests/python-coverage.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/bash -set -x -all_networks="admin_network private_network storage_network external_network" - -# exercise help -coverage3 run ../lib/python/apex-python-utils.py -l /dev/null > /dev/null - -# exercise parse-net-settings -# throw debug on the first to exercise it -coverage3 run -a ../lib/python/apex-python-utils.py --debug parse-net-settings -s ../config/network/network_settings.yaml -i True -e ../build/network-environment.yaml > /dev/null - -# exercise proper nic-template runs -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e interface -af 4 > /dev/null -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e interface -af 6 > /dev/null -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e br-ex -af 4 > /dev/null -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null nic-template -t ../config/network/network_settings.yaml -n "$all_networks" -e br-ex -af 6 > /dev/null - -# exercise find-ip -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null find-ip -i $(ip a | grep 2: | cut -d \ -f 2 | head -n 1 | cut -d : -f 1) > /dev/null - -# exercise parse-deploy-settings -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f ../config/deploy/os-nosdn-nofeature-noha.yaml > /dev/null -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f ../config/deploy/os-nosdn-performance-ha.yaml > /dev/null - -# exercise parse-deploy-settings errors -echo "global_params:" > /tmp/python-coverage.test -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null -echo "deploy_options: string" > /tmp/python-coverage.test -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null -echo "global_params:" >> /tmp/python-coverage.test -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null -cat > /tmp/python-coverage.test << EOF -global_params: -deploy_options: - error: error -EOF -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null -cat > /tmp/python-coverage.test << EOF -global_params: -deploy_options: - performance: string -EOF -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null -cat > /tmp/python-coverage.test << EOF -global_params: -deploy_options: - performance: - error: error -EOF -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null -cat > /tmp/python-coverage.test << EOF -global_params: -deploy_options: - performance: - Controller: - error: error -EOF -coverage3 run -a ../lib/python/apex-python-utils.py -l /dev/null parse-deploy-settings -f /tmp/python-coverage.test &> /dev/null - -# coverage for ip_utils -PYTHONPATH=../lib/python/ coverage3 run -a python_coverage_ip_utils.py $(ip r | grep default | awk '{ print $5 }') - -# generate reports -coverage3 html --include '*lib/python/*' -coverage3 report --include '*lib/python/*' -m diff --git a/tests/python_coverage_ip_utils.py b/tests/python_coverage_ip_utils.py deleted file mode 100644 index 35280c18..00000000 --- a/tests/python_coverage_ip_utils.py +++ /dev/null @@ -1,25 +0,0 @@ -import sys -from apex import ip_utils - -iface = ip_utils.get_interface(sys.argv[1]) - -erroring_tests = ( - "ip_utils.get_interface('')", - "ip_utils.get_interface('lo', address_family=0)", - "ip_utils.get_interface('lo', address_family=6)", - "ip_utils.get_interface('lo')", - "ip_utils.get_ip_range()", - "ip_utils.get_ip_range(interface=iface)") - -for t in erroring_tests: - try: - eval(t) - except: - pass - -ip_utils.find_gateway(interface=iface) -ip_utils.get_ip(1, cidr="10.10.10.0/24") -ip_utils.get_ip(1, interface=iface) -ip_utils.get_ip_range(interface=iface, start_offset=1, end_offset=20) -ip_utils.get_ip_range(interface=iface, start_offset=1, count=10) -ip_utils.get_ip_range(interface=iface, end_offset=20, count=10) diff --git a/tests/test_apex_common_utils.py b/tests/test_apex_common_utils.py new file mode 100644 index 00000000..7c988e3d --- /dev/null +++ b/tests/test_apex_common_utils.py @@ -0,0 +1,34 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from apex.common.utils import str2bool + +from nose.tools import assert_equal + + +class TestCommonUtils(object): + @classmethod + def setup_class(klass): + """This method is run once for each class before any tests are run""" + + @classmethod + def teardown_class(klass): + """This method is run once for each class _after_ all tests are run""" + + def setUp(self): + """This method is run once before _each_ test method is executed""" + + def teardown(self): + """This method is run once after _each_ test method is executed""" + + def test_str2bool(self): + assert_equal(str2bool(True), True) + assert_equal(str2bool(False), False) + assert_equal(str2bool("True"), True) + assert_equal(str2bool("YES"), True) diff --git a/tests/test_apex_deploy_env.py b/tests/test_apex_deploy_env.py new file mode 100644 index 00000000..0cd144ef --- /dev/null +++ b/tests/test_apex_deploy_env.py @@ -0,0 +1,88 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import io +# https://docs.python.org/3/library/io.html + +from apex.deploy_env import DeploySettings +from apex.deploy_env import DeploySettingsException + +from nose.tools import assert_equal +from nose.tools import assert_raises + +deploy_files = ('deploy_settings.yaml', + 'os-nosdn-nofeature-noha.yaml', + 'os-nosdn-ovs-noha.yaml', + 'os-ocl-nofeature-ha.yaml', + 'os-odl_l2-sdnvpn-ha.yaml', + 'os-odl_l3-nofeature-ha.yaml', + 'os-nosdn-nofeature-ha.yaml', + 'os-nosdn-ovs-ha.yaml', + 'os-nosdn-performance-ha.yaml', + 'os-odl_l2-nofeature-ha.yaml', + 'os-odl_l2-sfc-noha.yaml', + 'os-onos-nofeature-ha.yaml') + +test_deploy_content = ( +'global_params:', +'deploy_options: string', +"""deploy_options: string +global_params:""", +"""global_params: +deploy_options: + error: error +""", +"""global_params: +deploy_options: + performance: string +""", +"""global_params: +deploy_options: + dataplane: invalid +""", +"""global_params: +deploy_options: + performance: + Controller: + error: error +""",) + + +class TestIpUtils(object): + @classmethod + def setup_class(klass): + """This method is run once for each class before any tests are run""" + + @classmethod + def teardown_class(klass): + """This method is run once for each class _after_ all tests are run""" + + def setUp(self): + """This method is run once before _each_ test method is executed""" + + def teardown(self): + """This method is run once after _each_ test method is executed""" + + def test_init(self): + for f in deploy_files: + ds = DeploySettings('../config/deploy/{}'.format(f)) + + def test__validate_settings(self): + for c in test_deploy_content: + f = open('/tmp/apex_deploy_test_file', 'w') + f.write(c) + f.close() + assert_raises(DeploySettingsException, DeploySettings, '/tmp/apex_deploy_test_file') + + def test_dump_bash(self): + # the performance file has the most use of the function + # so using that as the test case + ds = DeploySettings('../config/deploy/os-nosdn-performance-ha.yaml') + assert_equal(ds.dump_bash(), None) + assert_equal(ds.dump_bash(path='/dev/null'), None) diff --git a/tests/test_apex_ip_utils.py b/tests/test_apex_ip_utils.py new file mode 100644 index 00000000..0b44bdd0 --- /dev/null +++ b/tests/test_apex_ip_utils.py @@ -0,0 +1,95 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import re + +from apex.ip_utils import IPUtilsException +from apex.ip_utils import get_interface +from apex.ip_utils import find_gateway +from apex.ip_utils import get_ip +from apex.ip_utils import get_ip_range + +from nose.tools import assert_equal +from nose.tools import assert_raises +from nose.tools import assert_is_instance +from nose.tools import assert_regexp_matches + +from ipaddress import IPv4Address +from ipaddress import IPv6Address +from ipaddress import ip_network + + +ip4_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') +ip4_range_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3},\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') + +def get_default_gateway_linux(): + """Read the default gateway directly from /proc.""" + with open("/proc/net/route") as fh: + for line in fh: + fields = line.strip().split() + if fields[2] not in ('00000000', 'Gateway'): + return fields[0] + + +class TestIpUtils(object): + @classmethod + def setup_class(klass): + """This method is run once for each class before any tests are run""" + klass.iface_name = get_default_gateway_linux() + iface = get_interface(klass.iface_name) + klass.iface = iface + + @classmethod + def teardown_class(klass): + """This method is run once for each class _after_ all tests are run""" + + def setUp(self): + """This method is run once before _each_ test method is executed""" + + def teardown(self): + """This method is run once after _each_ test method is executed""" + + def test_get_interface(self): + assert_equal(get_interface(''), None) + assert_equal(get_interface('notreal'), None) + assert_is_instance(get_interface( + self.iface_name, + address_family=4), IPv4Address) + assert_is_instance(get_interface( + self.iface_name, + address_family=6), IPv6Address) + assert_raises(IPUtilsException, + get_interface, self.iface_name, 0) + + def test_find_gateway(self): + assert_is_instance(find_gateway(self.iface), str) + iface_virbr0 = get_interface('virbr0') + assert_equal(find_gateway(iface_virbr0), None) + + def test_get_ip(self): + assert_equal(get_ip(1, cidr="10.10.10.0/24"), "0") + assert_regexp_matches(get_ip(1, interface=self.iface), ip4_pattern) + assert_raises(IPUtilsException, get_ip, 1) + + + def test_get_ip_range_raises(self): + assert_raises(IPUtilsException, get_ip_range) + assert_raises(IPUtilsException, get_ip_range, interface=self.iface) + + def test_get_ip_range_with_interface(self): + assert_regexp_matches(get_ip_range(interface=self.iface, start_offset=1, end_offset=20), ip4_range_pattern) + assert_regexp_matches(get_ip_range(interface=self.iface, start_offset=1, count=10), ip4_range_pattern) + assert_regexp_matches(get_ip_range(interface=self.iface, end_offset=20, count=10), ip4_range_pattern) + + def test_get_ip_range_with_cidr(self): + cidr = ip_network('10.10.10.0/24') + assert_raises(IPUtilsException, get_ip_range, cidr=cidr) + assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1, end_offset=20), ip4_pattern) + assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1, count=10), ip4_pattern) + assert_regexp_matches(get_ip_range(cidr=cidr, end_offset=20, count=10), ip4_pattern) diff --git a/tests/test_apex_network_environment.py b/tests/test_apex_network_environment.py new file mode 100644 index 00000000..90c89073 --- /dev/null +++ b/tests/test_apex_network_environment.py @@ -0,0 +1,42 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from apex.network_settings import NetworkSettings +from apex.network_environment import NetworkEnvironment +from apex.network_environment import NetworkEnvException + +from nose.tools import assert_equal +from nose.tools import assert_raises +from nose.tools import assert_is_instance +from nose.tools import assert_not_equal + + +class TestNetworkEnvironment(object): + @classmethod + def setup_class(klass): + """This method is run once for each class before any tests are run""" + + @classmethod + def teardown_class(klass): + """This method is run once for each class _after_ all tests are run""" + + def setUp(self): + """This method is run once before _each_ test method is executed""" + + def teardown(self): + """This method is run once after _each_ test method is executed""" + + def test_init(self): + assert_raises(NetworkEnvException, NetworkEnvironment, None, '../build/network-environment.yaml') + + def test_get_netenv_settings(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + ne = NetworkEnvironment(ns, '../build/network-environment.yaml') + assert_is_instance(ne.get_netenv_settings(), dict) + assert_not_equal(ne.get_netenv_settings(), {}) diff --git a/tests/test_apex_network_settings.py b/tests/test_apex_network_settings.py new file mode 100644 index 00000000..a891473f --- /dev/null +++ b/tests/test_apex_network_settings.py @@ -0,0 +1,45 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (Red Hat) +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from apex.network_settings import NetworkSettings + +from nose.tools import assert_equal +from nose.tools import assert_is_instance + + +class TestNetworkSettings(object): + @classmethod + def setup_class(klass): + """This method is run once for each class before any tests are run""" + + @classmethod + def teardown_class(klass): + """This method is run once for each class _after_ all tests are run""" + + def setUp(self): + """This method is run once before _each_ test method is executed""" + + def teardown(self): + """This method is run once after _each_ test method is executed""" + + def test_init(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + + def test_dump_bash(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + assert_equal(ns.dump_bash(), None) + assert_equal(ns.dump_bash(path='/dev/null'), None) + + def test_get_network_settings(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + assert_is_instance(ns.get_network_settings(), dict) + + def test_get_enabled_networks(self): + ns = NetworkSettings('../config/network/network_settings.yaml', True) + assert_is_instance(ns.get_enabled_networks(), list) |