diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..94514b2 --- /dev/null +++ b/Gemfile @@ -0,0 +1,2 @@ +gem 'rake' +gem 'serverspec' diff --git a/README.md b/README.md index 839aeb1..8cfca12 100644 --- a/README.md +++ b/README.md @@ -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= + +# 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 + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..b4d1cf3 --- /dev/null +++ b/Rakefile @@ -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 + diff --git a/default/serverspec/spec_helper.rb b/default/serverspec/spec_helper.rb index ba9b6d3..cbfbfe9 100644 --- a/default/serverspec/spec_helper.rb +++ b/default/serverspec/spec_helper.rb @@ -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