Search code examples
rubyserverspec

Why does serverspec connect to the localhost instead of the remote host


My serverspec example, setup using serverspec-init, then I generated this simple test file ./spec/altspf01/sample_spec.rb

require 'spec_helper'

describe command( '/bin/hostname -s' ) do
   its(:stdout) { should match /atlspf01/ }
end

I expect it to ssh to a remote host (atlspf01) and check its hostname (atlspf01). Instead it connects to the localhost (ltipc682).

  1) Command "/bin/hostname -s" stdout should match /atlspf01/
     Failure/Error: its(:stdout) { should match /atlspf01/ }
       expected "ltipc682\n" to match /atlspf01/
       Diff:
       @@ -1,2 +1,2 @@
       -/atlspf01/
       +ltipc682

What have I done wrong?

All code here: https://gist.github.com/neilhwatson/a3f4a26ad8cf27d62307


Solution

  • Serverspec's specs should be run via rake. Otherwise, the spec doesn't know the target host.

    However, if you wish to run it via rspec, you could use this workaround:

    env TARGET_HOST='atlspf01' rspec spec/atlspf01/sample_spec.rb

    Hope this helps.