Class: VagrantSpec::TestPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_spec/test_plan.rb

Overview

Run serverspec tests

env [Vagrant::Environment]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TestPlan

Returns a new instance of TestPlan.



17
18
19
20
21
22
23
# File 'lib/vagrant_spec/test_plan.rb', line 17

def initialize(env)
  @env       = env
  @config    = VagrantSpec::Config.load(env)
  @test_plan = @config.spec.test_plan
  @m_finder  = VagrantSpec::MachineFinder.new(env)
  @ret_code  = 0
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/vagrant_spec/test_plan.rb', line 16

def config
  @config
end

#envObject (readonly)

Returns the value of attribute env.



16
17
18
# File 'lib/vagrant_spec/test_plan.rb', line 16

def env
  @env
end

#m_finderObject (readonly)

Returns the value of attribute m_finder.



16
17
18
# File 'lib/vagrant_spec/test_plan.rb', line 16

def m_finder
  @m_finder
end

#test_planObject (readonly)

Returns the value of attribute test_plan.



16
17
18
# File 'lib/vagrant_spec/test_plan.rb', line 16

def test_plan
  @test_plan
end

Instance Method Details

#close_sshObject

Close existing SSH



61
62
63
64
65
66
# File 'lib/vagrant_spec/test_plan.rb', line 61

def close_ssh
  if ::Specinfra::Backend::Ssh.instance.get_config(:ssh)
    ::Specinfra::Backend::Ssh.instance.get_config(:ssh).close
    ::Specinfra::Backend::Ssh.instance.set_config(:ssh, nil)
  end
end

#configure_serverspec(node) ⇒ Object

Configures ServerSpec

node [Vagrant::Machine]



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vagrant_spec/test_plan.rb', line 71

def configure_serverspec(node)
  set :backend, :ssh
  host           = node.ssh_info[:host].to_s
  options        = Net::SSH::Config.for(host)
  options[:user] = node.ssh_info[:username].to_s
  options[:keys] = node.ssh_info[:private_key_path][0].to_s
  options[:port] = node.ssh_info[:port].to_s

  set :host,        host
  set :ssh_options, options
end

#execute_plan_tests(node, plan) ⇒ Object

Execute a test_suite and return the code

node [Vagrant::Machine] plan [Hash] item in the @config.spec.test_plan



87
88
89
90
91
92
93
94
95
# File 'lib/vagrant_spec/test_plan.rb', line 87

def execute_plan_tests(node, plan)
  @env.ui.info("[#{node.name}]")
  close_ssh
  configure_serverspec(node)
  plan['flags'].prepend "-I #{@config.spec.directory} "
  ret_code = RSpec::Core::Runner.run(plan['flags'].split, $stderr, $stdout)
  RSpec.clear_examples
  @ret_code = ret_code unless ret_code.zero?
end

#nodes(plan) ⇒ Object

Return array of active Vagrant machines

plan [Hash] item in the @config.spec.test_plan

return [Array<Vagrant::Machine>]



52
53
54
55
56
57
58
# File 'lib/vagrant_spec/test_plan.rb', line 52

def nodes(plan)
  if plan['nodes'].is_a?(Regexp)
    @m_finder.match_nodes(plan['nodes'])
  elsif plan['nodes'].is_a?(Array)
    plan['nodes'].collect { |n| @m_finder.machine(n.to_sym) }
  end
end


40
41
42
43
44
45
# File 'lib/vagrant_spec/test_plan.rb', line 40

def print_banner
  @env.ui.info('*******************************************************')
  @env.ui.info('***************** ServerSpec Test Run *****************')
  @env.ui.info('*******************************************************')
  @env.ui.info('')
end

#runObject

Run the test suite and exit based on the return codes of the tests

This will fail if any of the tests fail, but it will allow all tests to run



29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant_spec/test_plan.rb', line 29

def run
  print_banner
  @test_plan.each do |plan|
    found_nodes = nodes(plan)
    if found_nodes
      found_nodes.each { |node| execute_plan_tests(node, plan) }
    end
  end
  exit @ret_code
end