Class: Inferno::CLI::Execute
- Inherits:
-
Object
- Object
- Inferno::CLI::Execute
show all
- Includes:
- Utils::PersistInputs, Utils::VerifyRunnable
- Defined in:
- lib/inferno/apps/cli/execute.rb,
lib/inferno/apps/cli/execute/serialize.rb,
lib/inferno/apps/cli/execute/json_outputter.rb,
lib/inferno/apps/cli/execute/plain_outputter.rb,
lib/inferno/apps/cli/execute/quiet_outputter.rb,
lib/inferno/apps/cli/execute/console_outputter.rb
Defined Under Namespace
Modules: Serialize
Classes: ConsoleOutputter, JSONOutputter, PlainOutputter, QuietOutputter
Constant Summary
collapse
- OUTPUTTERS =
{
'console' => Inferno::CLI::Execute::ConsoleOutputter,
'plain' => Inferno::CLI::Execute::PlainOutputter,
'json' => Inferno::CLI::Execute::JSONOutputter,
'quiet' => Inferno::CLI::Execute::QuietOutputter
}.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#persist_inputs
#verify_runnable
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
21
22
23
|
# File 'lib/inferno/apps/cli/execute.rb', line 21
def options
@options
end
|
Class Method Details
.boot_full_inferno ⇒ Object
.suppress_output ⇒ Object
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/inferno/apps/cli/execute.rb', line 23
def self.suppress_output
begin
original_stdout = $stdout.clone
$stdout.reopen(File.new(File::NULL, 'w+'))
retval = yield
ensure
$stdout.reopen(original_stdout)
end
retval
end
|
Instance Method Details
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/inferno/apps/cli/execute.rb', line 123
def all_inputs
if preset
test_sessions_repo.apply_preset(test_session, preset.id)
preset_inputs = session_data_repo.get_all_from_session(test_session.id)
thor_hash_to_inputs_array(options.fetch(:inputs, {})) + preset_inputs.map(&:to_hash)
else
thor_hash_to_inputs_array(options.fetch(:inputs, {}))
end
end
|
#all_selected_groups_and_tests ⇒ Object
107
108
109
|
# File 'lib/inferno/apps/cli/execute.rb', line 107
def all_selected_groups_and_tests
@all_selected_groups_and_tests ||= runnables_by_short_id + groups + tests
end
|
#create_params(test_session, runnable) ⇒ Object
209
210
211
212
213
214
215
|
# File 'lib/inferno/apps/cli/execute.rb', line 209
def create_params(test_session, runnable)
{
test_session_id: test_session.id,
runnable_id_key(runnable) => runnable.id,
inputs: all_inputs
}
end
|
#create_test_run(runnable) ⇒ Object
170
171
172
173
174
|
# File 'lib/inferno/apps/cli/execute.rb', line 170
def create_test_run(runnable)
test_runs_repo.create(
create_params(test_session, runnable).merge({ status: 'queued' })
)
end
|
#dispatch_job(test_run) ⇒ Object
#find_by_short_id(repo_symbol, short_id) ⇒ Object
245
246
247
248
249
250
251
252
|
# File 'lib/inferno/apps/cli/execute.rb', line 245
def find_by_short_id(repo_symbol, short_id)
repo_symbol_to_array(repo_symbol).each do |repo|
repo.all.each do |entity|
return entity if short_id == entity.short_id && suite.id == entity.suite.id
end
end
raise StandardError, "#{repo_symbol.to_s.humanize} #{short_id} not found."
end
|
#groups ⇒ Object
233
234
235
236
237
|
# File 'lib/inferno/apps/cli/execute.rb', line 233
def groups
return [] if options[:groups].blank?
@groups ||= options[:groups]&.map { |short_id| find_by_short_id(:group, short_id) }
end
|
#load_preset_file_and_set_preset_id ⇒ Object
149
150
151
152
153
154
155
156
|
# File 'lib/inferno/apps/cli/execute.rb', line 149
def load_preset_file_and_set_preset_id
return unless options[:preset_file]
raise StandardError, 'Cannot use `--preset-id` and `--preset-file` options together' if options[:preset_id]
raise StandardError, "File #{options[:preset_file]} not found" unless File.exist? options[:preset_file]
options[:preset_id] = presets_repo.insert_from_file(options[:preset_file]).id
end
|
#outputter ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/inferno/apps/cli/execute.rb', line 98
def outputter
unless OUTPUTTERS.key? options[:outputter]
raise StandardError,
"Unrecognized outputter #{options[:outputter]}"
end
@outputter ||= OUTPUTTERS[options[:outputter]].new
end
|
#preset ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/inferno/apps/cli/execute.rb', line 134
def preset
return unless options[:preset_id]
@preset ||= presets_repo.find(options[:preset_id])
raise StandardError, "Preset #{options[:preset_id]} not found" if @preset.nil?
unless presets_repo.presets_for_suite(suite.id).include?(@preset)
raise StandardError,
"Preset #{options[:preset_id]} is incompatible with suite #{suite.id}"
end
@preset
end
|
#presets_repo ⇒ Object
196
197
198
|
# File 'lib/inferno/apps/cli/execute.rb', line 196
def presets_repo
@presets_repo ||= Inferno::Repositories::Presets.new
end
|
#print_error_and_exit(err, code) ⇒ Object
275
276
277
278
279
280
281
|
# File 'lib/inferno/apps/cli/execute.rb', line 275
def print_error_and_exit(err, code)
outputter.print_error(options || {}, err)
rescue StandardError => e
puts "Caught exception #{e} while printing exception #{err}. Exiting."
ensure
exit(code)
end
|
#print_help_and_exit ⇒ Object
93
94
95
96
|
# File 'lib/inferno/apps/cli/execute.rb', line 93
def print_help_and_exit
puts `NO_DB=true bundle exec inferno help execute`
exit(0)
end
|
#repo_symbol_to_array(repo_symbol) ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/inferno/apps/cli/execute.rb', line 254
def repo_symbol_to_array(repo_symbol)
case repo_symbol
when :group
[test_groups_repo]
when :test
[tests_repo]
when :group_or_test
[test_groups_repo, tests_repo]
else
raise StandardError, "Unrecognized repo_symbol #{repo_symbol} for `find_by_short_id`"
end
end
|
#results_repo ⇒ Object
176
177
178
|
# File 'lib/inferno/apps/cli/execute.rb', line 176
def results_repo
@results_repo ||= Inferno::Repositories::Results.new
end
|
#run(options) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/inferno/apps/cli/execute.rb', line 43
def run(options)
print_help_and_exit if options[:help]
self.options = options
outputter.print_start_message(self.options)
load_preset_file_and_set_preset_id
results = []
outputter.print_around_run(self.options) do
if all_selected_groups_and_tests.empty?
test_run = create_test_run(suite)
run_one(suite, test_run)
results = test_runs_repo.results_for_test_run(test_run.id)
results = sort_results(results)
else
all_selected_groups_and_tests.each do |runnable|
test_run = create_test_run(runnable)
run_one(runnable, test_run)
results += sort_results(test_runs_repo.results_for_test_run(test_run.id))
end
end
end
results.uniq!(&:id)
outputter.print_results(options, results)
outputter.print_end_message(options)
exit(0) if Inferno::ResultSummarizer.new(results).summarize == 'pass'
exit(3)
rescue Sequel::ValidationFailed => e
print_error_and_exit(e, 4)
rescue Sequel::ForeignKeyConstraintViolation => e
print_error_and_exit(e, 5)
rescue Inferno::Exceptions::RequiredInputsNotFound => e
print_error_and_exit(e, 6)
rescue Inferno::Exceptions::NotUserRunnableException => e
print_error_and_exit(e, 7)
rescue StandardError => e
print_error_and_exit(e, 8)
end
|
#run_one(runnable, test_run) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/inferno/apps/cli/execute.rb', line 111
def run_one(runnable, test_run)
verify_runnable(
runnable,
all_inputs,
test_session.suite_options
)
persist_inputs(session_data_repo, create_params(test_session, suite), runnable)
dispatch_job(test_run)
end
|
#runnable_id_key(runnable) ⇒ Object
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/inferno/apps/cli/execute.rb', line 295
def runnable_id_key(runnable)
case runnable_type(runnable)
when :suite
:test_suite_id
when :group
:test_group_id
else
:test_id
end
end
|
#runnable_type(runnable) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/inferno/apps/cli/execute.rb', line 283
def runnable_type(runnable)
if runnable < Inferno::TestSuite
:suite
elsif runnable < Inferno::TestGroup
:group
elsif runnable < Inferno::Test
:test
else
raise StandardError, "Unidentified runnable #{runnable}"
end
end
|
#runnables_by_short_id ⇒ Object
227
228
229
230
231
|
# File 'lib/inferno/apps/cli/execute.rb', line 227
def runnables_by_short_id
return [] if options[:short_ids].blank?
@runnables_by_short_id ||= options[:short_ids]&.map { |short_id| find_by_short_id(:group_or_test, short_id) }
end
|
#session_data_repo ⇒ Object
192
193
194
|
# File 'lib/inferno/apps/cli/execute.rb', line 192
def session_data_repo
@session_data_repo ||= Inferno::Repositories::SessionData.new
end
|
#sort_results(results) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/inferno/apps/cli/execute.rb', line 306
def sort_results(results)
results.sort do |result, other|
if result.runnable < Inferno::TestSuite
-1
elsif other.runnable < Inferno::TestSuite
1
else
result.runnable.short_id <=> other.runnable.short_id
end
end
end
|
#suite ⇒ Object
158
159
160
161
162
163
164
|
# File 'lib/inferno/apps/cli/execute.rb', line 158
def suite
@suite ||= Inferno::Repositories::TestSuites.new.find(options[:suite])
raise StandardError, "Test suite #{options[:suite]} not found" if @suite.nil?
@suite
end
|
#test_groups_repo ⇒ Object
180
181
182
|
# File 'lib/inferno/apps/cli/execute.rb', line 180
def test_groups_repo
@test_groups_repo ||= Inferno::Repositories::TestGroups.new
end
|
#test_runs_repo ⇒ Object
166
167
168
|
# File 'lib/inferno/apps/cli/execute.rb', line 166
def test_runs_repo
@test_runs_repo ||= Inferno::Repositories::TestRuns.new
end
|
#test_session ⇒ Object
200
201
202
203
204
205
206
207
|
# File 'lib/inferno/apps/cli/execute.rb', line 200
def test_session
@test_session ||= test_sessions_repo.create({
test_suite_id: suite.id,
suite_options: thor_hash_to_suite_options_array(
options[:suite_options]
)
})
end
|
#test_sessions_repo ⇒ Object
#tests ⇒ Object
239
240
241
242
243
|
# File 'lib/inferno/apps/cli/execute.rb', line 239
def tests
return [] if options[:tests].blank?
@tests ||= options[:tests]&.map { |short_id| find_by_short_id(:test, short_id) }
end
|
#tests_repo ⇒ Object
184
185
186
|
# File 'lib/inferno/apps/cli/execute.rb', line 184
def tests_repo
@tests_repo ||= Inferno::Repositories::Tests.new
end
|
271
272
273
|
# File 'lib/inferno/apps/cli/execute.rb', line 271
def thor_hash_to_inputs_array(hash = {})
hash.to_a.map { |pair| { name: pair[0], value: pair[1] } }
end
|
#thor_hash_to_suite_options_array(hash = {}) ⇒ Object
267
268
269
|
# File 'lib/inferno/apps/cli/execute.rb', line 267
def thor_hash_to_suite_options_array(hash = {})
hash.to_a.map { |pair| Inferno::DSL::SuiteOption.new({ id: pair[0], value: pair[1] }) }
end
|