10
10
from canonical.launchpad.scripts.logger import QuietFakeLogger
11
11
from canonical.testing.layers import LaunchpadZopelessLayer
12
12
from lp.archiveuploader.dscfile import (
16
17
format_to_file_checker_map,
18
20
from lp.archiveuploader.nascentuploadfile import UploadError
19
21
from lp.archiveuploader.tests import (
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()
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))
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)
66
"Symbolic link for debian/copyright not allowed")
60
error.args[0], "Symbolic link for debian/copyright not allowed")
68
62
def testGoodDebianCopyright(self):
69
63
"""Test that a proper copyright file will be accepted"""
72
66
file.write(copyright)
75
errors = list(findCopyright(
76
self.dsc_file, self.tmpdir, mock_logger_quiet))
78
self.assertEqual(len(errors), 0)
79
self.assertEqual(self.dsc_file.copyright, copyright)
70
copyright, find_copyright(self.tmpdir, mock_logger_quiet))
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))
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)
95
"Symbolic link for debian/changelog not allowed")
82
error.args[0], "Symbolic link for debian/changelog not allowed")
97
84
def testGoodDebianChangelog(self):
98
85
"""Test that a proper changelog file will be accepted"""
101
88
file.write(changelog)
104
errors = list(findChangelog(
105
self.dsc_file, self.tmpdir, mock_logger_quiet))
107
self.assertEqual(len(errors), 0)
108
self.assertEqual(self.dsc_file.changelog_path,
92
changelog, find_changelog(self.tmpdir, mock_logger_quiet))
111
94
def testOversizedFile(self):
112
95
"""Test that a file larger than 10MiB will fail.
125
108
file.write(empty_file)
128
errors = list(findChangelog(
129
self.dsc_file, self.tmpdir, mock_logger_quiet))
131
self.assertIsInstance(errors[0], UploadError)
111
error = self.assertRaises(
112
UploadError, find_changelog, self.tmpdir, mock_logger_quiet)
132
113
self.assertEqual(
134
"debian/changelog file too large, 10MiB max")
114
error.args[0], "debian/changelog file too large, 10MiB max")
137
117
class TestDscFileLibrarian(TestCaseWithFactory):
142
122
def getDscFile(self, name):
143
123
dsc_path = datadir(os.path.join('suite', name, name + '.dsc'))
145
126
architectures = ['source']
146
127
logger = QuietFakeLogger()
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})
275
class UnpackedDirTests(TestCase):
276
"""Tests for unpack_source and cleanup_unpacked_dir."""
278
def test_unpack_source(self):
279
# unpack_source unpacks in a temporary directory and returns the
281
unpacked_dir = unpack_source(
282
datadir(os.path.join('suite', 'bar_1.0-1', 'bar_1.0-1.dsc')))
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")))
289
cleanup_unpacked_dir(unpacked_dir)
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))
300
def test_cleanup_invalid_mode(self):
301
# cleanup_dir can remove a directory even if the mode does
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")
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))