Я использую упаковщик для создания ami на aws, после предоставления ami я тестирую образ с помощью serverpec, но тест не прошел тестирование nginx, это тест
cat test/spec/localhost/nginx_spec.rb 2.4.2
require 'spec_helper'
describe package('nginx'), :if => os[:family] == 'redhat' do
it { should be_installed }
end
describe service('nginx'), :if => os[:family] == 'redhat' do
it { should be_enabled }
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
Это результат
amazon-ebs: Port "80"
amazon-ebs: should be listening (FAILED - 1)
amazon-ebs:
amazon-ebs: Failures:
amazon-ebs:
amazon-ebs: 1) Port "80" should be listening
amazon-ebs: On host `localhost'
amazon-ebs: Failure/Error: it { should be_listening }
amazon-ebs: expected Port "80" to be listening
amazon-ebs: /bin/sh -c ss\ -tunl\ \|\ grep\ --\ :80\\\
amazon-ebs:
amazon-ebs: # ./spec/localhost/nginx_spec.rb:13:in `block (2 levels) in <top (required)>'
amazon-ebs:
amazon-ebs: Finished in 0.0683 seconds (files took 0.36256 seconds to load)
amazon-ebs: 4 examples, 1 failure
amazon-ebs:
amazon-ebs: Failed examples:
amazon-ebs:
amazon-ebs: rspec ./spec/localhost/nginx_spec.rb:13 # Port "80" should be listening
Теперь я добавил ss на упаковщик, чтобы проверить, слушает ли nginx
{
"type": "shell",
"inline": ["cd /tmp/test", "/sbin/ss -tlenp | grep :80", "rake"]
}
Вывод:
==> amazon-ebs: Provisioning with shell script: /var/folders/d1/wm78jkb141lgjt9xn8x02dd5m7khx2/T/packer-shell407127288
amazon-ebs: LISTEN 0 128 *:80 *:* ino:32776 sk:ffff880033844d80 <->
amazon-ebs: LISTEN 0 128 *:80 *:* ino:32775 sk:ffff880033845d00 <->
Ошибка теста servepec, но nginx прослушивает порт 80
Проблема заключалась в том, что команда ss находится по пути /sbin
, если тест использовать sh -c и не иметь /sbin
в PATH
без sbin в PATH
export PATH=/usr/bin:/root/bin:/usr/local/bin
cd /root/test
rake
Package "nginx"
should be installed
Service "nginx"
should be enabled
should be running
Port "80"
should be listening (FAILED - 1)
Failures:
1) Port "80" should be listening
On host `localhost'
Failure/Error: it { should be_listening }
expected Port "80" to be listening
/bin/sh -c ss\ -tunl\ \|\ grep\ --\ :80\\\
# ./spec/localhost/nginx_spec.rb:13:in `block (2 levels) in <top (required)>'
Finished in 0.08334 seconds (files took 0.34911 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/localhost/nginx_spec.rb:13 # Port "80" should be listening
с sbin в PATH
export PATH=/usr/bin:/root/bin:/usr/local/bin:/sbin
rake
cd /root/test
Package "nginx"
should be installed
Service "nginx"
should be enabled
should be running
Port "80"
should be listening
Finished in 0.06054 seconds (files took 0.33548 seconds to load)
4 examples, 0 failures
Чтобы установить PATH в моем модульном тесте
echo "set :path, '/sbin:/usr/sbin:\$PATH'" >> spec/spec_helper.rb