xref: /openbmc/openbmc/poky/meta/lib/oeqa/sdk/case.py (revision c9f7865a)
1#
2# Copyright (C) 2016 Intel Corporation
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import subprocess
9
10from oeqa.core.case import OETestCase
11
12class OESDKTestCase(OETestCase):
13    def _run(self, cmd):
14        return subprocess.check_output(". %s > /dev/null; %s;" % \
15                (self.tc.sdk_env, cmd), shell=True, executable="/bin/bash",
16                stderr=subprocess.STDOUT, universal_newlines=True)
17
18    def fetch(self, workdir, dl_dir, url, archive=None):
19        if not archive:
20            from urllib.parse import urlparse
21            archive = os.path.basename(urlparse(url).path)
22
23        if dl_dir:
24            tarball = os.path.join(dl_dir, archive)
25            if os.path.exists(tarball):
26                return tarball
27
28        tarball = os.path.join(workdir, archive)
29        subprocess.check_output(["wget", "-O", tarball, url], stderr=subprocess.STDOUT)
30        return tarball
31
32    def check_elf(self, path, target_os=None, target_arch=None):
33        """
34        Verify that the ELF binary $path matches the specified target
35        OS/architecture, or if not specified the currently configured MACHINE's
36        OS/architecture.
37        """
38        import oe.qa, oe.elf
39
40        if not target_os or not target_arch:
41            output = self._run("echo $OECORE_TARGET_OS:$OECORE_TARGET_ARCH")
42            target_os, target_arch = output.strip().split(":")
43
44        machine_data = oe.elf.machine_dict(None)[target_os][target_arch]
45        (machine, osabi, abiversion, endian, bits) = machine_data
46
47        elf = oe.qa.ELFFile(path)
48        elf.open()
49
50        self.assertEqual(machine, elf.machine(),
51                         "Binary was %s but expected %s" %
52                         (oe.qa.elf_machine_to_string(elf.machine()), oe.qa.elf_machine_to_string(machine)))
53        self.assertEqual(bits, elf.abiSize())
54        self.assertEqual(endian, elf.isLittleEndian())
55