1# Copyright (C) 2018 Wind River Systems, Inc.
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5
6import os
7import bb
8
9import layerindexlib
10from layerindexlib.tests.common import LayersTest
11
12
13class LayerIndexCookerTest(LayersTest):
14
15    def setUp(self):
16        LayersTest.setUp(self)
17
18        # Note this is NOT a comprehensive test of cooker, as we can't easily
19        # configure the test data.  But we can emulate the basics of the layer.conf
20        # files, so that is what we will do.
21
22        new_topdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata")
23        new_bbpath = os.path.join(new_topdir, "build")
24
25        self.d.setVar('TOPDIR', new_topdir)
26        self.d.setVar('BBPATH', new_bbpath)
27
28        self.d = bb.parse.handle("%s/conf/bblayers.conf" % new_bbpath, self.d, True)
29        for layer in self.d.getVar('BBLAYERS').split():
30            self.d = bb.parse.handle("%s/conf/layer.conf" % layer, self.d, True)
31
32        self.layerindex = layerindexlib.LayerIndex(self.d)
33        self.layerindex.load_layerindex('cooker://', load=['layerDependencies'])
34
35    def test_layerindex_is_empty(self):
36        self.assertFalse(self.layerindex.is_empty(), msg="Layerindex is not empty!")
37
38    def test_dependency_resolution(self):
39        # Verify depth first searching...
40        (dependencies, invalidnames) = self.layerindex.find_dependencies(names=['meta-python'])
41
42        first = True
43        for deplayerbranch in dependencies:
44            layerBranch = dependencies[deplayerbranch][0]
45            layerDeps = dependencies[deplayerbranch][1:]
46
47            if not first:
48                continue
49
50            first = False
51
52            # Top of the deps should be openembedded-core, since everything depends on it.
53            self.assertEqual(layerBranch.layer.name, "openembedded-core", msg='Top dependency not openembedded-core')
54
55            # meta-python should cause an openembedded-core dependency, if not assert!
56            for dep in layerDeps:
57                if dep.layer.name == 'meta-python':
58                    break
59            else:
60                self.assertTrue(False, msg='meta-python was not found')
61
62            # Only check the first element...
63            break
64        else:
65            if first:
66                # Empty list, this is bad.
67                self.assertTrue(False, msg='Empty list of dependencies')
68
69            # Last dep should be the requested item
70            layerBranch = dependencies[deplayerbranch][0]
71            self.assertEqual(layerBranch.layer.name, "meta-python", msg='Last dependency not meta-python')
72
73    def test_find_collection(self):
74        def _check(collection, expected):
75            self.logger.debug("Looking for collection %s..." % collection)
76            result = self.layerindex.find_collection(collection)
77            if expected:
78                self.assertIsNotNone(result, msg="Did not find %s when it shouldn't be there" % collection)
79            else:
80                self.assertIsNone(result, msg="Found %s when it should be there" % collection)
81
82        tests = [ ('core', True),
83                  ('openembedded-core', False),
84                  ('networking-layer', True),
85                  ('meta-python', True),
86                  ('openembedded-layer', True),
87                  ('notpresent', False) ]
88
89        for collection,result in tests:
90            _check(collection, result)
91
92    def test_find_layerbranch(self):
93        def _check(name, expected):
94            self.logger.debug("Looking for layerbranch %s..." % name)
95            result = self.layerindex.find_layerbranch(name)
96            if expected:
97                self.assertIsNotNone(result, msg="Did not find %s when it shouldn't be there" % collection)
98            else:
99                self.assertIsNone(result, msg="Found %s when it should be there" % collection)
100
101        tests = [ ('openembedded-core', True),
102                  ('core', False),
103                  ('networking-layer', True),
104                  ('meta-python', True),
105                  ('openembedded-layer', True),
106                  ('notpresent', False) ]
107
108        for collection,result in tests:
109            _check(collection, result)
110
111