2016-07-13 3 views
0

Testinfraの範囲内で、ターゲットオペレーティングシステムのテスト条件を作成するにはどうすればよいですか?テストをリモートで実行しているときに、TestinfraでターゲットOSタイプを取得する方法は?

私は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" 
をしかし、これらは基本的に中のショットでした例と文書の欠如のために暗いですし、うまくいかなかったのです。

答えて

1

私はちょっと同じ問題を抱えていたが、私はこの部分では、より良い見て取ったとき、このようにそれを解決: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これを望んでいると私は、私の答えを再edittedまし 。

私はのZabbixエージェントのための私の役割のために今働いて、次があります。

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" 

幸運!

+1

ああ、私の悪いです。私はcentos dockerコンテナのcentosホストを使用します。私もMoleculeでそれを使用し、OSを信頼できるものにする方法を探していました。それを動作させるためにもう少し検索する必要があります。私に知らせてくれてありがとう! –

+0

もう一度。 TestInfraが独自のチェックを行っているかどうかを確認しました:https://github.com/philpep/testinfra/blob/master/testinfra/test/test_modules.py def test_passwd_file(File、SystemInfo):を使用して、次のようにします: SystemInfo.type == '関数内のdarwin'。 –

+0

これは素晴らしい作品です。ビッグ、大きなありがとう! – techraf

関連する問題