Enable running unit test and coverage in py3.
- Migrate run_tests.sh to python3
- Running run_tests.sh in py2 is not supported.
Bug: 157392770
Test: ./run_test.sh
./run_test_py2.sh
atest --host acloud_test
atest --host acloud_test_py3
Change-Id: Ibf6a535f36c9ebd196b075eed092c10992f338eb
diff --git a/run_tests.sh b/run_tests.sh
index c1be9ab..c1ee76f 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -14,6 +14,8 @@
"dateutil"
"google-api-python-client"
"oauth2client"
+ "uritemplates"
+ "rsa"
)
for lib in ${third_party_libs[*]};
do
@@ -27,8 +29,8 @@
local test_results=$1
local tmp_dir=$(mktemp -d)
local rc_file=${ACLOUD_DIR}/.coveragerc
- PYTHONPATH=$(get_python_path) python -m coverage report -m
- PYTHONPATH=$(get_python_path) python -m coverage html -d $tmp_dir --rcfile=$rc_file
+ PYTHONPATH=$(get_python_path) python3 -m coverage report -m
+ PYTHONPATH=$(get_python_path) python3 -m coverage html -d $tmp_dir --rcfile=$rc_file
echo "coverage report available at file://${tmp_dir}/index.html"
if [[ $test_results -eq 0 ]]; then
@@ -41,16 +43,16 @@
function run_unittests() {
local specified_tests=$@
local rc=0
- local run_cmd="python -m coverage run --append"
+ local run_cmd="python3 -m coverage run --append"
# clear previously collected coverage data.
- PYTHONPATH=$(get_python_path) python -m coverage erase
+ PYTHONPATH=$(get_python_path) python3 -m coverage erase
# Get all unit tests under tools/acloud.
local all_tests=$(find $ACLOUD_DIR -type f -name "*_test.py" ! -name "acloud_test.py");
local tests_to_run=$all_tests
- # Filter out the tests if specifed.
+ # Filter out the tests if specified.
if [[ ! -z $specified_tests ]]; then
tests_to_run=()
for t in $all_tests;
@@ -90,8 +92,8 @@
local missing_py_packages=false
for py_lib in {coverage,mock};
do
- if ! pip list | grep $py_lib &> /dev/null; then
- echo "Missing required python package: $py_lib (pip install $py_lib)"
+ if ! python3 -m pip list | grep $py_lib &> /dev/null; then
+ echo "Missing required python package: $py_lib (python3 -m pip install $py_lib)"
missing_py_packages=true
fi
done
diff --git a/run_tests_py2.sh b/run_tests_py2.sh
new file mode 100755
index 0000000..cb524c4
--- /dev/null
+++ b/run_tests_py2.sh
@@ -0,0 +1,123 @@
+#!/bin/bash
+
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+NC='\033[0m' # No Color
+ACLOUD_DIR=$(dirname $(realpath $0))
+TOOLS_DIR=$(dirname $ACLOUD_DIR)
+THIRD_PARTY_DIR=$(dirname $TOOLS_DIR)/external/python
+
+function get_python_path() {
+ local python_path=$TOOLS_DIR
+ local third_party_libs=(
+ "apitools"
+ "dateutil"
+ "google-api-python-client"
+ "oauth2client"
+ )
+ for lib in ${third_party_libs[*]};
+ do
+ python_path=$THIRD_PARTY_DIR/$lib:$python_path
+ done
+ python_path=$python_path:$PYTHONPATH
+ echo $python_path
+}
+
+function print_summary() {
+ local test_results=$1
+ local tmp_dir=$(mktemp -d)
+ local rc_file=${ACLOUD_DIR}/.coveragerc
+ PYTHONPATH=$(get_python_path) python -m coverage report -m
+ PYTHONPATH=$(get_python_path) python -m coverage html -d $tmp_dir --rcfile=$rc_file
+ echo "coverage report available at file://${tmp_dir}/index.html"
+
+ if [[ $test_results -eq 0 ]]; then
+ echo -e "${GREEN}All unittests pass${NC}!"
+ else
+ echo -e "${RED}There was a unittest failure${NC}"
+ fi
+}
+
+function run_unittests() {
+ local specified_tests=$@
+ local rc=0
+ local run_cmd="python -m coverage run --append"
+
+ # clear previously collected coverage data.
+ PYTHONPATH=$(get_python_path) python -m coverage erase
+
+ # Get all unit tests under tools/acloud.
+ local all_tests=$(find $ACLOUD_DIR -type f -name "*_test.py" ! -name "acloud_test.py");
+ local tests_to_run=$all_tests
+
+ # Filter out the tests if specified.
+ if [[ ! -z $specified_tests ]]; then
+ tests_to_run=()
+ for t in $all_tests;
+ do
+ for t_pattern in $specified_tests;
+ do
+ if [[ "$t" =~ "$t_pattern" ]]; then
+ tests_to_run=("${tests_to_run[@]}" "$t")
+ fi
+ done
+ done
+ fi
+
+ for t in $tests_to_run;
+ do
+ if ! PYTHONPATH=$(get_python_path):$PYTHONPATH $run_cmd $t; then
+ rc=1
+ echo -e "${RED}$t failed${NC}"
+ fi
+ done
+
+ print_summary $rc
+ cleanup
+ exit $rc
+}
+
+function check_env() {
+ if [ -z "$ANDROID_HOST_OUT" ]; then
+ echo "Missing ANDROID_HOST_OUT env variable. Run 'lunch' first."
+ exit 1
+ fi
+ if [ ! -f "$ANDROID_HOST_OUT/bin/aprotoc" ]; then
+ echo "Missing aprotoc. Run 'm aprotoc' first."
+ exit 1
+ fi
+
+ local missing_py_packages=false
+ for py_lib in {coverage,mock};
+ do
+ if ! python -m pip list | grep $py_lib &> /dev/null; then
+ echo "Missing required python package: $py_lib (python -m pip install $py_lib)"
+ missing_py_packages=true
+ fi
+ done
+ if $missing_py_packages; then
+ exit 1
+ fi
+}
+
+function gen_proto_py() {
+ # Use aprotoc to generate python proto files.
+ local protoc_cmd=$ANDROID_HOST_OUT/bin/aprotoc
+ pushd $ACLOUD_DIR &> /dev/null
+ $protoc_cmd internal/proto/*.proto --python_out=./
+ touch internal/proto/__init__.py
+ popd &> /dev/null
+}
+
+function cleanup() {
+ # Search for *.pyc and delete them.
+ find $ACLOUD_DIR -name "*.pyc" -exec rm -f {} \;
+
+ # Delete the generated proto files too.
+ find $ACLOUD_DIR/internal/proto -name "*.py" -exec rm -f {} \;
+}
+
+check_env
+cleanup
+gen_proto_py
+run_unittests $@
diff --git a/setup/gcp_setup_runner_test.py b/setup/gcp_setup_runner_test.py
index fa12d1f..7e4dac4 100644
--- a/setup/gcp_setup_runner_test.py
+++ b/setup/gcp_setup_runner_test.py
@@ -19,7 +19,6 @@
import os
import mock
import six
-from six import b
# pylint: disable=no-name-in-module,import-error,no-member
from acloud import errors
@@ -28,7 +27,7 @@
from acloud.public import config
from acloud.setup import gcp_setup_runner
-_GCP_USER_CONFIG = b("""
+_GCP_USER_CONFIG = """
[compute]
region = new_region
zone = new_zone
@@ -36,8 +35,7 @@
account = new@google.com
disable_usage_reporting = False
project = new_project
-""")
-
+"""
def _CreateCfgFile():
"""A helper method that creates a mock configuration object."""
@@ -147,7 +145,7 @@
@mock.patch.object(gcp_setup_runner, "GoogleSDKBins")
def testSetupGcloudInfo(self, mock_sdk, mock_set, mock_run, mock_create):
"""test setup gcloud info"""
- with mock.patch("google_sdk.GoogleSDK"):
+ with mock.patch("acloud.setup.google_sdk.GoogleSDK"):
self.gcp_env_runner._SetupGcloudInfo()
mock_sdk.assert_called_once()
mock_set.assert_called_once()
diff --git a/setup/host_setup_runner_test.py b/setup/host_setup_runner_test.py
index e435be6..111540e 100644
--- a/setup/host_setup_runner_test.py
+++ b/setup/host_setup_runner_test.py
@@ -17,7 +17,6 @@
import tempfile
import unittest
import mock
-from six import b
from acloud.internal.lib import driver_test_lib
from acloud.internal.lib import utils
@@ -30,13 +29,13 @@
class CuttlefishHostSetupTest(driver_test_lib.BaseDriverTest):
"""Test CuttlsfishHostSetup."""
- LSMOD_OUTPUT = b("""nvidia_modeset 860160 6 nvidia_drm
+ LSMOD_OUTPUT = """nvidia_modeset 860160 6 nvidia_drm
module1 12312 1
module2 12312 1
ghash_clmulni_intel 16384 0
aesni_intel 167936 3
aes_x86_64 20480 1 aesni_intel
-lrw 16384 1 aesni_intel""")
+lrw 16384 1 aesni_intel"""
# pylint: disable=invalid-name
def setUp(self):
diff --git a/setup/setup_common_test.py b/setup/setup_common_test.py
index e17e529..5a00cfc 100644
--- a/setup/setup_common_test.py
+++ b/setup/setup_common_test.py
@@ -14,7 +14,6 @@
"""Tests for host_setup_runner."""
import subprocess
import unittest
-from six import b
from acloud import errors
from acloud.internal.lib import driver_test_lib
@@ -23,21 +22,21 @@
class SetupCommonTest(driver_test_lib.BaseDriverTest):
"""Test HostPkgTaskRunner."""
- PKG_INFO_INSTALLED = b("""fake_pkg:
+ PKG_INFO_INSTALLED = """fake_pkg:
Installed: 0.7
Candidate: 0.7
Version table:
-""")
- PKG_INFO_NONE_INSTALL = b("""fake_pkg:
+"""
+ PKG_INFO_NONE_INSTALL = """fake_pkg:
Installed: (none)
Candidate: 0.7
Version table:
-""")
- PKG_INFO_OLD_VERSION = b("""fake_pkg:
+"""
+ PKG_INFO_OLD_VERSION = """fake_pkg:
Installed: 0.2
Candidate: 0.7
Version table:
-""")
+"""
# pylint: disable=invalid-name
def testPackageNotInstalled(self):