ksample.py 8.7 KB

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