温故OpenStack中的测试(by Joshua)

版权声明:本文为博主原创文章,如需转载,请注明出处! https://blog.csdn.net/quqi99/article/details/79567620

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (作者:张华 发表于:2018-03-15)

  1. 沿用tox调用virtualenv自动创建的虚拟环境(virtualenv -p python3.5 .tox/py35)
source .tox/py35/bin/activate
sudo pip install --upgrade -r requirements.txt 
sudo pip install --upgrade -r test-requirements.txt
  1. 使用unittest和nose运行测试。nose是对unittest的扩展,使得python的测试更加简单,nose自动发现测试代码并执行,nose提供了大量的插件,比如覆盖报表等。
python -m unittest -v unit_tests.test_neutron_utils.TestNeutronUtils.test_get_packages_ovs_newton
.tox/py35/bin/python nosetests -v unit_tests/test_neutron_utils.py:TestNeutronUtils.test_get_packages_ovs_newton

注意:上面采用nosetests运行时会报错,因为我们的测试采用了python3, 所以需要在安装了python3-nose之后(sudo apt-get install python3-nose python3-mock)再采用下列三种方式之一运行:

nosetests3 -v unit_tests/test_neutron_utils.py:TestNeutronUtils.test_restart_map_ovs_odl
/bak/work/charms/neutron-gateway/.tox/py35/bin/python /usr/local/bin/nosetests -v unit_tests/test_neutron_utils.py:TestNeutronUtils.test_get_packages_ovs_newton
python -m nose unit_tests/test_neutron_utils.py:TestNeutronUtils.test_restart_map_ovs_odl

但实际上仍然找不找nose模块,那是因为nose与virtualenv结合地不大好,在这个网页找着了答案(https://stackoverflow.com/questions/864956/problems-using-nose-in-a-virtualenv) - You need to have a copy of nose installed in the virtual environment. In order to force installation of nose into the virtualenv, even though it is already installed in the global site-packages, run pip install with the -I flag: pip install nose -I

  1. 上面使用unittest与nose运行测试的方式只是将结果输出到stdout,不便于分析。所以可以使用python-subunit模块来运行测试,并将测试结果通过subunit协议输出到文件中便于日后分析。因为subunit是基于二进制的不便于人眼看,所以可使用subunit2pyunit工具将其人类可读化。
python -m subunit.run discover |subunit2pyunit
python -m subunit.run discover -t ./ ./unit_tests |subunit2pyunit
python -m subunit.run unit_tests.test_neutron_utils.TestNeutronUtils. |subunit2pyunit
  1. 在大型应用中分析测试结果很重要,testrepository可以调用subunit来用python-subunit模块来运行测试,并将测试结果通过subunit协议输出到文件中,然后testrepository在些基础上有更多的分析,如分析哪些用例运行的时间最长,如显示失败的用例,如仅运行上次运行失败的用例。
testr init
testr run
testr run --parallel
$ cat .testr.conf 
[DEFAULT]
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 ./ ./unit_tests $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list
  1. tox用于创建虚拟python环境,也可以集成上面的testrepository(commands = ostestr {posargs})
$ cat tox.ini 
[tox]
envlist = pep8,py27,py35
skipsdist = True

[testenv]
setenv = VIRTUAL_ENV={envdir}
         PYTHONHASHSEED=0
         CHARM_DIR={envdir}
         AMULET_SETUP_TIMEOUT=5400
install_command =
  pip install --allow-unverified python-apt {opts} {packages}
commands = ostestr {posargs}
whitelist_externals = juju
passenv = HOME TERM AMULET_* CS_API_*

[testenv:py27]
basepython = python2.7
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/test-requirements.txt
commands = /bin/true

[testenv:py35]
basepython = python3.5
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/test-requirements.txt

[testenv:pep8]
basepython = python2.7
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/test-requirements.txt
commands = flake8 {posargs} hooks unit_tests tests actions lib
           charm-proof

[flake8]
ignore = E402,E226
exclude = */helpers
  1. pydev使用virtualenv中的py35

    在eclipse的"Preferences -> Pydev -> Interpreters -> Python Interpreters"菜单中定义python35=/bak/work/charms/neutron-gateway/.tox/py35/bin/python,然后在工程上点右键从"Properties -> Pydev - Interpreter/Grammar"定义使用python35。注意,需要将/bak/work/charms/neutron-gateway/.tox/py35/lib/python3.5/site-packages也选到环境变量中,否则后面会报ImportError: No module named 'mock。
    为一个测试类定义"Python unitest"类型的"Debug Configurations", 也在其Interpreter选项卡中定义使用python35 (结果:eclipse似乎有bug,此处选择了python35后无法保存)
    所以无法成功,似乎是pydev与python3协作不大好。最后还是pudb好使(sudo pip install pudb, import pudb; pdb.set_trace())

  2. py27下的测试运行方法(如openstack), charm似乎只能用py35

cat tox.ini
tox -r -epy27 
tox -e py27,pep8
tox -e py27 neutron.tests.unit.agent.linux.test_keepalived
tox -e py27 neutron.tests.unit.agent.linux.test_keepalived.KeepalivedInstanceTestCase.test_remove_addresses_by_interface
mkdir /opt/stack && sudo chown -R hua /opt/stack
tox -e functional neutron.tests.functional.agent.l3.test_ha_router.L3HATestCase.test_keepalived_configuration
tox -e dsvm-fullstack
source .tox/functional/bin/activate
.tox/functional/bin/pip install -r requirements.txt
.tox/functional/bin/pip install -r test-requirements.txt
.tox/functional/bin/pip install -r neutron/tests/functional/requirements.txt
.tox/functional/bin/pip freeze |grep neutron
.tox/functional/bin/pip install 'neutron-lib==1.13.0'
OS_SUDO_TESTING=True .tox/functional/bin/python -m unittest -v neutron.tests.functional.agent.l3.test_ha_router.L3HATestCase.test_keepalived_configuration

有时如mitaka已经eol了, 例如它的origain-stable-mitaka这个分支都没有了, 这会导致运行’tox -r -epy27 '不成功, 那么可以手工执行:

virtualenv venv
. venv/bin/activate

# pg_config executable not found
# Error: could not determine PostgreSQL version from '10.5'
sudo apt install python-setuptools python-dev libpython-dev libssl-dev python-pip libmysqlclient-dev libxml2-dev libxslt-dev libxslt1-dev libpq-dev git git-review libffi-dev gettext graphviz libjpeg-dev zlib1g-dev build-essential python-nose python-mock libssl1.0
sudo apt install python3.6 python3.6-dev python3-pip python3-dev python3-nose python3-mock
#sudo pip install --upgrade setuptools
#sudo pip3 install --upgrade setuptools
#sudo pip install --upgrade --force-reinstall pip virtualenv

# Failed to install Cryptography - sudo apt install libssl1.0
# No module named dulwich - ./venv/bin/pip install dulwich
# unittest has no attribute 'virt' - 
./venv/bin/pip install -c https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt?h=mitaka-eol .
./venv/bin/pip install -r requirements.txt
./venv/bin/pip install -r test-requirements.txt

find . -name "*.pyc" -exec rm -rf {} \;

git clone https://github.com/openstack/oslo.cache.git
cd oslo.cache && git checkout -b mitaka mitaka-eol
../nova/venv/bin/pip install -r requirements.txt
../nova/venv/bin/pip install -r test-requirements.txt

# dogpile can't cannot import name threading - venv/bin/pip uninstall dogpile.cache dogpile && venv/bin/pip install dogpile.cache
venv/bin/python -m subunit.run discover ./nova/tests/unit/compute |subunit2pyunit
venv/bin/python -m subunit.run nova.tests.unit.compute.test_compute |subunit2pyunit
venv/bin/python -m subunit.run nova.tests.unit.compute.test_compute.ComputeAPITestCase.test_attach_volume |subunit2pyunit
venv/bin/python -m unittest -v nova.tests.unit.compute.test_compute  #make sure the package nova.tests.virt exists

#venv/bin/pep8
venv/bin/flake8
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页