Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | import unittest, os, shutil |
| 2 | from oeqa.oetest import oeRuntimeTest, skipModule |
| 3 | from oeqa.utils.decorators import * |
| 4 | from oeqa.utils.logparser import * |
| 5 | from oeqa.utils.httpserver import HTTPService |
| 6 | import bb |
| 7 | import glob |
| 8 | from oe.package_manager import RpmPkgsList |
| 9 | import subprocess |
| 10 | |
| 11 | def setUpModule(): |
| 12 | if not oeRuntimeTest.hasFeature("package-management"): |
| 13 | skipModule("Image doesn't have package management feature") |
| 14 | if not oeRuntimeTest.hasPackage("smart"): |
| 15 | skipModule("Image doesn't have smart installed") |
| 16 | if "package_rpm" != oeRuntimeTest.tc.d.getVar("PACKAGE_CLASSES", True).split()[0]: |
| 17 | skipModule("Rpm is not the primary package manager") |
| 18 | |
| 19 | class PtestRunnerTest(oeRuntimeTest): |
| 20 | |
| 21 | # a ptest log parser |
| 22 | def parse_ptest(self, logfile): |
| 23 | parser = Lparser(test_0_pass_regex="^PASS:(.+)", test_0_fail_regex="^FAIL:(.+)", section_0_begin_regex="^BEGIN: .*/(.+)/ptest", section_0_end_regex="^END: .*/(.+)/ptest") |
| 24 | parser.init() |
| 25 | result = Result() |
| 26 | |
| 27 | with open(logfile) as f: |
| 28 | for line in f: |
| 29 | result_tuple = parser.parse_line(line) |
| 30 | if not result_tuple: |
| 31 | continue |
| 32 | result_tuple = line_type, category, status, name = parser.parse_line(line) |
| 33 | |
| 34 | if line_type == 'section' and status == 'begin': |
| 35 | current_section = name |
| 36 | continue |
| 37 | |
| 38 | if line_type == 'section' and status == 'end': |
| 39 | current_section = None |
| 40 | continue |
| 41 | |
| 42 | if line_type == 'test' and status == 'pass': |
| 43 | result.store(current_section, name, status) |
| 44 | continue |
| 45 | |
| 46 | if line_type == 'test' and status == 'fail': |
| 47 | result.store(current_section, name, status) |
| 48 | continue |
| 49 | |
| 50 | result.sort_tests() |
| 51 | return result |
| 52 | |
| 53 | @classmethod |
| 54 | def setUpClass(self): |
| 55 | #note the existing channels that are on the board before creating new ones |
| 56 | # self.existingchannels = set() |
| 57 | # (status, result) = oeRuntimeTest.tc.target.run('smart channel --show | grep "\["', 0) |
| 58 | # for x in result.split("\n"): |
| 59 | # self.existingchannels.add(x) |
| 60 | self.repo_server = HTTPService(oeRuntimeTest.tc.d.getVar('DEPLOY_DIR', True), oeRuntimeTest.tc.target.server_ip) |
| 61 | self.repo_server.start() |
| 62 | |
| 63 | @classmethod |
| 64 | def tearDownClass(self): |
| 65 | self.repo_server.stop() |
| 66 | #remove created channels to be able to repeat the tests on same image |
| 67 | # (status, result) = oeRuntimeTest.tc.target.run('smart channel --show | grep "\["', 0) |
| 68 | # for x in result.split("\n"): |
| 69 | # if x not in self.existingchannels: |
| 70 | # oeRuntimeTest.tc.target.run('smart channel --remove '+x[1:-1]+' -y', 0) |
| 71 | |
| 72 | def add_smart_channel(self): |
| 73 | image_pkgtype = self.tc.d.getVar('IMAGE_PKGTYPE', True) |
| 74 | deploy_url = 'http://%s:%s/%s' %(self.target.server_ip, self.repo_server.port, image_pkgtype) |
| 75 | pkgarchs = self.tc.d.getVar('PACKAGE_ARCHS', True).replace("-","_").split() |
| 76 | for arch in os.listdir('%s/%s' % (self.repo_server.root_dir, image_pkgtype)): |
| 77 | if arch in pkgarchs: |
| 78 | self.target.run('smart channel -y --add {a} type=rpm-md baseurl={u}/{a}'.format(a=arch, u=deploy_url), 0) |
| 79 | self.target.run('smart update', 0) |
| 80 | |
| 81 | def install_complementary(self, globs=None): |
| 82 | installed_pkgs_file = os.path.join(oeRuntimeTest.tc.d.getVar('WORKDIR', True), |
| 83 | "installed_pkgs.txt") |
| 84 | self.pkgs_list = RpmPkgsList(oeRuntimeTest.tc.d, oeRuntimeTest.tc.d.getVar('IMAGE_ROOTFS', True), oeRuntimeTest.tc.d.getVar('arch_var', True), oeRuntimeTest.tc.d.getVar('os_var', True)) |
| 85 | with open(installed_pkgs_file, "w+") as installed_pkgs: |
| 86 | installed_pkgs.write(self.pkgs_list.list("arch")) |
| 87 | |
| 88 | cmd = [bb.utils.which(os.getenv('PATH'), "oe-pkgdata-util"), |
| 89 | "-p", oeRuntimeTest.tc.d.getVar('PKGDATA_DIR', True), "glob", installed_pkgs_file, |
| 90 | globs] |
| 91 | try: |
| 92 | bb.note("Installing complementary packages ...") |
| 93 | complementary_pkgs = subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
| 94 | except subprocess.CalledProcessError as e: |
| 95 | bb.fatal("Could not compute complementary packages list. Command " |
| 96 | "'%s' returned %d:\n%s" % |
| 97 | (' '.join(cmd), e.returncode, e.output)) |
| 98 | |
| 99 | return complementary_pkgs.split() |
| 100 | |
| 101 | def setUp(self): |
| 102 | self.ptest_log = os.path.join(oeRuntimeTest.tc.d.getVar("TEST_LOG_DIR",True), "ptest-%s.log" % oeRuntimeTest.tc.d.getVar('DATETIME', True)) |
| 103 | |
| 104 | @skipUnlessPassed('test_ssh') |
| 105 | def test_ptestrunner(self): |
| 106 | self.add_smart_channel() |
| 107 | (runnerstatus, result) = self.target.run('which ptest-runner', 0) |
| 108 | cond = oeRuntimeTest.hasPackage("ptest-runner") and oeRuntimeTest.hasFeature("ptest") and oeRuntimeTest.hasPackage("-ptest") and (runnerstatus != 0) |
| 109 | if cond: |
| 110 | self.install_packages(self.install_complementary("*-ptest")) |
| 111 | self.install_packages(['ptest-runner']) |
| 112 | |
| 113 | (runnerstatus, result) = self.target.run('/usr/bin/ptest-runner > /tmp/ptest.log 2>&1', 0) |
| 114 | #exit code is !=0 even if ptest-runner executes because some ptest tests fail. |
| 115 | self.assertTrue(runnerstatus != 127, msg="Cannot execute ptest-runner!") |
| 116 | self.target.copy_from('/tmp/ptest.log', self.ptest_log) |
| 117 | shutil.copyfile(self.ptest_log, "ptest.log") |
| 118 | |
| 119 | result = self.parse_ptest("ptest.log") |
| 120 | log_results_to_location = "./results" |
| 121 | if os.path.exists(log_results_to_location): |
| 122 | shutil.rmtree(log_results_to_location) |
| 123 | os.makedirs(log_results_to_location) |
| 124 | |
| 125 | result.log_as_files(log_results_to_location, test_status = ['pass','fail']) |