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