Merge branch 'master' into 4644-collection-file-filtering
[arvados.git] / apps / workbench / test / integration / websockets_test.rb
1 require 'integration_helper'
2 require 'selenium-webdriver'
3 require 'headless'
4
5 class WebsocketTest < ActionDispatch::IntegrationTest
6
7   setup do
8     headless = Headless.new
9     headless.start
10     Capybara.current_driver = :selenium
11   end
12
13   test "test page" do
14     visit(page_with_token("admin", "/websockets"))
15     fill_in("websocket-message-content", :with => "Stuff")
16     click_button("Send")
17     assert_text '"status":400'
18   end
19
20   test "test live logging" do
21     visit(page_with_token("admin", "/pipeline_instances/zzzzz-d1hrv-9fm8l10i9z2kqc6"))
22     click_link("Log")
23     assert_no_text '123 hello'
24
25     api = ArvadosApiClient.new
26
27     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
28     api.api("logs", "", {log: {
29                 object_uuid: "zzzzz-d1hrv-9fm8l10i9z2kqc6",
30                 event_type: "stderr",
31                 properties: {"text" => "123 hello"}}})
32     assert_text '123 hello'
33     Thread.current[:arvados_api_token] = nil
34   end
35
36
37   [["pipeline_instances", api_fixture("pipeline_instances")['pipeline_with_newer_template']['uuid']],
38    ["jobs", api_fixture("jobs")['running']['uuid']]].each do |c|
39     test "test live logging scrolling #{c[0]}" do
40
41       controller = c[0]
42       uuid = c[1]
43
44       visit(page_with_token("admin", "/#{controller}/#{uuid}"))
45       click_link("Log")
46       assert_no_text '123 hello'
47
48       api = ArvadosApiClient.new
49
50       text = ""
51       (1..1000).each do |i|
52         text << "#{i} hello\n"
53       end
54
55       Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
56       api.api("logs", "", {log: {
57                   object_uuid: uuid,
58                   event_type: "stderr",
59                   properties: {"text" => text}}})
60       assert_text '1000 hello'
61
62       # First test that when we're already at the bottom of the page, it scrolls down
63       # when a new line is added.
64       old_top = page.evaluate_script("$('#event_log_div').scrollTop()")
65
66       api.api("logs", "", {log: {
67                   object_uuid: uuid,
68                   event_type: "stderr",
69                   properties: {"text" => "1001 hello\n"}}})
70       assert_text '1001 hello'
71
72       # Check that new value of scrollTop is greater than the old one
73       assert page.evaluate_script("$('#event_log_div').scrollTop()") > old_top
74
75       # Now scroll to 30 pixels from the top
76       page.execute_script "$('#event_log_div').scrollTop(30)"
77       assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
78
79       api.api("logs", "", {log: {
80                   object_uuid: uuid,
81                   event_type: "stderr",
82                   properties: {"text" => "1002 hello\n"}}})
83       assert_text '1002 hello'
84
85       # Check that we haven't changed scroll position
86       assert_equal 30, page.evaluate_script("$('#event_log_div').scrollTop()")
87
88       Thread.current[:arvados_api_token] = nil
89     end
90   end
91
92   test "pipeline instance arv-refresh-on-log-event" do
93     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
94     # Do something and check that the pane reloads.
95     p = PipelineInstance.create({state: "RunningOnServer",
96                                   components: {
97                                     c1: {
98                                       script: "test_hash.py",
99                                       script_version: "1de84a854e2b440dc53bf42f8548afa4c17da332"
100                                     }
101                                   }
102                                 })
103
104     visit(page_with_token("admin", "/pipeline_instances/#{p.uuid}"))
105
106     assert_text 'Active'
107     assert page.has_link? 'Pause'
108     assert_no_text 'Complete'
109     assert page.has_no_link? 'Re-run with latest'
110
111     p.state = "Complete"
112     p.save!
113
114     assert_no_text 'Active'
115     assert page.has_no_link? 'Pause'
116     assert_text 'Complete'
117     assert page.has_link? 'Re-run with latest'
118
119     Thread.current[:arvados_api_token] = nil
120   end
121
122   test "job arv-refresh-on-log-event" do
123     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
124     # Do something and check that the pane reloads.
125     p = Job.where(uuid: api_fixture('jobs')['running_will_be_completed']['uuid']).results.first
126
127     visit(page_with_token("admin", "/jobs/#{p.uuid}"))
128
129     assert_no_text 'complete'
130     assert_no_text 'Re-run same version'
131
132     p.state = "Complete"
133     p.save!
134
135     assert_text 'complete'
136     assert_text 'Re-run same version'
137
138     Thread.current[:arvados_api_token] = nil
139   end
140
141   test "dashboard arv-refresh-on-log-event" do
142     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
143
144     visit(page_with_token("admin", "/"))
145
146     assert_no_text 'test dashboard arv-refresh-on-log-event'
147
148     # Do something and check that the pane reloads.
149     p = PipelineInstance.create({state: "RunningOnServer",
150                                   name: "test dashboard arv-refresh-on-log-event",
151                                   components: {
152                                   }
153                                 })
154
155     assert_text 'test dashboard arv-refresh-on-log-event'
156
157     Thread.current[:arvados_api_token] = nil
158   end
159
160   test "live log charting" do
161     uuid = api_fixture("jobs")['running']['uuid']
162
163     visit page_with_token "admin", "/jobs/#{uuid}"
164     click_link "Log"
165
166     api = ArvadosApiClient.new
167
168     # should give 45.3% or (((36.39+0.86)/10.0002)/8)*100 rounded to 1 decimal place
169     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"
170
171     Thread.current[:arvados_api_token] = @@API_AUTHS["admin"]['api_token']
172     api.api("logs", "", {log: {
173                 object_uuid: uuid,
174                 event_type: "stderr",
175                 properties: {"text" => text}}})
176     wait_for_ajax
177
178     # 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
179     cpu_stat = page.evaluate_script("jobGraphData[1]['T1-cpu']")
180
181     assert_equal 45.3, (cpu_stat.to_f*100).round(1)
182
183     Thread.current[:arvados_api_token] = nil
184   end
185
186   test "live log charting from replayed log" do
187     uuid = api_fixture("jobs")['running']['uuid']
188
189     visit page_with_token "admin", "/jobs/#{uuid}"
190     click_link "Log"
191
192     ApiServerForTests.new.run_rake_task("replay_job_log", "test/job_logs/crunchstatshort.log,1.0,#{uuid}")
193     wait_for_ajax
194
195     # see above comment as to why we use datapoint 1 rather than 0
196     cpu_stat = page.evaluate_script("jobGraphData[1]['T1-cpu']")
197
198     assert_equal 45.3, (cpu_stat.to_f*100).round(1)
199   end
200
201 end