Merge pull request #10 from ehaselwanter/master

add standalone usage feature
This commit is contained in:
Dominik Richter 2014-06-05 11:33:13 +02:00
commit d53a3dd602
4 changed files with 117 additions and 7 deletions

2
Gemfile Normal file
View file

@ -0,0 +1,2 @@
gem 'rake'
gem 'serverspec'

View file

@ -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
Rakefile Normal file
View file

@ -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

View file

@ -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