如何在 运行 远程测试时在 Testinfra 中输入 OS 目标?

How to get target OS type in Testinfra when running a test remotely?

Testinfra 中,如何为目标操作系统创建测试条件(如果有的话)?

我想 运行 通过 target 主机进行测试:

$ testinfra -v --host=target test.py

我试过了:

def test_passwd_file(File):
    passwd = File("/etc/passwd")
    if SystemInfo.type == "darwin"
        assert passwd.group == "wheel"

我试过了:

if SystemInfo.type == "darwin"
    def test_passwd_file(File):
        passwd = File("/etc/passwd")
        assert passwd.group == "wheel"

但由于缺乏示例和文档,这些基本上都是在黑暗中拍摄的,没有用。

我遇到了同样的问题,但是当我仔细查看这部分时就这样解决了:http://testinfra.readthedocs.io/en/latest/examples.html#test-docker-images

我的测试文件中有: 导入 testinfra

os = testinfra.get_backend(
  "local://"
).get_module("SystemInfo").distribution

def test_zabbix_package(Package):
    zabbixagent = Package('zabbix-agent')
    assert zabbixagent.is_installed

    if os == 'centos':
        assert zabbixagent.version.startswith("3.0")
    elif os == 'debian':
        assert zabbixagent.version.startswith("1:3.0")

首先导入'testinfra'模块。 通过执行 testinfra_get_backend 模块创建 os 变量。在我的例子中,我必须 运行 具有 distribution 功能的 SystemInfo 模块。

在测试中我可以使用 os 变量并在 if 语句中使用它。

对于你的问题,我想这样建议: 导入 testinfra

os = testinfra.get_backend(
  "local://"
).get_module("SystemInfo").type

def test_passwd_file(File):
    passwd = File("/etc/passwd")
    if os == "darwin":
        assert passwd.group == "wheel"

编辑: 我已经重新编辑了我的答案,正如 SO 所希望的那样。

我的 Zabbix Agent 角色现在有以下工作:

def test_zabbix_package(Package, SystemInfo):
    zabbixagent = Package('zabbix-agent')
    assert zabbixagent.is_installed

    if SystemInfo.distribution == 'debian':
        assert zabbixagent.version.startswith("1:3.0")
    if SystemInfo.distribution == 'centos':
        assert zabbixagent.version.startswith("3.0")

这适用于 Debian 和 CentOS 容器。

def test_passwd_file(File, SystemInfo):
    passwd = File("/etc/passwd")
    if SystemInfo.type == "darwin":
        assert passwd.group == "wheel"

祝你好运!