~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/buildd/tests/test_translationtemplatesbuildmanager.py

  • Committer: mbp at canonical
  • Date: 2011-11-20 23:37:23 UTC
  • mto: This revision was merged to the branch mainline in revision 14344.
  • Revision ID: mbp@canonical.com-20111120233723-370p96db2crru5tm
Delete canonical.buildd again

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
__metaclass__ = type
5
 
 
6
 
import os
7
 
 
8
 
from lp.testing import TestCase
9
 
from lp.testing.fakemethod import FakeMethod
10
 
 
11
 
from canonical.buildd.translationtemplates import (
12
 
    TranslationTemplatesBuildManager, TranslationTemplatesBuildState)
13
 
 
14
 
 
15
 
class FakeConfig:
16
 
    def get(self, section, key):
17
 
        return key
18
 
 
19
 
 
20
 
class FakeSlave:
21
 
    def __init__(self, tempdir):
22
 
        self._cachepath = tempdir
23
 
        self._config = FakeConfig()
24
 
        self._was_called = set()
25
 
 
26
 
    def cachePath(self, file):
27
 
        return os.path.join(self._cachepath, file)
28
 
 
29
 
    def anyMethod(self, *args, **kwargs):
30
 
        pass
31
 
 
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:
36
 
            raise AttributeError(
37
 
                "'%s' object has no attribute '%s'" % (self.__class__, name))
38
 
        self._was_called.add(name)
39
 
        return self.anyMethod
40
 
 
41
 
    def wasCalled(self, name):
42
 
        return name in self._was_called
43
 
 
44
 
    def getArch(self):
45
 
        return 'i386'
46
 
 
47
 
    addWaitingFile = FakeMethod()
48
 
 
49
 
 
50
 
class MockBuildManager(TranslationTemplatesBuildManager):
51
 
    def __init__(self, *args, **kwargs):
52
 
        super(MockBuildManager, self).__init__(*args, **kwargs)
53
 
        self.commands = []
54
 
 
55
 
    def runSubProcess(self, path, command):
56
 
        self.commands.append([path]+command)
57
 
        return 0
58
 
 
59
 
 
60
 
class TestTranslationTemplatesBuildManagerIteration(TestCase):
61
 
    """Run TranslationTemplatesBuildManager through its iteration steps."""
62
 
    def setUp(self):
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):
68
 
            os.mkdir(dir)
69
 
        self.slave = FakeSlave(slave_dir)
70
 
        self.buildid = '123'
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')
75
 
 
76
 
    def getState(self):
77
 
        """Retrieve build manager's state."""
78
 
        return self.buildmanager._state
79
 
 
80
 
    def test_iterate(self):
81
 
        # Two iteration steps are specific to this build manager.
82
 
        url = 'lp:~my/branch'
83
 
        # The build manager's iterate() kicks off the consecutive states
84
 
        # after INIT.
85
 
        self.buildmanager.initiate({}, 'chroot.tar.gz', {'branch_url': url})
86
 
 
87
 
        # Skip states that are done in DebianBuldManager to the state
88
 
        # directly before INSTALL.
89
 
        self.buildmanager._state = TranslationTemplatesBuildState.UPDATE
90
 
 
91
 
        # INSTALL: Install additional packages needed for this job into
92
 
        # the chroot.
93
 
        self.buildmanager.iterate(0)
94
 
        self.assertEqual(
95
 
            TranslationTemplatesBuildState.INSTALL, self.getState())
96
 
        expected_command = [
97
 
            '/usr/bin/sudo',
98
 
            'sudo', 'chroot', self.chrootdir,
99
 
            'apt-get',
100
 
            ]
101
 
        self.assertEqual(expected_command, self.buildmanager.commands[-1][:5])
102
 
 
103
 
        # GENERATE: Run the slave's payload, the script that generates
104
 
        # templates.
105
 
        self.buildmanager.iterate(0)
106
 
        self.assertEqual(
107
 
            TranslationTemplatesBuildState.GENERATE, self.getState())
108
 
        expected_command = [
109
 
            'generatepath', 'generatepath', self.buildid, url, 'resultarchive'
110
 
            ]
111
 
        self.assertEqual(expected_command, self.buildmanager.commands[-1])
112
 
        self.assertFalse(self.slave.wasCalled('chrootFail'))
113
 
 
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))
118
 
 
119
 
        outfile = open(outfile_path, 'w')
120
 
        outfile.write("I am a template tarball. Seriously.")
121
 
        outfile.close()
122
 
 
123
 
        # The control returns to the DebianBuildManager in the REAP state.
124
 
        self.buildmanager.iterate(0)
125
 
        expected_command = [
126
 
            'processscanpath', 'processscanpath', self.buildid
127
 
            ]
128
 
        self.assertEqual(
129
 
            TranslationTemplatesBuildState.REAP, self.getState())
130
 
        self.assertEqual(expected_command, self.buildmanager.commands[-1])
131
 
        self.assertFalse(self.slave.wasCalled('buildFail'))
132
 
        self.assertEqual(
133
 
            [((outfile_path,), {})], self.slave.addWaitingFile.calls)
134
 
 
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
139
 
        # after INIT.
140
 
        self.buildmanager.initiate({}, 'chroot.tar.gz', {'branch_url': url})
141
 
 
142
 
        # Skip states to the INSTALL state.
143
 
        self.buildmanager._state = TranslationTemplatesBuildState.INSTALL
144
 
 
145
 
        # The buildmanager fails and iterates to the UMOUNT state.
146
 
        self.buildmanager.iterate(-1)
147
 
        self.assertEqual(
148
 
            TranslationTemplatesBuildState.UMOUNT, self.getState())
149
 
        expected_command = [
150
 
            'umountpath', 'umount-chroot', self.buildid
151
 
            ]
152
 
        self.assertEqual(expected_command, self.buildmanager.commands[-1])
153
 
        self.assertTrue(self.slave.wasCalled('chrootFail'))
154
 
 
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
159
 
        # after INIT.
160
 
        self.buildmanager.initiate({}, 'chroot.tar.gz', {'branch_url': url})
161
 
 
162
 
        # Skip states to the INSTALL state.
163
 
        self.buildmanager._state = TranslationTemplatesBuildState.GENERATE
164
 
 
165
 
        # The buildmanager fails and iterates to the REAP state.
166
 
        self.buildmanager.iterate(-1)
167
 
        expected_command = [
168
 
            'processscanpath', 'processscanpath', self.buildid
169
 
            ]
170
 
        self.assertEqual(
171
 
            TranslationTemplatesBuildState.REAP, self.getState())
172
 
        self.assertEqual(expected_command, self.buildmanager.commands[-1])
173
 
        self.assertTrue(self.slave.wasCalled('buildFail'))