-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminitest_redis_test.rb
More file actions
793 lines (722 loc) · 26.9 KB
/
minitest_redis_test.rb
File metadata and controls
793 lines (722 loc) · 26.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
164
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
212
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
253
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# frozen_string_literal: true
require 'test_helper'
require 'tmpdir'
require 'active_support/testing/time_helpers'
module Integration
class MinitestRedisTest < Minitest::Test
include OutputTestHelpers
include ActiveSupport::Testing::TimeHelpers
def setup
@junit_path = File.expand_path('../../fixtures/log/junit.xml', __FILE__)
File.delete(@junit_path) if File.exist?(@junit_path)
@test_data_path = File.expand_path('../../fixtures/log/test_data.json', __FILE__)
File.delete(@test_data_path) if File.exist?(@test_data_path)
@order_path = File.expand_path('../../fixtures/log/test_order.log', __FILE__)
File.delete(@order_path) if File.exist?(@order_path)
@redis_url = ENV.fetch('REDIS_URL', 'redis://localhost:6379/0')
@redis = Redis.new(url: @redis_url)
@redis.flushdb
@exe = File.expand_path('../../../exe/minitest-queue', __FILE__)
end
def test_buildkite_output
out, err = capture_subprocess_io do
system(
{ 'BUILDKITE' => '1' },
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal '--- Ran 11 tests, 8 assertions, 2 failures, 1 errors, 1 skips, 4 requeues in X.XXs', output
end
def test_custom_requeue
out, err = capture_subprocess_io do
system(
{ 'BUILDKITE' => '1' },
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/custom_requeue_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal '--- Ran 3 tests, 0 assertions, 0 failures, 2 errors, 0 skips, 1 requeues in X.XXs', output
end
def test_max_test_failed
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'--max-test-failed', '3',
'-Itest',
'test/failing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_equal 'This worker is exiting early because too many failed tests were encountered.', err.chomp
output = normalize(out.lines.last.strip)
assert_equal 'Ran 47 tests, 47 assertions, 3 failures, 0 errors, 0 skips, 44 requeues in X.XXs', output
# Run the reporter
exit_code = nil
out, err = capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--timeout', '1',
'--max-test-failed', '3',
chdir: 'test/fixtures/',
)
end
refute_predicate $?, :success?
assert_empty err
expected = <<~EXPECTED
Waiting for workers to complete
Encountered too many failed tests. Test run was ended early.
97 tests weren't run.
EXPECTED
assert_equal expected.strip, normalize(out.lines[0..4].join.strip)
end
def test_circuit_breaker
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'--max-consecutive-failures', '3',
'-Itest',
'test/failing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_equal "This worker is exiting early because it encountered too many consecutive test failures, probably because of some corrupted state.\n", err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips, 3 requeues in X.XXs', output
end
def test_redis_runner
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 11 tests, 8 assertions, 2 failures, 1 errors, 1 skips, 4 requeues in X.XXs', output
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 6 tests, 4 assertions, 2 failures, 1 errors, 0 skips, 3 requeues in X.XXs', output
end
def test_retry_success
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/passing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues in X.XXs', output
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/passing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'All tests were ran already', output
end
def test_retry_fails_when_test_run_is_expired
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/passing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues in X.XXs', output
one_day = 60 * 60 * 24
key = CI::Queue::Redis::KeyShortener.key("1", "created-at")
@redis.set(key, Time.now - one_day)
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/passing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal "The test run is too old and can't be retried", output
end
def test_retry_report
# Run first worker, failing all tests
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'-Itest',
'test/failing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 100 tests, 100 assertions, 100 failures, 0 errors, 0 skips, 0 requeues in X.XXs', output
# Run the reporter
out, err = capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--timeout', '1',
chdir: 'test/fixtures/',
)
end
assert_empty err
expect = 'Ran 100 tests, 100 assertions, 100 failures, 0 errors, 0 skips, 0 requeues in X.XXs (aggregated)'
assert_equal expect, normalize(out.strip.lines[1].strip)
# Simulate another worker successfuly retrying all errors (very hard to reproduce properly)
queue_config = CI::Queue::Configuration.new(
timeout: 1,
build_id: '1',
worker_id: '2',
)
queue = CI::Queue.from_uri(@redis_url, queue_config)
error_reports = queue.build.error_reports
assert_equal 100, error_reports.size
error_reports.keys.each_with_index do |test_id, index|
queue.build.record_success(test_id.dup, stats: {
'assertions' => index + 1,
'errors' => 0,
'failures' => 0,
'skips' => 0,
'requeues' => 0,
'total_time' => index + 1,
})
end
# Retry first worker, bailing out
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'-Itest',
'test/failing_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'All tests were ran already', output
# Re-run the reporter
out, err = capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--timeout', '1',
chdir: 'test/fixtures/',
)
end
assert_empty err
expect = 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues in X.XXs (aggregated)'
assert_equal expect, normalize(out.strip.lines[1].strip)
end
def test_down_redis
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', 'redis://localhost:1337',
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 0 tests, 0 assertions, 0 failures, 0 errors, 0 skips, 0 requeues in X.XXs', output
end
def test_test_data_reporter
out, err = capture_subprocess_io do
system(
{'CI_QUEUE_FLAKY_TESTS' => 'test/ci_queue_flaky_tests_list.txt'},
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--namespace', 'foo',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 9 tests, 6 assertions, 1 failures, 1 errors, 1 skips, 2 requeues in X.XXs', output
content = File.read(@test_data_path)
failures = JSON.parse(content, symbolize_names: true)
.sort_by { |h| "#{h[:test_id]}##{h[:test_index]}" }
assert_equal 'foo', failures[0][:namespace]
assert_equal 'ATest#test_bar', failures[0][:test_id]
assert_equal 'test_bar', failures[0][:test_name]
assert_equal 'ATest', failures[0][:test_suite]
assert_equal 'failure', failures[0][:test_result]
assert_equal true, failures[0][:test_retried]
assert_equal false, failures[0][:test_result_ignored]
assert_equal 1, failures[0][:test_assertions]
assert_equal 'test/dummy_test.rb', failures[0][:test_file_path]
assert_equal 9, failures[0][:test_file_line_number]
assert_equal 'Minitest::Assertion', failures[0][:error_class]
assert_equal 'Expected false to be truthy.', failures[0][:error_message]
assert_equal 'test/dummy_test.rb', failures[0][:error_file_path]
assert_equal 10, failures[0][:error_file_number]
assert_equal 'foo', failures[1][:namespace]
assert_equal 'ATest#test_bar', failures[1][:test_id]
assert_equal 'test_bar', failures[1][:test_name]
assert_equal 'ATest', failures[1][:test_suite]
assert_equal 'failure', failures[1][:test_result]
assert_equal false, failures[1][:test_result_ignored]
assert_equal false, failures[1][:test_retried]
assert_equal 1, failures[1][:test_assertions]
assert_equal 'test/dummy_test.rb', failures[1][:test_file_path]
assert_equal 9, failures[1][:test_file_line_number]
assert_equal 'Minitest::Assertion', failures[1][:error_class]
assert_equal 'Expected false to be truthy.', failures[1][:error_message]
assert_equal 'test/dummy_test.rb', failures[1][:error_file_path]
assert_equal 10, failures[1][:error_file_number]
assert failures[0][:test_index] < failures[1][:test_index]
assert_equal 'ATest#test_flaky', failures[2][:test_id]
assert_equal 'skipped', failures[2][:test_result]
assert_equal false, failures[2][:test_retried]
assert_equal true, failures[2][:test_result_ignored]
assert_equal 1, failures[2][:test_assertions]
assert_equal 'test/dummy_test.rb', failures[2][:test_file_path]
assert_equal 13, failures[2][:test_file_line_number]
assert_equal 'Minitest::Assertion', failures[2][:error_class]
assert_equal 18, failures[2][:error_file_number]
assert_equal 'ATest#test_flaky_passes', failures[4][:test_id]
assert_equal 'success', failures[4][:test_result]
end
def test_test_data_time_reporter
start_time = Time.now
travel_to(start_time) do
capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--namespace', 'foo',
'--build', '1',
'--worker', '1',
'--timeout', '10',
'-Itest',
'--record-duration-averages=true',
'test/time_test.rb',
chdir: 'test/fixtures/',
)
end
end
end_time = Time.now
content = File.read(@test_data_path)
failure = JSON.parse(content, symbolize_names: true)
.sort_by { |h| "#{h[:test_id]}##{h[:test_index]}" }
.first
assert_in_delta start_time.to_i, failure[:test_start_timestamp], 5
assert_in_delta end_time.to_i, failure[:test_finish_timestamp], 5
assert failure[:test_finish_timestamp] > failure[:test_start_timestamp]
end
def test_junit_reporter
out, err = capture_subprocess_io do
system(
{'CI_QUEUE_FLAKY_TESTS' => 'test/ci_queue_flaky_tests_list.txt'},
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 9 tests, 6 assertions, 1 failures, 1 errors, 1 skips, 2 requeues in X.XXs', output
# NOTE: To filter the TypeError backtrace below see test/fixtures/test/backtrace_filters.rb
assert_equal <<~XML, normalize_xml(File.read(@junit_path))
<?xml version="1.1" encoding="UTF-8"?>
<testsuites>
<testsuite name="ATest" filepath="test/dummy_test.rb" skipped="5" failures="1" errors="0" tests="6" assertions="5" time="X.XX">
<testcase name="test_foo" classname="ATest" assertions="0" time="X.XX" flaky_test="false" run-command=\"bundle exec ruby -Ilib:test test/dummy_test.rb -n ATest\\#test_foo\" lineno="5">
<skipped type="Minitest::Skip"/>
</testcase>
<testcase name="test_bar" classname="ATest" assertions="1" time="X.XX" flaky_test="false" run-command=\"bundle exec ruby -Ilib:test test/dummy_test.rb -n ATest\\#test_bar\" lineno="5">
<skipped type="Minitest::Assertion"/>
</testcase>
<testcase name="test_flaky" classname="ATest" assertions="1" time="X.XX" flaky_test="true" run-command=\"bundle exec ruby -Ilib:test test/dummy_test.rb -n ATest\\#test_flaky\" lineno="5">
<failure type="Minitest::Assertion" message="Expected false to be truthy.">
<![CDATA[
Skipped:
test_flaky(ATest) [test/dummy_test.rb]:
Expected false to be truthy.
]]>
</failure>
</testcase>
<testcase name="test_flaky_passes" classname="ATest" assertions="1" time="X.XX" flaky_test="true" run-command="bundle exec ruby -Ilib:test test/dummy_test.rb -n ATest\\#test_flaky_passes" lineno="5"/>
<testcase name="test_flaky_fails_retry" classname="ATest" assertions="1" time="X.XX" flaky_test="true" run-command="bundle exec ruby -Ilib:test test/dummy_test.rb -n ATest\\#test_flaky_fails_retry" lineno="5">
<failure type="Minitest::Assertion" message="Expected false to be truthy.">
<![CDATA[
Skipped:
test_flaky_fails_retry(ATest) [test/dummy_test.rb]:
Expected false to be truthy.
]]>
</failure>
</testcase>
<testcase name="test_bar" classname="ATest" assertions="1" time="X.XX" flaky_test="false" run-command="bundle exec ruby -Ilib:test test/dummy_test.rb -n ATest\\#test_bar" lineno="5">
<failure type="Minitest::Assertion" message="Expected false to be truthy.">
<![CDATA[
Failure:
test_bar(ATest) [test/dummy_test.rb]:
Expected false to be truthy.
]]>
</failure>
</testcase>
</testsuite>
<testsuite name="BTest" filepath="test/dummy_test.rb" skipped="1" failures="0" errors="1" tests="3" assertions="1" time="X.XX">
<testcase name="test_bar" classname="BTest" assertions="0" time="X.XX" flaky_test="false" run-command="bundle exec ruby -Ilib:test test/dummy_test.rb -n BTest\\#test_bar" lineno="36">
<skipped type="TypeError"/>
</testcase>
<testcase name="test_foo" classname="BTest" assertions="1" time="X.XX" flaky_test="false" run-command="bundle exec ruby -Ilib:test test/dummy_test.rb -n BTest\\#test_foo" lineno="36"/>
<testcase name="test_bar" classname="BTest" assertions="0" time="X.XX" flaky_test="false" run-command="bundle exec ruby -Ilib:test test/dummy_test.rb -n BTest\\#test_bar" lineno="36">
<error type="TypeError" message="TypeError: String can't be coerced into Integer">
<![CDATA[
Failure:
test_bar(BTest) [test/dummy_test.rb]:
TypeError: String can't be coerced into Integer
test/dummy_test.rb:37:in `+'
test/dummy_test.rb:37:in `test_bar'
]]>
</error>
</testcase>
</testsuite>
</testsuites>
XML
end
def test_redis_reporter_failure_file
Dir.mktmpdir do |dir|
failure_file = File.join(dir, 'failure_file.json')
capture_subprocess_io do
system(
{ 'BUILDKITE' => '1' },
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--build', '1',
'--timeout', '1',
'--failure-file', failure_file,
chdir: 'test/fixtures/',
)
end
content = File.read(failure_file)
failure = JSON.parse(content, symbolize_names: true)
.sort_by { |failure_report| failure_report[:test_line] }
.first
## output and test_file
expected = {
test_file: "ci-queue/ruby/test/fixtures/test/dummy_test.rb",
test_line: 9,
test_and_module_name: "ATest#test_bar",
error_class: "Minitest::Assertion",
test_name: "test_bar",
test_suite: "ATest",
}
assert_includes failure[:test_file], expected[:test_file]
assert_equal failure[:test_line], expected[:test_line]
assert_equal failure[:test_suite], expected[:test_suite]
assert_equal failure[:test_and_module_name], expected[:test_and_module_name]
assert_equal failure[:test_name], expected[:test_name]
assert_equal failure[:error_class], expected[:error_class]
end
end
def test_redis_reporter_flaky_tests_file
Dir.mktmpdir do |dir|
flaky_tests_file = File.join(dir, 'flaky_tests_file.json')
capture_subprocess_io do
system(
{ 'BUILDKITE' => '1' },
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--build', '1',
'--timeout', '1',
'--export-flaky-tests-file', flaky_tests_file,
chdir: 'test/fixtures/',
)
end
content = File.read(flaky_tests_file)
flaky_tests = JSON.parse(content)
assert_includes flaky_tests, "ATest#test_flaky"
end
end
def test_redis_reporter_export_timing_file
Dir.mktmpdir do |dir|
timing_file = File.join(dir, 'timing_file.json')
capture_subprocess_io do
system(
{ 'BUILDKITE' => '1' },
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--track-test-duration',
'-Itest',
'test/passing_test.rb',
chdir: 'test/fixtures/',
)
end
out, err = capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--build', '1',
'--timeout', '1',
'--track-test-duration',
'--export-timing-file', timing_file,
chdir: 'test/fixtures/',
)
end
assert_empty err
assert_includes out, "Exported timing data for"
assert_includes out, "tests to #{timing_file}"
assert File.exist?(timing_file), "Timing file should exist"
content = File.read(timing_file)
timing_data = JSON.parse(content)
assert_operator timing_data.size, :>, 0, "Should have timing data"
timing_data.each do |test_id, duration|
assert_match(/\w+#test_\w+/, test_id, "Test ID should be in format Class#method")
assert_kind_of Numeric, duration, "Duration should be numeric"
assert_operator duration, :>=, 0, "Duration should be non-negative"
end
end
end
def test_redis_reporter
# HACK: Simulate a timeout
config = CI::Queue::Configuration.new(build_id: '1', worker_id: '1', timeout: '1')
build_record = CI::Queue::Redis::BuildRecord.new(self, ::Redis.new(url: @redis_url), config)
build_record.record_warning(CI::Queue::Warnings::RESERVED_LOST_TEST, test: 'Atest#test_bar', timeout: 2)
out, err = capture_subprocess_io do
system(
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'--max-requeues', '1',
'--requeue-tolerance', '1',
'-Itest',
'test/dummy_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out.lines.last.strip)
assert_equal 'Ran 11 tests, 8 assertions, 2 failures, 1 errors, 1 skips, 4 requeues in X.XXs', output
out, err = capture_subprocess_io do
system(
@exe, 'report',
'--queue', @redis_url,
'--build', '1',
'--timeout', '1',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out)
expected_output = <<~END
Waiting for workers to complete
[WARNING] Atest#test_bar was picked up by another worker because it didn't complete in the allocated 2 seconds.
You may want to either optimize this test or bump ci-queue timeout.
It's also possible that the worker that was processing it was terminated without being able to report back.
Ran 7 tests, 8 assertions, 2 failures, 1 errors, 1 skips, 4 requeues in X.XXs (aggregated)
FAIL ATest#test_bar
Expected false to be truthy.
test/dummy_test.rb:10:in `test_bar'
FAIL ATest#test_flaky_fails_retry
Expected false to be truthy.
test/dummy_test.rb:23:in `test_flaky_fails_retry'
ERROR BTest#test_bar
END
assert_includes output, expected_output
end
def test_utf8_tests_and_marshal
out, err = capture_subprocess_io do
system(
{ 'MARSHAL' => '1' },
@exe, 'run',
'--queue', @redis_url,
'--seed', 'foobar',
'--build', '1',
'--worker', '1',
'--timeout', '1',
'-Itest',
'test/utf8_test.rb',
chdir: 'test/fixtures/',
)
end
assert_empty err
output = normalize(out)
assert_equal <<~END, output
Ran 1 tests, 1 assertions, 1 failures, 0 errors, 0 skips, 0 requeues in X.XXs
END
end
private
def normalize_xml(output)
freeze_xml_timing(rewrite_paths(output))
end
end
end