~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

[r=gmb][bug=849159] convert scripts/reprocess-hwdb-submissions.py
        into cronscripts/reprocess-hwdb-submissions.py

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):
102
107
        submissions = loop.getUnprocessedSubmissions(1)
103
108
        self.assertEqual(1, len(submissions))
104
109
 
105
 
    def test_BadSubmissions_respects_start(self):
 
110
    # XXX 2011-09-13, Abel Deuring: Disabled due to bug 849056.
 
111
    def xxx_test_BadSubmissions_respects_start(self):
106
112
        # It is possible to request a start id. Previous entries are ignored.
107
113
        submission1 = self.factory.makeHWSubmission(
108
114
            status=HWSubmissionProcessingStatus.INVALID)
113
119
        # The sample data already contains one submission.
114
120
        submissions = loop.getUnprocessedSubmissions(2)
115
121
        self.assertEqual([submission2], submissions)
 
122
 
 
123
    # XXX 2011-09-13, Abel Deuring: Disabled due to bug 849056.
 
124
    def xxx_test_run_reprocessing_script_no_params(self):
 
125
        # cronscripts/reprocess-hwdb-submissions.py needs at least the
 
126
        # parameter --start-file
 
127
        retcode, stdout, stderr = run_script(
 
128
            'cronscripts/reprocess-hwdb-submissions.py', [])
 
129
        self.assertThat(
 
130
            stderr, Contains('Option --start-file not specified.'))
 
131
 
 
132
    # XXX 2011-09-13, Abel Deuring: Disabled due to bug 849056.
 
133
    def xxx_test_run_reprocessing_script_startfile_does_not_exist(self):
 
134
        # If the specified start file does not exist,
 
135
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
136
        does_not_exist = mktemp()
 
137
        retcode, stdout, stderr = run_script(
 
138
            'cronscripts/reprocess-hwdb-submissions.py',
 
139
            ['--start-file', does_not_exist])
 
140
        self.assertThat(
 
141
            stderr, Contains('Cannot access file %s' % does_not_exist))
 
142
 
 
143
    # XXX 2011-09-13, Abel Deuring: Disabled due to bug 849056.
 
144
    def xxx_test_run_reprocessing_script_startfile_without_integer(self):
 
145
        # If the specified start file contains any non-integer string,
 
146
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
147
        start_file_name = mktemp()
 
148
        start_file = open(start_file_name, 'w')
 
149
        start_file.write('nonsense')
 
150
        start_file.close()
 
151
        retcode, stdout, stderr = run_script(
 
152
            'cronscripts/reprocess-hwdb-submissions.py',
 
153
            ['--start-file', start_file_name])
 
154
        self.assertThat(
 
155
            stderr,
 
156
            Contains('%s must contain only an integer' % start_file_name))
 
157
 
 
158
    # XXX 2011-09-13, Abel Deuring: Disabled due to bug 849056.
 
159
    def xxx_test_run_reprocessing_script_startfile_with_negative_integer(self):
 
160
        # If the specified start file contains any non-integer string,
 
161
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
162
        start_file_name = mktemp()
 
163
        start_file = open(start_file_name, 'w')
 
164
        start_file.write('-1')
 
165
        start_file.close()
 
166
        retcode, stdout, stderr = run_script(
 
167
            'cronscripts/reprocess-hwdb-submissions.py',
 
168
            ['--start-file', start_file_name])
 
169
        self.assertThat(
 
170
            stderr,
 
171
            Contains('%s must contain a positive integer' % start_file_name))
 
172
 
 
173
    # XXX 2011-09-13, Abel Deuring: Disabled due to bug 849056.
 
174
    def xxx_test_run_reprocessing_script_max_submission_not_integer(self):
 
175
        # If the parameter --max-submissions is not an integer,
 
176
        # cronscripts/reprocess-hwdb-submissions.py reports an error.
 
177
        retcode, stdout, stderr = run_script(
 
178
            'cronscripts/reprocess-hwdb-submissions.py',
 
179
            ['--max-submissions', 'nonsense'])
 
180
        expected = "Invalid value for --max_submissions specified: 'nonsense'"
 
181
        self.assertThat(stderr, Contains(expected))
 
182
 
 
183
    def test_run_reprocessing_script_two_batches(self):
 
184
        # cronscripts/reprocess-hwdb-submissions.py begings to process
 
185
        # submissions with IDs starting at the value stored in the
 
186
        # file given as the parameter --start-file. When is has
 
187
        # finished processing the number of submissions specified by
 
188
        # --max-submissions, it stores the ID of the last prcessed
 
189
        # submission in start-file.
 
190
        new_submissions = []
 
191
        for count in range(5):
 
192
            new_submissions.append(
 
193
                self.factory.makeHWSubmission(
 
194
                    status=HWSubmissionProcessingStatus.INVALID))
 
195
 
 
196
        start_file_name = mktemp()
 
197
        start_file = open(start_file_name, 'w')
 
198
        start_file.write('%i' % new_submissions[1].id)
 
199
        start_file.close()
 
200
        transaction.commit()
 
201
        Store.of(new_submissions[0]).invalidate()
 
202
 
 
203
        retcode, stdout, stderr = run_script(
 
204
            'cronscripts/reprocess-hwdb-submissions.py',
 
205
            ['--max-submissions', '2', '--start-file', start_file_name])
 
206
 
 
207
        # We started with the ID of the second submission created abvoe,
 
208
        # so the first submission still has the status INVALID.
 
209
        self.assertEqual(
 
210
            HWSubmissionProcessingStatus.INVALID,
 
211
            new_submissions[0].status)
 
212
        # We processed two submissions, they now have the status
 
213
        # PROCESSED.
 
214
        self.assertEqual(
 
215
            HWSubmissionProcessingStatus.PROCESSED,
 
216
            new_submissions[1].status)
 
217
        self.assertEqual(
 
218
            HWSubmissionProcessingStatus.PROCESSED,
 
219
            new_submissions[2].status)
 
220
        # The  following submissions were not yet touched,
 
221
        self.assertEqual(
 
222
            HWSubmissionProcessingStatus.INVALID,
 
223
            new_submissions[3].status)
 
224
        self.assertEqual(
 
225
            HWSubmissionProcessingStatus.INVALID,
 
226
            new_submissions[4].status)
 
227
 
 
228
        # The start file now contains the ID of the 4th submission.
 
229
        new_start = int(open(start_file_name).read())
 
230
        self.assertEqual(new_submissions[3].id, new_start)
 
231
 
 
232
        # When we run the script again, for only one submission,
 
233
        # the 4th submission is processed.
 
234
        transaction.abort()
 
235
        Store.of(new_submissions[0]).invalidate()
 
236
        retcode, stdout, stderr = run_script(
 
237
            'cronscripts/reprocess-hwdb-submissions.py',
 
238
            ['--max-submissions', '1', '--start-file', start_file_name])
 
239
        self.assertEqual(
 
240
            HWSubmissionProcessingStatus.PROCESSED,
 
241
            new_submissions[3].status)
 
242
        self.assertEqual(
 
243
            HWSubmissionProcessingStatus.INVALID,
 
244
            new_submissions[4].status)