aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/scripts/test/test_tap2rpm.py
blob: 509e69cb173207728fcf940ed8b4a0cdb767eae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Tests for L{twisted.scripts.tap2rpm}.
"""
import os

from twisted.trial.unittest import TestCase, SkipTest
from twisted.python import procutils
from twisted.python import versions
from twisted.python import deprecate
from twisted.python.failure import Failure
from twisted.internet import utils
from twisted.scripts import tap2rpm

# When we query the RPM metadata, we get back a string we'll have to parse, so
# we'll use suitably rare delimiter characters to split on. Luckily, ASCII
# defines some for us!
RECORD_SEPARATOR = "\x1E"
UNIT_SEPARATOR = "\x1F"



def _makeRPMs(tapfile=None, maintainer=None, protocol=None, description=None,
        longDescription=None, setVersion=None, rpmfile=None, type_=None):
    """
    Helper function to invoke tap2rpm with the given parameters.
    """
    args = []

    if not tapfile:
        tapfile = "dummy-tap-file"
        handle = open(tapfile, "w")
        handle.write("# Dummy TAP file\n")
        handle.close()

    args.extend(["--quiet", "--tapfile", tapfile])

    if maintainer:
        args.extend(["--maintainer", maintainer])
    if protocol:
        args.extend(["--protocol", protocol])
    if description:
        args.extend(["--description", description])
    if longDescription:
        args.extend(["--long_description", longDescription])
    if setVersion:
        args.extend(["--set-version", setVersion])
    if rpmfile:
        args.extend(["--rpmfile", rpmfile])
    if type_:
        args.extend(["--type", type_])

    return tap2rpm.run(args)



def _queryRPMTags(rpmfile, taglist):
    """
    Helper function to read the given header tags from the given RPM file.

    Returns a Deferred that fires with dictionary mapping a tag name to a list
    of the associated values in the RPM header. If a tag has only a single
    value in the header (like NAME or VERSION), it will be returned as a 1-item
    list.

    Run "rpm --querytags" to see what tags can be queried.
    """

    # Build a query format string that will return appropriately delimited
    # results. Every field is treated as an array field, so single-value tags
    # like VERSION will be returned as 1-item lists.
    queryFormat = RECORD_SEPARATOR.join([
            "[%%{%s}%s]" % (tag, UNIT_SEPARATOR) for tag in taglist
           ])

    def parseTagValues(output):
        res = {}

        for tag, values in zip(taglist, output.split(RECORD_SEPARATOR)):
            values = values.strip(UNIT_SEPARATOR).split(UNIT_SEPARATOR)
            res[tag] = values

        return res

    def checkErrorResult(failure):
        # The current rpm packages on Debian and Ubuntu don't properly set up
        # the RPM database, which causes rpm to print a harmless warning to
        # stderr. Unfortunately, .getProcessOutput() assumes all warnings are
        # catastrophic and panics whenever it sees one.
        #
        # See also:
        #   http://twistedmatrix.com/trac/ticket/3292#comment:42
        #   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=551669
        #   http://rpm.org/ticket/106

        failure.trap(IOError)

        # Depending on kernel scheduling, we might read the whole error
        # message, or only the first few bytes.
        if str(failure.value).startswith("got stderr: 'error: "):
            newFailure = Failure(SkipTest("rpm is missing its package "
                    "database. Run 'sudo rpm -qa > /dev/null' to create one."))
        else:
            # Not the exception we were looking for; we should report the
            # original failure.
            newFailure = failure

        # We don't want to raise the exception right away; we want to wait for
        # the process to exit, otherwise we'll get extra useless errors
        # reported.
        d = failure.value.processEnded
        d.addBoth(lambda _: newFailure)
        return d

    d = utils.getProcessOutput("rpm",
            ("-q", "--queryformat", queryFormat, "-p", rpmfile))
    d.addCallbacks(parseTagValues, checkErrorResult)
    return d



class TestTap2RPM(TestCase):


    def setUp(self):
        return self._checkForRpmbuild()


    def _checkForRpmbuild(self):
        """
        tap2rpm requires rpmbuild; skip tests if rpmbuild is not present.
        """
        if not procutils.which("rpmbuild"):
            raise SkipTest("rpmbuild must be present to test tap2rpm")


    def _makeTapFile(self, basename="dummy"):
        """
        Make a temporary .tap file and returns the absolute path.
        """
        path = basename + ".tap"
        handle = open(path, "w")
        handle.write("# Dummy .tap file")
        handle.close()
        return path


    def _verifyRPMTags(self, rpmfile, **tags):
        """
        Check the given file has the given tags set to the given values.
        """

        d = _queryRPMTags(rpmfile, tags.keys())
        d.addCallback(self.assertEqual, tags)
        return d


    def test_optionDefaults(self):
        """
        Commandline options should default to sensible values.

        "sensible" here is defined as "the same values that previous versions
        defaulted to".
        """
        config = tap2rpm.MyOptions()
        config.parseOptions([])

        self.assertEqual(config['tapfile'], 'twistd.tap')
        self.assertEqual(config['maintainer'], 'tap2rpm')
        self.assertEqual(config['protocol'], 'twistd')
        self.assertEqual(config['description'], 'A TCP server for twistd')
        self.assertEqual(config['long_description'],
                'Automatically created by tap2rpm')
        self.assertEqual(config['set-version'], '1.0')
        self.assertEqual(config['rpmfile'], 'twisted-twistd')
        self.assertEqual(config['type'], 'tap')
        self.assertEqual(config['quiet'], False)
        self.assertEqual(config['twistd_option'], 'file')
        self.assertEqual(config['release-name'], 'twisted-twistd-1.0')


    def test_protocolCalculatedFromTapFile(self):
        """
        The protocol name defaults to a value based on the tapfile value.
        """
        config = tap2rpm.MyOptions()
        config.parseOptions(['--tapfile', 'pancakes.tap'])

        self.assertEqual(config['tapfile'], 'pancakes.tap')
        self.assertEqual(config['protocol'], 'pancakes')


    def test_optionsDefaultToProtocolValue(self):
        """
        Many options default to a value calculated from the protocol name.
        """
        config = tap2rpm.MyOptions()
        config.parseOptions([
                '--tapfile', 'sausages.tap',
                '--protocol', 'eggs',
            ])

        self.assertEqual(config['tapfile'], 'sausages.tap')
        self.assertEqual(config['maintainer'], 'tap2rpm')
        self.assertEqual(config['protocol'], 'eggs')
        self.assertEqual(config['description'], 'A TCP server for eggs')
        self.assertEqual(config['long_description'],
                'Automatically created by tap2rpm')
        self.assertEqual(config['set-version'], '1.0')
        self.assertEqual(config['rpmfile'], 'twisted-eggs')
        self.assertEqual(config['type'], 'tap')
        self.assertEqual(config['quiet'], False)
        self.assertEqual(config['twistd_option'], 'file')
        self.assertEqual(config['release-name'], 'twisted-eggs-1.0')


    def test_releaseNameDefaultsToRpmfileValue(self):
        """
        The release-name option is calculated from rpmfile and set-version.
        """
        config = tap2rpm.MyOptions()
        config.parseOptions([
                "--rpmfile", "beans",
                "--set-version", "1.2.3",
            ])

        self.assertEqual(config['release-name'], 'beans-1.2.3')


    def test_basicOperation(self):
        """
        Calling tap2rpm should produce an RPM and SRPM with default metadata.
        """
        basename = "frenchtoast"

        # Create RPMs based on a TAP file with this name.
        rpm, srpm = _makeRPMs(tapfile=self._makeTapFile(basename))

        # Verify the resulting RPMs have the correct tags.
        d = self._verifyRPMTags(rpm,
                NAME=["twisted-%s" % (basename,)],
                VERSION=["1.0"],
                RELEASE=["1"],
                SUMMARY=["A TCP server for %s" % (basename,)],
                DESCRIPTION=["Automatically created by tap2rpm"],
            )
        d.addCallback(lambda _: self._verifyRPMTags(srpm,
                NAME=["twisted-%s" % (basename,)],
                VERSION=["1.0"],
                RELEASE=["1"],
                SUMMARY=["A TCP server for %s" % (basename,)],
                DESCRIPTION=["Automatically created by tap2rpm"],
            ))

        return d


    def test_protocolOverride(self):
        """
        Setting 'protocol' should change the name of the resulting package.
        """
        basename = "acorn"
        protocol = "banana"

        # Create RPMs based on a TAP file with this name.
        rpm, srpm = _makeRPMs(tapfile=self._makeTapFile(basename),
                protocol=protocol)

        # Verify the resulting RPMs have the correct tags.
        d = self._verifyRPMTags(rpm,
                NAME=["twisted-%s" % (protocol,)],
                SUMMARY=["A TCP server for %s" % (protocol,)],
            )
        d.addCallback(lambda _: self._verifyRPMTags(srpm,
                NAME=["twisted-%s" % (protocol,)],
                SUMMARY=["A TCP server for %s" % (protocol,)],
            ))

        return d


    def test_rpmfileOverride(self):
        """
        Setting 'rpmfile' should change the name of the resulting package.
        """
        basename = "cherry"
        rpmfile = "donut"

        # Create RPMs based on a TAP file with this name.
        rpm, srpm = _makeRPMs(tapfile=self._makeTapFile(basename),
                rpmfile=rpmfile)

        # Verify the resulting RPMs have the correct tags.
        d = self._verifyRPMTags(rpm,
                NAME=[rpmfile],
                SUMMARY=["A TCP server for %s" % (basename,)],
            )
        d.addCallback(lambda _: self._verifyRPMTags(srpm,
                NAME=[rpmfile],
                SUMMARY=["A TCP server for %s" % (basename,)],
            ))

        return d


    def test_descriptionOverride(self):
        """
        Setting 'description' should change the SUMMARY tag.
        """
        description = "eggplant"

        # Create RPMs based on a TAP file with this name.
        rpm, srpm = _makeRPMs(tapfile=self._makeTapFile(),
                description=description)

        # Verify the resulting RPMs have the correct tags.
        d = self._verifyRPMTags(rpm,
                SUMMARY=[description],
            )
        d.addCallback(lambda _: self._verifyRPMTags(srpm,
                SUMMARY=[description],
            ))

        return d


    def test_longDescriptionOverride(self):
        """
        Setting 'longDescription' should change the DESCRIPTION tag.
        """
        longDescription = "fig"

        # Create RPMs based on a TAP file with this name.
        rpm, srpm = _makeRPMs(tapfile=self._makeTapFile(),
                longDescription=longDescription)

        # Verify the resulting RPMs have the correct tags.
        d = self._verifyRPMTags(rpm,
                DESCRIPTION=[longDescription],
            )
        d.addCallback(lambda _: self._verifyRPMTags(srpm,
                DESCRIPTION=[longDescription],
            ))

        return d


    def test_setVersionOverride(self):
        """
        Setting 'setVersion' should change the RPM's version info.
        """
        version = "123.456"

        # Create RPMs based on a TAP file with this name.
        rpm, srpm = _makeRPMs(tapfile=self._makeTapFile(),
                setVersion=version)

        # Verify the resulting RPMs have the correct tags.
        d = self._verifyRPMTags(rpm,
                VERSION=["123.456"],
                RELEASE=["1"],
            )
        d.addCallback(lambda _: self._verifyRPMTags(srpm,
                VERSION=["123.456"],
                RELEASE=["1"],
            ))

        return d


    def test_tapInOtherDirectory(self):
        """
        tap2rpm handles tapfiles outside the current directory.
        """
        # Make a tapfile outside the current directory.
        tempdir = self.mktemp()
        os.mkdir(tempdir)
        tapfile = self._makeTapFile(os.path.join(tempdir, "bacon"))

        # Try and make an RPM from that tapfile.
        _makeRPMs(tapfile=tapfile)


    def test_unsignedFlagDeprecationWarning(self):
        """
        The 'unsigned' flag in tap2rpm should be deprecated, and its use
        should raise a warning as such.
        """
        config = tap2rpm.MyOptions()
        config.parseOptions(['--unsigned'])
        warnings = self.flushWarnings()
        self.assertEqual(DeprecationWarning, warnings[0]['category'])
        self.assertEqual(
            deprecate.getDeprecationWarningString(
                config.opt_unsigned, versions.Version("Twisted", 12, 1, 0)),
            warnings[0]['message'])
        self.assertEqual(1, len(warnings))