1 require 'integration_helper'
3 class WebsocketTest < ActionDispatch::IntegrationTest
5 need_selenium "to make websockets work"
9 visit(page_with_token("admin", "/websockets"))
10 fill_in("websocket-message-content", :with => "Stuff")
12 assert_text '"status":400'
15 test "test live logging" do
16 visit(page_with_token("admin", "/pipeline_instances/zzzzz-d1hrv-9fm8l10i9z2kqc6"))
18 assert_no_text '123 hello'
20 api = ArvadosApiClient.new
22 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
23 api.api("logs", "", {log: {
24 object_uuid: "zzzzz-d1hrv-9fm8l10i9z2kqc6",
26 properties: {"text" => "123 hello"}}})
27 assert_text '123 hello'
31 [["pipeline_instances", api_fixture("pipeline_instances")['pipeline_with_newer_template']['uuid']],
32 ["jobs", api_fixture("jobs")['running']['uuid']]].each do |c|
33 test "test live logging scrolling #{c[0]}" do
38 visit(page_with_token("admin", "/#{controller}/#{uuid}"))
40 assert_no_text '123 hello'
42 api = ArvadosApiClient.new
46 text << "#{i} hello\n"
49 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
50 api.api("logs", "", {log: {
53 properties: {"text" => text}}})
54 assert_text '1000 hello'
56 # First test that when we're already at the bottom of the page, it scrolls down
57 # when a new line is added.
58 old_top = page.evaluate_script("$('#event_log_div').scrollTop()")
60 api.api("logs", "", {log: {
63 properties: {"text" => "1001 hello\n"}}})
64 assert_text '1001 hello'
66 # Check that new value of scrollTop is greater than the old one
67 new_top = page.evaluate_script("$('#event_log_div').scrollTop()")
68 assert_operator new_top, :>, old_top
70 # Now scroll to 30 pixels from the top
71 page.execute_script "$('#event_log_div').scrollTop(30)"
72 assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
74 api.api("logs", "", {log: {
77 properties: {"text" => "1002 hello\n"}}})
78 assert_text '1002 hello'
80 # Check that we haven't changed scroll position
81 assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
85 test "pipeline instance arv-refresh-on-log-event" do
86 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
87 # Do something and check that the pane reloads.
88 p = PipelineInstance.create({state: "RunningOnServer",
91 script: "test_hash.py",
92 script_version: "1de84a854e2b440dc53bf42f8548afa4c17da332"
97 visit(page_with_token("admin", "/pipeline_instances/#{p.uuid}"))
100 assert page.has_link? 'Pause'
101 assert_no_text 'Complete'
102 assert page.has_no_link? 'Re-run with latest'
107 assert_no_text 'Active'
108 assert page.has_no_link? 'Pause'
109 assert_text 'Complete'
110 assert page.has_link? 'Re-run with latest'
113 test "job arv-refresh-on-log-event" do
114 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
115 # Do something and check that the pane reloads.
116 p = Job.where(uuid: api_fixture('jobs')['running_will_be_completed']['uuid']).results.first
118 visit(page_with_token("admin", "/jobs/#{p.uuid}"))
120 assert_no_text 'complete'
121 assert_no_text 'Re-run job'
126 assert_text 'complete'
127 assert_text 'Re-run job'
130 test "dashboard arv-refresh-on-log-event" do
131 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
133 visit(page_with_token("admin", "/"))
135 assert_no_text 'test dashboard arv-refresh-on-log-event'
137 # Do something and check that the pane reloads.
138 p = PipelineInstance.create({state: "RunningOnServer",
139 name: "test dashboard arv-refresh-on-log-event",
144 assert_text 'test dashboard arv-refresh-on-log-event'
147 test 'job graph appears when first data point is already in logs table' do
148 job_graph_first_datapoint_test
151 test 'job graph appears when first data point arrives by websocket' do
153 Log.find(api_fixture('logs')['crunchstat_for_running_job']['uuid']).destroy
155 job_graph_first_datapoint_test expect_existing_datapoints: false
158 def job_graph_first_datapoint_test expect_existing_datapoints: true
159 uuid = api_fixture('jobs')['running']['uuid']
161 visit page_with_token "active", "/jobs/#{uuid}"
164 assert_selector '#event_log_div', visible: true
166 if expect_existing_datapoints
167 assert_selector '#log_graph_div', visible: true
168 # Magic numbers 12.99 etc come from the job log fixture:
169 assert_last_datapoint 'T1-cpu', (((12.99+0.99)/10.0002)/8)
171 # Until graphable data arrives, we should see the text log but not the graph.
172 assert_no_selector '#log_graph_div', visible: true
175 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"
177 assert_triggers_dom_event 'arv-log-event' do
179 api = ArvadosApiClient.new
180 api.api("logs", "", {log: {
182 event_type: "stderr",
183 properties: {"text" => text}}})
187 # Graph should have appeared (even if it hadn't above). It's
188 # important not to wait like matchers usually do: we are
189 # confirming the graph is visible _immediately_ after the first
190 # data point arrives.
192 assert_selector '#log_graph_div', visible: true
194 assert_last_datapoint 'T1-cpu', (((35.39+0.86)/10.0002)/8)
197 test "live log charting from replayed log" do
198 uuid = api_fixture("jobs")['running']['uuid']
200 visit page_with_token "active", "/jobs/#{uuid}"
203 assert_triggers_dom_event 'arv-log-event' do
204 ApiServerForTests.new.run_rake_task("replay_job_log", "test/job_logs/crunchstatshort.log,1.0,#{uuid}")
207 assert_last_datapoint 'T1-cpu', (((35.39+0.86)/10.0002)/8)
210 def assert_last_datapoint series, value
211 datum = page.evaluate_script("jobGraphData[jobGraphData.length-1]['#{series}']")
212 assert_in_epsilon value, datum.to_f
215 test "test running job with just a few previous log records" do
216 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
217 job = Job.where(uuid: api_fixture("jobs")['running']['uuid']).results.first
218 visit page_with_token("admin", "/jobs/#{job.uuid}")
220 api = ArvadosApiClient.new
222 # Create just one old log record
223 api.api("logs", "", {log: {
224 object_uuid: job.uuid,
225 event_type: "stderr",
226 properties: {"text" => "Historic log message"}}})
230 # Expect "all" historic log records because we have less than
231 # default Rails.configuration.running_job_log_records_to_fetch count
232 assert_text 'Historic log message'
234 # Create new log record and expect it to show up in log tab
235 api.api("logs", "", {log: {
236 object_uuid: job.uuid,
237 event_type: "stderr",
238 properties: {"text" => "Log message after subscription"}}})
239 assert_text 'Log message after subscription'
242 test "test running job with too many previous log records" do
243 Rails.configuration.running_job_log_records_to_fetch = 5
245 Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
246 job = Job.where(uuid: api_fixture("jobs")['running']['uuid']).results.first
248 visit page_with_token("admin", "/jobs/#{job.uuid}")
250 api = ArvadosApiClient.new
252 # Create Rails.configuration.running_job_log_records_to_fetch + 1 log records
253 (0..Rails.configuration.running_job_log_records_to_fetch).each do |count|
254 api.api("logs", "", {log: {
255 object_uuid: job.uuid,
256 event_type: "stderr",
257 properties: {"text" => "Old log message #{count}"}}})
260 # Go to log tab, which results in subscribing to websockets
263 # Expect all but the first historic log records,
264 # because that was one too many than fetch count.
265 (1..Rails.configuration.running_job_log_records_to_fetch).each do |count|
266 assert_text "Old log message #{count}"
268 assert_no_text 'Old log message 0'
270 # Create one more log record after subscription
271 api.api("logs", "", {log: {
272 object_uuid: job.uuid,
273 event_type: "stderr",
274 properties: {"text" => "Life goes on!"}}})
275 # Expect it to show up in log tab
276 assert_text 'Life goes on!'