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