Debian Codegraph @

interactive-notebook/tests/stap_test_kernel.py

raw
1 # A testsuite for the isystemtap kernel
2 # Copyright (C) 2023 Red Hat Inc.
3 #
4 # This file is part of systemtap, and is free software. You can
5 # redistribute it and/or modify it under the terms of the GNU General
6 # Public License (GPL); either version 2, or (at your option) any
7 # later version.
8 #
9 # This is based on jupyter_kernel_test and is required since we need to test with specified
10 # cell_ids (which their test framework does not support)
11
12 from unittest import TestCase
13
14 from jupyter_client.blocking.client import BlockingKernelClient
15 from jupyter_client.manager import KernelManager, start_new_kernel
16 from jupyter_kernel_test import TIMEOUT, ensure_sync, KernelTests
17
18 import unittest
19 import sys
20
21 from typing import Tuple, Dict
22
23 class ISystemtapKernelTests(TestCase):
24 kernel_name = "isystemtap"
25 kc: BlockingKernelClient
26 km: KernelManager
27
28 @classmethod
29 def setUpClass(cls):
30 cls.km, cls.kc = start_new_kernel(kernel_name=cls.kernel_name)
31
32 @classmethod
33 def tearDownClass(cls):
34 cls.kc.stop_channels()
35 cls.km.shutdown_kernel()
36
37 flush_channels = KernelTests.flush_channels
38 get_non_kernel_info_reply = KernelTests.get_non_kernel_info_reply
39
40 def execute_helper(self, code: str, cell_id: str) -> Tuple[Dict, Dict, Dict]:
41 self.flush_channels()
42 def client_execute(code, cell_id):
43 content = {
44 "code": code,
45 "silent": False,
46 "store_history": False,
47 "user_expressions": {},
48 "allow_stdin": True,
49 "stop_on_error": True,
50 }
51 msg = self.kc.session.msg("execute_request", content)
52 # The magic line which causes all the trouble of not using jupyter_kernel_test
53 msg['metadata'] = dict(deletedCells = [], recordTiming=False, cellId=cell_id)
54 self.kc.shell_channel.send(msg)
55 return msg["header"]["msg_id"]
56
57 msg_id = client_execute(code, cell_id)
58
59 reply = self.get_non_kernel_info_reply(timeout=TIMEOUT)
60 busy_msg = ensure_sync(self.kc.iopub_channel.get_msg)(timeout=1)
61 self.assertEqual(busy_msg["content"]["execution_state"], "busy")
62
63 output_msgs = []
64 while True:
65 msg = ensure_sync(self.kc.iopub_channel.get_msg)(timeout=0.1)
66 if msg["msg_type"] == "status":
67 self.assertEqual(msg["content"]["execution_state"], "idle")
68 break
69 elif msg["msg_type"] == "execute_input":
70 self.assertEqual(msg["content"]["code"], code)
71 continue
72 output_msgs.append(msg)
73
74 return reply, output_msgs, [msg for msg in output_msgs if msg['header']['msg_type'] in ('stream', 'display_data')]
75
76 def test_isystemtap_basic(self):
77 # A basic helloworld script cell (a)
78 r, _, stream_output_msgs = self.execute_helper(
79 code='%%script helloworld\n\
80 probe oneshot {println("Hello World")}',
81 cell_id="a")
82 # NOTE: The 0th output is always the control panel ***********************
83 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
84 self.assertEqual(stream_output_msgs[1]['content']['name'], 'stdout')
85 self.assertEqual(stream_output_msgs[1]['content']['text'], 'Hello World\n')
86
87 # Replace cell (a) with an edit cell, and then with
88 # another edit (b) and a run (c) make sure we can stitch cells together
89 r, _, stream_output_msgs = self.execute_helper(
90 code='%%edit two_parts\n\
91 global g = "g"\n\
92 function f() { return "f" }',
93 cell_id="a")
94 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
95
96 r, _, stream_output_msgs = self.execute_helper(
97 code='%%edit two_parts\n\
98 probe oneshot { printf("%s-%s\\n", g, f()) }',
99 cell_id="b")
100 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
101
102 r, _, stream_output_msgs = self.execute_helper(
103 code='%%run two_parts',
104 cell_id="c")
105 self.assertEqual(r['content']['status'], 'ok')
106 self.assertEqual(stream_output_msgs[1]['content']['text'], 'g-f\n', stream_output_msgs)
107
108 def test_isystemtap_cmdline_args(self):
109 r, _, stream_output_msgs = self.execute_helper(
110 code='%%script argtest1 --vp=01000\n\
111 probe oneshot {printf("hello\\n")}',
112 cell_id="a")
113 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
114 # Race to see if stderr or stdout is output first here
115 stdout = 1 if stream_output_msgs[1]['content']['name'] == 'stdout' else 2
116 stderr = 1 if stdout == 2 else 2
117 self.assertEqual(stream_output_msgs[stdout]['content']['text'], 'hello\n', stream_output_msgs[stdout])
118 self.assertIn('Pass 2: analyzed script:', stream_output_msgs[stderr]['content']['text'], stream_output_msgs[stderr])
119
120 r, _, stream_output_msgs = self.execute_helper(
121 code='%%script argtest2 --args(1,a)\n\
122 probe oneshot {printf("%d%s\\n", $1, @2)}',
123 cell_id="a")
124 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
125 self.assertEqual(stream_output_msgs[1]['content']['text'], '1a\n', stream_output_msgs[1])
126
127 def test_isystemtap_cell_lifetimes(self):
128 # Make a cell foo and then switch it to bar
129 # The foo namespace should be remove and replaced
130 r, _, stream_output_msgs = self.execute_helper(
131 code='%%edit foo\n\
132 probe oneshot {println("Foo")}',
133 cell_id="a")
134 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
135
136 r, _, stream_output_msgs = self.execute_helper(
137 code='%%edit bar\n\
138 probe oneshot {println("Baz")}',
139 cell_id="a")
140 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
141
142 r, _, stream_output_msgs = self.execute_helper(
143 code='%%edit bar\n\
144 probe oneshot {println("Bar")}',
145 cell_id="a")
146 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
147
148 r, _, stream_output_msgs = self.execute_helper(
149 code='%%run foo',
150 cell_id="b")
151 # FIXME: How do I feel about this behaviour
152 # self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
153 # self.assertEqual(stream_output_msgs[1]['content']['text'], 'Foo\n', stream_output_msgs)
154
155 r, _, stream_output_msgs = self.execute_helper(
156 code='%%run bar',
157 cell_id="b")
158 self.assertEqual(r['content']['status'], 'ok')
159 self.assertEqual(stream_output_msgs[1]['content']['text'], 'Bar\n', stream_output_msgs)
160
161 def test_isystemtap_histograms(self):
162 # A basic helloworld script cell (a)
163 r, _, output_messages = self.execute_helper(
164 code="""%%script histo
165 global g
166 global f
167 probe begin {f <<< 1}
168 probe timer.ms(10) {
169 if(@count(g) < 5){
170 g <<< 1
171 f <<< @max(f) * 2
172 }else{
173 exit()
174 }
175 }
176 probe end {
177 println(@hist_linear(g, 0, 2, 1))
178 # println(@hist_linear(g, 0, 2, 1))
179 // println(@hist_linear(g, 0, 2, 1))
180 /* println(@hist_linear(g, 0, 2, 1)) */
181 println(@hist_log(f))
182 println(@hist_log(f)[2])
183 }""",
184
185
186 cell_id="a")
187 self.assertEqual(len(output_messages), 4, output_messages)
188 # The linear histogram of g looks good
189 self.assertIn('text/plain', output_messages[1].get('content', {}).get('data', {}))
190 self.assertIn("""x=array(['0', '1', '2'], dtype='<U1'), y=array([0, 5, 0])""", output_messages[1]['content']['data']['text/plain'])
191 self.assertIn("""title='Linear Histogram of g'""", output_messages[1]['content']['data']['text/plain'])
192
193 # The next 3 lines are commented out, so we expect a logarithmic histo
194 self.assertIn('text/plain', output_messages[2].get('content', {}).get('data', {}))
195 self.assertIn("""x=array(['0', '1', '2', '4', '8', '16', '32', '64', '128'], dtype='<U3'), y=array([0, 1, 1, 1, 1, 1, 1, 0, 0])""",
196 output_messages[2]['content']['data']['text/plain'])
197 self.assertIn("""title='Logarithmic Histogram of f'""", output_messages[2]['content']['data']['text/plain'])
198 # Finally the last one isn't a histo, we just bring a bucket count
199 self.assertEqual(output_messages[3]['content']['name'], 'stdout')
200 self.assertEqual(output_messages[3]['content']['text'], '0\n')
201
202 def test_isystemtap_python_cells(self):
203 r, _, stream_output_msgs = self.execute_helper(
204 code="""%%edit python_scripts
205 global c = 1
206 global arr
207 probe timer.s(1) {
208 arr[c, "foo"] = c * 3
209 c++
210 if (c == 5) { exit() }
211 } """,
212 cell_id="a")
213 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
214 # FIXME: I need to wait for the monitor to startup, but this takes too long. How to ensure it runs?
215
216 r, _, stream_output_msgs = self.execute_helper(
217 code='%%run python_scripts',
218 cell_id="b")
219 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
220
221 r, _, stream_output_msgs = self.execute_helper(
222 code='%%python python_scripts\nprint(c, arr, arr[(1, "foo")])',
223 cell_id="c")
224 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
225 # FIXME: The result is 1 second behind the actual, so its technically missing the last one
226 # actual is len 5, so we only have util 4 so globals has 3
227 self.assertEqual(stream_output_msgs[0]['content']['text'], """4 {(1, 'foo'): 3, (2, 'foo'): 6, (3, 'foo'): 9} 3\n""", stream_output_msgs)
228 # NOTE: No need to test bqplot here, since its the same mechanism as histogram which is already tested. If one works both do
229
230 def test_isystemtap_errors(self):
231 # Regular stderr
232 r, _, stream_output_msgs = self.execute_helper(
233 code='%%edit bad1\nprobe oneshot',
234 cell_id="a")
235 self.assertEqual(r['content']['status'], 'error', stream_output_msgs)
236 self.assertEqual(stream_output_msgs[0]['content']['name'], 'stderr')
237 self.assertIn("parse error: expected one of ', { } = +='", stream_output_msgs[0]['content']['text'])
238
239 # Python stderr
240 r, _, stream_output_msgs = self.execute_helper(
241 code='%%python bad2\nprint(x)',
242 cell_id="b")
243 self.assertEqual(r['content']['status'], 'error', stream_output_msgs)
244 self.assertEqual(stream_output_msgs[0]['content']['name'], 'stderr')
245 self.assertEqual("name 'x' is not defined\n", stream_output_msgs[0]['content']['text'])
246
247 def test_isystemtap_bash(self):
248 r, _, stream_output_msgs = self.execute_helper(
249 code='!echo Hello World',
250 cell_id="a")
251 self.assertEqual(r['content']['status'], 'ok', stream_output_msgs)
252 self.assertEqual(stream_output_msgs[0]['content']['name'], 'stdout')
253 self.assertEqual(stream_output_msgs[0]['content']['text'], 'Hello World\n')
254
255
256 if __name__ == '__main__':
257 runner = unittest.TextTestRunner(failfast=True)
258
259 suite = unittest.TestSuite()
260 suite.addTests([
261 ISystemtapKernelTests('test_isystemtap_basic'),
262 ISystemtapKernelTests('test_isystemtap_cmdline_args'),
263 ISystemtapKernelTests('test_isystemtap_cell_lifetimes'),
264 ISystemtapKernelTests('test_isystemtap_histograms'),
265 ISystemtapKernelTests('test_isystemtap_python_cells'),
266 ISystemtapKernelTests('test_isystemtap_errors'),
267 ISystemtapKernelTests('test_isystemtap_bash')
268 ])
269
270 res = runner.run(suite)
271 sys.exit(0 if res.wasSuccessful() else 1)