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