blob: 719266e6d6e6171a801d2fadeb56ce164aae9c64 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#! /usr/bin/env python
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# BitBake Toaster Implementation
6#
7# Copyright (C) 2013-2015 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22"""Test cases for Toaster ORM."""
23
24from django.test import TestCase, TransactionTestCase
25from orm.models import LocalLayerSource, LayerIndexLayerSource, ImportedLayerSource, LayerSource
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026from orm.models import Branch, LayerVersionDependency
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
Patrick Williamsf1e5d692016-03-30 15:21:19 -050028from orm.models import Project, Layer, Layer_Version, Branch, ProjectLayer
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029from orm.models import Release, ReleaseLayerSourcePriority, BitbakeVersion
30
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031from django.db import IntegrityError
32
33import os
34
35# set TTS_LAYER_INDEX to the base url to use a different instance of the layer index
36
37class LayerSourceVerifyInheritanceSaveLoad(TestCase):
38 """
39 Tests to verify inheritance for the LayerSource proxy-inheritance classes.
40 """
41 def test_object_creation(self):
42 """Test LayerSource object creation."""
43 for name, sourcetype in [("a1", LayerSource.TYPE_LOCAL),
44 ("a2", LayerSource.TYPE_LAYERINDEX),
45 ("a3", LayerSource.TYPE_IMPORTED)]:
46 LayerSource.objects.create(name=name, sourcetype=sourcetype)
47
48 objects = LayerSource.objects.all()
49 self.assertTrue(isinstance(objects[0], LocalLayerSource))
50 self.assertTrue(isinstance(objects[1], LayerIndexLayerSource))
51 self.assertTrue(isinstance(objects[2], ImportedLayerSource))
52
53 def test_duplicate_error(self):
54 """Test creation of duplicate LayerSource objects."""
55 stype = LayerSource.TYPE_LOCAL
56 LayerSource.objects.create(name="a1", sourcetype=stype)
57 with self.assertRaises(IntegrityError):
58 LayerSource.objects.create(name="a1", sourcetype=stype)
59
60
61class LILSUpdateTestCase(TransactionTestCase):
62 """Test Layer Source update."""
63
64 def setUp(self):
65 """Create release."""
66 bbv = BitbakeVersion.objects.create(\
67 name="master", giturl="git://git.openembedded.org/bitbake")
68 Release.objects.create(name="default-release", bitbake_version=bbv,
69 branch_name="master")
70
71 def test_update(self):
72 """Check if LayerSource.update can fetch branches."""
73 url = os.getenv("TTS_LAYER_INDEX",
74 default="http://layers.openembedded.org/")
75
76 lsobj = LayerSource.objects.create(\
77 name="b1", sourcetype=LayerSource.TYPE_LAYERINDEX,
78 apiurl=url + "layerindex/api/")
79 lsobj.update()
80 self.assertTrue(lsobj.branch_set.all().count() > 0,
81 "no branches fetched")
82
83class LayerVersionEquivalenceTestCase(TestCase):
84 """Verify Layer_Version priority selection."""
85
86 def setUp(self):
87 """Create required objects."""
88 # create layer source
89 self.lsrc = LayerSource.objects.create(name="dummy-layersource",
90 sourcetype=LayerSource.TYPE_LOCAL)
91 # create release
92 bbv = BitbakeVersion.objects.create(\
93 name="master", giturl="git://git.openembedded.org/bitbake")
94 self.release = Release.objects.create(name="default-release",
95 bitbake_version=bbv,
96 branch_name="master")
97 # attach layer source to release
98 ReleaseLayerSourcePriority.objects.create(\
99 release=self.release, layer_source=self.lsrc, priority=1)
100
101 # create a layer version for the layer on the specified branch
102 self.layer = Layer.objects.create(name="meta-testlayer",
103 layer_source=self.lsrc)
104 self.branch = Branch.objects.create(name="master", layer_source=self.lsrc)
105 self.lver = Layer_Version.objects.create(\
106 layer=self.layer, layer_source=self.lsrc, up_branch=self.branch)
107
108 # create project and project layer
109 self.project = Project.objects.create_project(name="test-project",
110 release=self.release)
111 ProjectLayer.objects.create(project=self.project,
112 layercommit=self.lver)
113
114 # create spoof layer that should not appear in the search results
115 layer = Layer.objects.create(name="meta-notvalid",
116 layer_source=self.lsrc)
117 self.lver2 = Layer_Version.objects.create(layer=layer,
118 layer_source=self.lsrc,
119 up_branch=self.branch)
120
121 def test_single_layersource(self):
122 """
123 When we have a single layer version,
124 get_equivalents_wpriority() should return a list with
125 just this layer_version.
126 """
127 equivqs = self.lver.get_equivalents_wpriority(self.project)
128 self.assertEqual(list(equivqs), [self.lver])
129
130 def test_dual_layersource(self):
131 """
132 If we have two layers with the same name, from different layer sources,
133 we expect both layers in, in increasing priority of the layer source.
134 """
135 lsrc2 = LayerSource.objects.create(\
136 name="dummy-layersource2",
137 sourcetype=LayerSource.TYPE_LOCAL,
138 apiurl="test")
139
140 # assign a lower priority for the second layer source
141 self.release.releaselayersourcepriority_set.create(layer_source=lsrc2,
142 priority=2)
143
144 # create a new layer_version for a layer with the same name
145 # coming from the second layer source
146 layer2 = Layer.objects.create(name="meta-testlayer",
147 layer_source=lsrc2)
148 lver2 = Layer_Version.objects.create(layer=layer2, layer_source=lsrc2,
149 up_branch=self.branch)
150
151 # expect two layer versions, in the priority order
152 equivqs = self.lver.get_equivalents_wpriority(self.project)
153 self.assertEqual(list(equivqs), [lver2, self.lver])
154
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500155 def test_compatible_layer_versions(self):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 """
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500157 When we have a 2 layer versions, get_all_compatible_layerversions()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158 should return a queryset with both.
159 """
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500160 compat_lv = self.project.get_all_compatible_layer_versions()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161 self.assertEqual(list(compat_lv), [self.lver, self.lver2])
162
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500163 def test_layerversion_get_alldeps(self):
164 """Test Layer_Version.get_alldeps API."""
165 lvers = {}
166 for i in range(10):
167 name = "layer%d" % i
168 lvers[name] = Layer_Version.objects.create(layer=Layer.objects.create(name=name),
169 project=self.project)
170 if i:
171 LayerVersionDependency.objects.create(layer_version=lvers["layer%d" % (i - 1)],
172 depends_on=lvers[name])
173 # Check dinamically added deps
174 self.assertEqual(lvers['layer0'].get_alldeps(self.project.id),
175 [lvers['layer%d' % n] for n in range(1, i+1)])
176
177 # Check chain of deps created in previous loop
178 for i in range(10):
179 self.assertEqual(lvers['layer%d' % i].get_alldeps(self.project.id),
180 [lvers['layer%d' % n] for n in range(i+1, 10)])