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