Class: Inferno::CLI::Execute

Inherits:
Object
  • Object
show all
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

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/inferno/apps/cli/execute.rb', line 20

def options
  @options
end

Class Method Details

.boot_full_infernoObject



33
34
35
36
37
38
39
40
# File 'lib/inferno/apps/cli/execute.rb', line 33

def self.boot_full_inferno
  ENV['NO_DB'] = 'false'

  # Inferno boot flow triggers migration and logger outputs it
  Inferno::CLI::Execute.suppress_output { require_relative '../../../inferno' }

  Inferno::Application.start(:executor)
end

.suppress_outputObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/inferno/apps/cli/execute.rb', line 22

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

#all_selected_groups_and_testsObject



119
120
121
# File 'lib/inferno/apps/cli/execute.rb', line 119

def all_selected_groups_and_tests
  @all_selected_groups_and_tests ||= runnables_by_short_id + groups + tests
end

#create_params(test_session, runnable) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/inferno/apps/cli/execute.rb', line 213

def create_params(test_session, runnable)
  {
    test_session_id: test_session.id,
    runnable_id_key(runnable) => runnable.id,
    inputs: thor_hash_to_inputs_array(inputs_and_preset)
  }
end

#create_test_run(runnable) ⇒ Object



174
175
176
177
178
# File 'lib/inferno/apps/cli/execute.rb', line 174

def create_test_run(runnable)
  test_runs_repo.create(
    create_params(test_session, runnable).merge({ status: 'queued' })
  )
end

#dispatch_job(test_run) ⇒ Object



221
222
223
224
225
226
227
228
229
# File 'lib/inferno/apps/cli/execute.rb', line 221

def dispatch_job(test_run)
  if options[:verbose]
    Jobs.perform(Jobs::ExecuteTestRun, test_run.id, force_synchronous: true)
  else
    Inferno::CLI::Execute.suppress_output do
      Jobs.perform(Jobs::ExecuteTestRun, test_run.id, force_synchronous: true)
    end
  end
end

#find_by_short_id(repo_symbol, short_id) ⇒ Object

Raises:

  • (StandardError)


249
250
251
252
253
254
255
256
# File 'lib/inferno/apps/cli/execute.rb', line 249

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

#groupsObject



237
238
239
240
241
# File 'lib/inferno/apps/cli/execute.rb', line 237

def groups
  return [] if options[:groups].blank?

  @groups ||= options[:groups]&.map { |short_id| find_by_short_id(:group, short_id) }
end

#inputs_and_presetObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/inferno/apps/cli/execute.rb', line 135

def inputs_and_preset
  if preset
    preset_inputs = preset.inputs.to_h do |preset_input|
      [preset_input[:name], preset_input[:value]]
    end

    options.fetch(:inputs, {}).reverse_merge(preset_inputs)
  else
    options.fetch(:inputs, {})
  end
end

#load_preset_file_and_set_preset_idObject

Raises:

  • (StandardError)


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/inferno/apps/cli/execute.rb', line 107

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] = JSON.parse(File.read(options[:preset_file]))['id']
  raise StandardError, "Preset #{options[:preset_file]} is missing id" if options[:preset_id].nil?

  presets_repo.insert_from_file(options[:preset_file])
end

#outputterObject



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

#presetObject

Raises:

  • (StandardError)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/inferno/apps/cli/execute.rb', line 147

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_repoObject



200
201
202
# File 'lib/inferno/apps/cli/execute.rb', line 200

def presets_repo
  @presets_repo ||= Inferno::Repositories::Presets.new
end


279
280
281
282
283
284
285
# File 'lib/inferno/apps/cli/execute.rb', line 279

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


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



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/inferno/apps/cli/execute.rb', line 258

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_repoObject



180
181
182
# File 'lib/inferno/apps/cli/execute.rb', line 180

def results_repo
  @results_repo ||= Inferno::Repositories::Results.new
end

#run(options) ⇒ Object



42
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 42

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

  # User may enter duplicate runnables, in which case this prevents a bug of extraneous results
  results.uniq!(&:id)

  outputter.print_results(options, results)
  outputter.print_end_message(options)

  # TODO: respect customized rollups
  exit(0) if Inferno::ResultSummarizer.new(results).summarize == 'pass'

  # exit(1) is for Thor failures
  # exit(2) is for shell builtin failures
  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



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/inferno/apps/cli/execute.rb', line 123

def run_one(runnable, test_run)
  verify_runnable(
    runnable,
    thor_hash_to_inputs_array(inputs_and_preset),
    test_session.suite_options
  )

  persist_inputs(session_data_repo, create_params(test_session, suite), test_run)

  dispatch_job(test_run)
end

#runnable_id_key(runnable) ⇒ Object



299
300
301
302
303
304
305
306
307
308
# File 'lib/inferno/apps/cli/execute.rb', line 299

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



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/inferno/apps/cli/execute.rb', line 287

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_idObject



231
232
233
234
235
# File 'lib/inferno/apps/cli/execute.rb', line 231

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_repoObject



196
197
198
# File 'lib/inferno/apps/cli/execute.rb', line 196

def session_data_repo
  @session_data_repo ||= Inferno::Repositories::SessionData.new
end

#sort_results(results) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
# File 'lib/inferno/apps/cli/execute.rb', line 310

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

#suiteObject

Raises:

  • (StandardError)


162
163
164
165
166
167
168
# File 'lib/inferno/apps/cli/execute.rb', line 162

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_repoObject



184
185
186
# File 'lib/inferno/apps/cli/execute.rb', line 184

def test_groups_repo
  @test_groups_repo ||= Inferno::Repositories::TestGroups.new
end

#test_runs_repoObject



170
171
172
# File 'lib/inferno/apps/cli/execute.rb', line 170

def test_runs_repo
  @test_runs_repo ||= Inferno::Repositories::TestRuns.new
end

#test_sessionObject



204
205
206
207
208
209
210
211
# File 'lib/inferno/apps/cli/execute.rb', line 204

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_repoObject



192
193
194
# File 'lib/inferno/apps/cli/execute.rb', line 192

def test_sessions_repo
  @test_sessions_repo ||= Inferno::Repositories::TestSessions.new
end

#testsObject



243
244
245
246
247
# File 'lib/inferno/apps/cli/execute.rb', line 243

def tests
  return [] if options[:tests].blank?

  @tests ||= options[:tests]&.map { |short_id| find_by_short_id(:test, short_id) }
end

#tests_repoObject



188
189
190
# File 'lib/inferno/apps/cli/execute.rb', line 188

def tests_repo
  @tests_repo ||= Inferno::Repositories::Tests.new
end

#thor_hash_to_inputs_array(hash = {}) ⇒ Object



275
276
277
# File 'lib/inferno/apps/cli/execute.rb', line 275

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



271
272
273
# File 'lib/inferno/apps/cli/execute.rb', line 271

def thor_hash_to_suite_options_array(hash = {})
  hash.to_a.map { |pair| Inferno::DSL::SuiteOption.new({ id: pair[0], value: pair[1] }) }
end