I am using packer to build an ami on aws, after the provision of the ami, I am testing the image using serverspec, but the test fail testing nginx, this is the test
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
This is the output
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
Now I added ss on packer to check if the nginx is listening
{
"type": "shell",
"inline": ["cd /tmp/test", "/sbin/ss -tlenp | grep :80", "rake"]
}
Output:
==> 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 <->
servespec test fail, but the nginx is listening on the port 80
The problem was the ss command is under path
/sbin
, if test use sh -c and don't have/sbin
inPATH
without sbin in PATH
with sbin in PATH
To set the PATH in my unit test