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