Class: BarkingIguana::Compound::Test

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Benchmark, Logging::Helper
Defined in:
lib/barking_iguana/compound/test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suite, directory) ⇒ Test



15
16
17
18
# File 'lib/barking_iguana/compound/test.rb', line 15

def initialize suite, directory
  self.suite = suite
  self.directory = directory
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



9
10
11
# File 'lib/barking_iguana/compound/test.rb', line 9

def directory
  @directory
end

#suiteObject

Returns the value of attribute suite.



6
7
8
# File 'lib/barking_iguana/compound/test.rb', line 6

def suite
  @suite
end

Instance Method Details

#custom_teardown?Boolean



52
53
54
# File 'lib/barking_iguana/compound/test.rb', line 52

def custom_teardown?
  File.exists? custom_teardown_file
end

#custom_teardown_fileObject



56
57
58
# File 'lib/barking_iguana/compound/test.rb', line 56

def custom_teardown_file
  File.expand_path "teardown.sh", directory
end

#driver_optionsObject



80
81
82
83
84
85
86
87
# File 'lib/barking_iguana/compound/test.rb', line 80

def driver_options
  options = {}
  logger.debug { "Does #{vagrant_file_template_path} exist? -> #{File.exists? vagrant_file_template_path}" }
  options[:vagrant_file_template_path] = vagrant_file_template_path if File.exists? vagrant_file_template_path
  options[:root] = directory
  options[:environment] = suite.environment.merge environment
  options
end

#environmentObject



93
94
95
# File 'lib/barking_iguana/compound/test.rb', line 93

def environment
  @environment ||= Environment.new(environment_file)
end

#environment_fileObject



89
90
91
# File 'lib/barking_iguana/compound/test.rb', line 89

def environment_file
  File.join directory, 'env'
end

#host_managerObject



60
61
62
63
64
65
# File 'lib/barking_iguana/compound/test.rb', line 60

def host_manager
  @host_manger ||= begin
    hosts = stages.map(&:hosts).flatten.uniq.sort
    HostManager.new(hosts, driver_options)
  end
end

#nameObject



20
21
22
# File 'lib/barking_iguana/compound/test.rb', line 20

def name
  directory.sub(suite.directory + '/', '').tr('/', ':')
end

#runObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/barking_iguana/compound/test.rb', line 28

def run
  benchmark "test #{name}" do
    begin
      logger.debug { "#{name}: found #{stages.size} stages: #{stages.map(&:name).map(&:inspect).join(', ')}" }
      stages.each &:run
    ensure
      teardown
    end
  end
end

#stagesObject



24
25
26
# File 'lib/barking_iguana/compound/test.rb', line 24

def stages
  Dir[directory + '/*'].select { |d| File.directory? d }.map { |s| TestStage.new self, File.basename(s) }
end

#teardownObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/barking_iguana/compound/test.rb', line 39

def teardown
  benchmark "#{name}: destroying all hosts" do
    host_manager.destroy_all
  end
  return unless custom_teardown?
  command_line = "bash -ex #{custom_teardown_file}"
  c = Mixlib::ShellOut.new command_line, cwd: directory, live_stream: logger
  benchmark "#{name}: running custom teardown" do
    c.run_command
  end
  c.error!
end

#test_file_with_fallback(file_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/barking_iguana/compound/test.rb', line 67

def test_file_with_fallback file_name
  logger.debug { "Searching for #{file_name.inspect}" }
  test_file = File.expand_path file_name, directory
  logger.debug { "Checking #{test_file.inspect}" }
  if File.exists? test_file
    logger.debug { "Found #{file_name.inspect} at #{test_file.inspect}" }
    return test_file
  end
  suite_file = File.expand_path file_name, suite.directory
  logger.debug { "Assuming it'll be at #{suite_file.inspect}" }
  suite_file
end

#vagrant_file_template_pathObject



97
98
99
# File 'lib/barking_iguana/compound/test.rb', line 97

def vagrant_file_template_path
  test_file_with_fallback 'Vagrantfile.erb'
end