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