Fix typo.
[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   test 'Create and run a pipeline' do
9     visit page_with_token('active_trustedclient')
10
11     visit '/pipeline_templates'
12     within('tr', text: 'Two Part Pipeline Template') do
13       find('a,button', text: 'Run').click
14     end
15
16     # project chooser
17     within('.modal-dialog') do
18       find('.selectable', text: 'A Project').click
19       find('button', text: 'Choose').click
20     end
21
22     # This pipeline needs input. So, Run should be disabled
23     page.assert_selector 'a.disabled,button.disabled', text: 'Run'
24
25     instance_page = current_path
26
27     # Add this collection to the project
28     visit '/projects'
29     find("#projects-menu").click
30     find('.dropdown-menu a,button', text: 'A Project').click
31     find('.btn', text: 'Add data').click
32     find('.dropdown-menu a,button', text: 'Copy data from another project').click
33     within('.modal-dialog') do
34       wait_for_ajax
35       first('span', text: 'foo_tag').click
36       find('.btn', text: 'Copy').click
37     end
38     using_wait_time(Capybara.default_wait_time * 3) do
39       wait_for_ajax
40     end
41
42     click_link 'Jobs and pipelines'
43     find('tr[data-kind="arvados#pipelineInstance"]', text: '(none)').
44       find('a', text: 'Show').
45       click
46
47     assert find('p', text: 'Provide a value')
48
49     find('div.form-group', text: 'Foo/bar pair').
50       find('.btn', text: 'Choose').
51       click
52
53     within('.modal-dialog') do
54       assert(has_text?("Foo/bar pair"),
55              "pipeline input picker missing name of input")
56       wait_for_ajax
57       first('span', text: 'foo_tag').click
58       find('button', text: 'OK').click
59     end
60     wait_for_ajax
61
62     # The input, after being specified, should still be displayed (#3382)
63     assert find('div.form-group', text: 'Foo/bar pair')
64
65     # The input, after being specified, should still be editable (#3382)
66     find('div.form-group', text: 'Foo/bar pair').
67       find('.btn', text: 'Choose').click
68
69     within('.modal-dialog') do
70       assert(has_text?("Foo/bar pair"),
71              "pipeline input picker missing name of input")
72       wait_for_ajax
73       first('span', text: 'foo_tag').click
74       find('button', text: 'OK').click
75     end
76
77     # For good measure, check one last time that the input, after being specified twice, is still be displayed (#3382)
78     assert find('div.form-group', text: 'Foo/bar pair')
79
80     # Ensure that the collection's portable_data_hash, uuid and name
81     # are saved in the desired places. (#4015)
82
83     # foo_collection_in_aproject is the collection tagged with foo_tag.
84     collection = api_fixture('collections', 'foo_collection_in_aproject')
85     click_link 'Advanced'
86     click_link 'API response'
87     api_response = JSON.parse(find('div#advanced_api_response pre').text)
88     input_params = api_response['components']['part-one']['script_parameters']['input']
89     assert_equal input_params['value'], collection['portable_data_hash']
90     assert_equal input_params['selection_name'], collection['name']
91     assert_equal input_params['selection_uuid'], collection['uuid']
92
93     # "Run" button is now enabled
94     page.assert_no_selector 'a.disabled,button.disabled', text: 'Run'
95
96     first('a,button', text: 'Run').click
97
98     # Pipeline is running. We have a "Pause" button instead now.
99     page.assert_selector 'a,button', text: 'Pause'
100     find('a,button', text: 'Pause').click
101
102     # Pipeline is stopped. It should now be in paused state and Runnable again.
103     assert page.has_text? 'Paused'
104     page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
105     page.assert_selector 'a,button', text: 'Re-run with latest'
106     page.assert_selector 'a,button', text: 'Re-run options'
107
108     # Since it is test env, no jobs are created to run. So, graph not visible
109     assert_not page.has_text? 'Graph'
110   end
111
112   # Create a pipeline instance from within a project and run
113   test 'Create pipeline inside a project and run' do
114     visit page_with_token('active_trustedclient')
115
116     # Add this collection to the project using collections menu from top nav
117     visit '/projects'
118     find("#projects-menu").click
119     find('.dropdown-menu a,button', text: 'A Project').click
120     find('.btn', text: 'Add data').click
121     find('.dropdown-menu a,button', text: 'Copy data from another project').click
122     within('.modal-dialog') do
123       wait_for_ajax
124       first('span', text: 'foo_tag').click
125       find('.btn', text: 'Copy').click
126     end
127     using_wait_time(Capybara.default_wait_time * 3) do
128       wait_for_ajax
129     end
130
131     create_and_run_pipeline_in_aproject true, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false
132   end
133
134   # Create a pipeline instance from outside of a project
135   test 'Run a pipeline from dashboard' do
136     visit page_with_token('active_trustedclient')
137     create_and_run_pipeline_in_aproject false, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false
138   end
139
140   test 'view pipeline with job and see graph' do
141     visit page_with_token('active_trustedclient')
142
143     visit '/pipeline_instances'
144     assert page.has_text? 'pipeline_with_job'
145
146     find('a', text: 'pipeline_with_job').click
147
148     # since the pipeline component has a job, expect to see the graph
149     assert page.has_text? 'Graph'
150     click_link 'Graph'
151     page.assert_selector "#provenance_graph"
152   end
153
154   test 'pipeline description' do
155     visit page_with_token('active_trustedclient')
156
157     visit '/pipeline_instances'
158     assert page.has_text? 'pipeline_with_job'
159
160     find('a', text: 'pipeline_with_job').click
161
162     within('.arv-description-as-subtitle') do
163       find('.fa-pencil').click
164       find('.editable-input textarea').set('*Textile description for pipeline instance*')
165       find('.editable-submit').click
166     end
167     wait_for_ajax
168
169     # verify description
170     assert page.has_no_text? '*Textile description for pipeline instance*'
171     assert page.has_text? 'Textile description for pipeline instance'
172   end
173
174   test "JSON popup available for strange components" do
175     uuid = api_fixture("pipeline_instances")["components_is_jobspec"]["uuid"]
176     visit page_with_token("active", "/pipeline_instances/#{uuid}")
177     click_on "Components"
178     assert(page.has_no_text?("script_parameters"),
179            "components JSON visible without popup")
180     click_on "Show components JSON"
181     assert(page.has_text?("script_parameters"),
182            "components JSON not found")
183   end
184
185   def create_pipeline_from(template_name, project_name="Home")
186     # Visit the named pipeline template and create a pipeline instance from it.
187     # The instance will be created under the named project.
188     template_uuid = api_fixture("pipeline_templates", template_name, "uuid")
189     visit page_with_token("active", "/pipeline_templates/#{template_uuid}")
190     click_on "Run this pipeline"
191     within(".modal-dialog") do
192       # Set project for the new pipeline instance
193       find(".selectable", text: project_name).click
194       click_on "Choose"
195     end
196     assert(has_text?("This pipeline was created from the template"),
197            "did not land on pipeline instance page")
198   end
199
200   PROJECT_WITH_SEARCH_COLLECTION = "A Subproject"
201   def check_parameter_search(proj_name)
202     create_pipeline_from("parameter_with_search", proj_name)
203     search_text = api_fixture("pipeline_templates", "parameter_with_search",
204                               "components", "with-search",
205                               "script_parameters", "input", "search_for")
206     first("a.btn,button", text: "Choose").click
207     within(".modal-body") do
208       if (proj_name != PROJECT_WITH_SEARCH_COLLECTION)
209         # Switch finder modal to Subproject to find the Collection.
210         click_on proj_name
211         click_on PROJECT_WITH_SEARCH_COLLECTION
212       end
213       assert_equal(search_text, first("input").value,
214                    "parameter search not preseeded")
215       assert(has_text?(api_fixture("collections")["baz_collection_name_in_asubproject"]["name"]),
216              "baz Collection not in preseeded search results")
217     end
218   end
219
220   test "Workbench respects search_for parameter in templates" do
221     check_parameter_search(PROJECT_WITH_SEARCH_COLLECTION)
222   end
223
224   test "Workbench preserves search_for parameter after project switch" do
225     check_parameter_search("A Project")
226   end
227
228   test "enter a float for a number pipeline input" do
229     # Poltergeist either does not support the HTML 5 <input
230     # type="number">, or interferes with the associated X-Editable
231     # validation code.  If the input field has type=number (forcing an
232     # integer), this test will yield a false positive under
233     # Poltergeist.  --Brett, 2015-02-05
234     need_selenium "for strict X-Editable input validation"
235     create_pipeline_from("template_with_dataclass_number")
236     INPUT_SELECTOR =
237       ".editable[data-name='[components][work][script_parameters][input][value]']"
238     find(INPUT_SELECTOR).click
239     find(".editable-input input").set("12.34")
240     find("#editable-submit").click
241     assert_no_selector(".editable-popup")
242     assert_selector(INPUT_SELECTOR, text: "12.34")
243   end
244
245   [
246     [true, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false],
247     [false, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false],
248     [true, 'Two Part Template with dataclass File', 'foo_collection_in_aproject', true],
249     [false, 'Two Part Template with dataclass File', 'foo_collection_in_aproject', true],
250     [true, 'Two Part Pipeline Template', 'collection_with_no_name_in_aproject', false],
251   ].each do |in_aproject, template_name, collection, choose_file|
252     test "Run pipeline instance in #{in_aproject} with #{template_name} with #{collection} file #{choose_file}" do
253       if in_aproject
254         visit page_with_token 'active', \
255         '/projects/'+api_fixture('groups')['aproject']['uuid']
256       else
257         visit page_with_token 'active', '/'
258       end
259
260       # need bigger modal size when choosing a file from collection
261       if Capybara.current_driver == :selenium
262         Capybara.current_session.driver.browser.manage.window.resize_to(1200, 800)
263       end
264
265       create_and_run_pipeline_in_aproject in_aproject, template_name, collection, choose_file
266       instance_path = current_path
267
268       # Pause the pipeline
269       find('a,button', text: 'Pause').click
270       assert page.has_text? 'Paused'
271       page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
272       page.assert_selector 'a,button', text: 'Re-run with latest'
273       page.assert_selector 'a,button', text: 'Re-run options'
274
275       # Verify that the newly created instance is created in the right project.
276       assert page.has_text? 'Home'
277       if in_aproject
278         assert page.has_text? 'A Project'
279       else
280         assert page.has_no_text? 'A Project'
281       end
282     end
283   end
284
285   [
286     ['active', false, false, false],
287     ['active', false, false, true],
288     ['active', true, false, false],
289     ['active', true, true, false],
290     ['active', true, false, true],
291     ['active', true, true, true],
292     ['project_viewer', false, false, true],
293     ['project_viewer', true, true, true],
294   ].each do |user, with_options, choose_options, in_aproject|
295     test "Rerun pipeline instance as #{user} using options #{with_options} #{choose_options} in #{in_aproject}" do
296       if in_aproject
297         path = '/pipeline_instances/'+api_fixture('pipeline_instances')['pipeline_owned_by_active_in_aproject']['uuid']
298       else
299         path = '/pipeline_instances/'+api_fixture('pipeline_instances')['pipeline_owned_by_active_in_home']['uuid']
300       end
301
302       visit page_with_token(user, path)
303
304       page.assert_selector 'a,button', text: 'Re-run with latest'
305       page.assert_selector 'a,button', text: 'Re-run options'
306
307       if user == 'project_viewer' && in_aproject
308         assert page.has_text? 'A Project'
309       end
310
311       # Now re-run the pipeline
312       if with_options
313         assert_triggers_dom_event 'shown.bs.modal' do
314           find('a,button', text: 'Re-run options').click
315         end
316         within('.modal-dialog') do
317           page.assert_selector 'a,button', text: 'Copy and edit inputs'
318           page.assert_selector 'a,button', text: 'Run now'
319           if choose_options
320             find('button', text: 'Copy and edit inputs').click
321           else
322             find('button', text: 'Run now').click
323           end
324         end
325       else
326         find('a,button', text: 'Re-run with latest').click
327       end
328
329       # Verify that the newly created instance is created in the right
330       # project. In case of project_viewer user, since the user cannot
331       # write to the project, the pipeline should have been created in
332       # the user's Home project.
333       assert_not_equal path, current_path, 'Rerun instance path expected to be different'
334       assert_text 'Home'
335       if in_aproject && (user != 'project_viewer')
336         assert_text 'A Project'
337       else
338         assert_no_text 'A Project'
339       end
340     end
341   end
342
343   # Create and run a pipeline for 'Two Part Pipeline Template' in 'A Project'
344   def create_and_run_pipeline_in_aproject in_aproject, template_name, collection_fixture, choose_file=false
345     # collection in aproject to be used as input
346     collection = api_fixture('collections', collection_fixture)
347
348     # create a pipeline instance
349     find('.btn', text: 'Run a pipeline').click
350     within('.modal-dialog') do
351       find('.selectable', text: template_name).click
352       find('.btn', text: 'Next: choose inputs').click
353     end
354
355     assert find('p', text: 'Provide a value')
356
357     find('div.form-group', text: 'Foo/bar pair').
358       find('.btn', text: 'Choose').
359       click
360
361     within('.modal-dialog') do
362       if in_aproject
363         assert_selector 'button.dropdown-toggle', text: 'A Project'
364         wait_for_ajax
365       else
366         assert_selector 'button.dropdown-toggle', text: 'Home'
367         wait_for_ajax
368         click_button "Home"
369         click_link "A Project"
370         wait_for_ajax
371       end
372
373       if collection_fixture == 'foo_collection_in_aproject'
374         first('span', text: 'foo_tag').click
375       elsif collection['name']
376         first('span', text: "#{collection['name']}").click
377       else
378         collection_uuid = collection['uuid']
379         find("div[data-object-uuid=#{collection_uuid}]").click
380       end
381
382       if choose_file
383         wait_for_ajax
384         find('.preview-selectable', text: 'foo').click
385       end
386       find('button', text: 'OK').click
387     end
388
389     # The input, after being specified, should still be displayed (#3382)
390     assert find('div.form-group', text: 'Foo/bar pair')
391
392     # Ensure that the collection's portable_data_hash, uuid and name
393     # are saved in the desired places. (#4015)
394     click_link 'Advanced'
395     click_link 'API response'
396
397     api_response = JSON.parse(find('div#advanced_api_response pre').text)
398     input_params = api_response['components']['part-one']['script_parameters']['input']
399     assert_equal(input_params['selection_uuid'], collection['uuid'], "Not found expected input param uuid")
400     if choose_file
401       assert_equal(input_params['value'], collection['portable_data_hash']+'/foo', "Not found expected input file param value")
402       assert_equal(input_params['selection_name'], collection['name']+'/foo', "Not found expected input file param name")
403     else
404       assert_equal(input_params['value'], collection['portable_data_hash'], "Not found expected input param value")
405       assert_equal(input_params['selection_name'], collection['name'], "Not found expected input selection name")
406     end
407
408     # "Run" button present and enabled
409     page.assert_no_selector 'a.disabled,button.disabled', text: 'Run'
410     first('a,button', text: 'Run').click
411
412     # Pipeline is running. We have a "Pause" button instead now.
413     page.assert_no_selector 'a,button', text: 'Run'
414     page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
415     page.assert_selector 'a,button', text: 'Pause'
416
417     # Since it is test env, no jobs are created to run. So, graph not visible
418     assert_not page.has_text? 'Graph'
419   end
420
421   [
422     [1, 0], # run time 0 minutes
423     [10, 17*60*60 + 51*60], # run time 17 hours and 51 minutes
424   ].each do |index, run_time|
425     test "pipeline start and finish time display #{index}" do
426       visit page_with_token("user1_with_load", "/pipeline_instances/zzzzz-d1hrv-10pipelines0#{index.to_s.rjust(3, '0')}")
427
428       assert page.has_text? 'This pipeline started at'
429       page_text = page.text
430
431       match = /This pipeline started at (.*)\. It failed after (.*) seconds at (.*)\. Check the Log/.match page_text
432       assert_not_nil(match, 'Did not find text - This pipeline started at . . . ')
433
434       start_at = match[1]
435       finished_at = match[3]
436       assert_not_nil(start_at, 'Did not find start_at time')
437       assert_not_nil(finished_at, 'Did not find finished_at time')
438
439       # start and finished time display is of the format '2:20 PM 10/20/2014'
440       start_time = DateTime.strptime(start_at, '%H:%M %p %m/%d/%Y').to_time
441       finished_time = DateTime.strptime(finished_at, '%H:%M %p %m/%d/%Y').to_time
442       assert_equal(run_time, finished_time-start_time,
443         "Time difference did not match for start_at #{start_at}, finished_at #{finished_at}, ran_for #{match[2]}")
444     end
445   end
446
447   [
448     ['fuse', nil, 2, 20],                           # has 2 as of 11-07-2014
449     ['fuse', 'FUSE project', 1, 1],                 # 1 with this name
450     ['user1_with_load', nil, 30, 100],              # has 37 as of 11-07-2014
451     ['user1_with_load', 'pipeline_10', 2, 2],       # 2 with this name
452     ['user1_with_load', '000010pipelines', 10, 10], # owned_by the project zzzzz-j7d0g-000010pipelines
453     ['user1_with_load', '000025pipelines', 25, 25], # owned_by the project zzzzz-j7d0g-000025pipelines, two pages
454     ['admin', nil, 40, 200],
455     ['admin', 'FUSE project', 1, 1],
456     ['admin', 'pipeline_10', 2, 2],
457     ['active', 'containing at least two', 2, 100],
458     ['active', nil, 10, 100],
459     ['active', 'no such match', 0, 0],
460   ].each do |user, search_filter, expected_min, expected_max|
461     test "scroll pipeline instances page for #{user} with search filter #{search_filter}
462           and expect #{expected_min} <= found_items <= #{expected_max}" do
463       visit page_with_token(user, "/pipeline_instances")
464
465       if search_filter
466         find('.recent-pipeline-instances-filterable-control').set(search_filter)
467         # Wait for 250ms debounce timer (see filterable.js)
468         sleep 0.350
469         wait_for_ajax
470       end
471
472       page_scrolls = expected_max/20 + 2    # scroll num_pages+2 times to test scrolling is disabled when it should be
473       within('.arv-recent-pipeline-instances') do
474         (0..page_scrolls).each do |i|
475           page.driver.scroll_to 0, 999000
476           begin
477             wait_for_ajax
478           rescue
479           end
480         end
481       end
482
483       # Verify that expected number of pipeline instances are found
484       found_items = page.all('tr[data-kind="arvados#pipelineInstance"]')
485       found_count = found_items.count
486       if expected_min == expected_max
487         assert_equal(true, found_count == expected_min,
488           "Not found expected number of items. Expected #{expected_min} and found #{found_count}")
489         assert page.has_no_text? 'request failed'
490       else
491         assert_equal(true, found_count>=expected_min,
492           "Found too few items. Expected at least #{expected_min} and found #{found_count}")
493         assert_equal(true, found_count<=expected_max,
494           "Found too many items. Expected at most #{expected_max} and found #{found_count}")
495       end
496     end
497   end
498 end