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