0

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

c4f4t0r
  • 5,149
  • 3
  • 28
  • 41

1 Answers1

0

The problem was the ss command is under path /sbin, if test use sh -c and don't have /sbin in PATH

without sbin in 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

with sbin in 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

To set the PATH in my unit test

echo "set :path, '/sbin:/usr/sbin:\$PATH'" >> spec/spec_helper.rb
c4f4t0r
  • 5,149
  • 3
  • 28
  • 41