ksample.py 8.7 KB

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