7753: Add a Log link to pipeline_instance component panel
[arvados.git] / apps / workbench / test / integration / pipeline_instances_test.rb
index 9f4ce692e5c0b28c8661a5061af4d209140ec228..612493adf0f6f2ec2ecdb991b8144e9eb5a973af 100644 (file)
@@ -5,10 +5,53 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
     need_javascript
   end
 
-  test 'Create and run a pipeline' do
-    visit page_with_token('active_trustedclient')
+  def parse_browser_timestamp t
+    # Timestamps are displayed in the browser's time zone (which can
+    # differ from ours) and they come from toLocaleTimeString (which
+    # means they don't necessarily tell us which time zone they're
+    # using). In order to make sense of them, we need to ask the
+    # browser to parse them and generate a timestamp that can be
+    # parsed reliably.
+    #
+    # Note: Even with all this help, phantomjs seem to behave badly
+    # when parsing timestamps on the other side of a DST transition.
+    # See skipped tests below.
+    if /(\d+:\d+ [AP]M) (\d+\/\d+\/\d+)/ =~ t
+      # Currently dates.js renders timestamps as
+      # '{t.toLocaleTimeString()} {t.toLocaleDateString()}' which even
+      # browsers can't make sense of. First we need to flip it around
+      # so it looks like what toLocaleString() would have made.
+      t = $~[2] + ', ' + $~[1]
+    end
+    DateTime.parse(page.evaluate_script "new Date('#{t}').toUTCString()").to_time
+  end
+
+  if false
+    # No need to test (or mention) these all the time. If they start
+    # working (without need_selenium) then some real tests might not
+    # need_selenium any more.
+
+    test 'phantomjs DST' do
+      skip '^^'
+      t0s = '3/8/2015, 01:59 AM'
+      t1s = '3/8/2015, 03:01 AM'
+      t0 = parse_browser_timestamp t0s
+      t1 = parse_browser_timestamp t1s
+      assert_equal 120, t1-t0, "'#{t0s}' to '#{t1s}' was reported as #{t1-t0} seconds, should be 120"
+    end
+
+    test 'phantomjs DST 2' do
+      skip '^^'
+      t0s = '2015-03-08T10:43:00Z'
+      t1s = '2015-03-09T03:43:00Z'
+      t0 = parse_browser_timestamp page.evaluate_script("new Date('#{t0s}').toLocaleString()")
+      t1 = parse_browser_timestamp page.evaluate_script("new Date('#{t1s}').toLocaleString()")
+      assert_equal 17*3600, t1-t0, "'#{t0s}' to '#{t1s}' was reported as #{t1-t0} seconds, should be #{17*3600} (17 hours)"
+    end
+  end
 
-    visit '/pipeline_templates'
+  test 'Create and run a pipeline' do
+    visit page_with_token('active_trustedclient', '/pipeline_templates')
     within('tr', text: 'Two Part Pipeline Template') do
       find('a,button', text: 'Run').click
     end
@@ -35,7 +78,7 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
       first('span', text: 'foo_tag').click
       find('.btn', text: 'Copy').click
     end
-    using_wait_time(Capybara.default_wait_time * 3) do
+    using_wait_time(Capybara.default_max_wait_time * 3) do
       wait_for_ajax
     end
 
@@ -111,10 +154,9 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
 
   # Create a pipeline instance from within a project and run
   test 'Create pipeline inside a project and run' do
-    visit page_with_token('active_trustedclient')
+    visit page_with_token('active_trustedclient', '/projects')
 
-    # Add this collection to the project using collections menu from top nav
-    visit '/projects'
+    # Add collection to the project using Add data button
     find("#projects-menu").click
     find('.dropdown-menu a,button', text: 'A Project').click
     find('.btn', text: 'Add data').click
@@ -124,7 +166,7 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
       first('span', text: 'foo_tag').click
       find('.btn', text: 'Copy').click
     end
-    using_wait_time(Capybara.default_wait_time * 3) do
+    using_wait_time(Capybara.default_max_wait_time * 3) do
       wait_for_ajax
     end
 
@@ -138,9 +180,7 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
   end
 
   test 'view pipeline with job and see graph' do
-    visit page_with_token('active_trustedclient')
-
-    visit '/pipeline_instances'
+    visit page_with_token('active_trustedclient', '/pipeline_instances')
     assert page.has_text? 'pipeline_with_job'
 
     find('a', text: 'pipeline_with_job').click
@@ -152,9 +192,7 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
   end
 
   test 'pipeline description' do
-    visit page_with_token('active_trustedclient')
-
-    visit '/pipeline_instances'
+    visit page_with_token('active_trustedclient', '/pipeline_instances')
     assert page.has_text? 'pipeline_with_job'
 
     find('a', text: 'pipeline_with_job').click
@@ -182,17 +220,27 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
            "components JSON not found")
   end
 
-  PROJECT_WITH_SEARCH_COLLECTION = "A Subproject"
-  def check_parameter_search(proj_name)
-    template = api_fixture("pipeline_templates")["parameter_with_search"]
-    search_text = template["components"]["with-search"]["script_parameters"]["input"]["search_for"]
-    visit page_with_token("active", "/pipeline_templates/#{template['uuid']}")
+  def create_pipeline_from(template_name, project_name="Home")
+    # Visit the named pipeline template and create a pipeline instance from it.
+    # The instance will be created under the named project.
+    template_uuid = api_fixture("pipeline_templates", template_name, "uuid")
+    visit page_with_token("active", "/pipeline_templates/#{template_uuid}")
     click_on "Run this pipeline"
-    within(".modal-dialog") do  # Set project for the new pipeline instance
-      find(".selectable", text: proj_name).click
+    within(".modal-dialog") do
+      # Set project for the new pipeline instance
+      find(".selectable", text: project_name).click
       click_on "Choose"
     end
-    assert(has_text?("This pipeline was created from the template"), "did not land on pipeline instance page")
+    assert(has_text?("This pipeline was created from the template"),
+           "did not land on pipeline instance page")
+  end
+
+  PROJECT_WITH_SEARCH_COLLECTION = "A Subproject"
+  def check_parameter_search(proj_name)
+    create_pipeline_from("parameter_with_search", proj_name)
+    search_text = api_fixture("pipeline_templates", "parameter_with_search",
+                              "components", "with-search",
+                              "script_parameters", "input", "search_for")
     first("a.btn,button", text: "Choose").click
     within(".modal-body") do
       if (proj_name != PROJECT_WITH_SEARCH_COLLECTION)
@@ -215,6 +263,23 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
     check_parameter_search("A Project")
   end
 
+  test "enter a float for a number pipeline input" do
+    # Poltergeist either does not support the HTML 5 <input
+    # type="number">, or interferes with the associated X-Editable
+    # validation code.  If the input field has type=number (forcing an
+    # integer), this test will yield a false positive under
+    # Poltergeist.  --Brett, 2015-02-05
+    need_selenium "for strict X-Editable input validation"
+    create_pipeline_from("template_with_dataclass_number")
+    INPUT_SELECTOR =
+      ".editable[data-name='[components][work][script_parameters][input][value]']"
+    find(INPUT_SELECTOR).click
+    find(".editable-input input").set("12.34")
+    find("#editable-submit").click
+    assert_no_selector(".editable-popup")
+    assert_selector(INPUT_SELECTOR, text: "12.34")
+  end
+
   [
     [true, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false],
     [false, 'Two Part Pipeline Template', 'foo_collection_in_aproject', false],
@@ -267,34 +332,18 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
   ].each do |user, with_options, choose_options, in_aproject|
     test "Rerun pipeline instance as #{user} using options #{with_options} #{choose_options} in #{in_aproject}" do
       if in_aproject
-        visit page_with_token 'active', \
-        '/projects/'+api_fixture('groups')['aproject']['uuid']
+        path = '/pipeline_instances/'+api_fixture('pipeline_instances')['pipeline_owned_by_active_in_aproject']['uuid']
       else
-        visit page_with_token 'active', '/'
+        path = '/pipeline_instances/'+api_fixture('pipeline_instances')['pipeline_owned_by_active_in_home']['uuid']
       end
 
-      # need bigger modal size when choosing a file from collection
-      if Capybara.current_driver == :selenium
-        Capybara.current_session.driver.browser.manage.window.resize_to(1200, 800)
-      end
+      visit page_with_token(user, path)
 
-      create_and_run_pipeline_in_aproject in_aproject, 'Two Part Pipeline Template', 'foo_collection_in_aproject'
-      instance_path = current_path
-
-      # Pause the pipeline
-      find('a,button', text: 'Pause').click
-      assert page.has_text? 'Paused'
-      page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
       page.assert_selector 'a,button', text: 'Re-run with latest'
       page.assert_selector 'a,button', text: 'Re-run options'
 
-      # Pipeline can be re-run now. Access it as the specified user, and re-run
-      if user == 'project_viewer'
-        visit page_with_token(user, instance_path)
+      if user == 'project_viewer' && in_aproject
         assert page.has_text? 'A Project'
-        page.assert_no_selector 'a.disabled,button.disabled', text: 'Resume'
-        page.assert_selector 'a,button', text: 'Re-run with latest'
-        page.assert_selector 'a,button', text: 'Re-run options'
       end
 
       # Now re-run the pipeline
@@ -319,7 +368,7 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
       # project. In case of project_viewer user, since the user cannot
       # write to the project, the pipeline should have been created in
       # the user's Home project.
-      assert_not_equal instance_path, current_path, 'Rerun instance path expected to be different'
+      assert_not_equal path, current_path, 'Rerun instance path expected to be different'
       assert_text 'Home'
       if in_aproject && (user != 'project_viewer')
         assert_text 'A Project'
@@ -408,43 +457,45 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
   end
 
   [
-    [1, 0], # run time 0 minutes
-    [10, 17*60*60 + 51*60], # run time 17 hours and 51 minutes
-  ].each do |index, run_time|
-    test "pipeline start and finish time display #{index}" do
-      visit page_with_token("user1_with_load", "/pipeline_instances/zzzzz-d1hrv-10pipelines0#{index.to_s.rjust(3, '0')}")
+    ['user1_with_load', 'zzzzz-d1hrv-10pipelines0001', 0], # run time 0 minutes
+    ['user1_with_load', 'zzzzz-d1hrv-10pipelines0010', 17*60*60 + 51*60], # run time 17 hours and 51 minutes
+    ['active', 'zzzzz-d1hrv-runningpipeline', nil], # state = running
+  ].each do |user, uuid, run_time|
+    test "pipeline start and finish time display for #{uuid}" do
+      need_selenium 'to parse timestamps correctly across DST boundaries'
+      visit page_with_token(user, "/pipeline_instances/#{uuid}")
 
       assert page.has_text? 'This pipeline started at'
       page_text = page.text
 
-      match = /This pipeline started at (.*)\. It failed after (.*) seconds at (.*)\. Check the Log/.match page_text
+      if run_time
+        match = /This pipeline started at (.*)\. It failed after (.*) at (.*)\. Check the Log/.match page_text
+      else
+        match = /This pipeline started at (.*). It has been active for(.*)/.match page_text
+      end
       assert_not_nil(match, 'Did not find text - This pipeline started at . . . ')
 
       start_at = match[1]
-      finished_at = match[3]
       assert_not_nil(start_at, 'Did not find start_at time')
-      assert_not_nil(finished_at, 'Did not find finished_at time')
 
-      # start and finished time display is of the format '2:20 PM 10/20/2014'
-      start_time = DateTime.strptime(start_at, '%H:%M %p %m/%d/%Y').to_time
-      finished_time = DateTime.strptime(finished_at, '%H:%M %p %m/%d/%Y').to_time
-      assert_equal(run_time, finished_time-start_time,
-        "Time difference did not match for start_at #{start_at}, finished_at #{finished_at}, ran_for #{match[2]}")
+      start_time = parse_browser_timestamp start_at
+      if run_time
+        finished_at = match[3]
+        assert_not_nil(finished_at, 'Did not find finished_at time')
+        finished_time = parse_browser_timestamp finished_at
+        assert_equal(run_time, finished_time-start_time,
+          "Time difference did not match for start_at #{start_at}, finished_at #{finished_at}, ran_for #{match[2]}")
+      else
+        match = /\d(.*)/.match match[2]
+        assert_not_nil match, 'Did not find expected match for running component'
+      end
     end
   end
 
   [
     ['fuse', nil, 2, 20],                           # has 2 as of 11-07-2014
-    ['fuse', 'FUSE project', 1, 1],                 # 1 with this name
-    ['user1_with_load', nil, 30, 100],              # has 37 as of 11-07-2014
-    ['user1_with_load', 'pipeline_10', 2, 2],       # 2 with this name
-    ['user1_with_load', '000010pipelines', 10, 10], # owned_by the project zzzzz-j7d0g-000010pipelines
     ['user1_with_load', '000025pipelines', 25, 25], # owned_by the project zzzzz-j7d0g-000025pipelines, two pages
-    ['admin', nil, 40, 200],
-    ['admin', 'FUSE project', 1, 1],
-    ['admin', 'pipeline_10', 2, 2],
-    ['active', 'containing at least two', 2, 100],
-    ['active', nil, 10, 100],
+    ['admin', 'pipeline_20', 1, 1],
     ['active', 'no such match', 0, 0],
   ].each do |user, search_filter, expected_min, expected_max|
     test "scroll pipeline instances page for #{user} with search filter #{search_filter}
@@ -485,4 +536,50 @@ class PipelineInstancesTest < ActionDispatch::IntegrationTest
     end
   end
 
+  test 'render job run time when job record is inaccessible' do
+    pi = api_fixture('pipeline_instances', 'has_component_with_completed_jobs')
+    visit page_with_token 'active', '/pipeline_instances/' + pi['uuid']
+    assert_text 'Queued for '
+  end
+
+  test "job logs linked for running pipeline" do
+    pi = api_fixture("pipeline_instances", "running_pipeline_with_complete_job")
+    visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
+    click_on "Log"
+    within "#Log" do
+      assert_text "Log for previous"
+      log_link = find("a", text: "Log for previous")
+      assert_includes(log_link[:href],
+                      "/jobs/#{pi["components"]["previous"]["job"]["uuid"]}#Log")
+      assert_selector "#event_log_div"
+    end
+  end
+
+  test "job logs linked for complete pipeline" do
+    pi = api_fixture("pipeline_instances", "complete_pipeline_with_two_jobs")
+    visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
+    click_on "Log"
+    within "#Log" do
+      assert_text "Log for previous"
+      pi["components"].each do |cname, cspec|
+        log_link = find("a", text: "Log for #{cname}")
+        assert_includes(log_link[:href], "/jobs/#{cspec["job"]["uuid"]}#Log")
+      end
+      assert_no_selector "#event_log_div"
+    end
+  end
+
+  test "job logs linked for failed pipeline" do
+    pi = api_fixture("pipeline_instances", "failed_pipeline_with_two_jobs")
+    visit(page_with_token("active", "/pipeline_instances/#{pi['uuid']}"))
+    click_on "Log"
+    within "#Log" do
+      assert_text "Log for previous"
+      pi["components"].each do |cname, cspec|
+        log_link = find("a", text: "Log for #{cname}")
+        assert_includes(log_link[:href], "/jobs/#{cspec["job"]["uuid"]}#Log")
+      end
+      assert_no_selector "#event_log_div"
+    end
+  end
 end