Skip to content

Commit

Permalink
add standalone usage feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaselwanter committed Jun 5, 2014
1 parent d3ac41b commit 1f155b2
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gem 'rake'
gem 'serverspec'
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,32 @@ you can use the gem `kitchen-sharedtests`
- https://github.com/ehaselwanter/kitchen-sharedtests/

to make them available to your project. Use `thor kitchen:fetch-remote-tests` to put the repo into `test/integration`

## Standalone Usage

you can target the integration tests to any host were you have ssh access

rake -T gives you a list of suites you can run (well ignore directories which are obviously not suites for now)

```
± rake -T
rake serverspec:data_bags # Run serverspec suite data_bags
rake serverspec:default # Run serverspec suite default
```

run it with:

```
bundle install
# default user and ssh-key
bundle exec rake serverspec:default target_host=<name-or-ip-of-target-server>
# or with user, host, password
ASK_LOGIN_PASSWORD=true bundle exec rake serverspec:default target_host=192.168.1.222 user=stack
```

add `format=html` to get a report.html document

34 changes: 34 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rake'
require 'rspec/core/rake_task'

suites = Dir.glob('*').select{|entry| File.directory?(entry) }

class ServerspecTask < RSpec::Core::RakeTask

attr_accessor :target

def spec_command

if target.nil?
puts "specify either env TARGET_HOST or target_host="
exit 1
end

cmd = super
"env TARGET_HOST=#{target} STANDALONE_SPEC=true #{cmd} --format documentation --no-profile"
end

end

namespace :serverspec do
suites.each do |suite|
desc "Run serverspec suite #{suite}"
ServerspecTask.new(suite.to_sym) do |t|
t.rspec_opts = "--no-color --format html --out report.html" if ENV['format'] == 'html'
t.target = ENV['TARGET_HOST'] || ENV['target_host']
t.ruby_opts = "-I #{suite}/serverspec"
t.pattern = "#{suite}/serverspec/*_spec.rb"
end
end
end

59 changes: 52 additions & 7 deletions default/serverspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
require 'serverspec'
require 'pathname'
if ENV['STANDALONE_SPEC']

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS
require 'serverspec'
require 'pathname'
require 'net/ssh'
require 'highline/import'

RSpec.configure do |c|
c.before :all do
c.os = backend(Serverspec::Commands::Base).check_os
include Serverspec::Helper::Ssh
include Serverspec::Helper::DetectOS

RSpec.configure do |c|

if ENV['ASK_SUDO_PASSWORD']
c.sudo_password = ask('Enter sudo password: ') { |q| q.echo = false }
else
c.sudo_password = ENV['SUDO_PASSWORD']
end

options = {}

if ENV['ASK_LOGIN_PASSWORD']
options[:password] = ask("\nEnter login password: ") { |q| q.echo = false }
else
options[:password] = ENV['LOGIN_PASSWORD']
end

if ENV['ASK_LOGIN_USERNAME']
user = ask("\nEnter login username: ") { |q| q.echo = false }
else
user = ENV['LOGIN_USERNAME'] || ENV['user'] || Etc.getlogin
end

if user.nil?
puts 'specify login user env LOGIN_USERNAME= or user='
exit 1
end

c.host = ENV['TARGET_HOST']
options.merge(Net::SSH::Config.for(c.host))
c.ssh = Net::SSH.start(c.host, user, options)
c.os = backend.check_os

end

else
require 'serverspec'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
c.before :all do
c.path = '/sbin:/usr/sbin'
end
end
end

0 comments on commit 1f155b2

Please sign in to comment.