lib/serverspec/setup.rb in serverspec-2.0.0.beta5 vs lib/serverspec/setup.rb in serverspec-2.0.0.beta6

- old
+ new

@@ -92,27 +92,31 @@ def self.safe_create_spec content = <<-EOF require 'spec_helper' -describe package('httpd') do +describe package('httpd'), :if => os[:family] == 'RedHat' do it { should be_installed } end -describe service('httpd') do +describe package('apache2'), :if => os[:family] == 'Ubuntu' do + it { should be_installed } +end + +describe service('httpd'), :if => os[:family] == 'RedHat' do it { should be_enabled } it { should be_running } end +describe service('apache2'), :if => os[:family] == 'Ubuntu' do + it { should be_enabled } + it { should be_running } +end + describe port(80) do it { should be_listening } end - -describe file('/etc/httpd/conf/httpd.conf') do - it { should be_file } - its(:content) { should match /ServerName #{@hostname}/ } -end EOF if File.exists? "spec/#{@hostname}/httpd_spec.rb" old_content = File.read("spec/#{@hostname}/httpd_spec.rb") if old_content != content @@ -155,15 +159,31 @@ def self.safe_create_rakefile content = <<-'EOF' require 'rake' require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new(:spec) do |t| - t.pattern = 'spec/*/*_spec.rb' -end - +task :spec => 'spec:all' task :default => :spec + +namespace :spec do + targets = [] + Dir.glob('./spec/*').each do |dir| + next unless File.directory?(dir) + targets << File.basename(dir) + end + + task :all => targets + task :default => :all + + targets.each do |target| + desc "Run serverspec tests to #{target}" + RSpec::Core::RakeTask.new(target.to_sym) do |t| + ENV['TARGET_HOST'] = target + t.pattern = "spec/#{target}/*_spec.rb" + end + end +end EOF if File.exists? 'Rakefile' old_content = File.read('Rakefile') if old_content != content $stderr.puts '!! Rakefile already exists and differs from template' @@ -188,11 +208,11 @@ if find_vagrantfile vagrant_list = `vagrant status` list_of_vms = [] if vagrant_list != '' vagrant_list.each_line do |line| - if match = /([\w-]+[\s]+)(created|not created|poweroff|running|saved)[\s](\(virtualbox\)|\(vmware\))/.match(line) + if match = /([\w-]+[\s]+)(created|aborted|not created|poweroff|running|saved)[\s](\(virtualbox\)|\(vmware\))/.match(line) list_of_vms << match[1].strip! end end if list_of_vms.length == 1 @hostname = list_of_vms[0] @@ -213,80 +233,72 @@ end def self.spec_helper_template template = <<-EOF require 'serverspec' -<% if @os_type == 'UN*X' && @backend_type == 'Ssh' -%> -require 'pathname' -<% end -%> <% if @backend_type == 'Ssh' -%> require 'net/ssh' <% end -%> <% if @backend_type == 'WinRM' -%> require 'winrm' <% end -%> include Specinfra::Helper::<%= @backend_type %> <% if @os_type == 'UN*X' -%> include Specinfra::Helper::DetectOS -<% else -%> +<% else -%> include Specinfra::Helper::Windows <% end -%> -<% if @os_type == 'UN*X' -%> -RSpec.configure do |c| - if ENV['ASK_SUDO_PASSWORD'] +<% if @os_type == 'UN*X' && @backend_type == 'Ssh' -%> +if ENV['ASK_SUDO_PASSWORD'] + begin require 'highline/import' - c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false } - else - c.sudo_password = ENV['SUDO_PASSWORD'] + rescue LoadError + fail "highline is not available. Try installing it." end - <%- if @backend_type == 'Ssh' -%> - c.before :all do - block = self.class.metadata[:example_group_block] - if RUBY_VERSION.start_with?('1.8') - file = block.to_s.match(/.*@(.*):[0-9]+>/)[1] - else - file = block.source_location.first + set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false } +else + set :sudo_password, ENV['SUDO_PASSWORD'] +end + +<%- if @backend_type == 'Ssh' -%> +host = ENV['TARGET_HOST'] +options = Net::SSH::Config.for(host) + +<%- if @vagrant -%> +`vagrant up \#{ENV['TARGET_HOST']}` + +config = `vagrant ssh-config \#{ENV['TARGET_HOST']}` +if config != '' + config.each_line do |line| + if match = /HostName (.*)/.match(line) + host = match[1] + elsif match = /User (.*)/.match(line) + options[:user] = match[1] + elsif match = /IdentityFile (.*)/.match(line) + options[:keys] = [match[1].gsub(/\"/,'')] + elsif match = /Port (.*)/.match(line) + options[:port] = match[1] end - host = File.basename(Pathname.new(file).dirname) - if c.host != host - c.ssh.close if c.ssh - c.host = host - options = Net::SSH::Config.for(c.host) - user = options[:user] || Etc.getlogin - <%- if @vagrant -%> - vagrant_up = `vagrant up #{@hostname}` - config = `vagrant ssh-config #{@hostname}` - if config != '' - config.each_line do |line| - if match = /HostName (.*)/.match(line) - host = match[1] - elsif match = /User (.*)/.match(line) - user = match[1] - elsif match = /IdentityFile (.*)/.match(line) - options[:keys] = [match[1].gsub(/\"/,'')] - elsif match = /Port (.*)/.match(line) - options[:port] = match[1] - end - end - end - <%- end -%> - c.ssh = Net::SSH.start(host, user, options) - end end - <%- end -%> end -<% end -%> + +<%- end -%> +options[:user] ||= Etc.getlogin + +set :host, host +set :ssh_options, options +<%- end -%> +<%- end -%> + <% if @backend_type == 'WinRM'-%> -RSpec.configure do |c| - user = <username> - pass = <password> - endpoint = "http://<hostname>:5985/wsman" +user = <username> +pass = <password> +endpoint = "http://<hostname>:5985/wsman" - c.winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true) - c.winrm.set_timeout 300 # 5 minutes max timeout for any operation -end +c.winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true) +c.winrm.set_timeout 300 # 5 minutes max timeout for any operation <% end -%> EOF template end end