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