~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/hardwaredb/scripts/tests/test_hwdbsubmissions.py

  • Committer: Abel Deuring
  • Date: 2011-09-13 14:47:12 UTC
  • mto: This revision was merged to the branch mainline in revision 13946.
  • Revision ID: abel.deuring@canonical.com-20110913144712-kaf2u53te0f5gopp
transofrm reprocess-hwdb-submissions.py into a cronscript

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
__metaclass__ = type
7
7
 
 
8
from storm.store import Store
 
9
from tempfile import mktemp
8
10
 
 
11
from canonical.launchpad.ftests.script import run_script
9
12
from canonical.testing.layers import LaunchpadScriptLayer
10
13
from lp.hardwaredb.interfaces.hwdb import HWSubmissionProcessingStatus
11
14
from lp.hardwaredb.scripts.hwdbsubmissions import (
13
16
    ProcessingLoopForReprocessingBadSubmissions,
14
17
    )
15
18
from lp.testing import TestCaseWithFactory
 
19
from lp.testing.matchers import Contains
 
20
import transaction
16
21
 
17
22
 
18
23
class TestProcessingLoops(TestCaseWithFactory):
113
118
        # The sample data already contains one submission.
114
119
        submissions = loop.getUnprocessedSubmissions(2)
115
120
        self.assertEqual([submission2], submissions)
 
121
 
 
122
    def test_run_reprocessing_script_no_params(self):
 
123
        # cronscripts/reprocess-hwdb-submissions.py needs at least the
 
124
        # parameter --start-file
 
125
        retcode, stdout, stderr = run_script(
 
126
            'cronscripts/reprocess-hwdb-submissions.py', [])
 
127
        self.assertThat(
 
128
            stderr, Contains('Option --start-file not specified.'))
 
129
 
 
130
    def test_run_reprocessing_script_startfile_does_not_exist(self):
 
131
        # If the specified start file does not exist,
 
132
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
133
        does_not_exist = mktemp()
 
134
        retcode, stdout, stderr = run_script(
 
135
            'cronscripts/reprocess-hwdb-submissions.py',
 
136
            ['--start-file', does_not_exist])
 
137
        self.assertThat(
 
138
            stderr, Contains('Cannot access file %s' % does_not_exist))
 
139
 
 
140
    def test_run_reprocessing_script_startfile_without_integer(self):
 
141
        # If the specified start file contains any non-integer string,
 
142
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
143
        start_file_name = mktemp()
 
144
        start_file = open(start_file_name, 'w')
 
145
        start_file.write('nonsense')
 
146
        start_file.close()
 
147
        retcode, stdout, stderr = run_script(
 
148
            'cronscripts/reprocess-hwdb-submissions.py',
 
149
            ['--start-file', start_file_name])
 
150
        self.assertThat(
 
151
            stderr,
 
152
            Contains('%s must contain only an integer' % start_file_name))
 
153
 
 
154
    def test_run_reprocessing_script_startfile_with_negative_integer(self):
 
155
        # If the specified start file contains any non-integer string,
 
156
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
157
        start_file_name = mktemp()
 
158
        start_file = open(start_file_name, 'w')
 
159
        start_file.write('-1')
 
160
        start_file.close()
 
161
        retcode, stdout, stderr = run_script(
 
162
            'cronscripts/reprocess-hwdb-submissions.py',
 
163
            ['--start-file', start_file_name])
 
164
        self.assertThat(
 
165
            stderr,
 
166
            Contains('%s must contain a positive integer' % start_file_name))
 
167
 
 
168
    def test_run_reprocessing_script_max_submission_not_integer(self):
 
169
        # If the parameter --max-submissions is not an integer,
 
170
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
171
        retcode, stdout, stderr = run_script(
 
172
            'cronscripts/reprocess-hwdb-submissions.py',
 
173
            ['--max-submissions', 'nonsense'])
 
174
        expected = "Invalid value for --max_submissions specified: 'nonsense'"
 
175
        self.assertThat(stderr, Contains(expected))
 
176
 
 
177
    def test_run_reprocessing_script_two_batches(self):
 
178
        # cronscripts/reprocess-hwdb-submissions.py begings to process
 
179
        # submissions with IDs starting at the value stored in the
 
180
        # file given as the parameter --start-file. When is has
 
181
        # finished processing the number of submissions specified by
 
182
        # --max-submissions, it stores the ID of the last prcessed
 
183
        # submission in start-file.
 
184
        new_submissions = []
 
185
        for count in range(5):
 
186
            new_submissions.append(
 
187
                self.factory.makeHWSubmission(
 
188
                    status=HWSubmissionProcessingStatus.INVALID))
 
189
 
 
190
        start_file_name = mktemp()
 
191
        start_file = open(start_file_name, 'w')
 
192
        start_file.write('%i' % new_submissions[1].id)
 
193
        start_file.close()
 
194
        transaction.commit()
 
195
        Store.of(new_submissions[0]).invalidate()
 
196
 
 
197
        retcode, stdout, stderr = run_script(
 
198
            'cronscripts/reprocess-hwdb-submissions.py',
 
199
            ['--max-submissions', '2', '--start-file', start_file_name])
 
200
 
 
201
        # We started with the ID of the second submission created abvoe,
 
202
        # so the first submission still has the status INVALID.
 
203
        self.assertEqual(
 
204
            HWSubmissionProcessingStatus.INVALID,
 
205
            new_submissions[0].status)
 
206
        # We processed two submissions, they now have the status
 
207
        # PROCESSED.
 
208
        self.assertEqual(
 
209
            HWSubmissionProcessingStatus.PROCESSED,
 
210
            new_submissions[1].status)
 
211
        self.assertEqual(
 
212
            HWSubmissionProcessingStatus.PROCESSED,
 
213
            new_submissions[2].status)
 
214
        # The  following submissions were not yet touched,
 
215
        self.assertEqual(
 
216
            HWSubmissionProcessingStatus.INVALID,
 
217
            new_submissions[3].status)
 
218
        self.assertEqual(
 
219
            HWSubmissionProcessingStatus.INVALID,
 
220
            new_submissions[4].status)
 
221
 
 
222
        # The start file now contains the ID of the 4th submission.
 
223
        new_start = int(open(start_file_name).read())
 
224
        self.assertEqual(new_submissions[3].id, new_start)
 
225
 
 
226
        # When we run the script again, for only one submission,
 
227
        # the 4th submission is processed.
 
228
        transaction.abort()
 
229
        Store.of(new_submissions[0]).invalidate()
 
230
        retcode, stdout, stderr = run_script(
 
231
            'cronscripts/reprocess-hwdb-submissions.py',
 
232
            ['--max-submissions', '1', '--start-file', start_file_name])
 
233
        self.assertEqual(
 
234
            HWSubmissionProcessingStatus.PROCESSED,
 
235
            new_submissions[3].status)
 
236
        self.assertEqual(
 
237
            HWSubmissionProcessingStatus.INVALID,
 
238
            new_submissions[4].status)