我正在使用带有 ec2 驱动程序的 Kitchen 来学习 Chef 和测试过程,并拥有以下 serverspec 文件:
require "serverspec"
set :backend, :exec
describe "rbenv" do
describe file("/home/ec2-user/.rbenv") do
it { should be_directory }
end
describe command("rbenv versions") do
its(:exit_status) { should eq 0 }
end
end
当我运行kitchen verify amazon-linux
第一个测试通过但第二个测试失败并导致错误时:
rbenv
File "/home/ec2-user/.rbenv"
should be directory
Command "rbenv versions"
exit_status
should eq 0 (FAILED - 1)
Failures:
1) rbenv Command "rbenv versions" exit_status should eq 0
Failure/Error: its(:exit_status) { should eq 0 }
expected: 0
got: 127
(compared using ==)
/bin/sh -c rbenv\ versions
# /tmp/verifier/suites/serverspec/rbenv_spec.rb:11:in `block (3 levels) in <top (required)>'
Finished in 0.05481 seconds (files took 0.30304 seconds to load)
2 examples, 1 failure
Failed examples:
rspec /tmp/verifier/suites/serverspec/rbenv_spec.rb:11 # rbenv Command "rbenv versions" exit_status should eq 0
/opt/chef/embedded/bin/ruby -I/tmp/verifier/suites/serverspec -I/tmp/verifier/gems/gems/rspec-support-3.4.1/lib:/tmp/ver
ifier/gems/gems/rspec-core-3.4.4/lib /opt/chef/embedded/bin/rspec --pattern /tmp/verifier/suites/serverspec/\*\*/\*_spec.rb --c
olor --format documentation --default-path /tmp/verifier/suites/serverspec failed
!!!!!! Ruby Script [/tmp/verifier/gems/gems/busser-serverspec-0.5.9/lib/busser/runner_plugin/../serverspec/runner.rb /tm
p/verifier/suites/serverspec] exit code was 1
>>>>>> Verify failed on instance <default-amazon-linux>.
>>>>>> Please see .kitchen/logs/default-amazon-linux.log for more details
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: SSH exited (1) for command: [sh -c '
BUSSER_ROOT="/tmp/verifier"; export BUSSER_ROOT
GEM_HOME="/tmp/verifier/gems"; export GEM_HOME
GEM_PATH="/tmp/verifier/gems"; export GEM_PATH
GEM_CACHE="/tmp/verifier/gems/cache"; export GEM_CACHE
sudo -E /tmp/verifier/bin/busser test
']
>>>>>> ----------------------
我手动登录服务器并运行相同的命令/bin/sh -c rbenv\ versions
,它工作正常。
谁能告诉我这里是否做错了什么?
编辑 1
我刚刚发现,当 serverspec 运行命令时,$PATH
变量与我手动登录时的变量不同。我添加puts ENV['PATH']
到测试中,输出是这样的:
/sbin:/bin:/usr/sbin:/usr/bin
但是,当我手动登录时,我得到了这个:
/home/ec2-user/.rbenv/shims:/home/ec2-user/.rbenv/bin:/home/ec2-user/.rbenv/shims:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin
我将测试中的命令更改为/home/ec2-user/.rbenv/rbenv versions
并通过了。知道我需要做什么才能使路径在这里正常工作吗?
编辑 2
所以我发现 serverspec 正在运行命令 as root
,这意味着在ec2-user
's path 期间发生的更改kitchen converge
不可用。
我不知道是否有更惯用的方法来使用 serverspec 执行此操作,但我将命令更改为此并且测试现在通过了:
runuser -l ec2-user -c 'rbenv versions'
这允许root
以另一个用户身份运行命令。除非有人知道进行此类测试的更好方法,否则我会将其添加为问题的答案。