Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / test / integration / jobs_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'fileutils'
6 require 'tmpdir'
7
8 require 'integration_helper'
9
10 class JobsTest < ActionDispatch::IntegrationTest
11   include KeepWebConfig
12
13   setup do
14       need_javascript
15   end
16
17   def fakepipe_with_log_data
18     content =
19       "2014-01-01_12:00:01 zzzzz-8i9sb-0vsrcqi7whchuil 0  log message 1\n" +
20       "2014-01-01_12:00:02 zzzzz-8i9sb-0vsrcqi7whchuil 0  log message 2\n" +
21       "2014-01-01_12:00:03 zzzzz-8i9sb-0vsrcqi7whchuil 0  log message 3\n"
22     StringIO.new content, 'r'
23   end
24
25   test "add job description" do
26     job = api_fixture('jobs')['nearly_finished_job']
27     visit page_with_token("active", "/jobs/#{job['uuid']}")
28
29     # edit job description
30     within('.arv-description-as-subtitle') do
31       find('.fa-pencil').click
32       find('.editable-input textarea').set('*Textile description for job* - "Go to dashboard":/')
33       find('.editable-submit').click
34     end
35
36     # Verify edited description
37     assert_no_text '*Textile description for job*'
38     assert_text 'Textile description for job'
39     assert_selector 'a[href="/"]', text: 'Go to dashboard'
40   end
41
42   test "view job log" do
43     job = api_fixture('jobs')['job_with_real_log']
44
45     IO.expects(:popen).returns(fakepipe_with_log_data)
46
47     visit page_with_token("active", "/jobs/#{job['uuid']}")
48     assert page.has_text? job['script_version']
49
50     find(:xpath, "//a[@href='#Log']").click
51     wait_for_ajax
52     assert page.has_text? 'Started at'
53     assert page.has_text? 'Finished at'
54     assert page.has_text? 'log message 1'
55     assert page.has_text? 'log message 2'
56     assert page.has_text? 'log message 3'
57     assert page.has_no_text? 'Showing only 100 bytes of this log'
58   end
59
60   test 'view partial job log' do
61     # This config will be restored during teardown by ../test_helper.rb:
62     Rails.configuration.log_viewer_max_bytes = 100
63
64     IO.expects(:popen).returns(fakepipe_with_log_data)
65     job = api_fixture('jobs')['job_with_real_log']
66
67     visit page_with_token("active", "/jobs/#{job['uuid']}")
68     assert page.has_text? job['script_version']
69
70     find(:xpath, "//a[@href='#Log']").click
71     wait_for_ajax
72     assert page.has_text? 'Showing only 100 bytes of this log'
73   end
74
75   test 'view log via keep-web redirect' do
76     use_keep_web_config
77
78     token = api_fixture('api_client_authorizations')['active']['api_token']
79     logdata = fakepipe_with_log_data.read
80     logblock = `echo -n #{logdata.shellescape} | ARVADOS_API_TOKEN=#{token.shellescape} arv-put --no-progress --raw -`.strip
81     assert $?.success?, $?
82
83     job = nil
84     use_token 'active' do
85       job = Job.find api_fixture('jobs')['running']['uuid']
86       mtxt = ". #{logblock} 0:#{logdata.length}:#{job.uuid}.log.txt\n"
87       logcollection = Collection.create(manifest_text: mtxt)
88       job.update_attributes log: logcollection.portable_data_hash
89     end
90     visit page_with_token 'active', '/jobs/'+job.uuid
91     find('a[href="#Log"]').click
92     assert_text 'log message 1'
93   end
94
95   [
96     ['foobar', false, false],
97     ['job_with_latest_version', true, false],
98     ['job_with_latest_version', true, true],
99   ].each do |job_name, expect_options, use_latest|
100     test "Rerun #{job_name} job, expect options #{expect_options},
101           and use latest version option #{use_latest}" do
102       job = api_fixture('jobs')[job_name]
103       visit page_with_token 'active', '/jobs/'+job['uuid']
104
105       if expect_options
106         assert_text 'supplied_script_version: master'
107       else
108         assert_no_text 'supplied_script_version'
109       end
110
111       assert_triggers_dom_event 'shown.bs.modal' do
112         find('a,button', text: 'Re-run job...').click
113       end
114       within('.modal-dialog') do
115         assert_selector 'a,button', text: 'Cancel'
116         if use_latest
117           page.choose("job_script_version_#{job['supplied_script_version']}")
118         end
119         click_on "Run now"
120       end
121
122       # Re-running jobs doesn't currently work because the test API
123       # server has no git repository to check against.  For now, check
124       # that the error message says something appropriate for that
125       # situation.
126       if expect_options && use_latest
127         assert_text "077ba2ad3ea24a929091a9e6ce545c93199b8e57"
128       else
129         assert_text "Script version #{job['script_version']} does not resolve to a commit"
130       end
131     end
132   end
133
134   [
135     ['active', true],
136     ['job_reader2', false],
137   ].each do |user, readable|
138     test "view job with components as #{user} user" do
139       job = api_fixture('jobs')['running_job_with_components']
140       component1 = api_fixture('jobs')['completed_job_in_publicly_accessible_project']
141       component2 = api_fixture('pipeline_instances')['running_pipeline_with_complete_job']
142       component2_child1 = api_fixture('jobs')['previous_job_run']
143       component2_child2 = api_fixture('jobs')['running']
144
145       visit page_with_token(user, "/jobs/#{job['uuid']}")
146       assert page.has_text? job['script_version']
147       assert page.has_no_text? 'script_parameters'
148
149       # The job_reader2 is allowed to read job, component2, and component2_child1,
150       # and component2_child2 only as a component of the pipeline component2
151       if readable
152         assert page.has_link? 'component1'
153         assert page.has_link? 'component2'
154       else
155         assert page.has_no_link? 'component1'
156         assert page.has_link? 'component2'
157       end
158
159       if readable
160         click_link('component1')
161         within('.panel-collapse') do
162           assert(has_text? component1['uuid'])
163           assert(has_text? component1['script_version'])
164           assert(has_text? 'script_parameters')
165         end
166         click_link('component1')
167       end
168
169       click_link('component2')
170       within('.panel-collapse') do
171         assert(has_text? component2['uuid'])
172         assert(has_text? component2['script_version'])
173         assert(has_no_text? 'script_parameters')
174         assert(has_link? 'previous')
175         assert(has_link? 'running')
176
177         click_link('previous')
178         within('.panel-collapse') do
179           assert(has_text? component2_child1['uuid'])
180           assert(has_text? component2_child1['script_version'])
181         end
182         click_link('previous')
183
184         click_link('running')
185         within('.panel-collapse') do
186           assert(has_text? component2_child2['uuid'])
187           if readable
188             assert(has_text? component2_child2['script_version'])
189           else
190             assert(has_no_text? component2_child2['script_version'])
191           end
192         end
193       end
194     end
195   end
196 end