~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/archiveuploader/tests/test_dscfile.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-08-24 14:02:15 UTC
  • mfrom: (11398.2.11 617393-cleanup-tmp)
  • Revision ID: launchpad@pqm.canonical.com-20100824140215-ltgsvkkvl2dy8my9
[r=adeuring][ui=none][bug=617393] Clean up temporary directory after
        unpacking source package for changelog/copyright inspection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
from canonical.launchpad.scripts.logger import QuietFakeLogger
11
11
from canonical.testing.layers import LaunchpadZopelessLayer
12
12
from lp.archiveuploader.dscfile import (
 
13
    cleanup_unpacked_dir,
13
14
    DSCFile,
14
 
    findChangelog,
15
 
    findCopyright,
 
15
    find_changelog,
 
16
    find_copyright,
16
17
    format_to_file_checker_map,
 
18
    unpack_source,
17
19
    )
18
20
from lp.archiveuploader.nascentuploadfile import UploadError
19
21
from lp.archiveuploader.tests import (
37
39
 
38
40
class TestDscFile(TestCase):
39
41
 
40
 
    class MockDSCFile:
41
 
        copyright = None
42
 
 
43
42
    def setUp(self):
44
43
        super(TestDscFile, self).setUp()
45
44
        self.tmpdir = self.makeTemporaryDirectory()
47
46
        os.makedirs(self.dir_path)
48
47
        self.copyright_path = os.path.join(self.dir_path, "copyright")
49
48
        self.changelog_path = os.path.join(self.dir_path, "changelog")
50
 
        self.dsc_file = self.MockDSCFile()
51
49
 
52
50
    def testBadDebianCopyright(self):
53
51
        """Test that a symlink as debian/copyright will fail.
56
54
        dangling symlink in an attempt to try and access files on the system
57
55
        processing the source packages."""
58
56
        os.symlink("/etc/passwd", self.copyright_path)
59
 
        errors = list(findCopyright(
60
 
            self.dsc_file, self.tmpdir, mock_logger_quiet))
61
 
 
62
 
        self.assertEqual(len(errors), 1)
63
 
        self.assertIsInstance(errors[0], UploadError)
 
57
        error = self.assertRaises(
 
58
            UploadError, find_copyright, self.tmpdir, mock_logger_quiet)
64
59
        self.assertEqual(
65
 
            errors[0].args[0],
66
 
            "Symbolic link for debian/copyright not allowed")
 
60
            error.args[0], "Symbolic link for debian/copyright not allowed")
67
61
 
68
62
    def testGoodDebianCopyright(self):
69
63
        """Test that a proper copyright file will be accepted"""
72
66
        file.write(copyright)
73
67
        file.close()
74
68
 
75
 
        errors = list(findCopyright(
76
 
            self.dsc_file, self.tmpdir, mock_logger_quiet))
77
 
 
78
 
        self.assertEqual(len(errors), 0)
79
 
        self.assertEqual(self.dsc_file.copyright, copyright)
 
69
        self.assertEquals(
 
70
            copyright, find_copyright(self.tmpdir, mock_logger_quiet))
80
71
 
81
72
    def testBadDebianChangelog(self):
82
73
        """Test that a symlink as debian/changelog will fail.
85
76
        dangling symlink in an attempt to try and access files on the system
86
77
        processing the source packages."""
87
78
        os.symlink("/etc/passwd", self.changelog_path)
88
 
        errors = list(findChangelog(
89
 
            self.dsc_file, self.tmpdir, mock_logger_quiet))
90
 
 
91
 
        self.assertEqual(len(errors), 1)
92
 
        self.assertIsInstance(errors[0], UploadError)
 
79
        error = self.assertRaises(
 
80
            UploadError, find_changelog, self.tmpdir, mock_logger_quiet)
93
81
        self.assertEqual(
94
 
            errors[0].args[0],
95
 
            "Symbolic link for debian/changelog not allowed")
 
82
            error.args[0], "Symbolic link for debian/changelog not allowed")
96
83
 
97
84
    def testGoodDebianChangelog(self):
98
85
        """Test that a proper changelog file will be accepted"""
101
88
        file.write(changelog)
102
89
        file.close()
103
90
 
104
 
        errors = list(findChangelog(
105
 
            self.dsc_file, self.tmpdir, mock_logger_quiet))
106
 
 
107
 
        self.assertEqual(len(errors), 0)
108
 
        self.assertEqual(self.dsc_file.changelog_path,
109
 
                         self.changelog_path)
 
91
        self.assertEquals(
 
92
            changelog, find_changelog(self.tmpdir, mock_logger_quiet))
110
93
 
111
94
    def testOversizedFile(self):
112
95
        """Test that a file larger than 10MiB will fail.
125
108
        file.write(empty_file)
126
109
        file.close()
127
110
 
128
 
        errors = list(findChangelog(
129
 
            self.dsc_file, self.tmpdir, mock_logger_quiet))
130
 
 
131
 
        self.assertIsInstance(errors[0], UploadError)
 
111
        error = self.assertRaises(
 
112
            UploadError, find_changelog, self.tmpdir, mock_logger_quiet)
132
113
        self.assertEqual(
133
 
            errors[0].args[0],
134
 
            "debian/changelog file too large, 10MiB max")
 
114
            error.args[0], "debian/changelog file too large, 10MiB max")
135
115
 
136
116
 
137
117
class TestDscFileLibrarian(TestCaseWithFactory):
141
121
 
142
122
    def getDscFile(self, name):
143
123
        dsc_path = datadir(os.path.join('suite', name, name + '.dsc'))
 
124
 
144
125
        class Changes:
145
126
            architectures = ['source']
146
127
        logger = QuietFakeLogger()
157
138
        os.chmod(tempdir, 0555)
158
139
        try:
159
140
            dsc_file = self.getDscFile('bar_1.0-1')
160
 
            try:
161
 
                list(dsc_file.verify())
162
 
            finally:
163
 
                dsc_file.cleanUp()
 
141
            list(dsc_file.verify())
164
142
        finally:
165
143
            os.chmod(tempdir, 0755)
166
144
 
292
270
        # A 3.0 (native) source with component tarballs is invalid.
293
271
        self.assertErrorsForFiles(
294
272
            [self.wrong_files_error], {NATIVE_TARBALL: 1}, {'foo': 1})
 
273
 
 
274
 
 
275
class UnpackedDirTests(TestCase):
 
276
    """Tests for unpack_source and cleanup_unpacked_dir."""
 
277
 
 
278
    def test_unpack_source(self):
 
279
        # unpack_source unpacks in a temporary directory and returns the
 
280
        # path.
 
281
        unpacked_dir = unpack_source(
 
282
            datadir(os.path.join('suite', 'bar_1.0-1', 'bar_1.0-1.dsc')))
 
283
        try:
 
284
            self.assertEquals(["bar-1.0"], os.listdir(unpacked_dir))
 
285
            self.assertContentEqual(
 
286
                ["THIS_IS_BAR", "debian"],
 
287
                os.listdir(os.path.join(unpacked_dir, "bar-1.0")))
 
288
        finally:
 
289
            cleanup_unpacked_dir(unpacked_dir)
 
290
 
 
291
    def test_cleanup(self):
 
292
        # cleanup_dir removes the temporary directory and all files under it.
 
293
        temp_dir = self.makeTemporaryDirectory()
 
294
        unpacked_dir = os.path.join(temp_dir, "unpacked")
 
295
        os.mkdir(unpacked_dir)
 
296
        os.mkdir(os.path.join(unpacked_dir, "bar_1.0"))
 
297
        cleanup_unpacked_dir(unpacked_dir)
 
298
        self.assertFalse(os.path.exists(unpacked_dir))
 
299
 
 
300
    def test_cleanup_invalid_mode(self):
 
301
        # cleanup_dir can remove a directory even if the mode does
 
302
        # not allow it.
 
303
        temp_dir = self.makeTemporaryDirectory()
 
304
        unpacked_dir = os.path.join(temp_dir, "unpacked")
 
305
        os.mkdir(unpacked_dir)
 
306
        bar_path = os.path.join(unpacked_dir, "bar_1.0")
 
307
        os.mkdir(bar_path)
 
308
        os.chmod(bar_path, 0600)
 
309
        os.chmod(unpacked_dir, 0600)
 
310
        cleanup_unpacked_dir(unpacked_dir)
 
311
        self.assertFalse(os.path.exists(unpacked_dir))