blob: 5dbc77fda5f38a0969a08cf01403c41550a8667d [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
12from bldcontrol.sshbecontroller import SSHBEController
13from bldcontrol.models import BuildEnvironment, BuildRequest
14from bldcontrol.management.commands.runbuilds import Command
15
16import socket
17import subprocess
18import os
19
20# standard poky data hardcoded for testing
21BITBAKE_LAYERS = [type('bitbake_info', (object,), { "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "", "commit": "HEAD"})]
22POKY_LAYERS = [
23 type('poky_info', (object,), { "name": "meta", "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "meta", "commit": "HEAD"}),
24 type('poky_info', (object,), { "name": "meta-yocto", "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "meta-yocto", "commit": "HEAD"}),
25 type('poky_info', (object,), { "name": "meta-yocto-bsp", "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "meta-yocto-bsp", "commit": "HEAD"}),
26 ]
27
28
29
30# we have an abstract test class designed to ensure that the controllers use a single interface
31# specific controller tests only need to override the _getBuildEnvironment() method
32
33test_sourcedir = os.getenv("TTS_SOURCE_DIR")
34test_builddir = os.getenv("TTS_BUILD_DIR")
35test_address = os.getenv("TTS_TEST_ADDRESS", "localhost")
36
37if test_sourcedir == None or test_builddir == None or test_address == None:
38 raise Exception("Please set TTTS_SOURCE_DIR, TTS_BUILD_DIR and TTS_TEST_ADDRESS")
39
40# The bb server will expect a toaster-pre.conf file to exist. If it doesn't exit then we make
41# an empty one here.
42open(test_builddir + 'conf/toaster-pre.conf', 'a').close()
43
44class BEControllerTests(object):
45
46 def _serverForceStop(self, bc):
47 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")
48 self.assertTrue(err == '', "bitbake server pid %s not stopped" % err)
49
50 def test_serverStartAndStop(self):
51 from bldcontrol.sshbecontroller import NotImplementedException
52 obe = self._getBuildEnvironment()
53 bc = self._getBEController(obe)
54 try:
55 # setting layers, skip any layer info
56 bc.setLayers(BITBAKE_LAYERS, POKY_LAYERS)
57 except NotImplementedException, e:
58 print "Test skipped due to command not implemented yet"
59 return True
60 # We are ok with the exception as we're handling the git already exists
61 except BuildSetupException:
62 pass
63
64 bc.pokydirname = test_sourcedir
65 bc.islayerset = True
66
67 hostname = test_address.split("@")[-1]
68
69 # test start server and stop
70 bc.startBBServer()
71
72 self.assertFalse(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex((hostname, int(bc.be.bbport))), "Server not answering")
73
74 bc.stopBBServer()
75 self.assertTrue(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex((hostname, int(bc.be.bbport))), "Server not stopped")
76
77 self._serverForceStop(bc)
78
79 def test_getBBController(self):
80 from bldcontrol.sshbecontroller import NotImplementedException
81 obe = self._getBuildEnvironment()
82 bc = self._getBEController(obe)
83 layerSet = False
84 try:
85 # setting layers, skip any layer info
86 layerSet = bc.setLayers(BITBAKE_LAYERS, POKY_LAYERS)
87 except NotImplementedException:
88 print "Test skipped due to command not implemented yet"
89 return True
90 # We are ok with the exception as we're handling the git already exists
91 except BuildSetupException:
92 pass
93
94 bc.pokydirname = test_sourcedir
95 bc.islayerset = True
96
97 bbc = bc.getBBController()
98 self.assertTrue(isinstance(bbc, BitbakeController))
99 bc.stopBBServer()
100
101 self._serverForceStop(bc)
102
103class LocalhostBEControllerTests(TestCase, BEControllerTests):
104 def __init__(self, *args):
105 super(LocalhostBEControllerTests, self).__init__(*args)
106
107
108 def _getBuildEnvironment(self):
109 return BuildEnvironment.objects.create(
110 lock = BuildEnvironment.LOCK_FREE,
111 betype = BuildEnvironment.TYPE_LOCAL,
112 address = test_address,
113 sourcedir = test_sourcedir,
114 builddir = test_builddir )
115
116 def _getBEController(self, obe):
117 return LocalhostBEController(obe)
118
119class SSHBEControllerTests(TestCase, BEControllerTests):
120 def __init__(self, *args):
121 super(SSHBEControllerTests, self).__init__(*args)
122
123 def _getBuildEnvironment(self):
124 return BuildEnvironment.objects.create(
125 lock = BuildEnvironment.LOCK_FREE,
126 betype = BuildEnvironment.TYPE_SSH,
127 address = test_address,
128 sourcedir = test_sourcedir,
129 builddir = test_builddir )
130
131 def _getBEController(self, obe):
132 return SSHBEController(obe)
133
134 def test_pathExists(self):
135 obe = BuildEnvironment.objects.create(betype = BuildEnvironment.TYPE_SSH, address= test_address)
136 sbc = SSHBEController(obe)
137 self.assertTrue(sbc._pathexists("/"))
138 self.assertFalse(sbc._pathexists("/.deadbeef"))
139 self.assertTrue(sbc._pathexists(sbc._shellcmd("pwd")))
140
141
142class RunBuildsCommandTests(TestCase):
143 def test_bec_select(self):
144 """
145 Tests that we can find and lock a build environment
146 """
147
148 obe = BuildEnvironment.objects.create(lock = BuildEnvironment.LOCK_FREE, betype = BuildEnvironment.TYPE_LOCAL)
149 command = Command()
150 bec = command._selectBuildEnvironment()
151
152 # make sure we select the object we've just built
153 self.assertTrue(bec.be.id == obe.id, "Environment is not properly selected")
154 # we have a locked environment
155 self.assertTrue(bec.be.lock == BuildEnvironment.LOCK_LOCK, "Environment is not locked")
156 # no more selections possible here
157 self.assertRaises(IndexError, command._selectBuildEnvironment)
158
159 def test_br_select(self):
160 from orm.models import Project, Release, BitbakeVersion, Branch
161 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])
162 obr = BuildRequest.objects.create(state = BuildRequest.REQ_QUEUED, project = p)
163 command = Command()
164 br = command._selectBuildRequest()
165
166 # make sure we select the object we've just built
167 self.assertTrue(obr.id == br.id, "Request is not properly selected")
168 # we have a locked environment
169 self.assertTrue(br.state == BuildRequest.REQ_INPROGRESS, "Request is not updated")
170 # no more selections possible here
171 self.assertRaises(IndexError, command._selectBuildRequest)
172
173
174class UtilityTests(TestCase):
175 def test_reduce_path(self):
176 from bldcontrol.management.commands.loadconf import _reduce_canon_path, _get_id_for_sourcetype
177
178 self.assertTrue( _reduce_canon_path("/") == "/")
179 self.assertTrue( _reduce_canon_path("/home/..") == "/")
180 self.assertTrue( _reduce_canon_path("/home/../ana") == "/ana")
181 self.assertTrue( _reduce_canon_path("/home/../ana/..") == "/")
182 self.assertTrue( _reduce_canon_path("/home/ana/mihai/../maria") == "/home/ana/maria")
183
184 def test_get_id_for_sorucetype(self):
185 from bldcontrol.management.commands.loadconf import _reduce_canon_path, _get_id_for_sourcetype
186 self.assertTrue( _get_id_for_sourcetype("layerindex") == 1)
187 self.assertTrue( _get_id_for_sourcetype("local") == 0)
188 self.assertTrue( _get_id_for_sourcetype("imported") == 2)
189 with self.assertRaises(Exception):
190 _get_id_for_sourcetype("unknown")