diff options
Diffstat (limited to 'bitbake/lib/toaster/bldcontrol/tests.py')
-rw-r--r-- | bitbake/lib/toaster/bldcontrol/tests.py | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/bldcontrol/tests.py b/bitbake/lib/toaster/bldcontrol/tests.py new file mode 100644 index 0000000000..4577c3f03b --- /dev/null +++ b/bitbake/lib/toaster/bldcontrol/tests.py | |||
@@ -0,0 +1,149 @@ | |||
1 | """ | ||
2 | This file demonstrates writing tests using the unittest module. These will pass | ||
3 | when you run "manage.py test". | ||
4 | |||
5 | Replace this with more appropriate tests for your application. | ||
6 | """ | ||
7 | |||
8 | from django.test import TestCase | ||
9 | |||
10 | from bldcontrol.bbcontroller import BitbakeController | ||
11 | from bldcontrol.localhostbecontroller import LocalhostBEController | ||
12 | from bldcontrol.sshbecontroller import SSHBEController | ||
13 | from bldcontrol.models import BuildEnvironment, BuildRequest | ||
14 | from bldcontrol.management.commands.runbuilds import Command | ||
15 | |||
16 | import socket | ||
17 | import subprocess | ||
18 | |||
19 | # standard poky data hardcoded for testing | ||
20 | BITBAKE_LAYERS = [type('bitbake_info', (object,), { "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "", "commit": "HEAD"})] | ||
21 | POKY_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 | |||
32 | class BEControllerTests(object): | ||
33 | |||
34 | def _serverForceStop(self, bc): | ||
35 | 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") | ||
36 | self.assertTrue(err == '', "bitbake server pid %s not stopped" % err) | ||
37 | |||
38 | def test_serverStartAndStop(self): | ||
39 | obe = self._getBuildEnvironment() | ||
40 | bc = self._getBEController(obe) | ||
41 | bc.setLayers(BITBAKE_LAYERS, POKY_LAYERS) # setting layers, skip any layer info | ||
42 | |||
43 | hostname = self.test_address.split("@")[-1] | ||
44 | |||
45 | # test start server and stop | ||
46 | self.assertTrue(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex((hostname, 8200)), "Port already occupied") | ||
47 | bc.startBBServer() | ||
48 | self.assertFalse(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex((hostname, 8200)), "Server not answering") | ||
49 | |||
50 | bc.stopBBServer() | ||
51 | self.assertTrue(socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex((hostname, 8200)), "Server not stopped") | ||
52 | |||
53 | self._serverForceStop(bc) | ||
54 | |||
55 | def test_getBBController(self): | ||
56 | obe = self._getBuildEnvironment() | ||
57 | bc = self._getBEController(obe) | ||
58 | bc.setLayers(BITBAKE_LAYERS, POKY_LAYERS) # setting layers, skip any layer info | ||
59 | |||
60 | bbc = bc.getBBController() | ||
61 | self.assertTrue(isinstance(bbc, BitbakeController)) | ||
62 | # test set variable, use no build marker -1 for BR value | ||
63 | try: | ||
64 | bbc.setVariable("TOASTER_BRBE", "%d:%d" % (-1, obe.pk)) | ||
65 | except Exception as e : | ||
66 | self.fail("setVariable raised %s", e) | ||
67 | |||
68 | bc.stopBBServer() | ||
69 | |||
70 | self._serverForceStop(bc) | ||
71 | |||
72 | class LocalhostBEControllerTests(TestCase, BEControllerTests): | ||
73 | def __init__(self, *args): | ||
74 | super(LocalhostBEControllerTests, self).__init__(*args) | ||
75 | # hardcoded for Alex's machine; since the localhost BE is machine-dependent, | ||
76 | # I found no good way to abstractize this | ||
77 | self.test_sourcedir = "/home/ddalex/ssd/yocto" | ||
78 | self.test_builddir = "/home/ddalex/ssd/yocto/build" | ||
79 | self.test_address = "localhost" | ||
80 | |||
81 | def _getBuildEnvironment(self): | ||
82 | return BuildEnvironment.objects.create( | ||
83 | lock = BuildEnvironment.LOCK_FREE, | ||
84 | betype = BuildEnvironment.TYPE_LOCAL, | ||
85 | address = self.test_address, | ||
86 | sourcedir = self.test_sourcedir, | ||
87 | builddir = self.test_builddir ) | ||
88 | |||
89 | def _getBEController(self, obe): | ||
90 | return LocalhostBEController(obe) | ||
91 | |||
92 | class SSHBEControllerTests(TestCase, BEControllerTests): | ||
93 | def __init__(self, *args): | ||
94 | super(SSHBEControllerTests, self).__init__(*args) | ||
95 | self.test_address = "ddalex-desktop.local" | ||
96 | # hardcoded for ddalex-desktop.local machine; since the localhost BE is machine-dependent, | ||
97 | # I found no good way to abstractize this | ||
98 | self.test_sourcedir = "/home/ddalex/ssd/yocto" | ||
99 | self.test_builddir = "/home/ddalex/ssd/yocto/build" | ||
100 | |||
101 | def _getBuildEnvironment(self): | ||
102 | return BuildEnvironment.objects.create( | ||
103 | lock = BuildEnvironment.LOCK_FREE, | ||
104 | betype = BuildEnvironment.TYPE_SSH, | ||
105 | address = self.test_address, | ||
106 | sourcedir = self.test_sourcedir, | ||
107 | builddir = self.test_builddir ) | ||
108 | |||
109 | def _getBEController(self, obe): | ||
110 | return SSHBEController(obe) | ||
111 | |||
112 | def test_pathExists(self): | ||
113 | obe = BuildEnvironment.objects.create(betype = BuildEnvironment.TYPE_SSH, address= self.test_address) | ||
114 | sbc = SSHBEController(obe) | ||
115 | self.assertTrue(sbc._pathexists("/")) | ||
116 | self.assertFalse(sbc._pathexists("/.deadbeef")) | ||
117 | self.assertTrue(sbc._pathexists(sbc._shellcmd("pwd"))) | ||
118 | |||
119 | |||
120 | class RunBuildsCommandTests(TestCase): | ||
121 | def test_bec_select(self): | ||
122 | """ | ||
123 | Tests that we can find and lock a build environment | ||
124 | """ | ||
125 | |||
126 | obe = BuildEnvironment.objects.create(lock = BuildEnvironment.LOCK_FREE, betype = BuildEnvironment.TYPE_LOCAL) | ||
127 | command = Command() | ||
128 | bec = command._selectBuildEnvironment() | ||
129 | |||
130 | # make sure we select the object we've just built | ||
131 | self.assertTrue(bec.be.id == obe.id, "Environment is not properly selected") | ||
132 | # we have a locked environment | ||
133 | self.assertTrue(bec.be.lock == BuildEnvironment.LOCK_LOCK, "Environment is not locked") | ||
134 | # no more selections possible here | ||
135 | self.assertRaises(IndexError, command._selectBuildEnvironment) | ||
136 | |||
137 | def test_br_select(self): | ||
138 | from orm.models import Project, Release, BitbakeVersion | ||
139 | p = Project.objects.create_project("test", Release.objects.get_or_create(name = "HEAD", bitbake_version = BitbakeVersion.objects.get_or_create(name="HEAD", branch="HEAD")[0])[0]) | ||
140 | obr = BuildRequest.objects.create(state = BuildRequest.REQ_QUEUED, project = p) | ||
141 | command = Command() | ||
142 | br = command._selectBuildRequest() | ||
143 | |||
144 | # make sure we select the object we've just built | ||
145 | self.assertTrue(obr.id == br.id, "Request is not properly selected") | ||
146 | # we have a locked environment | ||
147 | self.assertTrue(br.state == BuildRequest.REQ_INPROGRESS, "Request is not updated") | ||
148 | # no more selections possible here | ||
149 | self.assertRaises(IndexError, command._selectBuildRequest) | ||