Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / test / integration / pipeline_instances_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'integration_helper'
6
7 class PipelineInstancesTest < ActionDispatch::IntegrationTest
8   setup do
9     need_javascript
10   end
11
12   def parse_browser_timestamp t
13     # Timestamps are displayed in the browser's time zone (which can
14     # differ from ours) and they come from toLocaleTimeString (which
15     # means they don't necessarily tell us which time zone they're
16     # using). In order to make sense of them, we need to ask the
17     # browser to parse them and generate a timestamp that can be
18     # parsed reliably.
19     #
20     # Note: Even with all this help, phantomjs seem to behave badly
21     # when parsing timestamps on the other side of a DST transition.
22     # See skipped tests below.
23
24     # In some locales (e.g., en_CA.UTF-8) Firefox can't parse what its
25     # own toLocaleString() puts out.
26     t.sub!(/(\d\d\d\d)-(\d\d)-(\d\d)/, '\2/\3/\1')
27
28     if /(\d+:\d+ [AP]M) (\d+\/\d+\/\d+)/ =~ t
29       # Currently dates.js renders timestamps as
30       # '{t.toLocaleTimeString()} {t.toLocaleDateString()}' which even
31       # en_US browsers can't make sense of. First we need to flip it
32       # around so it looks like what toLocaleString() would have made.
33       t = $~[2] + ', ' + $~[1]
34     end
35
36     utc = page.evaluate_script("new Date('#{t}').toUTCString()")
37     DateTime.parse(utc).to_time
38   end
39
40   if false
41     # No need to test (or mention) these all the time. If they start
42     # working (without need_selenium) then some real tests might not
43     # need_selenium any more.
44
45     test 'phantomjs DST' do
46       skip '^^'
47       t0s = '3/8/2015, 01:59 AM'
48       t1s = '3/8/2015, 03:01 AM'
49       t0 = parse_browser_timestamp t0s
50       t1 = parse_browser_timestamp t1s
51       assert_equal 120, t1-t0, "'#{t0s}' to '#{t1s}' was reported as #{t1-t0} seconds, should be 120"
52     end
53
54     test 'phantomjs DST 2' do
55       skip '^^'
56       t0s = '2015-03-08T10:43:00Z'
57       t1s = '2015-03-09T03:43:00Z'
58       t0 = parse_browser_timestamp page.evaluate_script("new Date('#{t0s}').toLocaleString()")
59       t1 = parse_browser_timestamp page.evaluate_script("new Date('#{t1s}').toLocaleString()")
60       assert_equal 17*3600, t1-t0, "'#{t0s}' to '#{t1s}' was reported as #{t1-t0} seconds, should be #{17*3600} (17 hours)"
61     end
62   end
63
64   test 'Create and run a pipeline' do
65     visit page_with_token('active_trustedclient', '/pipeline_templates')
66     within('tr', text: 'Two Part Pipeline Template') do
67       find('a,button', text: 'Run').click
68     end
69
70     # project chooser
71     within('.modal-dialog') do #FIXME: source of 1 test error
72       find('.selectable', text: 'A Project').click
73       find('button', text: 'Choose').click
74     end
75
76     # This pipeline needs input. So, Run should be disabled
77     page.assert_selector 'a.disabled,button.disabled', text: 'Run'
78
79     instance_page = current_path
80
81     # Add this collection to the project
82     visit '/projects'
83     find("#projects-menu").click
84     find('.dropdown-menu a,button', text: 'A Project').click
85     find('.btn', text: 'Add data').click
86     find('.dropdown-menu a,button', text: 'Copy data from another project').click
87     within('.modal-dialog') do
88       wait_for_ajax
89       first('span', text: 'foo_tag').click
90       find('.btn', text: 'Copy').click
91     end
92     using_wait_time(Capybara.default_max_wait_time * 3) do
93       wait_for_ajax
94     end
95
96     click_link 'Pipelines and processes'
97     find('tr[data-kind="arvados#pipelineInstance"]', text: '(none)').
98       find('a', text: 'Show').
99       click
100
101     assert find('p', text: 'Provide a value')
102
103     find('div.form-group', text: 'Foo/bar pair').
104       find('.btn', text: 'Choose').
105       click
106
107     within('.modal-dialog') do
108       assert(has_text?("Foo/bar pair"),
109              "pipeline input picker missing name of input")
110       wait_for_ajax
111       first('span', text: 'foo_tag').click
112       find('button', text: 'OK').click
113     end
114     wait_for_ajax
115
116     # The input, after being specified, should still be displayed (#3382)
117     assert find('div.form-group', text: 'Foo/bar pair')
118
119     # The input, after being specified, should still be editable (#3382)
120     find('div.form-group', text: 'Foo/bar pair').
121       find('.btn', text: 'Choose').click
122
123     within('.modal-dialog') do
124       assert(has_text?("Foo/bar pair"),
125              "pipeline input picker missing name of input")
126       wait_for_ajax
127       first('span', text: 'foo_tag').click
128       find('button', text: 'OK').click
129     end
130
131     # For good measure, check one last time that the input, after being specified twice, is still be displayed (#3382)
132     assert find('div.form-group', text: 'Foo/bar pair')
133
134     # Ensure that the collection's portable_data_hash, uuid and name
135     # are saved in the desired places. (#4015)
136
137     # foo_collection_in_aproject is the collection tagged with foo_tag.
138     collection = api_fixture('collections', 'foo_collection_in_aproject')
139     click_link 'Advanced'
140     click_link 'API response'
141     api_response = JSON.parse(find('div#advanced_api_response pre').text)
142     input_params = api_response['components']['part-one']['script_parameters']['input']
143     assert_equal input_params['value'], collection['portable_data_hash']
144     assert_equal input_params['selection_name'], collection['name']
145     assert_equal input_params['selection_uuid'], collection['uuid']
146
147     # "Run" button is now enabled
148     page.assert_no_selector 'a.disabled,button.disabled', text: 'Run'
149
150     first('a,button', text: 'Run').click
151
152     # Pipeline is running. We have a "Pause" button instead now.
153     page.assert_selector 'a,button', text: 'Pause'
154     find('a,button', text: 'Pause').click
155
156     # Pipeline is stopped. It should now be in paused state and Runnable again.
157     assert page.has_text? 'Paused'
158     page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
159     page.assert_selector 'a,button', text: 'Re-run with latest'
160     page.assert_selector 'a,button', text: 'Re-run options'
161
162     # Since it is test env, no jobs are created to run. So, graph not visible
163     assert page.has_no_text? 'Graph'
164   end
165
166   # Create a pipeline instance from within a project and run
167   test 'Create pipeline inside a project and run' do
168     visit page_with_token('active_trustedclient', '/projects')
169
170     # Add collection to the project using Add data button
171     find("#projects-menu").click
172     find('.dropdown-menu a,button', text: 'A Project').click
173     find('.btn', text: 'Add data').click
174     find('.dropdown-menu a,button', text: 'Copy data from another project').click
175     within('.modal-dialog') do
176       wait_for_ajax
177       first('span', text: 'foo_tag').click
178       find('.btn', text: 'Copy').click
179     end
180     using_wait_time(Capybara.default_max_wait_time * 3) do
181       wait_for_ajax
182     end
183
184     create_and_run_pipeline_in_aproject true, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false
185   end
186
187   # Create a pipeline instance from outside of a project
188   test 'Run a pipeline from dashboard' do
189     visit page_with_token('active_trustedclient')
190     create_and_run_pipeline_in_aproject false, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false
191   end
192
193   test 'view pipeline with job and see graph' do
194     visit page_with_token('active_trustedclient', '/pipeline_instances')
195     assert page.has_text? 'pipeline_with_job'
196
197     find('a', text: 'pipeline_with_job').click
198
199     # since the pipeline component has a job, expect to see the graph
200     assert page.has_text? 'Graph'
201     click_link 'Graph'
202     page.assert_selector "#provenance_graph"
203   end
204
205   test 'pipeline description' do
206     visit page_with_token('active_trustedclient', '/pipeline_instances')
207     assert page.has_text? 'pipeline_with_job'
208
209     find('a', text: 'pipeline_with_job').click
210
211     within('.arv-description-as-subtitle') do
212       find('.fa-pencil').click
213       find('.editable-input textarea').set('*Textile description for pipeline instance*')
214       find('.editable-submit').click
215     end
216     wait_for_ajax
217
218     # verify description
219     assert page.has_no_text? '*Textile description for pipeline instance*'
220     assert page.has_text? 'Textile description for pipeline instance'
221   end
222
223   test "JSON popup available for strange components" do
224     uuid = api_fixture("pipeline_instances")["components_is_jobspec"]["uuid"]
225     visit page_with_token("active", "/pipeline_instances/#{uuid}")
226     click_on "Components"
227     assert(page.has_no_text?("script_parameters"),
228            "components JSON visible without popup")
229     click_on "Show components JSON"
230     assert(page.has_text?("script_parameters"),
231            "components JSON not found")
232   end
233
234   def create_pipeline_from(template_name, project_name="Home")
235     # Visit the named pipeline template and create a pipeline instance from it.
236     # The instance will be created under the named project.
237     template_uuid = api_fixture("pipeline_templates", template_name, "uuid")
238     visit page_with_token("active", "/pipeline_templates/#{template_uuid}")
239     click_on "Run this pipeline"
240     within(".modal-dialog") do # FIXME: source of 3 test errors
241       # Set project for the new pipeline instance
242       find(".selectable", text: project_name).click
243       click_on "Choose"
244     end
245     assert(has_text?("This pipeline was created from the template"),
246            "did not land on pipeline instance page")
247   end
248
249   PROJECT_WITH_SEARCH_COLLECTION = "A Subproject"
250   def check_parameter_search(proj_name)
251     create_pipeline_from("parameter_with_search", proj_name)
252     search_text = api_fixture("pipeline_templates", "parameter_with_search",
253                               "components", "with-search",
254                               "script_parameters", "input", "search_for")
255     first("a.btn,button", text: "Choose").click
256     within(".modal-body") do
257       if (proj_name != PROJECT_WITH_SEARCH_COLLECTION)
258         # Switch finder modal to Subproject to find the Collection.
259         click_on proj_name
260         click_on PROJECT_WITH_SEARCH_COLLECTION
261       end
262       assert_equal(search_text, first("input").value,
263                    "parameter search not preseeded")
264       assert(has_text?(api_fixture("collections")["baz_collection_name_in_asubproject"]["name"]),
265              "baz Collection not in preseeded search results")
266     end
267   end
268
269   test "Workbench respects search_for parameter in templates" do
270     check_parameter_search(PROJECT_WITH_SEARCH_COLLECTION)
271   end
272
273   test "Workbench preserves search_for parameter after project switch" do
274     check_parameter_search("A Project")
275   end
276
277   test "enter a float for a number pipeline input" do
278     # Poltergeist either does not support the HTML 5 <input
279     # type="number">, or interferes with the associated X-Editable
280     # validation code.  If the input field has type=number (forcing an
281     # integer), this test will yield a false positive under
282     # Poltergeist.  --Brett, 2015-02-05
283     need_selenium "for strict X-Editable input validation"
284     create_pipeline_from("template_with_dataclass_number")
285     INPUT_SELECTOR =
286       ".editable[data-name='[components][work][script_parameters][input][value]']"
287     find(INPUT_SELECTOR).click
288     find(".editable-input input").set("12.34")
289     find("#editable-submit").click
290     assert_no_selector(".editable-popup")
291     assert_selector(INPUT_SELECTOR, text: "12.34")
292   end
293
294   [
295     [true, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false],
296     [false, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false],
297     [true, 'Two Part Template with dataclass File', 'foo_collection_in_aproject', true],
298     [false, 'Two Part Template with dataclass File', 'foo_collection_in_aproject', true],
299     [true, 'Two Part Pipeline Template', 'collection_with_no_name_in_aproject', false],
300   ].each do |in_aproject, template_name, collection, choose_file|
301     test "Run pipeline instance in #{in_aproject} with #{template_name} with #{collection} file #{choose_file}" do
302       if in_aproject
303         visit page_with_token 'active', \
304         '/projects/'+api_fixture('groups')['aproject']['uuid']
305       else
306         visit page_with_token 'active', '/'
307       end
308
309       # need bigger modal size when choosing a file from collection
310       if Capybara.current_driver == :selenium
311         Capybara.current_session.driver.browser.manage.window.resize_to(1200, 800)
312       end
313
314       create_and_run_pipeline_in_aproject in_aproject, template_name, collection, choose_file
315       instance_path = current_path
316
317       # Pause the pipeline
318       find('a,button', text: 'Pause').click
319       assert page.has_text? 'Paused'
320       page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
321       page.assert_selector 'a,button', text: 'Re-run with latest'
322       page.assert_selector 'a,button', text: 'Re-run options'
323
324       # Verify that the newly created instance is created in the right project.
325       assert page.has_text? 'Home'
326       if in_aproject
327         assert page.has_text? 'A Project'
328       else
329         assert page.has_no_text? 'A Project'
330       end
331     end
332   end
333
334   [
335     ['active', false, false, false],
336     ['active', false, false, true],
337     ['active', true, false, false],
338     ['active', true, true, false],
339     ['active', true, false, true],
340     ['active', true, true, true],
341     ['project_viewer', false, false, true],
342     ['project_viewer', true, true, true],
343   ].each do |user, with_options, choose_options, in_aproject|
344     test "Rerun pipeline instance as #{user} using options #{with_options} #{choose_options} in #{in_aproject}" do
345       if in_aproject
346         path = '/pipeline_instances/'+api_fixture('pipeline_instances')['pipeline_owned_by_active_in_aproject']['uuid']
347       else
348         path = '/pipeline_instances/'+api_fixture('pipeline_instances')['pipeline_owned_by_active_in_home']['uuid']
349       end
350
351       visit page_with_token(user, path)
352
353       page.assert_selector 'a,button', text: 'Re-run with latest'
354       page.assert_selector 'a,button', text: 'Re-run options'
355
356       if user == 'project_viewer' && in_aproject
357         assert page.has_text? 'A Project'
358       end
359
360       # Now re-run the pipeline
361       if with_options
362         assert_triggers_dom_event 'shown.bs.modal' do
363           find('a,button', text: 'Re-run options').click
364         end
365         within('.modal-dialog') do
366           page.assert_selector 'a,button', text: 'Copy and edit inputs'
367           page.assert_selector 'a,button', text: 'Run now'
368           if choose_options
369             find('button', text: 'Copy and edit inputs').click
370           else
371             find('button', text: 'Run now').click
372           end
373         end
374       else
375         find('a,button', text: 'Re-run with latest').click
376       end
377
378       # Verify that the newly created instance is created in the right
379       # project. In case of project_viewer user, since the user cannot
380       # write to the project, the pipeline should have been created in
381       # the user's Home project.
382       assert_not_equal path, current_path, 'Rerun instance path expected to be different'
383       assert_text 'Home'
384       if in_aproject && (user != 'project_viewer')
385         assert_text 'A Project'
386       else
387         assert_no_text 'A Project'
388       end
389     end
390   end
391
392   # Create and run a pipeline for 'Two Part Pipeline Template' in 'A Project'
393   def create_and_run_pipeline_in_aproject in_aproject, template_name, collection_fixture, choose_file=false
394     # collection in aproject to be used as input
395     collection = api_fixture('collections', collection_fixture)
396
397     # create a pipeline instance
398     find('.btn', text: 'Run a process').click
399     within('.modal-dialog') do
400       find('.selectable', text: template_name).click
401       find('.btn', text: 'Next: choose inputs').click
402     end
403
404     assert find('p', text: 'Provide a value')
405
406     find('div.form-group', text: 'Foo/bar pair').
407       find('.btn', text: 'Choose').
408       click
409
410     within('.modal-dialog') do
411       if in_aproject
412         assert_selector 'button.dropdown-toggle', text: 'A Project'
413         wait_for_ajax
414       else
415         assert_selector 'button.dropdown-toggle', text: 'Home'
416         wait_for_ajax
417         click_button "Home"
418         click_link "A Project"
419         wait_for_ajax
420       end
421
422       if collection_fixture == 'foo_collection_in_aproject'
423         first('span', text: 'foo_tag').click
424       elsif collection['name']
425         first('span', text: "#{collection['name']}").click
426       else
427         collection_uuid = collection['uuid']
428         find("div[data-object-uuid=#{collection_uuid}]").click
429       end
430
431       if choose_file
432         wait_for_ajax
433         find('.preview-selectable', text: 'foo').click
434       end
435       find('button', text: 'OK').click
436     end
437
438     # The input, after being specified, should still be displayed (#3382)
439     assert find('div.form-group', text: 'Foo/bar pair')
440
441     # Ensure that the collection's portable_data_hash, uuid and name
442     # are saved in the desired places. (#4015)
443     click_link 'Advanced'
444     click_link 'API response'
445
446     api_response = JSON.parse(find('div#advanced_api_response pre').text)
447     input_params = api_response['components']['part-one']['script_parameters']['input']
448     assert_equal(input_params['selection_uuid'], collection['uuid'], "Not found expected input param uuid")
449     if choose_file
450       assert_equal(input_params['value'], collection['portable_data_hash']+'/foo', "Not found expected input file param value")
451       assert_equal(input_params['selection_name'], collection['name']+'/foo', "Not found expected input file param name")
452     else
453       assert_equal(input_params['value'], collection['portable_data_hash'], "Not found expected input param value")
454       assert_equal(input_params['selection_name'], collection['name'], "Not found expected input selection name")
455     end
456
457     # "Run" button present and enabled
458     page.assert_no_selector 'a.disabled,button.disabled', text: 'Run'
459     first('a,button', text: 'Run').click
460
461     # Pipeline is running. We have a "Pause" button instead now.
462     page.assert_no_selector 'a,button', text: 'Run'
463     page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
464     page.assert_selector 'a,button', text: 'Pause'
465
466     # Since it is test env, no jobs are created to run. So, graph not visible
467     assert page.has_no_text? 'Graph'
468   end
469
470   [
471     ['user1_with_load', 'zzzzz-d1hrv-10pipelines0001', 0], # run time 0 minutes
472     ['user1_with_load', 'zzzzz-d1hrv-10pipelines0010', 17*60*60 + 51*60], # run time 17 hours and 51 minutes
473     ['active', 'zzzzz-d1hrv-runningpipeline', nil], # state = running
474   ].each do |user, uuid, run_time|
475     test "pipeline start and finish time display for #{uuid}" do
476       need_selenium 'to parse timestamps correctly across DST boundaries'
477       visit page_with_token(user, "/pipeline_instances/#{uuid}")
478
479       assert page.has_text? 'This pipeline started at'
480       page_text = page.text
481
482       if run_time
483         match = /This pipeline started at (.*)\. It failed after (.*) at (.*)\. Check the Log/.match page_text
484       else
485         match = /This pipeline started at (.*). It has been active for(.*)/.match page_text
486       end
487       assert_not_nil(match, 'Did not find text - This pipeline started at . . . ')
488
489       start_at = match[1]
490       assert_not_nil(start_at, 'Did not find start_at time')
491
492       start_time = parse_browser_timestamp start_at
493       if run_time
494         finished_at = match[3]
495         assert_not_nil(finished_at, 'Did not find finished_at time')
496         finished_time = parse_browser_timestamp finished_at
497         assert_equal(run_time, finished_time-start_time,
498           "Time difference did not match for start_at #{start_at}, finished_at #{finished_at}, ran_for #{match[2]}")
499       else
500         match = /\d(.*)/.match match[2]
501         assert_not_nil match, 'Did not find expected match for running component'
502       end
503     end
504   end
505
506   [
507     ['fuse', nil, 2, 20],                           # has 2 as of 11-07-2014
508     ['user1_with_load', '000025pipelines', 25, 25], # owned_by the project zzzzz-j7d0g-000025pipelines, two pages
509     ['admin', 'pipeline_20', 1, 1],
510     ['active', 'no such match', 0, 0],
511   ].each do |user, search_filter, expected_min, expected_max|
512     test "scroll pipeline instances page for #{user} with search filter #{search_filter}
513           and expect #{expected_min} <= found_items <= #{expected_max}" do
514       visit page_with_token(user, "/pipeline_instances")
515
516       if search_filter
517         find('.recent-pipeline-instances-filterable-control').set(search_filter)
518         # Wait for 250ms debounce timer (see filterable.js)
519         sleep 0.350
520         wait_for_ajax
521       end
522
523       page_scrolls = expected_max/20 + 2    # scroll num_pages+2 times to test scrolling is disabled when it should be
524       within('.arv-recent-pipeline-instances') do
525         (0..page_scrolls).each do |i|
526           page.driver.scroll_to 0, 999000
527           begin
528             wait_for_ajax
529           rescue
530           end
531         end
532       end
533
534       # Verify that expected number of pipeline instances are found
535       found_items = page.all('tr[data-kind="arvados#pipelineInstance"]')
536       found_count = found_items.count
537       if expected_min == expected_max
538         assert_equal(true, found_count == expected_min,
539           "Not found expected number of items. Expected #{expected_min} and found #{found_count}")
540         assert page.has_no_text? 'request failed'
541       else
542         assert_equal(true, found_count>=expected_min,
543           "Found too few items. Expected at least #{expected_min} and found #{found_count}")
544         assert_equal(true, found_count<=expected_max,
545           "Found too many items. Expected at most #{expected_max} and found #{found_count}")
546       end
547     end
548   end
549
550   test 'render job run time when job record is inaccessible' do
551     pi = api_fixture('pipeline_instances', 'has_component_with_completed_jobs')
552     visit page_with_token 'active', '/pipeline_instances/' + pi['uuid']
553     assert_text 'Queued for '
554   end
555
556   test "job logs linked for running pipeline" do
557     pi = api_fixture("pipeline_instances", "running_pipeline_with_complete_job")
558     visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
559     find(:xpath, "//a[@href='#Log']").click
560     within "#Log" do
561       assert_text "Log for previous"
562       log_link = find("a", text: "Log for previous")
563       assert_includes(log_link[:href],
564                       "/jobs/#{pi["components"]["previous"]["job"]["uuid"]}#Log")
565       assert_selector "#event_log_div"
566     end
567   end
568
569   test "job logs linked for complete pipeline" do
570     pi = api_fixture("pipeline_instances", "complete_pipeline_with_two_jobs")
571     visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
572     find(:xpath, "//a[@href='#Log']").click
573     within "#Log" do
574       assert_text "Log for previous"
575       pi["components"].each do |cname, cspec|
576         log_link = find("a", text: "Log for #{cname}")
577         assert_includes(log_link[:href], "/jobs/#{cspec["job"]["uuid"]}#Log")
578       end
579       assert_no_selector "#event_log_div"
580     end
581   end
582
583   test "job logs linked for failed pipeline" do
584     pi = api_fixture("pipeline_instances", "failed_pipeline_with_two_jobs")
585     visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
586     find(:xpath, "//a[@href='#Log']").click
587     within "#Log" do
588       assert_text "Log for previous"
589       pi["components"].each do |cname, cspec|
590         log_link = find("a", text: "Log for #{cname}")
591         assert_includes(log_link[:href], "/jobs/#{cspec["job"]["uuid"]}#Log")
592       end
593       assert_no_selector "#event_log_div"
594     end
595   end
596 end