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