「git バージョン 2.5.2」をインストールする Chef クックブックがあります。CentOS 6.4 vm を使用してこのクックブックを適用し、git のバージョンを確認するテストを作成しました。
スニペットは次のようになります。
# Test if git exists
describe command('git --version') do
its(:stdout) { should match "git version 2.5.2" }
end
kitchen verify を実行すると、テストは実行されますが、予想とは異なるバージョンの git が返され、テストが失敗します。
エラーは次のとおりです。
2) Command "git --version" stdout should match "git version 2.5.2"
Failure/Error: its(:stdout) { should match "git version 2.5.2" }
expected "git version 1.7.1\n" to match "git version 2.5.2"
Diff:
@@ -1,2 +1,2 @@
-git version 2.5.2
+git version 1.7.1
/bin/sh -c git\ --version
git version 1.7.1
VM の /usr/bin ディレクトリには、たまたま git バージョン 1.7.1 がインストールされています。このレシピは、git バージョン 2.5.2 を /usr/local/bin ディレクトリにインストールします。コマンド「kitchen login」を使用して VM にログインすると、vagrant ユーザーとして接続されます。/usr/local/bin ディレクトリは PATH で /usr/bin より上位にあるため、「git --version」を実行すると、出力として「git version 2.5.2」が得られます。ただし、kitchen verify を実行すると、root ユーザーでテストが実行されます。root ユーザーの PATH には /usr/local/bin が含まれていませんが、/usr/bin が含まれているため、「git バージョン 1.7.1」が返されます。
テストの実行時にキッチン検証が使用する VM 上のユーザーを制御するにはどうすればよいですか?
次のようなテストで「su -c」コマンドを使用してみました。
describe command('su -c "whoami" vagrant') do
its(:stdout) { should match "vagrant" }
end
結果は予想通りでした:
Command "su -c "whoami" vagrant"
stdout
should match "vagrant"
git コマンドで変更します。
describe command('su -c "git --version" vagrant') do
its(:stdout) { should match "git version 2.5.2" }
end
結果:
1) Command "su -c "git --version" vagrant" stdout should match "git version 2.5.2"
Failure/Error: its(:stdout) { should match "git version 2.5.2" }
expected "" to match "git version 2.5.2"
/bin/sh -c su\ -c\ \"git\ --version\"\ vagrant
vagrant ユーザーのパスの確認:
describe command('su -c "echo $PATH" vagrant') do
its(:stdout) { should match "" }
end
この場合、テストは成功します。vagrant ユーザーのパスは設定されないため、単純な「git --version」コマンドは機能しません。