1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
8
from lp.testing import TestCase
9
from lp.testing.fakemethod import FakeMethod
11
from canonical.buildd.translationtemplates import (
12
TranslationTemplatesBuildManager, TranslationTemplatesBuildState)
16
def get(self, section, key):
21
def __init__(self, tempdir):
22
self._cachepath = tempdir
23
self._config = FakeConfig()
24
self._was_called = set()
26
def cachePath(self, file):
27
return os.path.join(self._cachepath, file)
29
def anyMethod(self, *args, **kwargs):
32
fake_methods = ['emptyLog', 'chrootFail', 'buildFail', 'builderFail',]
33
def __getattr__(self, name):
34
"""Remember which fake methods were called."""
35
if name not in self.fake_methods:
37
"'%s' object has no attribute '%s'" % (self.__class__, name))
38
self._was_called.add(name)
41
def wasCalled(self, name):
42
return name in self._was_called
47
addWaitingFile = FakeMethod()
50
class MockBuildManager(TranslationTemplatesBuildManager):
51
def __init__(self, *args, **kwargs):
52
super(MockBuildManager, self).__init__(*args, **kwargs)
55
def runSubProcess(self, path, command):
56
self.commands.append([path]+command)
60
class TestTranslationTemplatesBuildManagerIteration(TestCase):
61
"""Run TranslationTemplatesBuildManager through its iteration steps."""
63
super(TestTranslationTemplatesBuildManagerIteration, self).setUp()
64
self.working_dir = self.makeTemporaryDirectory()
65
slave_dir = os.path.join(self.working_dir, 'slave')
66
home_dir = os.path.join(self.working_dir, 'home')
67
for dir in (slave_dir, home_dir):
69
self.slave = FakeSlave(slave_dir)
71
self.buildmanager = MockBuildManager(self.slave, self.buildid)
72
self.buildmanager.home = home_dir
73
self.chrootdir = os.path.join(
74
home_dir, 'build-%s' % self.buildid, 'chroot-autobuild')
77
"""Retrieve build manager's state."""
78
return self.buildmanager._state
80
def test_iterate(self):
81
# Two iteration steps are specific to this build manager.
83
# The build manager's iterate() kicks off the consecutive states
85
self.buildmanager.initiate({}, 'chroot.tar.gz', {'branch_url': url})
87
# Skip states that are done in DebianBuldManager to the state
88
# directly before INSTALL.
89
self.buildmanager._state = TranslationTemplatesBuildState.UPDATE
91
# INSTALL: Install additional packages needed for this job into
93
self.buildmanager.iterate(0)
95
TranslationTemplatesBuildState.INSTALL, self.getState())
98
'sudo', 'chroot', self.chrootdir,
101
self.assertEqual(expected_command, self.buildmanager.commands[-1][:5])
103
# GENERATE: Run the slave's payload, the script that generates
105
self.buildmanager.iterate(0)
107
TranslationTemplatesBuildState.GENERATE, self.getState())
109
'generatepath', 'generatepath', self.buildid, url, 'resultarchive'
111
self.assertEqual(expected_command, self.buildmanager.commands[-1])
112
self.assertFalse(self.slave.wasCalled('chrootFail'))
114
outfile_path = os.path.join(
115
self.chrootdir, self.buildmanager.home[1:],
116
self.buildmanager._resultname)
117
os.makedirs(os.path.dirname(outfile_path))
119
outfile = open(outfile_path, 'w')
120
outfile.write("I am a template tarball. Seriously.")
123
# The control returns to the DebianBuildManager in the REAP state.
124
self.buildmanager.iterate(0)
126
'processscanpath', 'processscanpath', self.buildid
129
TranslationTemplatesBuildState.REAP, self.getState())
130
self.assertEqual(expected_command, self.buildmanager.commands[-1])
131
self.assertFalse(self.slave.wasCalled('buildFail'))
133
[((outfile_path,), {})], self.slave.addWaitingFile.calls)
135
def test_iterate_fail_INSTALL(self):
136
# See that a failing INSTALL is handled properly.
137
url = 'lp:~my/branch'
138
# The build manager's iterate() kicks off the consecutive states
140
self.buildmanager.initiate({}, 'chroot.tar.gz', {'branch_url': url})
142
# Skip states to the INSTALL state.
143
self.buildmanager._state = TranslationTemplatesBuildState.INSTALL
145
# The buildmanager fails and iterates to the UMOUNT state.
146
self.buildmanager.iterate(-1)
148
TranslationTemplatesBuildState.UMOUNT, self.getState())
150
'umountpath', 'umount-chroot', self.buildid
152
self.assertEqual(expected_command, self.buildmanager.commands[-1])
153
self.assertTrue(self.slave.wasCalled('chrootFail'))
155
def test_iterate_fail_GENERATE(self):
156
# See that a failing GENERATE is handled properly.
157
url = 'lp:~my/branch'
158
# The build manager's iterate() kicks off the consecutive states
160
self.buildmanager.initiate({}, 'chroot.tar.gz', {'branch_url': url})
162
# Skip states to the INSTALL state.
163
self.buildmanager._state = TranslationTemplatesBuildState.GENERATE
165
# The buildmanager fails and iterates to the REAP state.
166
self.buildmanager.iterate(-1)
168
'processscanpath', 'processscanpath', self.buildid
171
TranslationTemplatesBuildState.REAP, self.getState())
172
self.assertEqual(expected_command, self.buildmanager.commands[-1])
173
self.assertTrue(self.slave.wasCalled('buildFail'))