blob: 354cc9767325cb8091b4fd4b87b52db2176ac2f8 [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001import os
2import time
3
4from oeqa.runtime.case import OERuntimeTestCase
5from oeqa.core.decorator.depends import OETestDepends
6from oeqa.core.decorator.oeid import OETestID
7from oeqa.core.decorator.data import skipIfNotFeature
8
9# need some kernel fragments
10# echo "KERNEL_FEATURES_append += \" features\/kernel\-sample\/kernel\-sample.scc\"" >> local.conf
11class KSample(OERuntimeTestCase):
12 def cmd_and_check(self, cmd='', match_string=''):
13 status, output = self.target.run(cmd)
14 if not match_string:
15 # send cmd
16 msg = '%s failed, %s' % (cmd, output)
17 self.assertEqual(status, 0, msg=msg)
18 else:
19 # check result
20 result = ("%s" % match_string) in output
21 msg = output
22 self.assertTrue(result, msg)
23 self.assertEqual(status, 0, cmd)
24
25 def check_config(self, config_opt=''):
26 cmd = "zcat /proc/config.gz | grep %s" % config_opt
27 status, output = self.target.run(cmd)
28 result = ("%s=y" % config_opt) in output
29 if not result:
30 self.skipTest("%s is not set" % config_opt)
31
32 def check_module_exist(self, path='', module_name=''):
33 status, output = self.target.run("uname -r")
34 cmd = "ls " + "/lib/modules/" + output + "/kernel/samples/" + path + module_name
35 status, output = self.target.run(cmd)
36 if status != 0:
37 error_info = module_name + " doesn't exist"
38 self.skipTest(error_info)
39
40 def kfifo_func(self, name=''):
41 module_prename = name + "-example"
42 module_name = name + "-example.ko"
43 sysmbol_name = name + "_example"
44
45 # make sure if module exists
46 self.check_module_exist("kfifo/", module_name)
47 # modprobe
48 self.cmd_and_check("modprobe %s" % module_prename)
49 # lsmod
50 self.cmd_and_check("lsmod | grep %s | cut -d\' \' -f1" % sysmbol_name, sysmbol_name)
51 # check result
52 self.cmd_and_check("dmesg | grep \"test passed\" ", "test passed")
53 # rmmod
54 self.cmd_and_check("rmmod %s" % module_prename)
55
56 def kprobe_func(self, name=''):
57 # check config
58 self.check_config("CONFIG_KPROBES")
59
60 module_prename = name + "_example"
61 module_name = name + "_example.ko"
62 sysmbol_name = module_prename
63
64 # make sure if module exists
65 self.check_module_exist("kprobes/", module_name)
66 # modprobe
67 self.cmd_and_check("modprobe %s" % module_prename)
68 # lsmod
69 self.cmd_and_check("lsmod | grep %s | cut -d\' \' -f1" % sysmbol_name, sysmbol_name)
70 # check result
71 self.cmd_and_check("dmesg | grep Planted | head -n10", "Planted")
72 # rmmod
73 self.cmd_and_check("rmmod %s" % module_prename)
74
75 def kobject_func(self, name=''):
76 module_prename = name + "_example"
77 module_name = name + "-example.ko"
78 sysmbol_name = module_prename
79
80 # make sure if module exists
81 self.check_module_exist("kobject/", module_name)
82 # modprobe
83 self.cmd_and_check("modprobe %s" % module_prename)
84 # lsmod
85 self.cmd_and_check("lsmod | grep %s | cut -d\' \' -f1" % sysmbol_name, sysmbol_name)
86 # check result
87 self.cmd_and_check("ls /sys/kernel/%s/" % sysmbol_name, "bar")
88 # rmmod
89 self.cmd_and_check("rmmod %s" % module_prename)
90
91class KSampleTest(KSample):
92 # kfifo
93 @OETestDepends(['ssh.SSHTest.test_ssh'])
94 def test_kfifo_test(self):
95 index = ["dma", "bytestream", "inttype", "record"]
96 for i in index:
97 self.kfifo_func(i)
98
99 # kprobe
100 @OETestDepends(['ssh.SSHTest.test_ssh'])
101 def test_kprobe_test(self):
102 index = ["kprobe", "kretprobe"]
103 for i in index:
104 self.kprobe_func(i)
105
106 # kobject
107 @OETestDepends(['ssh.SSHTest.test_ssh'])
108 def test_kobject_test(self):
109 index = ["kobject", "kset"]
110 for i in index:
111 self.kobject_func(i)
112
113 #trace
114 @OETestDepends(['ssh.SSHTest.test_ssh'])
115 def test_trace_events(self):
116 # check config
117 self.check_config("CONFIG_TRACING_SUPPORT")
118 # make sure if module exists
119 self.check_module_exist("trace_events/", "trace-events-sample.ko")
120 # modprobe
121 self.cmd_and_check("modprobe trace-events-sample")
122 # lsmod
123 self.cmd_and_check("lsmod | grep trace_events_sample | cut -d\' \' -f1", "trace_events_sample")
124 # check dir
125 self.cmd_and_check("ls /sys/kernel/debug/tracing/events/ | grep sample-trace", "sample-trace")
126 # enable trace
127 self.cmd_and_check("echo 1 > /sys/kernel/debug/tracing/events/sample-trace/enable")
128 self.cmd_and_check("cat /sys/kernel/debug/tracing/events/sample-trace/enable")
129 # check result
130 status = 1
131 count = 0
132 while status != 0:
133 time.sleep(1)
134 status, output = self.target.run('cat /sys/kernel/debug/tracing/trace | grep hello | head -n1 | cut -d\':\' -f2')
135 if " foo_bar" in output:
136 break
137 count = count + 1
138 if count > 5:
139 self.assertTrue(False, "Time out when check result")
140 # disable trace
141 self.cmd_and_check("echo 0 > /sys/kernel/debug/tracing/events/sample-trace/enable")
142 # clean up trace
143 self.cmd_and_check("echo > /sys/kernel/debug/tracing/trace")
144 # rmmod
145 self.cmd_and_check("rmmod trace-events-sample")
146
147 @OETestDepends(['ssh.SSHTest.test_ssh'])
148 def test_trace_printk(self):
149 # check config
150 self.check_config("CONFIG_TRACING_SUPPORT")
151 # make sure if module exists
152 self.check_module_exist("trace_printk/", "trace-printk.ko")
153 # modprobe
154 self.cmd_and_check("modprobe trace-printk")
155 # lsmod
156 self.cmd_and_check("lsmod | grep trace_printk | cut -d\' \' -f1", "trace_printk")
157 # check result
158 self.cmd_and_check("cat /sys/kernel/debug/tracing/trace | grep trace_printk_irq_work | head -n1 | cut -d\':\' -f2", " trace_printk_irq_work")
159 # clean up trace
160 self.cmd_and_check("echo > /sys/kernel/debug/tracing/trace")
161 # rmmod
162 self.cmd_and_check("rmmod trace-printk")
163
164 # hw breakpoint
165 @OETestDepends(['ssh.SSHTest.test_ssh'])
166 def test_hw_breakpoint_example(self):
167 # check arch
168 status, output = self.target.run("uname -m")
169 result = ("x86" in output) or ("aarch64" in output)
170 if not result:
171 self.skipTest("the arch doesn't support hw breakpoint" % output)
172 # check config
173 self.check_config("CONFIG_KALLSYMS_ALL")
174 # make sure if module exists
175 self.check_module_exist("hw_breakpoint/", "data_breakpoint.ko")
176 # modprobe
177 self.cmd_and_check("modprobe data_breakpoint")
178 # lsmod
179 self.cmd_and_check("lsmod | grep data_breakpoint | cut -d\' \' -f1", "data_breakpoint")
180 # check result
181 self.cmd_and_check("cat /var/log/messages | grep sample_hbp_handler", "sample_hbp_handler")
182 # rmmod
183 self.cmd_and_check("rmmod data_breakpoint")
184
185 @OETestDepends(['ssh.SSHTest.test_ssh'])
186 def test_configfs_sample(self):
187 # check config
188 status, ret = self.target.run('zcat /proc/config.gz | grep CONFIG_CONFIGFS_FS')
189 if not ["CONFIG_CONFIGFS_FS=m" in ret or "CONFIG_CONFIGFS_FS=y" in ret]:
190 self.skipTest("CONFIG error")
191 # make sure if module exists
192 self.check_module_exist("configfs/", "configfs_sample.ko")
193 # modprobe
194 self.cmd_and_check("modprobe configfs_sample")
195 # lsmod
196 self.cmd_and_check("lsmod | grep configfs_sample | cut -d\' \' -f1 | head -n1", "configfs_sample")
197
198 status = 1
199 count = 0
200 while status != 0:
201 time.sleep(1)
202 status, ret = self.target.run('cat /sys/kernel/config/01-childless/description')
203 count = count + 1
204 if count > 200:
205 self.skipTest("Time out for check dir")
206
207 # rmmod
208 self.cmd_and_check("rmmod configfs_sample")
209
210 @OETestDepends(['ssh.SSHTest.test_ssh'])
211 def test_cn_test(self):
212 # make sure if module exists
213 self.check_module_exist("connector/", "cn_test.ko")
214 # modprobe
215 self.cmd_and_check("modprobe cn_test")
216 # lsmod
217 self.cmd_and_check("lsmod | grep cn_test | cut -d\' \' -f1", "cn_test")
218 # check result
219 self.cmd_and_check("cat /proc/net/connector | grep cn_test | head -n1 | cut -d\' \' -f1", "cn_test")
220 # rmmod
221 self.cmd_and_check("rmmod cn_test")