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

#chefdk_home, #err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #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



158
159
160
# File 'lib/chef-dk/component_test.rb', line 158

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

Instance Method Details

#assert_present!Object



115
116
117
118
119
# File 'lib/chef-dk/component_test.rb', line 115

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

#component_pathObject



133
134
135
136
137
138
139
140
141
# File 'lib/chef-dk/component_test.rb', line 133

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



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

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/chef-dk/component_test.rb', line 143

def gem_base_dir
  return nil if @gem_name_for_base_dir.nil?
  # There is no way to say "give me the latest prerelease OR normal version of this gem.
  # So we first ask if there is a normal version, and if there is not, we ask if there
  # is a prerelease version.  ">= 0.a" is how we ask for a prerelease version, because a
  # prerelease version is defined as "any version with a letter in it."
  gem = Gem::Specification::find_by_name(@gem_name_for_base_dir)
  gem ||= Gem::Specification::find_by_name(@gem_name_for_base_dir, '>= 0.a')
  gem.gem_dir
end

#gem_base_dir=(gem_name) ⇒ Object



154
155
156
# File 'lib/chef-dk/component_test.rb', line 154

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



162
163
164
# File 'lib/chef-dk/component_test.rb', line 162

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

#path_variable_keyObject



166
167
168
# File 'lib/chef-dk/component_test.rb', line 166

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

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



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

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

#sh!(*args) ⇒ Object

Just like #sh but raises an error if the the command returns an unexpected exit code.

Most verification steps just run a single command, then ChefDK::Command::Verify#invoke_tests handles the results by inspecting the return value of the test block. For tests that run a lot of commands, this is inconvenient so you can use #sh! instead.



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

def sh!(*args)
  sh(*args).tap { |result| result.error! }
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



109
110
111
112
113
# File 'lib/chef-dk/component_test.rb', line 109

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