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