11a4b7ee2SBrad Bishop# Copyright (C) 2018 Wind River Systems, Inc.
21a4b7ee2SBrad Bishop#
3*c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
41a4b7ee2SBrad Bishop#
51a4b7ee2SBrad Bishop
61a4b7ee2SBrad Bishopimport unittest
71a4b7ee2SBrad Bishopimport tempfile
81a4b7ee2SBrad Bishopimport os
91a4b7ee2SBrad Bishopimport bb
101a4b7ee2SBrad Bishop
111a4b7ee2SBrad Bishopimport layerindexlib
121a4b7ee2SBrad Bishopfrom layerindexlib.tests.common import LayersTest
131a4b7ee2SBrad Bishop
141a4b7ee2SBrad Bishopimport logging
151a4b7ee2SBrad Bishop
161a4b7ee2SBrad Bishopclass LayerIndexCookerTest(LayersTest):
171a4b7ee2SBrad Bishop
181a4b7ee2SBrad Bishop    def setUp(self):
191a4b7ee2SBrad Bishop        LayersTest.setUp(self)
201a4b7ee2SBrad Bishop
211a4b7ee2SBrad Bishop        # Note this is NOT a comprehensive test of cooker, as we can't easily
221a4b7ee2SBrad Bishop        # configure the test data.  But we can emulate the basics of the layer.conf
231a4b7ee2SBrad Bishop        # files, so that is what we will do.
241a4b7ee2SBrad Bishop
251a4b7ee2SBrad Bishop        new_topdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata")
261a4b7ee2SBrad Bishop        new_bbpath = os.path.join(new_topdir, "build")
271a4b7ee2SBrad Bishop
281a4b7ee2SBrad Bishop        self.d.setVar('TOPDIR', new_topdir)
291a4b7ee2SBrad Bishop        self.d.setVar('BBPATH', new_bbpath)
301a4b7ee2SBrad Bishop
311a4b7ee2SBrad Bishop        self.d = bb.parse.handle("%s/conf/bblayers.conf" % new_bbpath, self.d, True)
321a4b7ee2SBrad Bishop        for layer in self.d.getVar('BBLAYERS').split():
331a4b7ee2SBrad Bishop            self.d = bb.parse.handle("%s/conf/layer.conf" % layer, self.d, True)
341a4b7ee2SBrad Bishop
351a4b7ee2SBrad Bishop        self.layerindex = layerindexlib.LayerIndex(self.d)
361a4b7ee2SBrad Bishop        self.layerindex.load_layerindex('cooker://', load=['layerDependencies'])
371a4b7ee2SBrad Bishop
381a4b7ee2SBrad Bishop    def test_layerindex_is_empty(self):
391a4b7ee2SBrad Bishop        self.assertFalse(self.layerindex.is_empty(), msg="Layerindex is not empty!")
401a4b7ee2SBrad Bishop
411a4b7ee2SBrad Bishop    def test_dependency_resolution(self):
421a4b7ee2SBrad Bishop        # Verify depth first searching...
431a4b7ee2SBrad Bishop        (dependencies, invalidnames) = self.layerindex.find_dependencies(names=['meta-python'])
441a4b7ee2SBrad Bishop
451a4b7ee2SBrad Bishop        first = True
461a4b7ee2SBrad Bishop        for deplayerbranch in dependencies:
471a4b7ee2SBrad Bishop            layerBranch = dependencies[deplayerbranch][0]
481a4b7ee2SBrad Bishop            layerDeps = dependencies[deplayerbranch][1:]
491a4b7ee2SBrad Bishop
501a4b7ee2SBrad Bishop            if not first:
511a4b7ee2SBrad Bishop                continue
521a4b7ee2SBrad Bishop
531a4b7ee2SBrad Bishop            first = False
541a4b7ee2SBrad Bishop
551a4b7ee2SBrad Bishop            # Top of the deps should be openembedded-core, since everything depends on it.
561a4b7ee2SBrad Bishop            self.assertEqual(layerBranch.layer.name, "openembedded-core", msg='Top dependency not openembedded-core')
571a4b7ee2SBrad Bishop
581a4b7ee2SBrad Bishop            # meta-python should cause an openembedded-core dependency, if not assert!
591a4b7ee2SBrad Bishop            for dep in layerDeps:
601a4b7ee2SBrad Bishop                if dep.layer.name == 'meta-python':
611a4b7ee2SBrad Bishop                    break
621a4b7ee2SBrad Bishop            else:
631a4b7ee2SBrad Bishop                self.assertTrue(False, msg='meta-python was not found')
641a4b7ee2SBrad Bishop
651a4b7ee2SBrad Bishop            # Only check the first element...
661a4b7ee2SBrad Bishop            break
671a4b7ee2SBrad Bishop        else:
681a4b7ee2SBrad Bishop            if first:
691a4b7ee2SBrad Bishop                # Empty list, this is bad.
701a4b7ee2SBrad Bishop                self.assertTrue(False, msg='Empty list of dependencies')
711a4b7ee2SBrad Bishop
721a4b7ee2SBrad Bishop            # Last dep should be the requested item
731a4b7ee2SBrad Bishop            layerBranch = dependencies[deplayerbranch][0]
741a4b7ee2SBrad Bishop            self.assertEqual(layerBranch.layer.name, "meta-python", msg='Last dependency not meta-python')
751a4b7ee2SBrad Bishop
761a4b7ee2SBrad Bishop    def test_find_collection(self):
771a4b7ee2SBrad Bishop        def _check(collection, expected):
781a4b7ee2SBrad Bishop            self.logger.debug(1, "Looking for collection %s..." % collection)
791a4b7ee2SBrad Bishop            result = self.layerindex.find_collection(collection)
801a4b7ee2SBrad Bishop            if expected:
811a4b7ee2SBrad Bishop                self.assertIsNotNone(result, msg="Did not find %s when it shouldn't be there" % collection)
821a4b7ee2SBrad Bishop            else:
831a4b7ee2SBrad Bishop                self.assertIsNone(result, msg="Found %s when it should be there" % collection)
841a4b7ee2SBrad Bishop
851a4b7ee2SBrad Bishop        tests = [ ('core', True),
861a4b7ee2SBrad Bishop                  ('openembedded-core', False),
871a4b7ee2SBrad Bishop                  ('networking-layer', True),
881a4b7ee2SBrad Bishop                  ('meta-python', True),
891a4b7ee2SBrad Bishop                  ('openembedded-layer', True),
901a4b7ee2SBrad Bishop                  ('notpresent', False) ]
911a4b7ee2SBrad Bishop
921a4b7ee2SBrad Bishop        for collection,result in tests:
931a4b7ee2SBrad Bishop            _check(collection, result)
941a4b7ee2SBrad Bishop
951a4b7ee2SBrad Bishop    def test_find_layerbranch(self):
961a4b7ee2SBrad Bishop        def _check(name, expected):
971a4b7ee2SBrad Bishop            self.logger.debug(1, "Looking for layerbranch %s..." % name)
981a4b7ee2SBrad Bishop            result = self.layerindex.find_layerbranch(name)
991a4b7ee2SBrad Bishop            if expected:
1001a4b7ee2SBrad Bishop                self.assertIsNotNone(result, msg="Did not find %s when it shouldn't be there" % collection)
1011a4b7ee2SBrad Bishop            else:
1021a4b7ee2SBrad Bishop                self.assertIsNone(result, msg="Found %s when it should be there" % collection)
1031a4b7ee2SBrad Bishop
1041a4b7ee2SBrad Bishop        tests = [ ('openembedded-core', True),
1051a4b7ee2SBrad Bishop                  ('core', False),
1061a4b7ee2SBrad Bishop                  ('networking-layer', True),
1071a4b7ee2SBrad Bishop                  ('meta-python', True),
1081a4b7ee2SBrad Bishop                  ('openembedded-layer', True),
1091a4b7ee2SBrad Bishop                  ('notpresent', False) ]
1101a4b7ee2SBrad Bishop
1111a4b7ee2SBrad Bishop        for collection,result in tests:
1121a4b7ee2SBrad Bishop            _check(collection, result)
1131a4b7ee2SBrad Bishop
114