Class: ChefDK::ComponentTest
- Inherits:
-
Object
- Object
- ChefDK::ComponentTest
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, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix
Constructor Details
Returns a new instance of ComponentTest.
69
70
71
72
73
74
75
76
|
# File 'lib/chef-dk/component_test.rb', line 69
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_dir ⇒ Object
Returns the value of attribute base_dir.
63
64
65
|
# File 'lib/chef-dk/component_test.rb', line 63
def base_dir
@base_dir
end
|
#name ⇒ Object
Returns the value of attribute name.
67
68
69
|
# File 'lib/chef-dk/component_test.rb', line 67
def name
@name
end
|
#omnibus_root ⇒ Object
214
215
216
|
# File 'lib/chef-dk/component_test.rb', line 214
def omnibus_root
@omnibus_root || raise("`omnibus_root` must be set before running tests")
end
|
Instance Method Details
#assert_present! ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/chef-dk/component_test.rb', line 167
def assert_present!
unless File.exist?( component_path )
raise MissingComponentError.new(name, "Could not find #{component_path}")
end
rescue Gem::LoadError => e
raise MissingComponentError.new(name, e)
end
|
#bin(binary) ⇒ Object
102
103
104
|
# File 'lib/chef-dk/component_test.rb', line 102
def bin(binary)
File.join(omnibus_bin_dir, binary)
end
|
#component_path ⇒ Object
188
189
190
191
192
193
194
195
196
|
# File 'lib/chef-dk/component_test.rb', line 188
def component_path
if base_dir
File.join(omnibus_root, 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_options ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/chef-dk/component_test.rb', line 175
def default_command_options
{
cwd: component_path,
env: {
path_variable_key => omnibus_path,
"CHEF_LICENSE" => "accept-no-persist",
},
timeout: 3600,
}
end
|
#embedded_bin(binary) ⇒ Object
106
107
108
|
# File 'lib/chef-dk/component_test.rb', line 106
def embedded_bin(binary)
File.join(omnibus_embedded_bin_dir, binary)
end
|
#fail_if_exit_zero(cmd_string, failure_string = "") ⇒ Object
Run a command, if the command returns zero, raise an error, otherwise, if it returns non-zero or doesn’t exist, return a passing command so that the test parser doesn’t crash.
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/chef-dk/component_test.rb', line 135
def fail_if_exit_zero(cmd_string, failure_string = "")
result = sh(cmd_string)
if result.status.exitstatus == 0
raise failure_string
else
sh("true")
end
rescue Errno::ENOENT
sh("true")
end
|
#gem_base_dir ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/chef-dk/component_test.rb', line 198
def gem_base_dir
return nil if @gem_name_for_base_dir.nil?
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
210
211
212
|
# File 'lib/chef-dk/component_test.rb', line 210
def gem_base_dir=(gem_name)
@gem_name_for_base_dir = gem_name
end
|
#integration_test(&test_block) ⇒ Object
86
87
88
|
# File 'lib/chef-dk/component_test.rb', line 86
def integration_test(&test_block)
@integration_test = test_block
end
|
146
147
148
149
150
151
152
|
# File 'lib/chef-dk/component_test.rb', line 146
def nix_platform_native_bin_dir
if /darwin/ =~ RUBY_PLATFORM
"/usr/local/bin"
else
"/usr/bin"
end
end
|
#omnibus_path ⇒ Object
218
219
220
|
# File 'lib/chef-dk/component_test.rb', line 218
def omnibus_path
[omnibus_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"]].join(File::PATH_SEPARATOR)
end
|
#path_variable_key ⇒ Object
222
223
224
|
# File 'lib/chef-dk/component_test.rb', line 222
def path_variable_key
ENV.keys.grep(/\Apath\Z/i).first
end
|
#run_in_tmpdir(command, options = {}) ⇒ Object
154
155
156
157
158
159
|
# File 'lib/chef-dk/component_test.rb', line 154
def run_in_tmpdir(command, options = {})
tmpdir do |dir|
options[:cwd] = dir
sh(command, options)
end
end
|
#run_integration_test ⇒ Object
90
91
92
|
# File 'lib/chef-dk/component_test.rb', line 90
def run_integration_test
instance_eval(&@integration_test)
end
|
#run_smoke_test ⇒ Object
98
99
100
|
# File 'lib/chef-dk/component_test.rb', line 98
def run_smoke_test
instance_eval(&@smoke_test)
end
|
#run_unit_test ⇒ Object
82
83
84
|
# File 'lib/chef-dk/component_test.rb', line 82
def run_unit_test
instance_eval(&@unit_test)
end
|
#sh(command, options = {}) ⇒ Object
110
111
112
113
114
115
116
117
118
|
# File 'lib/chef-dk/component_test.rb', line 110
def sh(command, options = {})
combined_opts = default_command_options.merge(options)
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.
127
128
129
|
# File 'lib/chef-dk/component_test.rb', line 127
def sh!(*args)
sh(*args).tap(&:error!)
end
|
#smoke_test(&test_block) ⇒ Object
94
95
96
|
# File 'lib/chef-dk/component_test.rb', line 94
def smoke_test(&test_block)
@smoke_test = test_block
end
|
#tmpdir ⇒ Object
161
162
163
164
165
|
# File 'lib/chef-dk/component_test.rb', line 161
def tmpdir
Dir.mktmpdir do |tmpdir|
yield tmpdir
end
end
|
#unit_test(&test_block) ⇒ Object
78
79
80
|
# File 'lib/chef-dk/component_test.rb', line 78
def unit_test(&test_block)
@unit_test = test_block
end
|