Class: ChefDK::ComponentTest

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/chef-dk/component_test.rb

Defined Under Namespace

Classes: NullTestResult

Constant Summary collapse

DEFAULT_TEST =
lambda { |context| NullTestResult.new }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #stderr, #stdout, #system_command

Constructor Details

#initialize(name) ⇒ ComponentTest

Returns a new instance of ComponentTest.



48
49
50
51
52
53
54
55
# File 'lib/chef-dk/component_test.rb', line 48

def initialize(name)
  @name = name
  @unit_test = DEFAULT_TEST
  @integration_test = DEFAULT_TEST
  @smoke_test = DEFAULT_TEST
  @base_dir = nil
  @gem_name_for_base_dir = nil
end

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



42
43
44
# File 'lib/chef-dk/component_test.rb', line 42

def base_dir
  @base_dir
end

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/chef-dk/component_test.rb', line 46

def name
  @name
end

#omnibus_rootObject



141
142
143
# File 'lib/chef-dk/component_test.rb', line 141

def omnibus_root
  @omnibus_root or raise "`omnibus_root` must be set before running tests"
end

Instance Method Details

#assert_present!Object



104
105
106
107
108
# File 'lib/chef-dk/component_test.rb', line 104

def assert_present!
  unless File.exists?( component_path )
    raise MissingComponentError.new(name)
  end
end

#component_pathObject



122
123
124
125
126
127
128
129
130
# File 'lib/chef-dk/component_test.rb', line 122

def component_path
  if base_dir
    File.join(omnibus_apps_dir, base_dir)
  elsif gem_base_dir
    gem_base_dir
  else
    raise "`base_dir` or `gem_base_dir` must be defined for component `#{name}`"
  end
end

#default_command_optionsObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef-dk/component_test.rb', line 110

def default_command_options
  {
    :cwd => component_path,
    :env => {
      # Add the embedded/bin to the PATH so that bundle executable can
      # be found while running the tests.
      path_variable_key => omnibus_path
    },
    :timeout => 3600
  }
end

#gem_base_dirObject



132
133
134
135
# File 'lib/chef-dk/component_test.rb', line 132

def gem_base_dir
  return nil if @gem_name_for_base_dir.nil?
  Gem::Specification::find_by_name(@gem_name_for_base_dir).gem_dir
end

#gem_base_dir=(gem_name) ⇒ Object



137
138
139
# File 'lib/chef-dk/component_test.rb', line 137

def gem_base_dir=(gem_name)
  @gem_name_for_base_dir = gem_name
end

#integration_test(&test_block) ⇒ Object



65
66
67
# File 'lib/chef-dk/component_test.rb', line 65

def integration_test(&test_block)
  @integration_test = test_block
end

#omnibus_pathObject



145
146
147
# File 'lib/chef-dk/component_test.rb', line 145

def omnibus_path
  [omnibus_bin_dir, omnibus_embedded_bin_dir, ENV['PATH']].join(File::PATH_SEPARATOR)
end

#path_variable_keyObject



149
150
151
# File 'lib/chef-dk/component_test.rb', line 149

def path_variable_key
  ENV.keys.grep(/\Apath\Z/i).first
end

#run_in_tmpdir(command, options = {}) ⇒ Object



91
92
93
94
95
96
# File 'lib/chef-dk/component_test.rb', line 91

def run_in_tmpdir(command, options={})
  tmpdir do |dir|
    options[:cwd] = dir
    sh(command, options)
  end
end

#run_integration_testObject



69
70
71
# File 'lib/chef-dk/component_test.rb', line 69

def run_integration_test
  instance_eval(&@integration_test)
end

#run_smoke_testObject



77
78
79
# File 'lib/chef-dk/component_test.rb', line 77

def run_smoke_test
  instance_eval(&@smoke_test)
end

#run_unit_testObject



61
62
63
# File 'lib/chef-dk/component_test.rb', line 61

def run_unit_test
  instance_eval(&@unit_test)
end

#sh(command, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/chef-dk/component_test.rb', line 81

def sh(command, options={})
  combined_opts = default_command_options.merge(options)

  # Env is a hash, so it needs to be merged separately
  if options.key?(:env)
    combined_opts[:env] = default_command_options[:env].merge(options[:env])
  end
  system_command(command, combined_opts)
end

#smoke_test(&test_block) ⇒ Object



73
74
75
# File 'lib/chef-dk/component_test.rb', line 73

def smoke_test(&test_block)
  @smoke_test = test_block
end

#tmpdirObject



98
99
100
101
102
# File 'lib/chef-dk/component_test.rb', line 98

def tmpdir
  Dir.mktmpdir do |tmpdir|
    yield tmpdir
  end
end

#unit_test(&test_block) ⇒ Object



57
58
59
# File 'lib/chef-dk/component_test.rb', line 57

def unit_test(&test_block)
  @unit_test = test_block
end