9799: Merge branch 'master' into 9799-nonadmin-logs
[arvados.git] / apps / workbench / test / unit / work_unit_test.rb
1 require 'test_helper'
2
3 class WorkUnitTest < ActiveSupport::TestCase
4   setup do
5     Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
6   end
7
8   [
9     [Job, 'running_job_with_components', "jwu", 2, "Running", nil, 0.5],
10     [PipelineInstance, 'pipeline_in_running_state', nil, 1, "Running", nil, 0.0],
11     [PipelineInstance, 'has_component_with_completed_jobs', nil, 3, "Complete", true, 1.0],
12     [PipelineInstance, 'pipeline_with_tagged_collection_input', "pwu", 1, "Ready", nil, 0.0],
13     [Container, 'requester', 'cwu', 1, "Complete", true, 1.0],
14     [ContainerRequest, 'cr_for_requester', 'cwu', 1, "Complete", true, 1.0],
15   ].each do |type, fixture, label, num_children, state, success, progress|
16     test "children of #{fixture}" do
17       use_token 'active'
18       obj = find_fixture(type, fixture)
19       wu = obj.work_unit(label)
20
21       if label != nil
22         assert_equal(label, wu.label)
23       else
24         assert_equal(obj.name, wu.label)
25       end
26       assert_equal(obj['uuid'], wu.uuid)
27       assert_equal(state, wu.state_label)
28       assert_equal(success, wu.success?)
29       assert_equal(progress, wu.progress)
30
31       assert_equal(num_children, wu.children.size)
32       wu.children.each do |child|
33         assert_equal(true, child.respond_to?(:script))
34       end
35     end
36   end
37
38   [
39     ['cr_for_failed', 'Failed', 33],
40     ['completed', 'Complete', 0],
41   ].each do |cr_fixture, state, exit_code|
42     test "Completed ContainerRequest state = #{state} with exit_code = #{exit_code}" do
43       use_token 'active'
44       obj = find_fixture(ContainerRequest, cr_fixture)
45       wu = obj.work_unit
46       assert_equal state, wu.state_label
47       assert_equal exit_code, wu.exit_code
48     end
49   end
50
51   [
52     [Job, 'running_job_with_components', 1, 1, nil],
53     [Job, 'queued', nil, nil, 1],
54     [PipelineInstance, 'pipeline_in_running_state', 1, 1, nil],
55     [PipelineInstance, 'has_component_with_completed_jobs', 60, 60, nil],
56   ].each do |type, fixture, walltime, cputime, queuedtime|
57     test "times for #{fixture}" do
58       use_token 'active'
59       obj = find_fixture(type, fixture)
60       wu = obj.work_unit
61
62       if walltime
63         assert_equal true, (wu.walltime >= walltime)
64       else
65         assert_equal walltime, wu.walltime
66       end
67
68       if cputime
69         assert_equal true, (wu.cputime >= cputime)
70       else
71         assert_equal cputime, wu.cputime
72       end
73
74       if queuedtime
75         assert_equal true, (wu.queuedtime >= queuedtime)
76       else
77         assert_equal queuedtime, wu.queuedtime
78       end
79     end
80   end
81
82   [
83     [Job, 'active', 'running_job_with_components', true],
84     [Job, 'active', 'queued', false],
85     [Job, nil, 'completed_job_in_publicly_accessible_project', true],
86     [Job, 'active', 'completed_job_in_publicly_accessible_project', true],
87     [PipelineInstance, 'active', 'pipeline_in_running_state', true],  # no log, but while running the log link points to pi Log tab
88     [PipelineInstance, nil, 'pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false],
89     [PipelineInstance, 'active', 'pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false], #no log for completed pi
90     [Job, nil, 'job_in_publicly_accessible_project_but_other_objects_elsewhere', false, "Log unavailable"],
91   ].each do |type, token, fixture, has_log, log_link|
92     test "link_to_log for #{fixture} for #{token}" do
93       use_token token if token
94       obj = find_fixture(type, fixture)
95       wu = obj.work_unit
96
97       link = "#{wu.uri}#Log" if has_log
98       link_to_log = wu.link_to_log
99
100       if has_log
101         assert_includes link_to_log, link
102       else
103         assert_equal log_link, link_to_log
104       end
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