11# frozen_string_literal: true
22require 'test_helper'
3+ require 'tempfile'
34require 'tmpdir'
45require 'active_support/testing/time_helpers'
56require 'concurrent/set'
@@ -221,10 +222,10 @@ def test_retry_success
221222 end
222223
223224 def test_worker_history_retry_replays_all_reserved_tests
224- run_worker_history_worker ( retry_count : 0 )
225+ run_retry_worker ( retry_count : 0 )
225226 assert_predicate $?, :success?
226227
227- out , = run_worker_history_worker ( retry_count : 1 )
228+ out , = run_retry_worker ( retry_count : 1 )
228229
229230 assert_predicate $?, :success?
230231 assert_includes out , 'Replaying 100 tests from worker history.'
@@ -233,22 +234,111 @@ def test_worker_history_retry_replays_all_reserved_tests
233234 end
234235
235236 def test_worker_history_retry_rejoins_queue_without_worker_reservations
236- run_worker_history_worker ( retry_count : 0 , build_id : 'missing-history' , worker_id : '1' )
237+ run_retry_worker ( retry_count : 0 , build_id : 'missing-history' , worker_id : '1' )
237238
238- out , = run_worker_history_worker ( retry_count : 1 , build_id : 'missing-history' , worker_id : '2' )
239+ out , = run_retry_worker ( retry_count : 1 , build_id : 'missing-history' , worker_id : '2' )
239240
240241 assert_predicate $?, :success?
241242 assert_includes out , "The retry queue does not contain any failure, we'll process the main queue instead."
242243 assert_includes out , 'All tests were ran already'
243244 end
244245
246+ def test_buildkite_retry_starts_a_test_run_after_setup_failed
247+ %w[ failed-tests worker-history ] . each do |selection |
248+ [ 1 , 2 ] . each do |retry_count |
249+ build_id = "setup-failure-#{ selection } -#{ retry_count } "
250+ out , err = run_retry_worker (
251+ retry_count : retry_count ,
252+ build_id : build_id ,
253+ retry_selection : selection ,
254+ extra_args : [ '--namespace' , 'sinatra-tests' ]
255+ )
256+
257+ assert_predicate $?, :success? , out + err
258+ assert_includes normalize ( out ) , 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues'
259+ assert_equal 100 , @redis . scard ( "build:sinatra-tests:#{ build_id } :processed" )
260+ end
261+ end
262+ end
263+
264+ def test_semaphore_retry_starts_a_test_run_after_setup_failed
265+ out , err = run_retry_worker (
266+ retry_count : 0 ,
267+ build_id : 'semaphore-setup-failure' ,
268+ retry_selection : 'failed-tests' ,
269+ environment : { 'SEMAPHORE_PIPELINE_RERUN' => 'true' }
270+ )
271+
272+ assert_predicate $?, :success? , out + err
273+ assert_includes normalize ( out ) , 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues'
274+ end
275+
276+ def test_buildkite_retry_replays_file_queue
277+ Tempfile . create ( [ 'ci-queue-retry' , '.txt' ] ) do |file |
278+ file . write ( ( 0 ...100 ) . map { |index | "PassingTest#test_passing_#{ index } \n " } . join )
279+ file . flush
280+
281+ out , err = run_retry_worker (
282+ retry_count : 1 ,
283+ retry_selection : 'failed-tests' ,
284+ queue_url : file . path
285+ )
286+
287+ assert_predicate $?, :success? , out + err
288+ assert_includes normalize ( out ) , 'Ran 100 tests, 100 assertions, 0 failures, 0 errors, 0 skips, 0 requeues'
289+ end
290+ end
291+
292+ def test_retry_preserves_existing_test_run_timestamp
293+ %w[ failed-tests worker-history ] . each do |selection |
294+ build_id = "existing-run-#{ selection } "
295+ run_retry_worker ( retry_count : 0 , build_id : build_id )
296+ assert_predicate $?, :success?
297+ created_at = ( Time . now . to_f - 3600 ) . to_s
298+ @redis . set ( "build:#{ build_id } :created-at" , created_at )
299+
300+ out , err = run_retry_worker (
301+ retry_count : 1 ,
302+ build_id : build_id ,
303+ retry_selection : selection
304+ )
305+
306+ assert_predicate $?, :success? , out + err
307+ assert_equal created_at , @redis . get ( "build:#{ build_id } :created-at" )
308+ if selection == 'worker-history'
309+ assert_includes out , 'Replaying 100 tests from worker history.'
310+ else
311+ assert_includes out , 'All tests were ran already'
312+ end
313+ end
314+ end
315+
316+ def test_retry_rejects_expired_run_without_worker_history
317+ %w[ failed-tests worker-history ] . each do |selection |
318+ build_id = "expired-run-#{ selection } "
319+ created_at = ( Time . now . to_f - 24 * 60 * 60 ) . to_s
320+ @redis . set ( "build:#{ build_id } :created-at" , created_at )
321+
322+ out , err = run_retry_worker (
323+ retry_count : 1 ,
324+ build_id : build_id ,
325+ retry_selection : selection
326+ )
327+
328+ refute_predicate $?, :success? , out + err
329+ assert_includes out , "The test run is too old and can't be retried"
330+ assert_equal created_at , @redis . get ( "build:#{ build_id } :created-at" )
331+ assert_equal 0 , @redis . scard ( "build:#{ build_id } :processed" )
332+ end
333+ end
334+
245335 def test_worker_history_retry_fails_when_replay_stops_early
246- run_worker_history_worker (
336+ run_retry_worker (
247337 retry_count : 0 ,
248338 build_id : 'incomplete-history' ,
249339 test_file : 'test/failing_test.rb'
250340 )
251- out , err = run_worker_history_worker (
341+ out , err = run_retry_worker (
252342 retry_count : 1 ,
253343 build_id : 'incomplete-history' ,
254344 test_file : 'test/failing_test.rb' ,
@@ -827,28 +917,31 @@ def test_utf8_tests_and_marshal
827917
828918 private
829919
830- def run_worker_history_worker (
920+ def run_retry_worker (
831921 retry_count :,
832922 build_id : 'worker-history' ,
833923 worker_id : '1' ,
834924 test_file : 'test/passing_test.rb' ,
835- extra_args : [ ]
925+ extra_args : [ ] ,
926+ retry_selection : 'worker-history' ,
927+ environment : { } ,
928+ queue_url : @redis_url
836929 )
837930 args = [
838931 @exe , 'run' ,
839- '--queue' , @redis_url ,
932+ '--queue' , queue_url ,
840933 '--seed' , 'foobar' ,
841934 '--build' , build_id ,
842935 '--worker' , worker_id ,
843936 '--timeout' , '1' ,
844- '--retry-selection' , 'worker-history'
937+ '--retry-selection' , retry_selection
845938 ]
846939 args . concat ( extra_args )
847940 args . push ( '-Itest' , test_file )
848941
849942 capture_subprocess_io do
850943 system (
851- { 'BUILDKITE_RETRY_COUNT' => retry_count . to_s } ,
944+ { 'BUILDKITE_RETRY_COUNT' => retry_count . to_s } . merge ( environment ) ,
852945 *args ,
853946 chdir : 'test/fixtures/'
854947 )
0 commit comments