Last active
August 29, 2015 14:22
-
-
Save neilhwatson/a3f4a26ad8cf27d62307 to your computer and use it in GitHub Desktop.
A serverspec example that should ssh to a remote host but connects to localhost instead.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newatson@ltipc682:~/src/serverspec/test$ find | |
. | |
./spec | |
./spec/atlspf01 | |
./spec/atlspf01/sample_spec.rb | |
./spec/spec_helper.rb | |
./Rakefile | |
./.rspec | |
newatson@ltipc682:~/src/serverspec/test$ cat Rakefile | |
require 'rake' | |
require 'rspec/core/rake_task' | |
task :spec => 'spec:all' | |
task :default => :spec | |
namespace :spec do | |
targets = [] | |
Dir.glob('./spec/*').each do |dir| | |
next unless File.directory?(dir) | |
target = File.basename(dir) | |
target = "_#{target}" if target == "default" | |
targets << target | |
end | |
task :all => targets | |
task :default => :all | |
targets.each do |target| | |
original_target = target == "_default" ? target[1..-1] : target | |
desc "Run serverspec tests to #{original_target}" | |
RSpec::Core::RakeTask.new(target.to_sym) do |t| | |
ENV['TARGET_HOST'] = original_target | |
t.pattern = "spec/#{original_target}/*_spec.rb" | |
end | |
end | |
end | |
newatson@ltipc682:~/src/serverspec/test$ cat spec/spec_helper.rb | |
require 'serverspec' | |
require 'net/ssh' | |
set :backend, :ssh | |
if ENV['ASK_SUDO_PASSWORD'] | |
begin | |
require 'highline/import' | |
rescue LoadError | |
fail "highline is not available. Try installing it." | |
end | |
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false } | |
else | |
set :sudo_password, ENV['SUDO_PASSWORD'] | |
end | |
host = ENV['TARGET_HOST'] | |
options = Net::SSH::Config.for(host) | |
options[:user] ||= Etc.getlogin | |
set :host, options[:host_name] || host | |
set :ssh_options, options | |
# Disable sudo | |
# set :disable_sudo, true | |
# Set environment variables | |
# set :env, :LANG => 'C', :LC_MESSAGES => 'C' | |
# Set PATH | |
# set :path, '/sbin:/usr/local/sbin:$PATH' | |
newatson@ltipc682:~/src/serverspec/test$ cat spec/atlspf01/sample_spec.rb | |
require 'spec_helper' | |
describe command( '/bin/hostname -s' ) do | |
its(:stdout) { should match /atlspf01/ } | |
end | |
newatson@ltipc682:~/src/serverspec/test$ rspec | |
Command "/bin/hostname -s" | |
stdout | |
Password: | |
should match /atlspf01/ (FAILED - 1) | |
Failures: | |
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 | |
sudo -p 'Password: ' /bin/sh -c /bin/hostname\ -s | |
ltipc682 | |
# ./spec/atlspf01/sample_spec.rb:4:in `block (2 levels) in <top (required)>' | |
Finished in 27.27 seconds (files took 1.2 seconds to load) | |
1 example, 1 failure | |
Failed examples: | |
rspec ./spec/atlspf01/sample_spec.rb:4 # Command "/bin/hostname -s" stdout should match /atlspf01/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment