用于编写pytest多主机测试的实用工具
项目描述
pytest插件,用于多主机测试。
下载
- 发行版归档文件将通过Pagure发行版提供下载
目标是将其项目包含在Fedora仓库中。在此之前,您可以使用COPR的测试构建 – 请参阅下面的“开发人员链接”。
- 您也可以使用pip进行安装
用法
此插件接受您的基础设施描述,并通过一个 fixture 提供 Host 对象,可以在其上调用命令。
它旨在作为框架的通用基础;任何使用它的项目都需要根据其自身需求进行扩展。
提供给测试的对象是一个 Config 对象,它具有(包括其他)以下属性
test_dir – directory to store test-specific data in, defaults to /root/multihost_tests ipv6 – true if connecting via IPv6 domains – the list of domains
要运行的宿主机按域排列,域具有
name – the DNS name of the domain type – a string specifying the type of the domain ('default' by default) config – the Config this domain is part of hosts – list of hosts in this domain
并且宿主机有
role – type of this host; should encode the OS and installed packages hostname – fully qualified hostname, usually reachable from other hosts shortname – first component of hostname external_hostname – hostname used to connect to this host ip – IP address domain – the Domain this host is part of transport – allows operations like uploading and downloading files run_command() – runs the given command on the host
对于每个对象——配置、域、主机——都可以提供子类来修改行为(例如,FreeIPA会添加主机方法来运行LDAP查询或安装IPA服务器)。每个对象都有from_dict和to_dict方法,可以添加额外的属性——例如,Config.ntp_server。
要在测试中使用多主机插件,创建一个列出域和所需主机角色的数量列表的测试用例。
import pytest from pytest_multihost import make_multihost_fixture @pytest.fixture(scope='class') def multihost(request): mh = make_multihost_fixture( request, descriptions=[ { 'type': 'ipa', 'hosts': { 'master': 1, 'replica': 2, }, }, ], ) return mh
如果可用的主机不足,将跳过使用测试用例的所有测试。
从make_multihost_fixture返回的对象只有“config”属性。预期用户添加方便的属性。例如,FreeIPA通常使用一个域名,一个主服务器,几个副本和一些客户端,会这样做:
from pytest_multihost import make_multihost_fixture @pytest.fixture(scope='class') def multihost(request): mh = make_multihost_fixture(request, descriptions=[ { 'type': 'ipa', 'hosts': { 'master': 1, 'replica': 1, 'client': 1, }, }, ], ) # Set convenience attributes mh.domain = mh.config.domains[0] [mh.master] = mh.domain.hosts_by_role('master') mh.replicas = mh.domain.hosts_by_role('replica') mh.clients = mh.domain.hosts_by_role('client') # IPA-specific initialization/teardown of the hosts request.cls().install(mh) request.addfinalizer(lambda: request.cls().uninstall(mh)) # Return the fixture return mh
与任何pytest测试用例一样,这可以通过将函数参数来使用。以下是一个简化的示例,FreeIPA的使用可能如下所示:
class TestMultihost(object): def install(self, multihost): multihost.master.run_command(['ipa-server-install']) def uninstall(self, multihost): multihost.master.run_command(['ipa-server-install', '--uninstall']) def test_installed(self, multihost): multihost.master.run_command(['ipa', 'ping'])
基础设施的描述由JSON或YAML文件提供,该文件在pytest命令行上命名。例如:
ssh_key_filename: ~/.ssh/id_rsa domains: - name: adomain.test type: test-a hosts: - name: master ip: 192.0.2.1 role: master - name: replica1 ip: 192.0.2.2 role: replica - name: replica2 ip: 192.0.2.3 role: replica external_hostname: r2.adomain.test - name: client1 ip: 192.0.2.4 role: client - name: extra ip: 192.0.2.6 role: extrarole - name: bdomain.test type: test-b hosts: - name: master.bdomain.test ip='192.0.2.65 role: master
$ py.test –multihost-config=/path/to/configfile.yaml
要使用YAML文件,需要PyYAML包。没有它,只能使用JSON文件。
编码和字节/文本
在写入文件或发出命令时,字节字符串会保持不变,而文本字符串(Python 2中的unicode)将使用可配置的编码(默认为utf-8)进行编码。
在读取文件时,默认返回字节字符串,但可以给出编码以获取测试字符串。
对于命令输出,提供了独立的stdout_bytes和stdout_text属性。后者使用可配置的编码(默认为utf-8)。
贡献
项目乐于接受补丁!请将任何补丁作为项目Pagure仓库的拉取请求。所有开发讨论应在Pagure拉取请求和问题中进行。
开发者链接
要发布,请更新setup.py中的版本,添加类似于“v0.3”的Git标签,并运行make tarball。运行make upload会将tarball放入Fedora Hosted和PyPI,以及Fedorapeople上的SRPM(如果您有权限)。运行make release将上传并触发COPR构建。