serverspec nginx should be listening using packer

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

0
задан 7 March 2018 в 19:19
1 ответ

Проблема заключалась в том, что команда 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

Чтобы установить ПУТЬ в моем модульном тесте

echo "set :path, '/sbin:/usr/sbin:\$PATH'" >> spec/spec_helper.rb
0
ответ дан 5 December 2019 в 06:23

Теги

Похожие вопросы