philbert philbert - 2 months ago 15
Ruby Question

rspec/serverspec service test always fails

I believe this issue probably a duplicate of serverspec service test returns incorrect failure, but I include a bit more information of my execution environment.

I have a bunch of successful serverspec tests executing against a RHEL6 VM on AWS.

However any "service" test seems to fail with the matchers be_enabled and be_running.

I have the following in my spec_helper.rb:

set :os, :family => 'redhat', :release => '6', :arch => 'x86_64'

I tried both serverspec and rspec syntax for the tests and both fail as they run the same commands:

describe service('ntpd') do
it { should be_enabled }
it { should be_running }

it "is running ntpd" do
expect(service("ntpd")).to be_enabled
expect(service("ntpd")).to be_running

Failure/Error: it { should be_enabled }
expected Service "ntpd" to be enabled
sudo -p 'Password: ' /bin/sh -c chkconfig\ --list\ ntpd\ \|\ grep\ 3:on

Failure/Error: it { should be_running }
expected Service "ntpd" to be running
sudo -p 'Password: ' /bin/sh -c service\ ntpd\ status

However, running them locally on the server succeeds:

$ sudo -p 'Password: ' /bin/sh -c chkconfig\ --list\ ntpd\ \|\ grep\ 3:on
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
$ echo $?

$ sudo -p 'Password: ' /bin/sh -c service\ ntpd\ status
ntpd (pid 1101) is running...
$ echo $?

I tried looking into setting up some debugging with pry-byebug but that looks not-so-straightforward, so I kind of gave up on that for now.

I'm running ruby 2.0, serverspec 2.24, rspec 3.3

Can anyone help point me in the right direction?


I needed to specify the runlevel to check, and then things worked. I presume this is some backwards compatibility issue between RHEL6/7 and systemV/systemD as the documentation indicates that the tests above should work.

describe service('ntpd') do
    it { should be_enabled.with_level(2) }
    it { should be_enabled.with_level(3) }
    it { should be_enabled.with_level(4) }
    it { should be_enabled.with_level(5) }
    it { should be_running }