1#
2# SPDX-License-Identifier: MIT
3#
4
5import tempfile
6import textwrap
7import bb.tinfoil
8import oe.path
9from oeqa.selftest.case import OESelftestTestCase
10from oeqa.utils.commands import bitbake
11
12class Fetch(OESelftestTestCase):
13    def test_git_mirrors(self):
14        """
15        Verify that the git fetcher will fall back to the HTTP mirrors. The
16        recipe needs to be one that we have on the Yocto Project source mirror
17        and is hosted in git.
18        """
19
20        # TODO: mktempd instead of hardcoding
21        dldir = os.path.join(self.builddir, "download-git-mirrors")
22        self.track_for_cleanup(dldir)
23
24        # No mirrors, should use git to fetch successfully
25        features = """
26DL_DIR = "%s"
27MIRRORS:forcevariable = ""
28PREMIRRORS:forcevariable = ""
29""" % dldir
30        self.write_config(features)
31        oe.path.remove(dldir, recurse=True)
32        bitbake("dbus-wait -c fetch -f")
33
34        # No mirrors and broken git, should fail
35        features = """
36DL_DIR = "%s"
37GIT_PROXY_COMMAND = "false"
38MIRRORS:forcevariable = ""
39PREMIRRORS:forcevariable = ""
40""" % dldir
41        self.write_config(features)
42        oe.path.remove(dldir, recurse=True)
43        with self.assertRaises(AssertionError):
44            bitbake("dbus-wait -c fetch -f")
45
46        # Broken git but a specific mirror
47        features = """
48DL_DIR = "%s"
49GIT_PROXY_COMMAND = "false"
50MIRRORS:forcevariable = "git://.*/.* http://downloads.yoctoproject.org/mirror/sources/"
51""" % dldir
52        self.write_config(features)
53        oe.path.remove(dldir, recurse=True)
54        bitbake("dbus-wait -c fetch -f")
55
56
57class Dependencies(OESelftestTestCase):
58    def write_recipe(self, content, tempdir):
59        f = os.path.join(tempdir, "test.bb")
60        with open(f, "w") as fd:
61            fd.write(content)
62        return f
63
64    def test_dependencies(self):
65        """
66        Verify that the correct dependencies are generated for specific SRC_URI entries.
67        """
68
69        with bb.tinfoil.Tinfoil() as tinfoil, tempfile.TemporaryDirectory(prefix="selftest-fetch") as tempdir:
70            tinfoil.prepare(config_only=False, quiet=2)
71
72            r = """
73            LICENSE="CLOSED"
74            SRC_URI="http://example.com/tarball.zip"
75            """
76            f = self.write_recipe(textwrap.dedent(r), tempdir)
77            d = tinfoil.parse_recipe_file(f)
78            self.assertIn("wget-native", d.getVarFlag("do_fetch", "depends"))
79            self.assertIn("unzip-native", d.getVarFlag("do_unpack", "depends"))
80
81            # Verify that the downloadfilename overrides the URI
82            r = """
83            LICENSE="CLOSED"
84            SRC_URI="https://example.com/tarball;downloadfilename=something.zip"
85            """
86            f = self.write_recipe(textwrap.dedent(r), tempdir)
87            d = tinfoil.parse_recipe_file(f)
88            self.assertIn("wget-native", d.getVarFlag("do_fetch", "depends"))
89            self.assertIn("unzip-native", d.getVarFlag("do_unpack", "depends") or "")
90
91            r = """
92            LICENSE="CLOSED"
93            SRC_URI="ftp://example.com/tarball.lz"
94            """
95            f = self.write_recipe(textwrap.dedent(r), tempdir)
96            d = tinfoil.parse_recipe_file(f)
97            self.assertIn("wget-native", d.getVarFlag("do_fetch", "depends"))
98            self.assertIn("lzip-native", d.getVarFlag("do_unpack", "depends"))
99
100            r = """
101            LICENSE="CLOSED"
102            SRC_URI="git://example.com/repo"
103            """
104            f = self.write_recipe(textwrap.dedent(r), tempdir)
105            d = tinfoil.parse_recipe_file(f)
106            self.assertIn("git-native", d.getVarFlag("do_fetch", "depends"))
107