summaryrefslogtreecommitdiff
path: root/tests/test_rpm.py
blob: 06b5faa8dc7e3300320cd9eeba276750c32775d0 (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
# vim: set fileencoding=utf-8 :
#
# (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""Test the classes under L{gbp.rpm}"""

import filecmp
import os
import shutil
import tempfile
from nose.tools import assert_raises

from gbp.errors import GbpError
from gbp.rpm import (SrcRpmFile, SpecFile, parse_srpm, parse_spec, guess_spec,
                    NoSpecError)
from gbp.rpm import rpm as rpmlib

DATA_DIR = os.path.abspath(os.path.splitext(__file__)[0] + '_data')
SRPM_DIR = os.path.join(DATA_DIR, 'srpms')
SPEC_DIR = os.path.join(DATA_DIR, 'specs')

class SpecFileTester(SpecFile):
    """Helper class for testing"""

    def protected(self, name):
        """Get a protected member"""
        return super(SpecFileTester, self).__getattribute__(name)


class TestSrcRpmFile(object):
    """Test L{gbp.rpm.SrcRpmFile}"""

    def setup(self):
        self.tmpdir = tempfile.mkdtemp(prefix='gbp_%s_' % __name__, dir='.')

    def teardown(self):
        shutil.rmtree(self.tmpdir)

    def test_srpm(self):
        """Test parsing of a source rpm"""
        srpm = SrcRpmFile(os.path.join(SRPM_DIR, 'gbp-test-1.0-1.src.rpm'))
        assert srpm.version ==  {'release': '1', 'upstreamversion': '1.0'}
        assert srpm.name == 'gbp-test'
        assert srpm.upstreamversion == '1.0'
        assert srpm.packager is None

    def test_srpm_2(self):
        """Test parsing of another source rpm"""
        srpm = SrcRpmFile(os.path.join(SRPM_DIR, 'gbp-test2-3.0-0.src.rpm'))
        assert srpm.version == {'release': '0', 'upstreamversion': '3.0',
                                'epoch': '2'}
        assert srpm.packager == 'Markus Lehtonen '\
                                '<markus.lehtonen@linux.intel.com>'

    def test_unpack_srpm(self):
        """Test unpacking of a source rpm"""
        srpm = SrcRpmFile(os.path.join(SRPM_DIR, 'gbp-test-1.0-1.src.rpm'))
        srpm.unpack(self.tmpdir)
        for fn in ['gbp-test-1.0.tar.bz2', 'foo.txt', 'bar.tar.gz', 'my.patch',
                   'my2.patch', 'my3.patch']:
            assert os.path.exists(os.path.join(self.tmpdir, fn)), \
                    "%s not found" % fn


class TestSpecFile(object):
    """Test L{gbp.rpm.SpecFile}"""

    def setup(self):
        os.environ['GBP_RPM_VERSION'] = rpmlib.__version__
        self.tmpdir = tempfile.mkdtemp(prefix='gbp_%s_' % __name__, dir='.')

    def teardown(self):
        os.environ.pop('GBP_RPM_VERSION')
        shutil.rmtree(self.tmpdir)

    def test_spec(self):
        """Test parsing of a valid spec file"""
        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test.spec')
        spec = SpecFileTester(spec_filepath)

        # Test basic properties
        assert spec.specfile == spec_filepath
        assert spec.specdir == os.path.dirname(spec_filepath)

        assert spec.name == 'gbp-test'
        assert spec.packager is None

        assert spec.upstreamversion == '1.0'
        assert spec.release == '1'
        assert spec.epoch is None
        assert spec.version == {'release': '1', 'upstreamversion': '1.0'}

        orig = spec.orig_src
        assert orig['filename'] == 'gbp-test-1.0.tar.bz2'
        assert orig['filename_base'] == 'gbp-test-1.0'
        assert orig['archive_fmt'] == 'tar'
        assert orig['compression'] == 'bzip2'
        assert orig['prefix'] == 'gbp-test/'

    def test_spec_2(self):
        """Test parsing of another valid spec file"""
        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test2.spec')
        spec = SpecFile(spec_filepath)

        # Test basic properties
        assert spec.name == 'gbp-test2'
        assert spec.packager == 'Markus Lehtonen ' \
                                '<markus.lehtonen@linux.intel.com>'

        assert spec.epoch == '2'
        assert spec.version == {'release': '0', 'upstreamversion': '3.0',
                                'epoch': '2'}

        orig = spec.orig_src
        assert orig['filename'] == 'gbp-test2-3.0.tar.gz'
        assert orig['archive_fmt'] == 'tar'
        assert orig['compression'] == 'gzip'
        assert orig['prefix'] == ''

    def test_spec_3(self):
        """Test parsing of yet another valid spec file"""
        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test-native.spec')
        spec = SpecFile(spec_filepath)

        # Test basic properties
        assert spec.name == 'gbp-test-native'
        orig = spec.orig_src
        assert orig['filename'] == 'gbp-test-native-1.0.zip'
        assert orig['archive_fmt'] == 'zip'
        assert orig['compression'] == None
        assert orig['prefix'] == 'gbp-test-native-1.0/'

    def test_spec_4(self):
        """Test parsing of spec without orig tarball"""
        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test-native2.spec')
        spec = SpecFile(spec_filepath)

        # Test basic properties
        assert spec.name == 'gbp-test-native2'
        assert spec.orig_src is None

    def test_update_spec(self):
        """Test spec autoupdate functionality"""
        # Create temporary spec file
        tmp_spec = os.path.join(self.tmpdir, 'gbp-test.spec')
        shutil.copy2(os.path.join(SPEC_DIR, 'gbp-test.spec'), tmp_spec)

        reference_spec = os.path.join(SPEC_DIR, 'gbp-test-reference.spec')
        spec = SpecFile(tmp_spec)
        spec.update_patches(['new.patch'])
        spec.write_spec_file()
        assert filecmp.cmp(tmp_spec, reference_spec) is True

        # Test adding the VCS tag
        reference_spec = os.path.join(SPEC_DIR, 'gbp-test-reference2.spec')
        spec.set_tag('VCS', None, 'myvcstag')
        spec.write_spec_file()
        assert filecmp.cmp(tmp_spec, reference_spec) is True

    def test_update_spec2(self):
        """Another test for spec autoupdate functionality"""
        tmp_spec = os.path.join(self.tmpdir, 'gbp-test.spec')
        shutil.copy2(os.path.join(SPEC_DIR, 'gbp-test2.spec'), tmp_spec)

        reference_spec = os.path.join(SPEC_DIR, 'gbp-test2-reference2.spec')
        spec = SpecFile(tmp_spec)
        spec.update_patches(['1.patch', '2.patch'])
        spec.set_tag('VCS', None, 'myvcstag')
        spec.update_patches(['new.patch'])
        spec.write_spec_file()
        assert filecmp.cmp(tmp_spec, reference_spec) is True

        # Test removing the VCS tag
        reference_spec = os.path.join(SPEC_DIR, 'gbp-test2-reference.spec')
        spec.set_tag('VCS', None, '')
        spec.write_spec_file()
        assert filecmp.cmp(tmp_spec, reference_spec) is True

    def test_modifying(self):
        """Test updating/deleting of tags and macros"""
        tmp_spec = os.path.join(self.tmpdir, 'gbp-test.spec')
        shutil.copy2(os.path.join(SPEC_DIR, 'gbp-test-updates.spec'), tmp_spec)
        reference_spec = os.path.join(SPEC_DIR,
                                      'gbp-test-updates-reference.spec')
        spec = SpecFileTester(tmp_spec)

        # Mangle tags
        prev = spec.protected('_delete_tag')('Vendor', None)
        spec.protected('_set_tag')('License', None, 'new license', prev)
        spec.protected('_delete_tag')('source', 0)
        spec.protected('_delete_tag')('patch', 1)
        spec.protected('_delete_tag')('patch', 0)
        prev = spec.protected('_delete_tag')('invalidtag', None)

        with assert_raises(GbpError):
            # Check that setting empty value fails
            spec.protected('_set_tag')('Version', None, '', prev)
        with assert_raises(GbpError):
            # Check that setting invalid tag with public method fails
            spec.set_tag('invalidtag', None, 'value')

        # Mangle macros
        prev = spec.protected('_delete_special_macro')('patch', 0)
        spec.protected('_delete_special_macro')('patch', 123)
        spec.protected('_set_special_macro')('patch', 1, 'my new args', prev)
        with assert_raises(GbpError):
            spec.protected('_delete_special_macro')('invalidmacro', 0)
        with assert_raises(GbpError):
            spec.protected('_set_special_macro')('invalidmacro', 0, 'args',
                           prev)

        # Check resulting spec file
        spec.write_spec_file()
        assert filecmp.cmp(tmp_spec, reference_spec) is True

    def test_quirks(self):
        """Test spec that is broken/has anomalities"""
        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test-quirks.spec')
        spec = SpecFile(spec_filepath)

        # Check that we quess orig source and prefix correctly
        assert spec.orig_src['prefix'] == 'foobar/'

    def test_tags(self):
        """Test parsing of all the different tags of spec file"""
        spec_filepath = os.path.join(SPEC_DIR, 'gbp-test-tags.spec')
        spec = SpecFileTester(spec_filepath)

        # Check all the tags
        for name, val in spec.protected('_tags').iteritems():
            rval = None
            if name in ('version', 'release', 'epoch', 'nosource', 'nopatch'):
                rval = '0'
            elif name in ('autoreq', 'autoprov', 'autoreqprov'):
                rval = 'No'
            elif name not in spec.protected('_listtags'):
                rval = 'my_%s' % name
            if rval:
                assert val['value'] == rval, ("'%s:' is '%s', expecting '%s'" %
                                              (name, val['value'], rval))
            assert spec.ignorepatches == []


class TestUtilityFunctions(object):
    """Test utility functions of L{gbp.rpm}"""

    def test_parse_spec(self):
        """Test parse_spec() function"""
        parse_spec(os.path.join(SPEC_DIR, 'gbp-test.spec'))
        with assert_raises(NoSpecError):
            parse_spec(os.path.join(DATA_DIR, 'notexists.spec'))
        with assert_raises(GbpError):
            parse_spec(os.path.join(SRPM_DIR, 'gbp-test-1.0-1.src.rpm'))

    def test_parse_srpm(self):
        """Test parse_srpm() function"""
        parse_srpm(os.path.join(SRPM_DIR, 'gbp-test-1.0-1.src.rpm'))
        with assert_raises(GbpError):
            parse_srpm(os.path.join(DATA_DIR, 'notexists.src.rpm'))
        with assert_raises(GbpError):
            parse_srpm(os.path.join(SPEC_DIR, 'gbp-test.spec'))

    def test_guess_spec(self):
        """Test guess_spec() function"""
        # Spec not found
        with assert_raises(NoSpecError):
            guess_spec(DATA_DIR, recursive=False)
        # Multiple spec files
        with assert_raises(NoSpecError):
            guess_spec(DATA_DIR, recursive=True)
        with assert_raises(NoSpecError):
            guess_spec(SPEC_DIR, recursive=False)
        # Spec found
        spec_fn = guess_spec(SPEC_DIR, recursive=False,
                             preferred_name = 'gbp-test2.spec')
        assert spec_fn == os.path.join(SPEC_DIR, 'gbp-test2.spec')

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: