Merge branch '8416-node-manager-find-node' closes #8416
[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_text 'supplied_script_version: (none)'
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 end