Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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, BuildSetupException |
| 11 | from bldcontrol.localhostbecontroller import LocalhostBEController |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | from bldcontrol.models import BuildEnvironment, BuildRequest |
| 13 | from bldcontrol.management.commands.runbuilds import Command |
| 14 | |
| 15 | import socket |
| 16 | import subprocess |
| 17 | import os |
| 18 | |
| 19 | # standard poky data hardcoded for testing |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 20 | BITBAKE_LAYER = type('bitbake_info', (object,), { "giturl": "git://git.yoctoproject.org/poky.git", "dirpath": "", "commit": "HEAD"}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | test_sourcedir = os.getenv("TTS_SOURCE_DIR") |
| 33 | test_builddir = os.getenv("TTS_BUILD_DIR") |
| 34 | test_address = os.getenv("TTS_TEST_ADDRESS", "localhost") |
| 35 | |
| 36 | if 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. |
| 41 | open(test_builddir + 'conf/toaster-pre.conf', 'a').close() |
| 42 | |
| 43 | class 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | obe = self._getBuildEnvironment() |
| 51 | bc = self._getBEController(obe) |
| 52 | try: |
| 53 | # setting layers, skip any layer info |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 54 | bc.setLayers(BITBAKE_LAYER, POKY_LAYERS) |
| 55 | except NotImplementedError: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | self._serverForceStop(bc) |
| 73 | |
| 74 | def test_getBBController(self): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | obe = self._getBuildEnvironment() |
| 76 | bc = self._getBEController(obe) |
| 77 | layerSet = False |
| 78 | try: |
| 79 | # setting layers, skip any layer info |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 80 | layerSet = bc.setLayers(BITBAKE_LAYER, POKY_LAYERS) |
| 81 | except NotImplementedError: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
| 94 | self._serverForceStop(bc) |
| 95 | |
| 96 | class 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | class 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 | |
| 144 | class 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") |