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.



14
15
16
17
18
19
20
# File 'lib/vagrant_spec/test_plan.rb', line 14

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.



13
14
15
# File 'lib/vagrant_spec/test_plan.rb', line 13

def config
  @config
end

#envObject (readonly)

Returns the value of attribute env.



13
14
15
# File 'lib/vagrant_spec/test_plan.rb', line 13

def env
  @env
end

#m_finderObject (readonly)

Returns the value of attribute m_finder.



13
14
15
# File 'lib/vagrant_spec/test_plan.rb', line 13

def m_finder
  @m_finder
end

#test_planObject (readonly)

Returns the value of attribute test_plan.



13
14
15
# File 'lib/vagrant_spec/test_plan.rb', line 13

def test_plan
  @test_plan
end

Instance Method Details

#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

return [@ret_code]



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vagrant_spec/test_plan.rb', line 56

def execute_plan_tests(node, plan)
  @env.ui.info("[#{node.name}]")
  ENV['VAGRANT_HOST'] = node.ssh_info[:host].to_s
  ENV['VAGRANT_PORT'] = node.ssh_info[:port].to_s
  ENV['VAGRANT_KEY']  = node.ssh_info[:private_key_path][0].to_s
  ENV['VAGRANT_USER'] = node.ssh_info[:username].to_s
  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>]



42
43
44
45
46
47
48
# File 'lib/vagrant_spec/test_plan.rb', line 42

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

#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



26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant_spec/test_plan.rb', line 26

def run
  @env.ui.info('*******************************************************')
  @env.ui.info('***************** ServerSpec Test Run *****************')
  @env.ui.info('*******************************************************')
  @env.ui.info('')
  @test_plan.each do |plan|
    nodes(plan).each { |node| execute_plan_tests(node, plan) }
  end
  exit @ret_code
end