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



105
106
107
# File 'lib/inferno/apps/cli/execute.rb', line 105

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

#create_params(test_session, runnable) ⇒ Object



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

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

#create_test_run(runnable) ⇒ Object



133
134
135
136
137
# File 'lib/inferno/apps/cli/execute.rb', line 133

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

#dispatch_job(test_run) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/inferno/apps/cli/execute.rb', line 176

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)


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

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



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

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

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

#outputterObject



96
97
98
99
100
101
102
103
# File 'lib/inferno/apps/cli/execute.rb', line 96

def outputter
  unless OUTPUTTERS.key? options[:outputter]
    raise StandardError,
          "Unrecognized outputter #{options[:outputter]}"
  end

  @outputter ||= OUTPUTTERS[options[:outputter]].new
end


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

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


91
92
93
94
# File 'lib/inferno/apps/cli/execute.rb', line 91

def print_help_and_exit
  puts `NO_DB=true bundle exec inferno help execute`
  exit(0)
end

#repo_symbol_to_array(repo_symbol) ⇒ Object



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

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



139
140
141
# File 'lib/inferno/apps/cli/execute.rb', line 139

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
# 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(options)

  results = []
  outputter.print_around_run(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



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

def run_one(runnable, test_run)
  verify_runnable(
    runnable,
    thor_hash_to_inputs_array(options[:inputs]),
    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



254
255
256
257
258
259
260
261
262
263
# File 'lib/inferno/apps/cli/execute.rb', line 254

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



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/inferno/apps/cli/execute.rb', line 242

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



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

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



155
156
157
# File 'lib/inferno/apps/cli/execute.rb', line 155

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

#sort_results(results) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/inferno/apps/cli/execute.rb', line 265

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)


121
122
123
124
125
126
127
# File 'lib/inferno/apps/cli/execute.rb', line 121

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



143
144
145
# File 'lib/inferno/apps/cli/execute.rb', line 143

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

#test_runs_repoObject



129
130
131
# File 'lib/inferno/apps/cli/execute.rb', line 129

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

#test_sessionObject



159
160
161
162
163
164
165
166
# File 'lib/inferno/apps/cli/execute.rb', line 159

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



151
152
153
# File 'lib/inferno/apps/cli/execute.rb', line 151

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

#testsObject



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

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

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

#tests_repoObject



147
148
149
# File 'lib/inferno/apps/cli/execute.rb', line 147

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

#thor_hash_to_inputs_array(hash = {}) ⇒ Object



230
231
232
# File 'lib/inferno/apps/cli/execute.rb', line 230

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



226
227
228
# File 'lib/inferno/apps/cli/execute.rb', line 226

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