1 require 'integration_helper'
3 class WebsocketTest < ActionDispatch::IntegrationTest
5 need_selenium "to make websockets work"
6 @dispatch_client = ArvadosApiClient.new
10 use_token :dispatch1 do
11 @dispatch_client.api('logs', '', log: body)
16 visit(page_with_token("active", "/websockets"))
17 fill_in("websocket-message-content", :with => "Stuff")
19 assert_text '"status":400'
23 ['pipeline_instances', 'pipeline_in_running_state', api_fixture('jobs')['running']],
25 ['containers', 'running'],
26 ['container_requests', 'running', api_fixture('containers')['running']],
27 ].each do |controller, view_fixture_name, log_target_fixture|
28 view_fixture = api_fixture(controller)[view_fixture_name]
29 log_target_fixture ||= view_fixture
31 test "test live logging and scrolling for #{controller}" do
33 visit(page_with_token("active", "/#{controller}/#{view_fixture['uuid']}\#Log"))
34 assert_no_text '123 hello'
38 text << "#{i} hello\n"
41 dispatch_log(owner_uuid: log_target_fixture['owner_uuid'],
42 object_uuid: log_target_fixture['uuid'],
44 properties: {"text" => text})
45 assert_text '1000 hello'
47 # First test that when we're already at the bottom of the page, it scrolls down
48 # when a new line is added.
49 old_top = page.evaluate_script("$('#event_log_div').scrollTop()")
51 dispatch_log(owner_uuid: log_target_fixture['owner_uuid'],
52 object_uuid: log_target_fixture['uuid'],
53 event_type: "dispatch",
54 properties: {"text" => "1001 hello\n"})
55 assert_text '1001 hello'
57 # Check that new value of scrollTop is greater than the old one
58 new_top = page.evaluate_script("$('#event_log_div').scrollTop()")
59 assert_operator new_top, :>, old_top
61 # Now scroll to 30 pixels from the top
62 page.execute_script "$('#event_log_div').scrollTop(30)"
63 assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
65 dispatch_log(owner_uuid: log_target_fixture['owner_uuid'],
66 object_uuid: log_target_fixture['uuid'],
68 properties: {"text" => "1002 hello\n"})
69 assert_text '1002 hello'
71 # Check that we haven't changed scroll position
72 assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
76 test "pipeline instance arv-refresh-on-log-event" do
77 # Do something and check that the pane reloads.
78 p = use_token :active do
79 PipelineInstance.create(state: "RunningOnServer",
82 script: "test_hash.py",
83 script_version: "1de84a854e2b440dc53bf42f8548afa4c17da332"
87 visit(page_with_token("active", "/pipeline_instances/#{p.uuid}"))
90 assert page.has_link? 'Pause'
91 assert_no_text 'Complete'
92 assert page.has_no_link? 'Re-run with latest'
94 use_token :dispatch1 do
95 p.update_attributes!(state: 'Complete')
98 assert_no_text 'Active'
99 assert page.has_no_link? 'Pause'
100 assert_text 'Complete'
101 assert page.has_link? 'Re-run with latest'
104 test "job arv-refresh-on-log-event" do
105 # Do something and check that the pane reloads.
106 uuid = api_fixture('jobs')['running_will_be_completed']['uuid']
107 visit(page_with_token("active", "/jobs/#{uuid}"))
109 assert_no_text 'complete'
110 assert_no_text 'Re-run job'
112 use_token :dispatch1 do
113 Job.find(uuid).update_attributes!(state: 'Complete')
116 assert_text 'complete'
117 assert_text 'Re-run job'
120 test "dashboard arv-refresh-on-log-event" do
121 visit(page_with_token("active", "/"))
123 assert_no_text 'test dashboard arv-refresh-on-log-event'
125 # Do something and check that the pane reloads.
127 p = PipelineInstance.create({state: "RunningOnServer",
128 name: "test dashboard arv-refresh-on-log-event",
134 assert_text 'test dashboard arv-refresh-on-log-event'
137 test 'job graph appears when first data point is already in logs table' do
138 job_graph_first_datapoint_test
141 test 'job graph appears when first data point arrives by websocket' do
143 Log.find(api_fixture('logs')['crunchstat_for_running_job']['uuid']).destroy
145 job_graph_first_datapoint_test expect_existing_datapoints: false
148 def job_graph_first_datapoint_test expect_existing_datapoints: true
149 uuid = api_fixture('jobs')['running']['uuid']
151 visit page_with_token "active", "/jobs/#{uuid}"
154 assert_selector '#event_log_div', visible: true
156 if expect_existing_datapoints
157 assert_selector '#log_graph_div', visible: true
158 # Magic numbers 12.99 etc come from the job log fixture:
159 assert_last_datapoint 'T1-cpu', (((12.99+0.99)/10.0002)/8)
161 # Until graphable data arrives, we should see the text log but not the graph.
162 assert_no_selector '#log_graph_div', visible: true
165 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"
167 assert_triggers_dom_event 'arv-log-event' do
168 dispatch_log(owner_uuid: api_fixture('jobs')['running']['owner_uuid'],
170 event_type: "stderr",
171 properties: {"text" => text})
174 # Graph should have appeared (even if it hadn't above). It's
175 # important not to wait like matchers usually do: we are
176 # confirming the graph is visible _immediately_ after the first
177 # data point arrives.
179 assert_selector '#log_graph_div', visible: true
181 assert_last_datapoint 'T1-cpu', (((35.39+0.86)/10.0002)/8)
184 test "live log charting from replayed log" do
185 uuid = api_fixture("jobs")['running']['uuid']
187 visit page_with_token "active", "/jobs/#{uuid}"
190 assert_triggers_dom_event 'arv-log-event' do
191 ApiServerForTests.new.run_rake_task("replay_job_log", "test/job_logs/crunchstatshort.log,1.0,#{uuid}")
194 assert_last_datapoint 'T1-cpu', (((35.39+0.86)/10.0002)/8)
197 def assert_last_datapoint series, value
198 datum = page.evaluate_script("jobGraphData[jobGraphData.length-1]['#{series}']")
199 assert_in_epsilon value, datum.to_f
202 test "test running job with just a few previous log records" do
203 job = api_fixture("jobs")['running']
205 # Create just one old log record
206 dispatch_log(owner_uuid: job['owner_uuid'],
207 object_uuid: job['uuid'],
208 event_type: "stderr",
209 properties: {"text" => "Historic log message"})
211 visit page_with_token("active", "/jobs/#{job['uuid']}\#Log")
213 # Expect "all" historic log records because we have less than
214 # default Rails.configuration.running_job_log_records_to_fetch count
215 assert_text 'Historic log message'
217 # Create new log record and expect it to show up in log tab
218 dispatch_log(owner_uuid: job['owner_uuid'],
219 object_uuid: job['uuid'],
220 event_type: "stderr",
221 properties: {"text" => "Log message after subscription"})
222 assert_text 'Log message after subscription'
225 test "test running job with too many previous log records" do
227 Rails.configuration.running_job_log_records_to_fetch = max
228 job = api_fixture("jobs")['running']
230 # Create max+1 log records
231 (0..max).each do |count|
232 dispatch_log(owner_uuid: job['owner_uuid'],
233 object_uuid: job['uuid'],
234 event_type: "stderr",
235 properties: {"text" => "Old log message #{count}"})
238 visit page_with_token("active", "/jobs/#{job['uuid']}\#Log")
240 # Expect all but the first historic log records,
241 # because that was one too many than fetch count.
242 (1..max).each do |count|
243 assert_text "Old log message #{count}"
245 assert_no_text 'Old log message 0'
247 # Create one more log record after subscription
248 dispatch_log(owner_uuid: job['owner_uuid'],
249 object_uuid: job['uuid'],
250 event_type: "stderr",
251 properties: {"text" => "Life goes on!"})
253 # Expect it to show up in log tab
254 assert_text 'Life goes on!'