Merge branch '19291-tzinfo-upgrade'. Closes #19291
[arvados.git] / apps / workbench / test / unit / work_unit_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class WorkUnitTest < ActiveSupport::TestCase
8
9   reset_api_fixtures :after_each_test, false
10
11   setup do
12     Rails.configuration.Users.AnonymousUserToken = api_fixture('api_client_authorizations')['anonymous']['api_token']
13   end
14
15   [
16     [Job, 'running_job_with_components', "jwu", 2, "Running", nil, 0.5],
17     [PipelineInstance, 'pipeline_in_running_state', nil, 1, "Running", nil, 0.0],
18     [PipelineInstance, 'has_component_with_completed_jobs', nil, 3, "Complete", true, 1.0],
19     [PipelineInstance, 'pipeline_with_tagged_collection_input', "pwu", 1, "Ready", nil, 0.0],
20     [PipelineInstance, 'failed_pipeline_with_two_jobs', nil, 2, "Cancelled", false, 0.0],
21     [Container, 'requester', 'cwu', 1, "Complete", true, 1.0],
22     [ContainerRequest, 'cr_for_requester', 'cwu', 1, "Complete", true, 1.0],
23     [ContainerRequest, 'queued', 'cwu', 0, "Queued", nil, 0.0],   # priority 1
24     [ContainerRequest, 'canceled_with_queued_container', 'cwu', 0, "Cancelled", false, 0.0],
25     [ContainerRequest, 'canceled_with_locked_container', 'cwu', 0, "Cancelled", false, 0.0],
26     [ContainerRequest, 'canceled_with_running_container', 'cwu', 1, "Running", nil, 0.0],
27   ].each do |type, fixture, label, num_children, state, success, progress|
28     test "children of #{fixture}" do
29       use_token 'active'
30       obj = find_fixture(type, fixture)
31       wu = obj.work_unit(label)
32
33       if label != nil
34         assert_equal(label, wu.label)
35       elsif obj.name.nil?
36         assert_nil(wu.label)
37       else
38         assert_equal(obj.name, wu.label)
39       end
40       assert_equal(obj['uuid'], wu.uuid)
41       assert_equal(state, wu.state_label)
42       if success.nil?
43         assert_nil(wu.success?)
44       else
45         assert_equal(success, wu.success?)
46       end
47       assert_equal(progress, wu.progress)
48
49       assert_equal(num_children, wu.children.size)
50       wu.children.each do |child|
51         assert_equal(true, child.respond_to?(:script))
52       end
53     end
54   end
55
56   [
57     ['cr_for_failed', 'Failed', 33],
58     ['completed', 'Complete', 0],
59   ].each do |cr_fixture, state, exit_code|
60     test "Completed ContainerRequest state = #{state} with exit_code = #{exit_code}" do
61       use_token 'active'
62       obj = find_fixture(ContainerRequest, cr_fixture)
63       wu = obj.work_unit
64       assert_equal state, wu.state_label
65       assert_equal exit_code, wu.exit_code
66     end
67   end
68
69   [
70     [Job, 'running_job_with_components', 1, 1, nil, true],
71     [Job, 'queued', nil, 0, 1, false],
72     [PipelineInstance, 'pipeline_in_running_state', 1, 1, nil, false],
73     [PipelineInstance, 'has_component_with_completed_jobs', 60, 60, nil, true],
74   ].each do |type, fixture, walltime, cputime, queuedtime, cputime_more_than_walltime|
75     test "times for #{fixture}" do
76       use_token 'active'
77       obj = find_fixture(type, fixture)
78       wu = obj.work_unit
79
80       if walltime
81         assert_equal true, (wu.walltime >= walltime)
82       else
83         if walltime.nil?
84           assert_nil wu.walltime
85         else
86           assert_equal walltime, wu.walltime
87         end
88       end
89
90       if cputime
91         assert_equal true, (wu.cputime >= cputime)
92       else
93         assert_equal cputime, wu.cputime
94       end
95
96       if queuedtime
97         assert_equal true, (wu.queuedtime >= queuedtime)
98       elsif queuedtime.nil?
99         assert_nil wu.queuedtime
100       else
101         assert_equal queuedtime, wu.queuedtime
102       end
103
104       assert_equal cputime_more_than_walltime, (wu.cputime > wu.walltime) if wu.cputime and wu.walltime
105     end
106   end
107
108   test 'can_cancel?' do
109     use_token 'active' do
110       assert find_fixture(Job, 'running').work_unit.can_cancel?
111       refute find_fixture(Container, 'running').work_unit.can_cancel?
112       assert find_fixture(ContainerRequest, 'running').work_unit.can_cancel?
113     end
114     use_token 'spectator' do
115       refute find_fixture(ContainerRequest, 'running_anonymous_accessible').work_unit.can_cancel?
116     end
117     use_token 'admin' do
118       assert find_fixture(ContainerRequest, 'running_anonymous_accessible').work_unit.can_cancel?
119     end
120   end
121 end