Enabling python coverage tests in build.sh 27/14927/6
authorDan Radez <dradez@redhat.com>
Wed, 1 Jun 2016 02:58:00 +0000 (22:58 -0400)
committerDan Radez <dradez@redhat.com>
Tue, 7 Jun 2016 13:26:24 +0000 (09:26 -0400)
Change-Id: Iececedb03a60dd0bfc1ad6ef1275f6a0427350eb
Signed-off-by: Dan Radez <dradez@redhat.com>
build/Makefile
ci/build.sh
lib/python/apex/deploy_env.py
lib/python/apex/ip_utils.py
tests/python-coverage.sh
tests/python_coverage_ip_utils.py [new file with mode: 0644]

index 8a498a6..4d024a3 100644 (file)
@@ -56,6 +56,15 @@ $(RPMCOM):
        # build the common RPM
        rpmbuild --clean -ba opnfv-apex-common.spec -D '_topdir %(echo `pwd`)' -D '_builddir %(echo `pwd`)' -D '_sourcedir %(echo `pwd`)' -D '_rpmdir %(echo `pwd`)' -D '_specdir %(echo `pwd`)' -D '_srcrpmdir %(echo `pwd`)' -D "release $(shell echo $(RELEASE) | tr -d '_-')"
 
+##################
+#  PYTHON TESTS  #
+##################
+
+.PHONY: python-tests
+python-tests:
+       cd ../tests && ./python-coverage.sh
+
+
 ###############
 #  UNDERCLOUD #
 ###############
index dd9f9fd..12e0f9d 100755 (executable)
@@ -34,6 +34,7 @@ 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"
 
@@ -63,6 +64,11 @@ 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"
@@ -145,6 +151,21 @@ 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 "Does not meet 80% requirement"
+        exit 1
+    fi
+    popd
+fi
+
 # Execute make against targets
 for t in $MAKE_TARGETS; do
     run_make $t
index be8779a..bfb94f5 100644 (file)
@@ -45,10 +45,10 @@ class DeploySettings:
 
         if 'deploy_options' not in self.deploy_settings:
             raise DeploySettingsException("No deploy options provided in"
-                                          "deploy settings file")
+                                          " deploy settings file")
         if 'global_params' not in self.deploy_settings:
             raise DeploySettingsException("No global options provided in"
-                                          "deploy settings file")
+                                          " deploy settings file")
 
         deploy_options = self.deploy_settings['deploy_options']
         if not isinstance(deploy_options, dict):
@@ -61,7 +61,7 @@ class DeploySettings:
 
         for required_setting in REQ_DEPLOY_SETTINGS:
             if required_setting not in deploy_options:
-                self.deploy_settings['deploy_options'][required] = False
+                self.deploy_settings['deploy_options']['required'] = False
 
         if 'performance' in deploy_options:
             if not isinstance(deploy_options['performance'], dict):
index d7099db..f51f227 100644 (file)
@@ -56,7 +56,7 @@ def get_ip_range(start_offset=None, count=None, end_offset=None,
             end_index = -1 - end_offset
         else:
             raise IPUtilsException("Argument error: must pass in exactly 2 of"
-                                   "start_offset, end_offset and count")
+                                   " start_offset, end_offset and count")
 
         start_ip = cidr[start_index]
         end_ip = cidr[end_index]
@@ -77,7 +77,7 @@ def get_ip_range(start_offset=None, count=None, end_offset=None,
             else:
                 raise IPUtilsException(
                     "Argument error: must pass in exactly 2 of"
-                    "start_offset, end_offset and count")
+                    " start_offset, end_offset and count")
         else:
             if count and start_offset and not end_offset:
                 start_ip = network[start_offset]
@@ -91,7 +91,7 @@ def get_ip_range(start_offset=None, count=None, end_offset=None,
             else:
                 raise IPUtilsException(
                     "Argument error: must pass in exactly 2 of"
-                    "start_offset, end_offset and count")
+                    " start_offset, end_offset and count")
 
     else:
         raise IPUtilsException("Must pass in cidr or interface to generate"
index ee47144..8de6157 100755 (executable)
@@ -3,21 +3,63 @@ set -x
 all_networks="admin_network private_network storage_network external_network"
 
 # exercise help
-coverage-3.4 run ../lib/python/apex-python-utils.py -l /dev/null > /dev/null
+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
-coverage-3.4 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
+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
-coverage-3.4 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
-coverage-3.4 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
-coverage-3.4 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
-coverage-3.4 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
+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
-coverage-3.4 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
+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
-coverage-3.4 report --include '*lib/python/*' -m
-coverage-3.4 html --include '*lib/python/*'
+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
new file mode 100644 (file)
index 0000000..35280c1
--- /dev/null
@@ -0,0 +1,25 @@
+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)