18995: Merge branch 'main' into 18995-code-cleanup-5
[arvados.git] / apps / workbench / test / integration / pipeline_instances_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'integration_helper'
6
7 class PipelineInstancesTest < ActionDispatch::IntegrationTest
8   setup do
9     need_javascript
10   end
11
12   def parse_browser_timestamp t
13     # Timestamps are displayed in the browser's time zone (which can
14     # differ from ours) and they come from toLocaleTimeString (which
15     # means they don't necessarily tell us which time zone they're
16     # using). In order to make sense of them, we need to ask the
17     # browser to parse them and generate a timestamp that can be
18     # parsed reliably.
19     #
20     # Note: Even with all this help, phantomjs seem to behave badly
21     # when parsing timestamps on the other side of a DST transition.
22     # See skipped tests below.
23
24     # In some locales (e.g., en_CA.UTF-8) Firefox can't parse what its
25     # own toLocaleString() puts out.
26     t.sub!(/(\d\d\d\d)-(\d\d)-(\d\d)/, '\2/\3/\1')
27
28     if /(\d+:\d+ [AP]M) (\d+\/\d+\/\d+)/ =~ t
29       # Currently dates.js renders timestamps as
30       # '{t.toLocaleTimeString()} {t.toLocaleDateString()}' which even
31       # en_US browsers can't make sense of. First we need to flip it
32       # around so it looks like what toLocaleString() would have made.
33       t = $~[2] + ', ' + $~[1]
34     end
35
36     utc = page.evaluate_script("new Date('#{t}').toUTCString()")
37     DateTime.parse(utc).to_time
38   end
39
40   test 'view pipeline with job and see graph' do
41     visit page_with_token('active_trustedclient', '/pipeline_instances')
42     assert page.has_text? 'pipeline_with_job'
43
44     find('a', text: 'pipeline_with_job').click
45
46     # since the pipeline component has a job, expect to see the graph
47     assert page.has_text? 'Graph'
48     click_link 'Graph'
49     page.assert_selector "#provenance_graph"
50   end
51
52   test "JSON popup available for strange components" do
53     uuid = api_fixture("pipeline_instances")["components_is_jobspec"]["uuid"]
54     visit page_with_token("active", "/pipeline_instances/#{uuid}")
55     click_on "Components"
56     assert(page.has_no_text?("script_parameters"),
57            "components JSON visible without popup")
58     click_on "Show components JSON"
59     assert(page.has_text?("script_parameters"),
60            "components JSON not found")
61   end
62
63   def create_pipeline_from(template_name, project_name="Home")
64     # Visit the named pipeline template and create a pipeline instance from it.
65     # The instance will be created under the named project.
66     template_uuid = api_fixture("pipeline_templates", template_name, "uuid")
67     visit page_with_token("active", "/pipeline_templates/#{template_uuid}")
68     click_on "Run this pipeline"
69     within(".modal-dialog") do # FIXME: source of 3 test errors
70       # Set project for the new pipeline instance
71       find(".selectable", text: project_name).click
72       click_on "Choose"
73     end
74     assert(has_text?("This pipeline was created from the template"),
75            "did not land on pipeline instance page")
76   end
77
78   [
79     ['user1_with_load', 'zzzzz-d1hrv-10pipelines0001', 0], # run time 0 minutes
80     ['user1_with_load', 'zzzzz-d1hrv-10pipelines0010', 17*60*60 + 51*60], # run time 17 hours and 51 minutes
81     ['active', 'zzzzz-d1hrv-runningpipeline', nil], # state = running
82   ].each do |user, uuid, run_time|
83     test "pipeline start and finish time display for #{uuid}" do
84       need_selenium 'to parse timestamps correctly across DST boundaries'
85       visit page_with_token(user, "/pipeline_instances/#{uuid}")
86
87       regexp = "This pipeline started at (.+?)\\. "
88       if run_time
89         regexp += "It failed after (.+?) at (.+?)\\. Check the Log"
90       else
91         regexp += "It has been active for \\d"
92       end
93       assert_match /#{regexp}/, page.text
94
95       return if !run_time
96
97       # match again to capture (.*)
98       _, started, duration, finished = *(/#{regexp}/.match(page.text))
99       assert_equal(
100         run_time,
101         parse_browser_timestamp(finished) - parse_browser_timestamp(started),
102         "expected: #{run_time}, got: started #{started}, finished #{finished}, duration #{duration}")
103     end
104   end
105
106   [
107     ['fuse', nil, 2, 20],                           # has 2 as of 11-07-2014
108     ['user1_with_load', '000025pipelines', 25, 25], # owned_by the project zzzzz-j7d0g-000025pipelines, two pages
109     ['admin', 'pipeline_20', 1, 1],
110     ['active', 'no such match', 0, 0],
111   ].each do |user, search_filter, expected_min, expected_max|
112     test "scroll pipeline instances page for #{user} with search filter #{search_filter}
113           and expect #{expected_min} <= found_items <= #{expected_max}" do
114       visit page_with_token(user, "/pipeline_instances")
115
116       if search_filter
117         find('.recent-pipeline-instances-filterable-control').set(search_filter)
118         # Wait for 250ms debounce timer (see filterable.js)
119         sleep 0.350
120         wait_for_ajax
121       end
122
123       page_scrolls = expected_max/20 + 2    # scroll num_pages+2 times to test scrolling is disabled when it should be
124       within('.arv-recent-pipeline-instances') do
125         (0..page_scrolls).each do |i|
126           page.driver.scroll_to 0, 999000
127           begin
128             wait_for_ajax
129           rescue
130           end
131         end
132       end
133
134       # Verify that expected number of pipeline instances are found
135       found_items = page.all('tr[data-kind="arvados#pipelineInstance"]')
136       found_count = found_items.count
137       if expected_min == expected_max
138         assert_equal(true, found_count == expected_min,
139           "Not found expected number of items. Expected #{expected_min} and found #{found_count}")
140         assert page.has_no_text? 'request failed'
141       else
142         assert_equal(true, found_count>=expected_min,
143           "Found too few items. Expected at least #{expected_min} and found #{found_count}")
144         assert_equal(true, found_count<=expected_max,
145           "Found too many items. Expected at most #{expected_max} and found #{found_count}")
146       end
147     end
148   end
149
150   test 'render job run time when job record is inaccessible' do
151     pi = api_fixture('pipeline_instances', 'has_component_with_completed_jobs')
152     visit page_with_token 'active', '/pipeline_instances/' + pi['uuid']
153     assert_text 'Queued for '
154   end
155
156   test "job logs linked for running pipeline" do
157     pi = api_fixture("pipeline_instances", "running_pipeline_with_complete_job")
158     visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
159     find(:xpath, "//a[@href='#Log']").click
160     within "#Log" do
161       assert_text "Log for previous"
162       log_link = find("a", text: "Log for previous")
163       assert_includes(log_link[:href],
164                       "/jobs/#{pi["components"]["previous"]["job"]["uuid"]}#Log")
165       assert_selector "#event_log_div"
166     end
167   end
168
169   test "job logs linked for complete pipeline" do
170     pi = api_fixture("pipeline_instances", "complete_pipeline_with_two_jobs")
171     visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
172     find(:xpath, "//a[@href='#Log']").click
173     within "#Log" do
174       assert_text "Log for previous"
175       pi["components"].each do |cname, cspec|
176         log_link = find("a", text: "Log for #{cname}")
177         assert_includes(log_link[:href], "/jobs/#{cspec["job"]["uuid"]}#Log")
178       end
179       assert_no_selector "#event_log_div"
180     end
181   end
182
183   test "job logs linked for failed pipeline" do
184     pi = api_fixture("pipeline_instances", "failed_pipeline_with_two_jobs")
185     visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
186     find(:xpath, "//a[@href='#Log']").click
187     within "#Log" do
188       assert_text "Log for previous"
189       pi["components"].each do |cname, cspec|
190         log_link = find("a", text: "Log for #{cname}")
191         assert_includes(log_link[:href], "/jobs/#{cspec["job"]["uuid"]}#Log")
192       end
193       assert_no_selector "#event_log_div"
194     end
195   end
196 end