Merge remote-tracking branch 'origin/master' into 4031-fix-graph-connections
[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     # Go over to the collections page and select something
33     visit '/collections'
34     within('tr', text: 'GNU_General_Public_License') do
35       find('input[type=checkbox]').click
36     end
37     find('#persistent-selection-count').click
38
39     # Add this collection to the project
40     visit '/projects'
41     find("#projects-menu").click
42     find('.dropdown-menu a,button', text: 'A Project').click
43     find('.btn', text: 'Add data').click
44     within('.modal-dialog') do
45       wait_for_ajax
46       first('span', text: 'foo_tag').click
47       find('.btn', text: 'Add').click
48     end
49     using_wait_time(Capybara.default_wait_time * 3) do
50       wait_for_ajax
51     end
52
53     click_link 'Jobs and pipelines'
54     find('tr[data-kind="arvados#pipelineInstance"]', text: '(none)').
55       find('a', text: 'Show').
56       click
57
58     assert find('p', text: 'Provide a value')
59
60     find('div.form-group', text: 'Foo/bar pair').
61       find('.btn', text: 'Choose').
62       click
63
64     within('.modal-dialog') do
65       assert(has_text?("Foo/bar pair"),
66              "pipeline input picker missing name of input")
67       wait_for_ajax
68       first('span', text: 'foo_tag').click
69       find('button', text: 'OK').click
70     end
71     wait_for_ajax
72
73     # Ensure that the collection's portable_data_hash, uuid and name
74     # are saved in the desired places. (#4015)
75
76     # foo_collection_in_aproject is the collection tagged with foo_tag.
77     col = api_fixture('collections', 'foo_collection_in_aproject')
78     click_link 'Advanced'
79     click_link 'API response'
80     api_response = JSON.parse(find('div#advanced_api_response pre').text)
81     input_params = api_response['components']['part-one']['script_parameters']['input']
82     assert_equal input_params['value'], col['portable_data_hash']
83     assert_equal input_params['selection_name'], col['name']
84     assert_equal input_params['selection_uuid'], col['uuid']
85
86     # "Run" button is now enabled
87     page.assert_no_selector 'a.disabled,button.disabled', text: 'Run'
88
89     first('a,button', text: 'Run').click
90
91     # Pipeline is running. We have a "Pause" button instead now.
92     page.assert_selector 'a,button', text: 'Pause'
93     find('a,button', text: 'Pause').click
94
95     # Pipeline is stopped. It should now be in paused state and Runnable again.
96     assert page.has_text? 'Paused'
97     page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
98     page.assert_selector 'a,button', text: 'Re-run with latest'
99     page.assert_selector 'a,button', text: 'Re-run options'
100
101     # Since it is test env, no jobs are created to run. So, graph not visible
102     assert_not page.has_text? 'Graph'
103   end
104
105   # Create a pipeline instance from within a project and run
106   test 'Create pipeline inside a project and run' do
107     visit page_with_token('active_trustedclient')
108
109     # Go over to the collections page and select something
110     visit '/collections'
111     within('tr', text: 'GNU_General_Public_License') do
112       find('input[type=checkbox]').click
113     end
114     find('#persistent-selection-count').click
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     within('.modal-dialog') do
122       wait_for_ajax
123       first('span', text: 'foo_tag').click
124       find('.btn', text: 'Add').click
125     end
126     using_wait_time(Capybara.default_wait_time * 3) do
127       wait_for_ajax
128     end
129
130     create_and_run_pipeline_in_aproject true
131   end
132
133   # Create a pipeline instance from outside of a project
134   test 'Run a pipeline from dashboard' do
135     visit page_with_token('active_trustedclient')
136     create_and_run_pipeline_in_aproject false
137   end
138
139   test 'view pipeline with job and see graph' 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     # since the pipeline component has a job, expect to see the graph
148     assert page.has_text? 'Graph'
149     click_link 'Graph'
150     page.assert_selector "#provenance_graph"
151   end
152
153   test 'pipeline description' do
154     visit page_with_token('active_trustedclient')
155
156     visit '/pipeline_instances'
157     assert page.has_text? 'pipeline_with_job'
158
159     find('a', text: 'pipeline_with_job').click
160
161     within('.arv-description-as-subtitle') do
162       find('.fa-pencil').click
163       find('.editable-input textarea').set('*Textile description for pipeline instance*')
164       find('.editable-submit').click
165     end
166     wait_for_ajax
167
168     # verify description
169     assert page.has_no_text? '*Textile description for pipeline instance*'
170     assert page.has_text? 'Textile description for pipeline instance'
171   end
172
173   test "JSON popup available for strange components" do
174     uuid = api_fixture("pipeline_instances")["components_is_jobspec"]["uuid"]
175     visit page_with_token("active", "/pipeline_instances/#{uuid}")
176     click_on "Components"
177     assert(page.has_no_text?("script_parameters"),
178            "components JSON visible without popup")
179     click_on "Show components JSON"
180     assert(page.has_text?("script_parameters"),
181            "components JSON not found")
182   end
183
184   PROJECT_WITH_SEARCH_COLLECTION = "A Subproject"
185   def check_parameter_search(proj_name)
186     template = api_fixture("pipeline_templates")["parameter_with_search"]
187     search_text = template["components"]["with-search"]["script_parameters"]["input"]["search_for"]
188     visit page_with_token("active", "/pipeline_templates/#{template['uuid']}")
189     click_on "Run this pipeline"
190     within(".modal-dialog") do  # Set project for the new pipeline instance
191       find(".selectable", text: proj_name).click
192       click_on "Choose"
193     end
194     assert(has_text?("This pipeline was created from the template"), "did not land on pipeline instance page")
195     first("a.btn,button", text: "Choose").click
196     within(".modal-body") do
197       if (proj_name != PROJECT_WITH_SEARCH_COLLECTION)
198         # Switch finder modal to Subproject to find the Collection.
199         click_on proj_name
200         click_on PROJECT_WITH_SEARCH_COLLECTION
201       end
202       assert_equal(search_text, first("input").value,
203                    "parameter search not preseeded")
204       assert(has_text?(api_fixture("collections")["baz_collection_name_in_asubproject"]["name"]),
205              "baz Collection not in preseeded search results")
206     end
207   end
208
209   test "Workbench respects search_for parameter in templates" do
210     check_parameter_search(PROJECT_WITH_SEARCH_COLLECTION)
211   end
212
213   test "Workbench preserves search_for parameter after project switch" do
214     check_parameter_search("A Project")
215   end
216
217   [
218     ['active', false, false, false],
219     ['active', false, false, true],
220     ['active', true, false, false],
221     ['active', true, true, false],
222     ['active', true, false, true],
223     ['active', true, true, true],
224     ['project_viewer', false, false, true],
225     ['project_viewer', true, false, true],
226     ['project_viewer', true, true, true],
227   ].each do |user, with_options, choose_options, in_aproject|
228     test "Rerun pipeline instance as #{user} using options #{with_options} #{choose_options} in #{in_aproject}" do
229       visit page_with_token('active')
230
231       if in_aproject
232         find("#projects-menu").click
233         find('.dropdown-menu a,button', text: 'A Project').click
234       end
235
236       create_and_run_pipeline_in_aproject in_aproject
237       instance_path = current_path
238
239       # Pause the pipeline
240       find('a,button', text: 'Pause').click
241       assert page.has_text? 'Paused'
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
246       # Pipeline can be re-run now. Access it as the specified user, and re-run
247       if user == 'project_viewer'
248         visit page_with_token(user, instance_path)
249         assert page.has_text? 'A Project'
250         page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
251         page.assert_selector 'a,button', text: 'Re-run with latest'
252         page.assert_selector 'a,button', text: 'Re-run options'
253       end
254
255       # Now re-run the pipeline
256       if with_options
257         find('a,button', text: 'Re-run options').click
258         within('.modal-dialog') do
259           page.assert_selector 'a,button', text: 'Copy and edit inputs'
260           page.assert_selector 'a,button', text: 'Run now'
261           if choose_options
262             find('button', text: 'Copy and edit inputs').click
263           else
264             find('button', text: 'Run now').click
265           end
266         end
267       else
268         find('a,button', text: 'Re-run with latest').click
269       end
270
271       # Verify that the newly created instance is created in the right project.
272       # In case of project_viewer user, since the use cannot write to the project,
273       # the pipeline should have been created in the user's Home project.
274       rerun_instance_path = current_path
275       assert_not_equal instance_path, rerun_instance_path, 'Rerun instance path expected to be different'
276       assert page.has_text? 'Home'
277       if in_aproject && (user != 'project_viewer')
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   # Create and run a pipeline for 'Two Part Pipeline Template' in 'A Project'
286   def create_and_run_pipeline_in_aproject in_aproject
287     # create a pipeline instance
288     find('.btn', text: 'Run a pipeline').click
289     within('.modal-dialog') do
290       find('.selectable', text: 'Two Part Pipeline Template').click
291       find('.btn', text: 'Next: choose inputs').click
292     end
293
294     assert find('p', text: 'Provide a value')
295
296     find('div.form-group', text: 'Foo/bar pair').
297       find('.btn', text: 'Choose').
298       click
299
300     within('.modal-dialog') do
301       if in_aproject
302         assert_selector 'button.dropdown-toggle', text: 'A Project'
303         wait_for_ajax
304       else
305         assert_selector 'button.dropdown-toggle', text: 'Home'
306         wait_for_ajax
307         click_button "Home"
308         click_link "A Project"
309         wait_for_ajax
310       end
311       first('span', text: 'foo_tag').click
312       find('button', text: 'OK').click
313     end
314     wait_for_ajax
315
316     # Ensure that the collection's portable_data_hash, uuid and name
317     # are saved in the desired places. (#4015)
318
319     # foo_collection_in_aproject is the collection tagged with foo_tag.
320     col = api_fixture('collections', 'foo_collection_in_aproject')
321     click_link 'Advanced'
322     click_link 'API response'
323     api_response = JSON.parse(find('div#advanced_api_response pre').text)
324     input_params = api_response['components']['part-one']['script_parameters']['input']
325     assert_equal input_params['value'], col['portable_data_hash']
326     assert_equal input_params['selection_name'], col['name']
327     assert_equal input_params['selection_uuid'], col['uuid']
328
329     # "Run" button present and enabled
330     page.assert_no_selector 'a.disabled,button.disabled', text: 'Run'
331     first('a,button', text: 'Run').click
332
333     # Pipeline is running. We have a "Pause" button instead now.
334     page.assert_no_selector 'a,button', text: 'Run'
335     page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
336     page.assert_selector 'a,button', text: 'Pause'
337
338     # Since it is test env, no jobs are created to run. So, graph not visible
339     assert_not page.has_text? 'Graph'
340   end
341
342   [
343     [0, 0], # run time 0 minutes
344     [9, 17*60*60 + 51*60], # run time 17 hours and 51 minutes
345   ].each do |index, run_time|
346     test "pipeline start and finish time display #{index}" do
347       visit page_with_token("user1_with_load", "/pipeline_instances/zzzzz-d1hrv-10pipelines0#{index.to_s.rjust(3, '0')}")
348
349       assert page.has_text? 'This pipeline started at'
350       page_text = page.text
351
352       match = /This pipeline started at (.*)\. It failed after (.*) seconds at (.*)\. Check the Log/.match page_text
353       assert_not_nil(match, 'Did not find text - This pipeline started at . . . ')
354
355       start_at = match[1]
356       finished_at = match[3]
357       assert_not_nil(start_at, 'Did not find start_at time')
358       assert_not_nil(finished_at, 'Did not find finished_at time')
359
360       # start and finished time display is of the format '2:20 PM 10/20/2014'
361       start_time = DateTime.strptime(start_at, '%H:%M %p %m/%d/%Y').to_time
362       finished_time = DateTime.strptime(finished_at, '%H:%M %p %m/%d/%Y').to_time
363       assert_equal(run_time, finished_time-start_time,
364         "Time difference did not match for start_at #{start_at}, finished_at #{finished_at}, ran_for #{match[2]}")
365     end
366   end
367 end