1*96ff1984SBrad Bishop#
2*96ff1984SBrad Bishop# BitBake Tests for runqueue task processing
3*96ff1984SBrad Bishop#
4*96ff1984SBrad Bishop# Copyright (C) 2019 Richard Purdie
5*96ff1984SBrad Bishop#
6*96ff1984SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
7*96ff1984SBrad Bishop#
8*96ff1984SBrad Bishop
9*96ff1984SBrad Bishopimport unittest
10*96ff1984SBrad Bishopimport bb
11*96ff1984SBrad Bishopimport os
12*96ff1984SBrad Bishopimport tempfile
13*96ff1984SBrad Bishopimport subprocess
14*96ff1984SBrad Bishop
15*96ff1984SBrad Bishop#
16*96ff1984SBrad Bishop# TODO:
17*96ff1984SBrad Bishop# Add tests on task ordering (X happens before Y after Z)
18*96ff1984SBrad Bishop#
19*96ff1984SBrad Bishop
20*96ff1984SBrad Bishopclass RunQueueTests(unittest.TestCase):
21*96ff1984SBrad Bishop
22*96ff1984SBrad Bishop    alltasks = ['package', 'fetch', 'unpack', 'patch', 'prepare_recipe_sysroot', 'configure',
23*96ff1984SBrad Bishop                'compile', 'install', 'packagedata', 'package_qa', 'package_write_rpm', 'package_write_ipk',
24*96ff1984SBrad Bishop                'populate_sysroot', 'build']
25*96ff1984SBrad Bishop    a1_sstatevalid = "a1:do_package a1:do_package_qa a1:do_packagedata a1:do_package_write_ipk a1:do_package_write_rpm a1:do_populate_lic a1:do_populate_sysroot"
26*96ff1984SBrad Bishop    b1_sstatevalid = "b1:do_package b1:do_package_qa b1:do_packagedata b1:do_package_write_ipk b1:do_package_write_rpm b1:do_populate_lic b1:do_populate_sysroot"
27*96ff1984SBrad Bishop
28*96ff1984SBrad Bishop    def run_bitbakecmd(self, cmd, builddir, sstatevalid="", slowtasks="", extraenv=None):
29*96ff1984SBrad Bishop        env = os.environ.copy()
30*96ff1984SBrad Bishop        env["BBPATH"] = os.path.realpath(os.path.join(os.path.dirname(__file__), "runqueue-tests"))
31*96ff1984SBrad Bishop        env["BB_ENV_EXTRAWHITE"] = "SSTATEVALID SLOWTASKS"
32*96ff1984SBrad Bishop        env["SSTATEVALID"] = sstatevalid
33*96ff1984SBrad Bishop        env["SLOWTASKS"] = slowtasks
34*96ff1984SBrad Bishop        if extraenv:
35*96ff1984SBrad Bishop            for k in extraenv:
36*96ff1984SBrad Bishop                env[k] = extraenv[k]
37*96ff1984SBrad Bishop                env["BB_ENV_EXTRAWHITE"] = env["BB_ENV_EXTRAWHITE"] + " " + k
38*96ff1984SBrad Bishop        try:
39*96ff1984SBrad Bishop            output = subprocess.check_output(cmd, env=env, stderr=subprocess.STDOUT,universal_newlines=True, cwd=builddir)
40*96ff1984SBrad Bishop        except subprocess.CalledProcessError as e:
41*96ff1984SBrad Bishop            self.fail("Command %s failed with %s" % (cmd, e.output))
42*96ff1984SBrad Bishop        tasks = []
43*96ff1984SBrad Bishop        with open(builddir + "/task.log", "r") as f:
44*96ff1984SBrad Bishop            tasks = [line.rstrip() for line in f]
45*96ff1984SBrad Bishop        return tasks
46*96ff1984SBrad Bishop
47*96ff1984SBrad Bishop    def test_no_setscenevalid(self):
48*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
49*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
50*96ff1984SBrad Bishop            sstatevalid = ""
51*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
52*96ff1984SBrad Bishop            expected = ['a1:' + x for x in self.alltasks]
53*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
54*96ff1984SBrad Bishop
55*96ff1984SBrad Bishop    def test_single_setscenevalid(self):
56*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
57*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
58*96ff1984SBrad Bishop            sstatevalid = "a1:do_package"
59*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
60*96ff1984SBrad Bishop            expected = ['a1:package_setscene', 'a1:fetch', 'a1:unpack', 'a1:patch', 'a1:prepare_recipe_sysroot', 'a1:configure',
61*96ff1984SBrad Bishop                        'a1:compile', 'a1:install', 'a1:packagedata', 'a1:package_qa', 'a1:package_write_rpm', 'a1:package_write_ipk',
62*96ff1984SBrad Bishop                        'a1:populate_sysroot', 'a1:build']
63*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
64*96ff1984SBrad Bishop
65*96ff1984SBrad Bishop    def test_intermediate_setscenevalid(self):
66*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
67*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
68*96ff1984SBrad Bishop            sstatevalid = "a1:do_package a1:do_populate_sysroot"
69*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
70*96ff1984SBrad Bishop            expected = ['a1:package_setscene', 'a1:packagedata', 'a1:package_qa', 'a1:package_write_rpm', 'a1:package_write_ipk',
71*96ff1984SBrad Bishop                        'a1:populate_sysroot_setscene', 'a1:build']
72*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
73*96ff1984SBrad Bishop
74*96ff1984SBrad Bishop    def test_intermediate_notcovered(self):
75*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
76*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
77*96ff1984SBrad Bishop            sstatevalid = "a1:do_package_qa a1:do_packagedata a1:do_package_write_ipk a1:do_package_write_rpm a1:do_populate_lic a1:do_populate_sysroot"
78*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
79*96ff1984SBrad Bishop            expected = ['a1:package_write_ipk_setscene', 'a1:package_write_rpm_setscene', 'a1:packagedata_setscene',
80*96ff1984SBrad Bishop                        'a1:package_qa_setscene', 'a1:build', 'a1:populate_sysroot_setscene']
81*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
82*96ff1984SBrad Bishop
83*96ff1984SBrad Bishop    def test_all_setscenevalid(self):
84*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
85*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
86*96ff1984SBrad Bishop            sstatevalid = self.a1_sstatevalid
87*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
88*96ff1984SBrad Bishop            expected = ['a1:package_write_ipk_setscene', 'a1:package_write_rpm_setscene', 'a1:packagedata_setscene',
89*96ff1984SBrad Bishop                        'a1:package_qa_setscene', 'a1:build', 'a1:populate_sysroot_setscene']
90*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
91*96ff1984SBrad Bishop
92*96ff1984SBrad Bishop    def test_no_settasks(self):
93*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
94*96ff1984SBrad Bishop            cmd = ["bitbake", "a1", "-c", "patch"]
95*96ff1984SBrad Bishop            sstatevalid = self.a1_sstatevalid
96*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
97*96ff1984SBrad Bishop            expected = ['a1:fetch', 'a1:unpack', 'a1:patch']
98*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
99*96ff1984SBrad Bishop
100*96ff1984SBrad Bishop    def test_mix_covered_notcovered(self):
101*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
102*96ff1984SBrad Bishop            cmd = ["bitbake", "a1:do_patch", "a1:do_populate_sysroot"]
103*96ff1984SBrad Bishop            sstatevalid = self.a1_sstatevalid
104*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
105*96ff1984SBrad Bishop            expected = ['a1:fetch', 'a1:unpack', 'a1:patch', 'a1:populate_sysroot_setscene']
106*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
107*96ff1984SBrad Bishop
108*96ff1984SBrad Bishop
109*96ff1984SBrad Bishop    # Test targets with intermediate setscene tasks alongside a target with no intermediate setscene tasks
110*96ff1984SBrad Bishop    def test_mixed_direct_tasks_setscene_tasks(self):
111*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
112*96ff1984SBrad Bishop            cmd = ["bitbake", "c1:do_patch", "a1"]
113*96ff1984SBrad Bishop            sstatevalid = self.a1_sstatevalid
114*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
115*96ff1984SBrad Bishop            expected = ['c1:fetch', 'c1:unpack', 'c1:patch', 'a1:package_write_ipk_setscene', 'a1:package_write_rpm_setscene', 'a1:packagedata_setscene',
116*96ff1984SBrad Bishop                        'a1:package_qa_setscene', 'a1:build', 'a1:populate_sysroot_setscene']
117*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
118*96ff1984SBrad Bishop
119*96ff1984SBrad Bishop    # This test slows down the execution of do_package_setscene until after other real tasks have
120*96ff1984SBrad Bishop    # started running which tests for a bug where tasks were being lost from the buildable list of real
121*96ff1984SBrad Bishop    # tasks if they weren't in tasks_covered or tasks_notcovered
122*96ff1984SBrad Bishop    def test_slow_setscene(self):
123*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
124*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
125*96ff1984SBrad Bishop            sstatevalid = "a1:do_package"
126*96ff1984SBrad Bishop            slowtasks = "a1:package_setscene"
127*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid, slowtasks)
128*96ff1984SBrad Bishop            expected = ['a1:package_setscene', 'a1:fetch', 'a1:unpack', 'a1:patch', 'a1:prepare_recipe_sysroot', 'a1:configure',
129*96ff1984SBrad Bishop                        'a1:compile', 'a1:install', 'a1:packagedata', 'a1:package_qa', 'a1:package_write_rpm', 'a1:package_write_ipk',
130*96ff1984SBrad Bishop                        'a1:populate_sysroot', 'a1:build']
131*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
132*96ff1984SBrad Bishop
133*96ff1984SBrad Bishop    def test_setscenewhitelist(self):
134*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
135*96ff1984SBrad Bishop            cmd = ["bitbake", "a1"]
136*96ff1984SBrad Bishop            extraenv = {
137*96ff1984SBrad Bishop                "BB_SETSCENE_ENFORCE" : "1",
138*96ff1984SBrad Bishop                "BB_SETSCENE_ENFORCE_WHITELIST" : "a1:do_package_write_rpm a1:do_build"
139*96ff1984SBrad Bishop            }
140*96ff1984SBrad Bishop            sstatevalid = "a1:do_package a1:do_package_qa a1:do_packagedata a1:do_package_write_ipk a1:do_populate_lic a1:do_populate_sysroot"
141*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid, extraenv=extraenv)
142*96ff1984SBrad Bishop            expected = ['a1:packagedata_setscene', 'a1:package_qa_setscene', 'a1:package_write_ipk_setscene',
143*96ff1984SBrad Bishop                        'a1:populate_sysroot_setscene', 'a1:package_setscene']
144*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
145*96ff1984SBrad Bishop
146*96ff1984SBrad Bishop    # Tests for problems with dependencies between setscene tasks
147*96ff1984SBrad Bishop    def test_no_setscenevalid_harddeps(self):
148*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
149*96ff1984SBrad Bishop            cmd = ["bitbake", "d1"]
150*96ff1984SBrad Bishop            sstatevalid = ""
151*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
152*96ff1984SBrad Bishop            expected = ['a1:package', 'a1:fetch', 'a1:unpack', 'a1:patch', 'a1:prepare_recipe_sysroot', 'a1:configure',
153*96ff1984SBrad Bishop                        'a1:compile', 'a1:install', 'a1:packagedata', 'a1:package_write_rpm', 'a1:package_write_ipk',
154*96ff1984SBrad Bishop                        'a1:populate_sysroot', 'd1:package', 'd1:fetch', 'd1:unpack', 'd1:patch', 'd1:prepare_recipe_sysroot', 'd1:configure',
155*96ff1984SBrad Bishop                        'd1:compile', 'd1:install', 'd1:packagedata', 'd1:package_qa', 'd1:package_write_rpm', 'd1:package_write_ipk',
156*96ff1984SBrad Bishop                        'd1:populate_sysroot', 'd1:build']
157*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
158*96ff1984SBrad Bishop
159*96ff1984SBrad Bishop    def test_no_setscenevalid_withdeps(self):
160*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
161*96ff1984SBrad Bishop            cmd = ["bitbake", "b1"]
162*96ff1984SBrad Bishop            sstatevalid = ""
163*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
164*96ff1984SBrad Bishop            expected = ['a1:' + x for x in self.alltasks] + ['b1:' + x for x in self.alltasks]
165*96ff1984SBrad Bishop            expected.remove('a1:build')
166*96ff1984SBrad Bishop            expected.remove('a1:package_qa')
167*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
168*96ff1984SBrad Bishop
169*96ff1984SBrad Bishop    def test_single_a1_setscenevalid_withdeps(self):
170*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
171*96ff1984SBrad Bishop            cmd = ["bitbake", "b1"]
172*96ff1984SBrad Bishop            sstatevalid = "a1:do_package"
173*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
174*96ff1984SBrad Bishop            expected = ['a1:package_setscene', 'a1:fetch', 'a1:unpack', 'a1:patch', 'a1:prepare_recipe_sysroot', 'a1:configure',
175*96ff1984SBrad Bishop                        'a1:compile', 'a1:install', 'a1:packagedata', 'a1:package_write_rpm', 'a1:package_write_ipk',
176*96ff1984SBrad Bishop                        'a1:populate_sysroot'] + ['b1:' + x for x in self.alltasks]
177*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
178*96ff1984SBrad Bishop
179*96ff1984SBrad Bishop    def test_single_b1_setscenevalid_withdeps(self):
180*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
181*96ff1984SBrad Bishop            cmd = ["bitbake", "b1"]
182*96ff1984SBrad Bishop            sstatevalid = "b1:do_package"
183*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
184*96ff1984SBrad Bishop            expected = ['a1:package', 'a1:fetch', 'a1:unpack', 'a1:patch', 'a1:prepare_recipe_sysroot', 'a1:configure',
185*96ff1984SBrad Bishop                        'a1:compile', 'a1:install', 'a1:packagedata', 'a1:package_write_rpm', 'a1:package_write_ipk',
186*96ff1984SBrad Bishop                        'a1:populate_sysroot', 'b1:package_setscene'] + ['b1:' + x for x in self.alltasks]
187*96ff1984SBrad Bishop            expected.remove('b1:package')
188*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
189*96ff1984SBrad Bishop
190*96ff1984SBrad Bishop    def test_intermediate_setscenevalid_withdeps(self):
191*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
192*96ff1984SBrad Bishop            cmd = ["bitbake", "b1"]
193*96ff1984SBrad Bishop            sstatevalid = "a1:do_package a1:do_populate_sysroot b1:do_package"
194*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
195*96ff1984SBrad Bishop            expected = ['a1:package_setscene', 'a1:packagedata', 'a1:package_write_rpm', 'a1:package_write_ipk',
196*96ff1984SBrad Bishop                        'a1:populate_sysroot_setscene', 'b1:package_setscene'] + ['b1:' + x for x in self.alltasks]
197*96ff1984SBrad Bishop            expected.remove('b1:package')
198*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
199*96ff1984SBrad Bishop
200*96ff1984SBrad Bishop    def test_all_setscenevalid_withdeps(self):
201*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
202*96ff1984SBrad Bishop            cmd = ["bitbake", "b1"]
203*96ff1984SBrad Bishop            sstatevalid = self.a1_sstatevalid + " " + self.b1_sstatevalid
204*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid)
205*96ff1984SBrad Bishop            expected = ['a1:package_write_ipk_setscene', 'a1:package_write_rpm_setscene', 'a1:packagedata_setscene',
206*96ff1984SBrad Bishop                        'b1:build', 'a1:populate_sysroot_setscene', 'b1:package_write_ipk_setscene', 'b1:package_write_rpm_setscene',
207*96ff1984SBrad Bishop                        'b1:packagedata_setscene', 'b1:package_qa_setscene', 'b1:populate_sysroot_setscene']
208*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
209*96ff1984SBrad Bishop
210*96ff1984SBrad Bishop    def test_multiconfig_setscene_optimise(self):
211*96ff1984SBrad Bishop        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
212*96ff1984SBrad Bishop            extraenv = {
213*96ff1984SBrad Bishop                "BBMULTICONFIG" : "mc1 mc2",
214*96ff1984SBrad Bishop                "BB_SIGNATURE_HANDLER" : "basic"
215*96ff1984SBrad Bishop            }
216*96ff1984SBrad Bishop            cmd = ["bitbake", "b1", "mc:mc1:b1", "mc:mc2:b1"]
217*96ff1984SBrad Bishop            setscenetasks = ['package_write_ipk_setscene', 'package_write_rpm_setscene', 'packagedata_setscene',
218*96ff1984SBrad Bishop                             'populate_sysroot_setscene', 'package_qa_setscene']
219*96ff1984SBrad Bishop            sstatevalid = ""
220*96ff1984SBrad Bishop            tasks = self.run_bitbakecmd(cmd, tempdir, sstatevalid, extraenv=extraenv)
221*96ff1984SBrad Bishop            expected = ['a1:' + x for x in self.alltasks] + ['b1:' + x for x in self.alltasks] + \
222*96ff1984SBrad Bishop                       ['mc1:b1:' + x for x in setscenetasks] + ['mc1:a1:' + x for x in setscenetasks] + \
223*96ff1984SBrad Bishop                       ['mc2:b1:' + x for x in setscenetasks] + ['mc2:a1:' + x for x in setscenetasks] + \
224*96ff1984SBrad Bishop                       ['mc1:b1:build', 'mc2:b1:build']
225*96ff1984SBrad Bishop            for x in ['mc1:a1:package_qa_setscene', 'mc2:a1:package_qa_setscene', 'a1:build', 'a1:package_qa']:
226*96ff1984SBrad Bishop                expected.remove(x)
227*96ff1984SBrad Bishop            self.assertEqual(set(tasks), set(expected))
228*96ff1984SBrad Bishop
229