blob: f20cc7d4b1f80ae48c1292600f9f00ee50f28ce1 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2This file demonstrates writing tests using the unittest module. These will pass
3when you run "manage.py test".
4
5Replace this with more appropriate tests for your application.
6"""
7
8from django.test import TestCase
9
10from bldcontrol.bbcontroller import BitbakeController, BuildSetupException
11from bldcontrol.localhostbecontroller import LocalhostBEController
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012from bldcontrol.models import BuildEnvironment, BuildRequest
13from bldcontrol.management.commands.runbuilds import Command
14
15import socket
16import subprocess
17import os
18
19# standard poky data hardcoded for testing
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020BITBAKE_LAYER = type('bitbake_info', (object,), { "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "", "commit": "HEAD"})
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021POKY_LAYERS = [
22 type('poky_info', (object,), { "name": "meta", "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "meta", "commit": "HEAD"}),
23 type('poky_info', (object,), { "name": "meta-yocto", "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "meta-yocto", "commit": "HEAD"}),
24 type('poky_info', (object,), { "name": "meta-yocto-bsp", "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "meta-yocto-bsp", "commit": "HEAD"}),
25 ]
26
27
28
29# we have an abstract test class designed to ensure that the controllers use a single interface
30# specific controller tests only need to override the _getBuildEnvironment() method
31
32test_sourcedir = os.getenv("TTS_SOURCE_DIR")
33test_builddir = os.getenv("TTS_BUILD_DIR")
34test_address = os.getenv("TTS_TEST_ADDRESS", "localhost")
35
36if test_sourcedir == None or test_builddir == None or test_address == None:
37 raise Exception("Please set TTTS_SOURCE_DIR, TTS_BUILD_DIR and TTS_TEST_ADDRESS")
38
39# The bb server will expect a toaster-pre.conf file to exist. If it doesn't exit then we make
40# an empty one here.
41open(test_builddir + 'conf/toaster-pre.conf', 'a').close()
42
43class BEControllerTests(object):
44
45 def _serverForceStop(self, bc):
46 err = bc._shellcmd("netstat -tapn 2>/dev/null | grep 8200 | awk '{print $7}' | sort -fu | cut -d \"/\" -f 1 | grep -v -- - | tee /dev/fd/2 | xargs -r kill")
47 self.assertTrue(err == '', "bitbake server pid %s not stopped" % err)
48
49 def test_serverStartAndStop(self):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 obe = self._getBuildEnvironment()
51 bc = self._getBEController(obe)
52 try:
53 # setting layers, skip any layer info
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050054 bc.setLayers(BITBAKE_LAYER, POKY_LAYERS)
55 except NotImplementedError:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 print "Test skipped due to command not implemented yet"
57 return True
58 # We are ok with the exception as we're handling the git already exists
59 except BuildSetupException:
60 pass
61
62 bc.pokydirname = test_sourcedir
63 bc.islayerset = True
64
65 hostname = test_address.split("@")[-1]
66
67 # test start server and stop
68 bc.startBBServer()
69
70 self.assertFalse(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex((hostname, int(bc.be.bbport))), "Server not answering")
71
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 self._serverForceStop(bc)
73
74 def test_getBBController(self):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 obe = self._getBuildEnvironment()
76 bc = self._getBEController(obe)
77 layerSet = False
78 try:
79 # setting layers, skip any layer info
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050080 layerSet = bc.setLayers(BITBAKE_LAYER, POKY_LAYERS)
81 except NotImplementedError:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 print "Test skipped due to command not implemented yet"
83 return True
84 # We are ok with the exception as we're handling the git already exists
85 except BuildSetupException:
86 pass
87
88 bc.pokydirname = test_sourcedir
89 bc.islayerset = True
90
91 bbc = bc.getBBController()
92 self.assertTrue(isinstance(bbc, BitbakeController))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093
94 self._serverForceStop(bc)
95
96class LocalhostBEControllerTests(TestCase, BEControllerTests):
97 def __init__(self, *args):
98 super(LocalhostBEControllerTests, self).__init__(*args)
99
100
101 def _getBuildEnvironment(self):
102 return BuildEnvironment.objects.create(
103 lock = BuildEnvironment.LOCK_FREE,
104 betype = BuildEnvironment.TYPE_LOCAL,
105 address = test_address,
106 sourcedir = test_sourcedir,
107 builddir = test_builddir )
108
109 def _getBEController(self, obe):
110 return LocalhostBEController(obe)
111
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112class RunBuildsCommandTests(TestCase):
113 def test_bec_select(self):
114 """
115 Tests that we can find and lock a build environment
116 """
117
118 obe = BuildEnvironment.objects.create(lock = BuildEnvironment.LOCK_FREE, betype = BuildEnvironment.TYPE_LOCAL)
119 command = Command()
120 bec = command._selectBuildEnvironment()
121
122 # make sure we select the object we've just built
123 self.assertTrue(bec.be.id == obe.id, "Environment is not properly selected")
124 # we have a locked environment
125 self.assertTrue(bec.be.lock == BuildEnvironment.LOCK_LOCK, "Environment is not locked")
126 # no more selections possible here
127 self.assertRaises(IndexError, command._selectBuildEnvironment)
128
129 def test_br_select(self):
130 from orm.models import Project, Release, BitbakeVersion, Branch
131 p = Project.objects.create_project("test", Release.objects.get_or_create(name = "HEAD", bitbake_version = BitbakeVersion.objects.get_or_create(name="HEAD", branch=Branch.objects.get_or_create(name="HEAD"))[0])[0])
132 obr = BuildRequest.objects.create(state = BuildRequest.REQ_QUEUED, project = p)
133 command = Command()
134 br = command._selectBuildRequest()
135
136 # make sure we select the object we've just built
137 self.assertTrue(obr.id == br.id, "Request is not properly selected")
138 # we have a locked environment
139 self.assertTrue(br.state == BuildRequest.REQ_INPROGRESS, "Request is not updated")
140 # no more selections possible here
141 self.assertRaises(IndexError, command._selectBuildRequest)
142
143
144class UtilityTests(TestCase):
145 def test_reduce_path(self):
146 from bldcontrol.management.commands.loadconf import _reduce_canon_path, _get_id_for_sourcetype
147
148 self.assertTrue( _reduce_canon_path("/") == "/")
149 self.assertTrue( _reduce_canon_path("/home/..") == "/")
150 self.assertTrue( _reduce_canon_path("/home/../ana") == "/ana")
151 self.assertTrue( _reduce_canon_path("/home/../ana/..") == "/")
152 self.assertTrue( _reduce_canon_path("/home/ana/mihai/../maria") == "/home/ana/maria")
153
154 def test_get_id_for_sorucetype(self):
155 from bldcontrol.management.commands.loadconf import _reduce_canon_path, _get_id_for_sourcetype
156 self.assertTrue( _get_id_for_sourcetype("layerindex") == 1)
157 self.assertTrue( _get_id_for_sourcetype("local") == 0)
158 self.assertTrue( _get_id_for_sourcetype("imported") == 2)
159 with self.assertRaises(Exception):
160 _get_id_for_sourcetype("unknown")