1# Copyright (C) 2018 Wind River Systems, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License version 2 as
5# published by the Free Software Foundation.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10# See the GNU General Public License for more details.
11#
12# You should have received a copy of the GNU General Public License
13# along with this program; if not, write to the Free Software
14# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
16import unittest
17import tempfile
18import os
19import bb
20
21import layerindexlib
22from layerindexlib.tests.common import LayersTest
23
24import logging
25
26class LayerIndexCookerTest(LayersTest):
27
28    def setUp(self):
29        LayersTest.setUp(self)
30
31        # Note this is NOT a comprehensive test of cooker, as we can't easily
32        # configure the test data.  But we can emulate the basics of the layer.conf
33        # files, so that is what we will do.
34
35        new_topdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata")
36        new_bbpath = os.path.join(new_topdir, "build")
37
38        self.d.setVar('TOPDIR', new_topdir)
39        self.d.setVar('BBPATH', new_bbpath)
40
41        self.d = bb.parse.handle("%s/conf/bblayers.conf" % new_bbpath, self.d, True)
42        for layer in self.d.getVar('BBLAYERS').split():
43            self.d = bb.parse.handle("%s/conf/layer.conf" % layer, self.d, True)
44
45        self.layerindex = layerindexlib.LayerIndex(self.d)
46        self.layerindex.load_layerindex('cooker://', load=['layerDependencies'])
47
48    def test_layerindex_is_empty(self):
49        self.assertFalse(self.layerindex.is_empty(), msg="Layerindex is not empty!")
50
51    def test_dependency_resolution(self):
52        # Verify depth first searching...
53        (dependencies, invalidnames) = self.layerindex.find_dependencies(names=['meta-python'])
54
55        first = True
56        for deplayerbranch in dependencies:
57            layerBranch = dependencies[deplayerbranch][0]
58            layerDeps = dependencies[deplayerbranch][1:]
59
60            if not first:
61                continue
62
63            first = False
64
65            # Top of the deps should be openembedded-core, since everything depends on it.
66            self.assertEqual(layerBranch.layer.name, "openembedded-core", msg='Top dependency not openembedded-core')
67
68            # meta-python should cause an openembedded-core dependency, if not assert!
69            for dep in layerDeps:
70                if dep.layer.name == 'meta-python':
71                    break
72            else:
73                self.assertTrue(False, msg='meta-python was not found')
74
75            # Only check the first element...
76            break
77        else:
78            if first:
79                # Empty list, this is bad.
80                self.assertTrue(False, msg='Empty list of dependencies')
81
82            # Last dep should be the requested item
83            layerBranch = dependencies[deplayerbranch][0]
84            self.assertEqual(layerBranch.layer.name, "meta-python", msg='Last dependency not meta-python')
85
86    def test_find_collection(self):
87        def _check(collection, expected):
88            self.logger.debug(1, "Looking for collection %s..." % collection)
89            result = self.layerindex.find_collection(collection)
90            if expected:
91                self.assertIsNotNone(result, msg="Did not find %s when it shouldn't be there" % collection)
92            else:
93                self.assertIsNone(result, msg="Found %s when it should be there" % collection)
94
95        tests = [ ('core', True),
96                  ('openembedded-core', False),
97                  ('networking-layer', True),
98                  ('meta-python', True),
99                  ('openembedded-layer', True),
100                  ('notpresent', False) ]
101
102        for collection,result in tests:
103            _check(collection, result)
104
105    def test_find_layerbranch(self):
106        def _check(name, expected):
107            self.logger.debug(1, "Looking for layerbranch %s..." % name)
108            result = self.layerindex.find_layerbranch(name)
109            if expected:
110                self.assertIsNotNone(result, msg="Did not find %s when it shouldn't be there" % collection)
111            else:
112                self.assertIsNone(result, msg="Found %s when it should be there" % collection)
113
114        tests = [ ('openembedded-core', True),
115                  ('core', False),
116                  ('networking-layer', True),
117                  ('meta-python', True),
118                  ('openembedded-layer', True),
119                  ('notpresent', False) ]
120
121        for collection,result in tests:
122            _check(collection, result)
123
124