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