2 require 'crunch_dispatch'
4 class FailJobsTest < ActiveSupport::TestCase
11 act_as_user users(:admin) do
12 @job[:before_reboot] = Job.create!(state: 'Running',
14 started_at: Time.at(BOOT_TIME - 300))
15 @job[:after_reboot] = Job.create!(state: 'Running',
17 started_at: Time.at(BOOT_TIME + 300))
18 @job[:complete] = Job.create!(state: 'Running',
20 started_at: Time.at(BOOT_TIME - 300))
21 @job[:complete].update_attributes(state: 'Complete')
22 @job[:complete].update_attributes(finished_at: Time.at(BOOT_TIME + 100))
23 @job[:queued] = jobs(:queued)
25 @job.values.each do |job|
27 Job.where(uuid: job.uuid).
28 update_all(created_at: Time.at(BOOT_TIME - 330),
29 modified_at: (job.finished_at ||
31 Time.at(BOOT_TIME - 300)))
34 @dispatch = CrunchDispatch.new
35 @test_start_time = db_current_time
38 test 'cancel slurm jobs' do
39 Rails.configuration.crunch_job_wrapper = :slurm_immediate
40 Rails.configuration.crunch_job_user = 'foobar'
41 fake_squeue = IO.popen("echo #{@job[:before_reboot].uuid}")
42 fake_scancel = IO.popen("true")
44 with(['squeue', '-a', '-h', '-o', '%j']).
47 with(includes('sudo', '-u', 'foobar', 'scancel', '-n', @job[:before_reboot].uuid)).
49 @dispatch.fail_jobs(before: Time.at(BOOT_TIME).to_s)
53 test 'use reboot time' do
54 Rails.configuration.crunch_job_wrapper = nil
55 @dispatch.expects(:open).once.with('/proc/stat').
56 returns open(Rails.root.join('test/fixtures/files/proc_stat'))
57 @dispatch.fail_jobs(before: 'reboot')
61 test 'command line help' do
62 cmd = Rails.root.join('script/fail-jobs.rb').to_s
63 assert_match(/Options:.*--before=/m, File.popen([cmd, '--help']).read)
69 @job.values.map(&:reload)
70 assert_equal 'Failed', @job[:before_reboot].state
71 assert_equal false, @job[:before_reboot].running
72 assert_equal false, @job[:before_reboot].success
73 assert_operator @job[:before_reboot].finished_at, :>=, @test_start_time
74 assert_operator @job[:before_reboot].finished_at, :<=, db_current_time
75 assert_equal 'Running', @job[:after_reboot].state
76 assert_equal 'Complete', @job[:complete].state
77 assert_equal 'Queued', @job[:queued].state