summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/tests/builds/buildtest.py
diff options
context:
space:
mode:
authorMichael Wood <michael.g.wood@intel.com>2016-05-19 13:59:31 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-05-20 10:09:03 +0100
commit085688c8ed2a95ff47ca10275726ae7cbbab0902 (patch)
treed529557d6982491f7fe03886614c41ff5451a746 /bitbake/lib/toaster/tests/builds/buildtest.py
parent5ce1adb177b17dc7b7cfe1dcddd42f387482f600 (diff)
downloadpoky-085688c8ed2a95ff47ca10275726ae7cbbab0902.tar.gz
bitbake: toaster: tests Add a BuildTest helper class
Add a helper class for running build tests. Subclass this and call the build method to get setup for running tests on the resulting data from a build. (Bitbake rev: d1882371cae6b9a8296ea739e6e46ca6a6dc9da8) Signed-off-by: Michael Wood <michael.g.wood@intel.com> Signed-off-by: Elliot Smith <elliot.smith@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/toaster/tests/builds/buildtest.py')
-rw-r--r--bitbake/lib/toaster/tests/builds/buildtest.py134
1 files changed, 134 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/tests/builds/buildtest.py b/bitbake/lib/toaster/tests/builds/buildtest.py
new file mode 100644
index 0000000000..fc7bd5b644
--- /dev/null
+++ b/bitbake/lib/toaster/tests/builds/buildtest.py
@@ -0,0 +1,134 @@
1#! /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) 2016 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
22import os
23import sys
24import time
25import unittest
26
27from orm.models import Project, Release, ProjectTarget, Build
28from bldcontrol.models import BuildEnvironment
29
30from bldcontrol.management.commands.loadconf import Command\
31 as LoadConfigCommand
32
33from bldcontrol.management.commands.runbuilds import Command\
34 as RunBuildsCommand
35
36import subprocess
37
38# We use unittest.TestCase instead of django.test.TestCase because we don't
39# want to wrap everything in a database transaction as an external process
40# (bitbake needs access to the database)
41
42
43class BuildTest(unittest.TestCase):
44
45 PROJECT_NAME = "Testbuild"
46
47 def build(self, target):
48 # So that the buildinfo helper uses the test database'
49 self.assertEqual(
50 os.environ.get('DJANGO_SETTINGS_MODULE', ''),
51 'toastermain.settings-test',
52 "Please initialise django with the tests settings: "
53 "DJANGO_SETTINGS_MODULE='toastermain.settings-test'")
54
55 if self.target_already_built(target):
56 return
57
58 # Take a guess at the location of the toasterconf
59 poky_toaster_conf = '../../../meta-poky/conf/toasterconf.json'
60 oe_toaster_conf = '../../../meta/conf/toasterconf.json'
61 env_toaster_conf = os.environ.get('TOASTER_CONF')
62
63 config_file = None
64 if env_toaster_conf:
65 config_file = env_toaster_conf
66 else:
67 if os.path.exists(poky_toaster_conf):
68 config_file = poky_toaster_conf
69 elif os.path.exists(oe_toaster_conf):
70 config_file = oe_toaster_conf
71
72 self.assertIsNotNone(config_file,
73 "Default locations for toasterconf not found"
74 "please set $TOASTER_CONF manually")
75
76 # Setup the release information and default layers
77 print("\nImporting file: %s" % config_file)
78 os.environ['TOASTER_CONF'] = config_file
79 LoadConfigCommand()._import_layer_config(config_file)
80
81 os.environ['TOASTER_DIR'] = \
82 os.path.abspath(os.environ['BUILDDIR'] + "/../")
83
84 os.environ['BBBASEDIR'] = \
85 subprocess.check_output('which bitbake', shell=True)
86
87 BuildEnvironment.objects.get_or_create(
88 betype=BuildEnvironment.TYPE_LOCAL,
89 sourcedir=os.environ['TOASTER_DIR'],
90 builddir=os.environ['BUILDDIR']
91 )
92
93 release = Release.objects.get(name='local')
94
95 # Create a project for this build to run in
96 try:
97 project = Project.objects.get(name=BuildTest.PROJECT_NAME)
98 except Project.DoesNotExist:
99 project = Project.objects.create_project(
100 name=BuildTest.PROJECT_NAME,
101 release=release
102 )
103
104 ProjectTarget.objects.create(project=project,
105 target=target,
106 task="")
107 build_request = project.schedule_build()
108
109 # run runbuilds command to dispatch the build
110 # e.g. manage.py runubilds
111 RunBuildsCommand().runbuild()
112
113 build_pk = build_request.build.pk
114 while Build.objects.get(pk=build_pk).outcome == Build.IN_PROGRESS:
115 sys.stdout.write("\rBuilding %s %d%%" %
116 (target,
117 build_request.build.completeper()))
118 sys.stdout.flush()
119 time.sleep(1)
120
121 self.assertNotEqual(build_request.build.outcome,
122 Build.SUCCEEDED, "Build did not SUCCEEDED")
123 print("\nBuild finished")
124 return build_request.build
125
126 def target_already_built(self, target):
127 """ If the target is already built no need to build it again"""
128 for build in Build.objects.filter(
129 project__name=BuildTest.PROJECT_NAME):
130 targets = build.target_set.values_list('target', flat=True)
131 if target in targets:
132 return True
133
134 return False