Class: Minitest::Queue::Runner

Inherits:
Object
  • Object
show all
Includes:
CI::Queue::OutputHelpers
Defined in:
lib/minitest/queue/runner.rb

Constant Summary collapse

Error =
Class.new(StandardError)
MissingParameter =
Class.new(Error)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



22
23
24
25
26
27
28
# File 'lib/minitest/queue/runner.rb', line 22

def initialize(argv)
  @queue_config = CI::Queue::Configuration.from_env(ENV)
  @command, @argv = parse(argv)
  if Minitest.respond_to?(:seed=)
    Minitest.seed = @queue_config.seed.to_i
  end
end

Class Method Details

.invoke(argv) ⇒ Object



18
19
20
# File 'lib/minitest/queue/runner.rb', line 18

def self.invoke(argv)
  new(argv).run!
end

Instance Method Details

#bisect_commandObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/minitest/queue/runner.rb', line 165

def bisect_command
  invalid_usage! "Missing the FAILING_TEST argument." unless queue_config.failing_test

  @queue = CI::Queue::Bisect.new(queue_url, queue_config)
  Minitest.queue = queue
  set_load_path
  load_tests
  populate_queue

  step("Testing the failing test in isolation")
  unless run_tests_in_fork(queue.failing_test)
    puts reopen_previous_step
    puts red("The test fail when ran alone, no need to bisect.")
    exit! 0
  end

  run_index = 0
  while queue.suspects_left > 1
    run_index += 1
    step("Run ##{run_index}, #{queue.suspects_left} suspects left")
    if run_tests_in_fork(queue.candidates)
      queue.succeeded!
    else
      queue.failed!
    end
    puts
  end

  failing_order = queue.candidates
  step("Final validation")
  status = if run_tests_in_fork(failing_order)
    step(yellow("The bisection was inconclusive, there might not be any leaky test here."))
    exit! 1
  else
    step(green('The following command should reproduce the leak on your machine:'), collapsed: false)
    command = %w(bundle exec minitest-queue --queue - run)
    command << "-I#{load_paths}" if load_paths
    command += argv

    puts
    puts "cat <<EOF |\n#{failing_order.to_a.map(&:id).join("\n")}\nEOF\n#{command.join(' ')}"
    puts

    File.write('log/test_order.log', failing_order.to_a.map(&:id).join("\n"))
    exit! 0
  end
end

#grind_commandObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/minitest/queue/runner.rb', line 126

def grind_command
  invalid_usage!('No list to grind provided') if grind_list.nil?
  invalid_usage!('No grind count provided') if grind_count.nil?

  set_load_path

  queue_config.build_id = queue_config.build_id + '-grind'
  queue_config.grind_count = grind_count

  reporter_queue = CI::Queue::Redis::Grind.new(queue_url, queue_config)

  Minitest.queue = queue
  reporters = [
    GrindRecorder.new(build: reporter_queue.build),
    TestDataReporter.new(namespace: queue_config&.namespace),
  ]

  if queue_config.track_test_duration
    test_time_record = CI::Queue::Redis::TestTimeRecord.new(queue_url, queue_config)
    reporters << TestTimeRecorder.new(build: test_time_record)
  end

  if queue_config.statsd_endpoint
    reporters << Minitest::Reporters::StatsdReporter.new(statsd_endpoint: queue_config.statsd_endpoint)
  end
  Minitest.queue_reporters = reporters

  trap('TERM') { Minitest.queue.shutdown! }
  trap('INT') { Minitest.queue.shutdown! }

  load_tests

  @queue = CI::Queue::Grind.new(grind_list, queue_config)
  Minitest.queue = queue
  populate_queue

  # Let minitest's at_exit hook trigger
end

#release_commandObject



121
122
123
124
# File 'lib/minitest/queue/runner.rb', line 121

def release_command
  require_worker_id!
  queue.release!
end

#report_commandObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/minitest/queue/runner.rb', line 213

def report_command
  supervisor = begin
    queue.supervisor
  rescue NotImplementedError => error
    abort! error.message
  end

  step("Waiting for workers to complete")

  unless supervisor.wait_for_workers { display_warnings(supervisor.build) }
    unless supervisor.queue_initialized?
      abort! "No master was elected. Did all workers crash?"
    end

    unless supervisor.exhausted?
      msg = "#{supervisor.size} tests weren't run."
      if supervisor.max_test_failed?
        puts('Encountered too many failed tests. Test run was ended early.')
        abort!(msg)
      else
        abort!(msg)
      end
    end
  end

  reporter = BuildStatusReporter.new(build: supervisor.build)

  if queue_config.failure_file
    failures = reporter.error_reports.map(&:to_h).to_json
    File.write(queue_config.failure_file, failures)
  end

  if queue_config.export_flaky_tests_file
    failures = reporter.flaky_reports.to_json
    File.write(queue_config.export_flaky_tests_file, failures)
  end

  reporter.report
  exit! reporter.success? ? 0 : 1
end

#report_grind_commandObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/minitest/queue/runner.rb', line 254

def report_grind_command
  queue_config.build_id = queue_config.build_id + '-grind'
  @queue = CI::Queue::Redis::Grind.new(queue_url, queue_config)

  supervisor = begin
    queue.supervisor
  rescue NotImplementedError => error
    abort! error.message
  end

  grind_reporter = GrindReporter.new(build: supervisor.build)
  grind_reporter.report

  test_time_reporter_success = if queue_config.track_test_duration
    test_time_record = CI::Queue::Redis::TestTimeRecord.new(queue_url, queue_config)
    test_time_reporter = Minitest::Queue::TestTimeReporter.new(
      build: test_time_record,
      limit: queue_config.max_test_duration,
      percentile: queue_config.max_test_duration_percentile,
    )
    test_time_reporter.report

    test_time_reporter.success?
  else
    true
  end

  exit! grind_reporter.success? && test_time_reporter_success ? 0 : 1
end

#retry_commandObject



44
45
46
47
48
# File 'lib/minitest/queue/runner.rb', line 44

def retry_command
  require_worker_id!
  STDERR.puts "Warning: the retry subcommand is deprecated."
  run_command # aliased for backward compatibility purpose
end

#run!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/minitest/queue/runner.rb', line 30

def run!
  invalid_usage!("No command given") if command.nil?
  invalid_usage!('Missing queue URL') unless queue_url

  @queue = CI::Queue.from_uri(queue_url, queue_config)

  method = "#{command}_command"
  if respond_to?(method)
    public_send(method)
  else
    invalid_usage!("Unknown command: #{command}")
  end
end

#run_commandObject



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
92
93
94
95
96
# File 'lib/minitest/queue/runner.rb', line 50

def run_command
  require_worker_id!
  if queue.retrying? || retry?
    if queue.expired?
      abort! "The test run is too old and can't be retried"
    end
    reset_counters
    retry_queue = queue.retry_queue
    if retry_queue.exhausted?
      puts "The retry queue does not contain any failure, we'll process the main queue instead."
    else
      puts "Retrying failed tests."
      self.queue = retry_queue
    end
  end

  queue.rescue_connection_errors { queue.created_at = Time.now.to_f }

  set_load_path
  Minitest.queue = queue
  reporters = [
    LocalRequeueReporter.new(verbose: verbose),
    BuildStatusRecorder.new(build: queue.build),
    JUnitReporter.new,
    TestDataReporter.new(namespace: queue_config&.namespace),
    OrderReporter.new(path: 'log/test_order.log'),
  ]
  if queue_config.statsd_endpoint
    reporters << Minitest::Reporters::StatsdReporter.new(statsd_endpoint: queue_config.statsd_endpoint)
  end
  Minitest.queue_reporters = reporters

  trap('TERM') { Minitest.queue.shutdown! }
  trap('INT') { Minitest.queue.shutdown! }

  if queue.rescue_connection_errors { queue.exhausted? }
    puts green('All tests were ran already')
  else
    load_tests
    populate_queue
  end

  at_exit {
    verify_reporters!(reporters)
  }
  # Let minitest's at_exit hook trigger
end

#verify_reporters!(reporters) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/minitest/queue/runner.rb', line 98

def verify_reporters!(reporters)
  return unless reporters.any? { |r| !Minitest::Reporters.reporters.include?(r) }

  warn <<~WARNING
    WARNING!

    ci-queue requires several custom minitest reporters.
    Please do not overwrite them.
    If you have a statement in your test suite like this

      Minitest::Reporters.use!(SomeReporter.new)

    you should only run it when other reporters have not been configured
    to avoid breaking ci-queue's functionality and getting false test summaries.

    Use something like this:

      if Minitest::Reporters.reporters.nil?
        Minitest::Reporters.use!(SomeReporter.new)
      end
  WARNING
end