2 require 'crunch_dispatch'
3 require 'helpers/git_test_helper'
5 class CrunchDispatchTest < ActiveSupport::TestCase
8 test 'choose cheaper nodes first' do
10 # Replace test fixtures with a set suitable for testing dispatch
13 # Idle nodes with different prices
14 [['compute1', 3.20, 32],
15 ['compute2', 1.60, 16],
16 ['compute3', 0.80, 8]].each do |hostname, price, cores|
17 Node.create!(hostname: hostname,
19 'slurm_state' => 'idle',
25 'total_cpu_cores' => cores,
26 'total_ram_mb' => cores*1024,
27 'total_scratch_mb' => cores*10000,
31 # Node with no price information
32 Node.create!(hostname: 'compute4',
34 'slurm_state' => 'idle',
37 'total_cpu_cores' => 8,
38 'total_ram_mb' => 8192,
39 'total_scratch_mb' => 80000,
43 Node.create!(hostname: 'compute5',
45 'slurm_state' => 'alloc',
51 'total_cpu_cores' => 32,
52 'total_ram_mb' => 32768,
53 'total_scratch_mb' => 320000,
57 dispatch = CrunchDispatch.new
58 [[1, 16384, ['compute2']],
59 [2, 16384, ['compute2', 'compute1']],
60 [2, 8000, ['compute4', 'compute3']],
61 ].each do |min_nodes, min_ram, expect_nodes|
62 job = Job.new(uuid: 'zzzzz-8i9sb-382lhiizavzhqlp',
63 runtime_constraints: {
64 'min_nodes' => min_nodes,
65 'min_ram_mb_per_node' => min_ram,
67 nodes = dispatch.nodes_available_for_job_now job
68 assert_equal expect_nodes, nodes
72 test 'respond to TERM' do
73 lockfile = Rails.root.join 'tmp', 'dispatch.lock'
74 ENV['CRUNCH_DISPATCH_LOCKFILE'] = lockfile.to_s
78 # Abandon database connections inherited from parent
80 # https://github.com/kstephens/rails_is_forked
81 ActiveRecord::Base.connection_handler.connection_pools.each_value do |pool|
83 @reserved_connections = {}
87 ActiveRecord::Base.establish_connection
89 dispatch = CrunchDispatch.new
90 dispatch.stubs(:did_recently).returns true
96 assert_with_timeout 5, "Dispatch did not lock #{lockfile}" do
100 Process.kill("TERM", pid)
102 assert_with_timeout 20, "Dispatch did not unlock #{lockfile}" do
107 test 'override --cgroup-root with CRUNCH_CGROUP_ROOT' do
108 ENV['CRUNCH_CGROUP_ROOT'] = '/path/to/cgroup'
109 Rails.configuration.crunch_job_wrapper = :none
110 act_as_system_user do
111 j = Job.create(repository: 'active/foo',
113 script_version: '4fe459abe02d9b365932b8f5dc419439ab4e2577',
114 script_parameters: {})
116 Open3.expects(:popen3).at_least_once.with do |*args|
117 if args.index(j.uuid)
118 ok = ((i = args.index '--cgroup-root') and
119 (args[i+1] == '/path/to/cgroup'))
122 end.raises(StandardError.new('all is well'))
123 dispatch = CrunchDispatch.new
124 dispatch.parse_argv ['--jobs']
125 dispatch.refresh_todo
131 def assert_with_timeout timeout, message
133 while (t += 0.1) < timeout
139 assert false, message + " (waited #{timeout} seconds)"
142 def can_lock lockfile
143 lockfile.open(File::RDWR|File::CREAT, 0644) do |f|
144 return f.flock(File::LOCK_EX|File::LOCK_NB)
148 test 'rate limit of partial line segments' do
149 act_as_system_user do
150 Rails.configuration.crunch_log_partial_line_throttle_period = 1
153 job[:bytes_logged] = 0
154 job[:log_throttle_bytes_so_far] = 0
155 job[:log_throttle_lines_so_far] = 0
156 job[:log_throttle_bytes_skipped] = 0
157 job[:log_throttle_is_open] = true
158 job[:log_throttle_partial_line_last_at] = Time.new(0)
159 job[:log_throttle_first_partial_line] = true
161 dispatch = CrunchDispatch.new
163 line = "first log line"
164 limit = dispatch.rate_limit(job, line)
165 assert_equal true, limit
166 assert_equal "first log line", line
167 assert_equal 1, job[:log_throttle_lines_so_far]
169 # first partial line segment is skipped and counted towards skipped lines
170 now = Time.now.strftime('%Y-%m-%d-%H:%M:%S')
171 line = "#{now} localhost 100 0 stderr [...] this is first partial line segment [...]"
172 limit = dispatch.rate_limit(job, line)
173 assert_equal true, limit
174 assert_includes line, "Rate-limiting partial segments of long lines", line
175 assert_equal 2, job[:log_throttle_lines_so_far]
177 # next partial line segment within throttle interval is skipped but not counted towards skipped lines
178 line = "#{now} localhost 100 0 stderr [...] second partial line segment within the interval [...]"
179 limit = dispatch.rate_limit(job, line)
180 assert_equal false, limit
181 assert_equal 2, job[:log_throttle_lines_so_far]
183 # next partial line after interval is counted towards skipped lines
185 line = "#{now} localhost 100 0 stderr [...] third partial line segment after the interval [...]"
186 limit = dispatch.rate_limit(job, line)
187 assert_equal false, limit
188 assert_equal 3, job[:log_throttle_lines_so_far]
190 # this is not a valid line segment
191 line = "#{now} localhost 100 0 stderr [...] does not end with [...] and is not a partial segment"
192 limit = dispatch.rate_limit(job, line)
193 assert_equal true, limit
194 assert_equal "#{now} localhost 100 0 stderr [...] does not end with [...] and is not a partial segment", line
195 assert_equal 4, job[:log_throttle_lines_so_far]
197 # this also is not a valid line segment
198 line = "#{now} localhost 100 0 stderr does not start correctly but ends with [...]"
199 limit = dispatch.rate_limit(job, line)
200 assert_equal true, limit
201 assert_equal "#{now} localhost 100 0 stderr does not start correctly but ends with [...]", line
202 assert_equal 5, job[:log_throttle_lines_so_far]
206 test 'scancel orphaned job nodes' do
207 Rails.configuration.crunch_job_wrapper = :slurm_immediate
208 act_as_system_user do
209 dispatch = CrunchDispatch.new
211 squeue_resp = IO.popen("echo zzzzz-8i9sb-pshmckwoma9plh7\necho thisisnotvalidjobuuid\necho zzzzz-8i9sb-4cf0abc123e809j\necho zzzzz-dz642-o04e3r651turtdr\n")
212 scancel_resp = IO.popen("true")
215 with(['squeue', '-a', '-h', '-o', '%j']).
219 with(dispatch.sudo_preface + ['scancel', '-n', 'zzzzz-8i9sb-4cf0abc123e809j']).
220 returns(scancel_resp)
222 dispatch.check_orphaned_slurm_jobs