closes #9684
[arvados.git] / apps / workbench / test / integration / websockets_test.rb
index 4885129286ab5d4c66d9c3cf6a7bf94bb6215856..e9f5a799c0d5444ed453381aa063002fd0e74b9c 100644 (file)
@@ -25,16 +25,19 @@ class WebsocketTest < ActionDispatch::IntegrationTest
                 event_type: "stderr",
                 properties: {"text" => "123 hello"}}})
     assert_text '123 hello'
-    Thread.current[:arvados_api_token] = nil
   end
 
-
-  [["pipeline_instances", api_fixture("pipeline_instances")['pipeline_with_newer_template']['uuid']],
-   ["jobs", api_fixture("jobs")['running']['uuid']]].each do |c|
+  [
+   ["pipeline_instances", api_fixture("pipeline_instances")['pipeline_with_newer_template']['uuid']],
+   ["jobs", api_fixture("jobs")['running']['uuid']],
+   ["containers", api_fixture("containers")['running']['uuid']],
+   ["container_requests", api_fixture("container_requests")['running']['uuid'], api_fixture("containers")['running']['uuid']],
+  ].each do |c|
     test "test live logging scrolling #{c[0]}" do
 
       controller = c[0]
       uuid = c[1]
+      log_uuid = c[2] || c[1]
 
       visit(page_with_token("admin", "/#{controller}/#{uuid}"))
       click_link("Log")
@@ -49,7 +52,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest
 
       Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
       api.api("logs", "", {log: {
-                  object_uuid: uuid,
+                  object_uuid: log_uuid,
                   event_type: "stderr",
                   properties: {"text" => text}}})
       assert_text '1000 hello'
@@ -59,7 +62,7 @@ class WebsocketTest < ActionDispatch::IntegrationTest
       old_top = page.evaluate_script("$('#event_log_div').scrollTop()")
 
       api.api("logs", "", {log: {
-                  object_uuid: uuid,
+                  object_uuid: log_uuid,
                   event_type: "stderr",
                   properties: {"text" => "1001 hello\n"}}})
       assert_text '1001 hello'
@@ -73,15 +76,13 @@ class WebsocketTest < ActionDispatch::IntegrationTest
       assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
 
       api.api("logs", "", {log: {
-                  object_uuid: uuid,
+                  object_uuid: log_uuid,
                   event_type: "stderr",
                   properties: {"text" => "1002 hello\n"}}})
       assert_text '1002 hello'
 
       # Check that we haven't changed scroll position
       assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
-
-      Thread.current[:arvados_api_token] = nil
     end
   end
 
@@ -111,8 +112,6 @@ class WebsocketTest < ActionDispatch::IntegrationTest
     assert page.has_no_link? 'Pause'
     assert_text 'Complete'
     assert page.has_link? 'Re-run with latest'
-
-    Thread.current[:arvados_api_token] = nil
   end
 
   test "job arv-refresh-on-log-event" do
@@ -130,8 +129,6 @@ class WebsocketTest < ActionDispatch::IntegrationTest
 
     assert_text 'complete'
     assert_text 'Re-run job'
-
-    Thread.current[:arvados_api_token] = nil
   end
 
   test "dashboard arv-refresh-on-log-event" do
@@ -149,59 +146,137 @@ class WebsocketTest < ActionDispatch::IntegrationTest
                                 })
 
     assert_text 'test dashboard arv-refresh-on-log-event'
+  end
 
-    Thread.current[:arvados_api_token] = nil
+  test 'job graph appears when first data point is already in logs table' do
+    job_graph_first_datapoint_test
   end
 
-  test "live log charting" do
-    uuid = api_fixture("jobs")['running']['uuid']
+  test 'job graph appears when first data point arrives by websocket' do
+    use_token :admin do
+      Log.find(api_fixture('logs')['crunchstat_for_running_job']['uuid']).destroy
+    end
+    job_graph_first_datapoint_test expect_existing_datapoints: false
+  end
+
+  def job_graph_first_datapoint_test expect_existing_datapoints: true
+    uuid = api_fixture('jobs')['running']['uuid']
 
-    visit page_with_token "admin", "/jobs/#{uuid}"
+    visit page_with_token "active", "/jobs/#{uuid}"
     click_link "Log"
 
-    # Until graphable data arrives, we should see the text log but not the graph.
     assert_selector '#event_log_div', visible: true
-    assert_no_selector '#log_graph_div', visible: true
 
-    api = ArvadosApiClient.new
+    if expect_existing_datapoints
+      assert_selector '#log_graph_div', visible: true
+      # Magic numbers 12.99 etc come from the job log fixture:
+      assert_last_datapoint 'T1-cpu', (((12.99+0.99)/10.0002)/8)
+    else
+      # Until graphable data arrives, we should see the text log but not the graph.
+      assert_no_selector '#log_graph_div', visible: true
+    end
 
-    # should give 45.3% or (((36.39+0.86)/10.0002)/8)*100 rounded to 1 decimal place
     text = "2014-11-07_23:33:51 #{uuid} 31708 1 stderr crunchstat: cpu 1970.8200 user 60.2700 sys 8 cpus -- interval 10.0002 seconds 35.3900 user 0.8600 sys"
 
+    assert_triggers_dom_event 'arv-log-event' do
+      use_token :active do
+        api = ArvadosApiClient.new
+        api.api("logs", "", {log: {
+                    object_uuid: uuid,
+                    event_type: "stderr",
+                    properties: {"text" => text}}})
+      end
+    end
+
+    # Graph should have appeared (even if it hadn't above). It's
+    # important not to wait like matchers usually do: we are
+    # confirming the graph is visible _immediately_ after the first
+    # data point arrives.
+    using_wait_time 0 do
+      assert_selector '#log_graph_div', visible: true
+    end
+    assert_last_datapoint 'T1-cpu', (((35.39+0.86)/10.0002)/8)
+  end
+
+  test "live log charting from replayed log" do
+    uuid = api_fixture("jobs")['running']['uuid']
+
+    visit page_with_token "active", "/jobs/#{uuid}"
+    click_link "Log"
+
+    assert_triggers_dom_event 'arv-log-event' do
+      ApiServerForTests.new.run_rake_task("replay_job_log", "test/job_logs/crunchstatshort.log,1.0,#{uuid}")
+    end
+
+    assert_last_datapoint 'T1-cpu', (((35.39+0.86)/10.0002)/8)
+  end
+
+  def assert_last_datapoint series, value
+    datum = page.evaluate_script("jobGraphData[jobGraphData.length-1]['#{series}']")
+    assert_in_epsilon value, datum.to_f
+  end
+
+  test "test running job with just a few previous log records" do
     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
+    job = Job.where(uuid: api_fixture("jobs")['running']['uuid']).results.first
+    visit page_with_token("admin", "/jobs/#{job.uuid}")
+
+    api = ArvadosApiClient.new
+
+    # Create just one old log record
     api.api("logs", "", {log: {
-                object_uuid: uuid,
+                object_uuid: job.uuid,
                 event_type: "stderr",
-                properties: {"text" => text}}})
-
-    # Log div should appear when the first data point arrives by websocket.
-    assert_selector '#log_graph_div', visible: true
+                properties: {"text" => "Historic log message"}}})
 
-    # Using datapoint 1 instead of datapoint 0 because there will be a
-    # "dummy" datapoint with no actual stats 10 minutes previous to
-    # the one we're looking for, for the sake of making the x-axis of
-    # the graph show a full 10 minutes of time even though there is
-    # only a single real datapoint.
-    cpu_stat = page.evaluate_script("jobGraphData[1]['T1-cpu']")
+    click_link("Log")
 
-    assert_equal 45.3, (cpu_stat.to_f*100).round(1)
+    # Expect "all" historic log records because we have less than
+    # default Rails.configuration.running_job_log_records_to_fetch count
+    assert_text 'Historic log message'
 
-    Thread.current[:arvados_api_token] = nil
+    # Create new log record and expect it to show up in log tab
+    api.api("logs", "", {log: {
+                object_uuid: job.uuid,
+                event_type: "stderr",
+                properties: {"text" => "Log message after subscription"}}})
+    assert_text 'Log message after subscription'
   end
 
-  test "live log charting from replayed log" do
-    uuid = api_fixture("jobs")['running']['uuid']
+  test "test running job with too many previous log records" do
+    Rails.configuration.running_job_log_records_to_fetch = 5
 
-    visit page_with_token "admin", "/jobs/#{uuid}"
-    click_link "Log"
+    Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
+    job = Job.where(uuid: api_fixture("jobs")['running']['uuid']).results.first
 
-    ApiServerForTests.new.run_rake_task("replay_job_log", "test/job_logs/crunchstatshort.log,1.0,#{uuid}")
-    wait_for_ajax
+    visit page_with_token("admin", "/jobs/#{job.uuid}")
 
-    # see above comment as to why we use datapoint 1 rather than 0
-    cpu_stat = page.evaluate_script("jobGraphData[1]['T1-cpu']")
+    api = ArvadosApiClient.new
 
-    assert_equal 45.3, (cpu_stat.to_f*100).round(1)
-  end
+    # Create Rails.configuration.running_job_log_records_to_fetch + 1 log records
+    (0..Rails.configuration.running_job_log_records_to_fetch).each do |count|
+      api.api("logs", "", {log: {
+                object_uuid: job.uuid,
+                event_type: "stderr",
+                properties: {"text" => "Old log message #{count}"}}})
+    end
+
+    # Go to log tab, which results in subscribing to websockets
+    click_link("Log")
+
+    # Expect all but the first historic log records,
+    # because that was one too many than fetch count.
+    (1..Rails.configuration.running_job_log_records_to_fetch).each do |count|
+      assert_text "Old log message #{count}"
+    end
+    assert_no_text 'Old log message 0'
 
+    # Create one more log record after subscription
+    api.api("logs", "", {log: {
+                object_uuid: job.uuid,
+                event_type: "stderr",
+                properties: {"text" => "Life goes on!"}}})
+    # Expect it to show up in log tab
+    assert_text 'Life goes on!'
+  end
 end