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