Fix long wait for Capybara test
[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     [ContainerRequest, 'queued', 'cwu', 0, "Queued", nil, 0.0],   # priority 1
16     [ContainerRequest, 'canceled_with_queued_container', 'cwu', 0, "Ready", nil, 0.0],
17     [ContainerRequest, 'canceled_with_locked_container', 'cwu', 0, "Ready", nil, 0.0],
18     [ContainerRequest, 'canceled_with_running_container', 'cwu', 0, "Running", nil, 0.0],
19   ].each do |type, fixture, label, num_children, state, success, progress|
20     test "children of #{fixture}" do
21       use_token 'active'
22       obj = find_fixture(type, fixture)
23       wu = obj.work_unit(label)
24
25       if label != nil
26         assert_equal(label, wu.label)
27       else
28         assert_equal(obj.name, wu.label)
29       end
30       assert_equal(obj['uuid'], wu.uuid)
31       assert_equal(state, wu.state_label)
32       assert_equal(success, wu.success?)
33       assert_equal(progress, wu.progress)
34
35       assert_equal(num_children, wu.children.size)
36       wu.children.each do |child|
37         assert_equal(true, child.respond_to?(:script))
38       end
39     end
40   end
41
42   [
43     ['cr_for_failed', 'Failed', 33],
44     ['completed', 'Complete', 0],
45   ].each do |cr_fixture, state, exit_code|
46     test "Completed ContainerRequest state = #{state} with exit_code = #{exit_code}" do
47       use_token 'active'
48       obj = find_fixture(ContainerRequest, cr_fixture)
49       wu = obj.work_unit
50       assert_equal state, wu.state_label
51       assert_equal exit_code, wu.exit_code
52     end
53   end
54
55   [
56     [Job, 'running_job_with_components', 1, 1, nil],
57     [Job, 'queued', nil, nil, 1],
58     [PipelineInstance, 'pipeline_in_running_state', 1, 1, nil],
59     [PipelineInstance, 'has_component_with_completed_jobs', 60, 60, nil],
60   ].each do |type, fixture, walltime, cputime, queuedtime|
61     test "times for #{fixture}" do
62       use_token 'active'
63       obj = find_fixture(type, fixture)
64       wu = obj.work_unit
65
66       if walltime
67         assert_equal true, (wu.walltime >= walltime)
68       else
69         assert_equal walltime, wu.walltime
70       end
71
72       if cputime
73         assert_equal true, (wu.cputime >= cputime)
74       else
75         assert_equal cputime, wu.cputime
76       end
77
78       if queuedtime
79         assert_equal true, (wu.queuedtime >= queuedtime)
80       else
81         assert_equal queuedtime, wu.queuedtime
82       end
83     end
84   end
85
86   [
87     [Job, 'active', 'running_job_with_components', true],
88     [Job, 'active', 'queued', false],
89     [Job, nil, 'completed_job_in_publicly_accessible_project', true],
90     [Job, 'active', 'completed_job_in_publicly_accessible_project', true],
91     [PipelineInstance, 'active', 'pipeline_in_running_state', true],  # no log, but while running the log link points to pi Log tab
92     [PipelineInstance, nil, 'pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false],
93     [PipelineInstance, 'active', 'pipeline_in_publicly_accessible_project_but_other_objects_elsewhere', false], #no log for completed pi
94     [Job, nil, 'job_in_publicly_accessible_project_but_other_objects_elsewhere', false, "Log unavailable"],
95   ].each do |type, token, fixture, has_log, log_link|
96     test "link_to_log for #{fixture} for #{token}" do
97       use_token token if token
98       obj = find_fixture(type, fixture)
99       wu = obj.work_unit
100
101       link = "#{wu.uri}#Log" if has_log
102       link_to_log = wu.link_to_log
103
104       if has_log
105         assert_includes link_to_log, link
106       else
107         assert_equal log_link, link_to_log
108       end
109     end
110   end
111
112   test 'can_cancel?' do
113     use_token 'active' do
114       assert find_fixture(Job, 'running').work_unit.can_cancel?
115       refute find_fixture(Container, 'running').work_unit.can_cancel?
116       assert find_fixture(ContainerRequest, 'running').work_unit.can_cancel?
117     end
118     use_token 'spectator' do
119       refute find_fixture(ContainerRequest, 'running_anonymous_accessible').work_unit.can_cancel?
120     end
121     use_token 'admin' do
122       assert find_fixture(ContainerRequest, 'running_anonymous_accessible').work_unit.can_cancel?
123     end
124   end
125 end