blob: 783aea890ffd8ccdc7fc4192fdb109aa338373c0 [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
26from orm.models import Branch
27
28from orm.models import Project, Build, Layer, Layer_Version, Branch, ProjectLayer
29from orm.models import Release, ReleaseLayerSourcePriority, BitbakeVersion
30
31from django.utils import timezone
32from django.db import IntegrityError
33
34import os
35
36# set TTS_LAYER_INDEX to the base url to use a different instance of the layer index
37
38class LayerSourceVerifyInheritanceSaveLoad(TestCase):
39 """
40 Tests to verify inheritance for the LayerSource proxy-inheritance classes.
41 """
42 def test_object_creation(self):
43 """Test LayerSource object creation."""
44 for name, sourcetype in [("a1", LayerSource.TYPE_LOCAL),
45 ("a2", LayerSource.TYPE_LAYERINDEX),
46 ("a3", LayerSource.TYPE_IMPORTED)]:
47 LayerSource.objects.create(name=name, sourcetype=sourcetype)
48
49 objects = LayerSource.objects.all()
50 self.assertTrue(isinstance(objects[0], LocalLayerSource))
51 self.assertTrue(isinstance(objects[1], LayerIndexLayerSource))
52 self.assertTrue(isinstance(objects[2], ImportedLayerSource))
53
54 def test_duplicate_error(self):
55 """Test creation of duplicate LayerSource objects."""
56 stype = LayerSource.TYPE_LOCAL
57 LayerSource.objects.create(name="a1", sourcetype=stype)
58 with self.assertRaises(IntegrityError):
59 LayerSource.objects.create(name="a1", sourcetype=stype)
60
61
62class LILSUpdateTestCase(TransactionTestCase):
63 """Test Layer Source update."""
64
65 def setUp(self):
66 """Create release."""
67 bbv = BitbakeVersion.objects.create(\
68 name="master", giturl="git://git.openembedded.org/bitbake")
69 Release.objects.create(name="default-release", bitbake_version=bbv,
70 branch_name="master")
71
72 def test_update(self):
73 """Check if LayerSource.update can fetch branches."""
74 url = os.getenv("TTS_LAYER_INDEX",
75 default="http://layers.openembedded.org/")
76
77 lsobj = LayerSource.objects.create(\
78 name="b1", sourcetype=LayerSource.TYPE_LAYERINDEX,
79 apiurl=url + "layerindex/api/")
80 lsobj.update()
81 self.assertTrue(lsobj.branch_set.all().count() > 0,
82 "no branches fetched")
83
84class LayerVersionEquivalenceTestCase(TestCase):
85 """Verify Layer_Version priority selection."""
86
87 def setUp(self):
88 """Create required objects."""
89 # create layer source
90 self.lsrc = LayerSource.objects.create(name="dummy-layersource",
91 sourcetype=LayerSource.TYPE_LOCAL)
92 # create release
93 bbv = BitbakeVersion.objects.create(\
94 name="master", giturl="git://git.openembedded.org/bitbake")
95 self.release = Release.objects.create(name="default-release",
96 bitbake_version=bbv,
97 branch_name="master")
98 # attach layer source to release
99 ReleaseLayerSourcePriority.objects.create(\
100 release=self.release, layer_source=self.lsrc, priority=1)
101
102 # create a layer version for the layer on the specified branch
103 self.layer = Layer.objects.create(name="meta-testlayer",
104 layer_source=self.lsrc)
105 self.branch = Branch.objects.create(name="master", layer_source=self.lsrc)
106 self.lver = Layer_Version.objects.create(\
107 layer=self.layer, layer_source=self.lsrc, up_branch=self.branch)
108
109 # create project and project layer
110 self.project = Project.objects.create_project(name="test-project",
111 release=self.release)
112 ProjectLayer.objects.create(project=self.project,
113 layercommit=self.lver)
114
115 # create spoof layer that should not appear in the search results
116 layer = Layer.objects.create(name="meta-notvalid",
117 layer_source=self.lsrc)
118 self.lver2 = Layer_Version.objects.create(layer=layer,
119 layer_source=self.lsrc,
120 up_branch=self.branch)
121
122 def test_single_layersource(self):
123 """
124 When we have a single layer version,
125 get_equivalents_wpriority() should return a list with
126 just this layer_version.
127 """
128 equivqs = self.lver.get_equivalents_wpriority(self.project)
129 self.assertEqual(list(equivqs), [self.lver])
130
131 def test_dual_layersource(self):
132 """
133 If we have two layers with the same name, from different layer sources,
134 we expect both layers in, in increasing priority of the layer source.
135 """
136 lsrc2 = LayerSource.objects.create(\
137 name="dummy-layersource2",
138 sourcetype=LayerSource.TYPE_LOCAL,
139 apiurl="test")
140
141 # assign a lower priority for the second layer source
142 self.release.releaselayersourcepriority_set.create(layer_source=lsrc2,
143 priority=2)
144
145 # create a new layer_version for a layer with the same name
146 # coming from the second layer source
147 layer2 = Layer.objects.create(name="meta-testlayer",
148 layer_source=lsrc2)
149 lver2 = Layer_Version.objects.create(layer=layer2, layer_source=lsrc2,
150 up_branch=self.branch)
151
152 # expect two layer versions, in the priority order
153 equivqs = self.lver.get_equivalents_wpriority(self.project)
154 self.assertEqual(list(equivqs), [lver2, self.lver])
155
156 def test_build_layerversion(self):
157 """
158 Any layer version coming from the build should show up
159 before any layer version coming from upstream
160 """
161 build = Build.objects.create(project=self.project,
162 started_on=timezone.now(),
163 completed_on=timezone.now())
164 lvb = Layer_Version.objects.create(layer=self.layer, build=build,
165 commit="deadbeef")
166
167 # a build layerversion must be in the equivalence
168 # list for the original layerversion
169 equivqs = self.lver.get_equivalents_wpriority(self.project)
170 self.assertTrue(len(equivqs) == 2)
171 self.assertTrue(equivqs[0] == self.lver)
172 self.assertTrue(equivqs[1] == lvb)
173
174 # getting the build layerversion equivalent list must
175 # return the same list as the original layer
176 bequivqs = lvb.get_equivalents_wpriority(self.project)
177
178 self.assertEqual(list(equivqs), list(bequivqs))
179
180 def test_compatible_layerversions(self):
181 """
182 When we have a 2 layer versions, compatible_layerversions()
183 should return a queryset with both.
184 """
185 compat_lv = self.project.compatible_layerversions()
186 self.assertEqual(list(compat_lv), [self.lver, self.lver2])
187