test: fix archive tests

Assumptions were made about the implementation by mocking
that are no longer true, and the tests generally need to be
fixed to be more robust about testing
This commit is contained in:
Dan Streetman 2021-07-13 15:57:10 -04:00
parent 0eaf71737d
commit b4ca04efaa

View File

@ -15,51 +15,40 @@
# PERFORMANCE OF THIS SOFTWARE. # PERFORMANCE OF THIS SOFTWARE.
import os.path import filecmp
import shutil
import tempfile import tempfile
import unittest import unittest
from io import BytesIO
from unittest import mock
import ubuntutools.archive import ubuntutools.archive
from pathlib import Path
from ubuntutools.test.example_package import ExamplePackage from ubuntutools.test.example_package import ExamplePackage
def setUpModule(): class BaseVerificationTestCase(unittest.TestCase):
if not os.path.exists('test-data/example-0.1-1.dsc'):
ex_pkg = ExamplePackage()
ex_pkg.create_orig()
ex_pkg.create()
ex_pkg.cleanup()
class DscVerificationTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
with open('test-data/example_1.0-1.dsc', 'rb') as f: d = tempfile.TemporaryDirectory()
self.dsc = ubuntutools.archive.Dsc(f.read()) self.addCleanup(d.cleanup)
self.pkg = ExamplePackage(destdir=Path(d.name))
self.pkg.create()
self.dsc = ubuntutools.archive.Dsc(self.pkg.dsc.read_bytes())
class DscVerificationTestCase(BaseVerificationTestCase):
def test_good(self): def test_good(self):
self.assertTrue(self.dsc.verify_file( self.assertTrue(self.dsc.verify_file(self.pkg.orig))
'test-data/example_1.0.orig.tar.gz')) self.assertTrue(self.dsc.verify_file(self.pkg.debian))
self.assertTrue(self.dsc.verify_file(
'test-data/example_1.0-1.debian.tar.xz'))
def test_missing(self): def test_missing(self):
self.assertFalse(self.dsc.verify_file( self.assertFalse(self.dsc.verify_file(self.pkg.destdir / 'does.not.exist'))
'test-data/does.not.exist'))
def test_bad(self): def test_bad(self):
fn = 'test-data/example_1.0.orig.tar.gz' data = self.pkg.orig.read_bytes()
with open(fn, 'rb') as f:
data = f.read()
last_byte = chr(data[-1] ^ 8).encode() last_byte = chr(data[-1] ^ 8).encode()
data = data[:-1] + last_byte data = data[:-1] + last_byte
m = mock.MagicMock(name='open', spec=open) self.pkg.orig.write_bytes(data)
m.return_value = BytesIO(data) self.assertFalse(self.dsc.verify_file(self.pkg.orig))
with mock.patch('builtins.open', m):
self.assertFalse(self.dsc.verify_file(fn))
def test_sha1(self): def test_sha1(self):
del self.dsc['Checksums-Sha256'] del self.dsc['Checksums-Sha256']
@ -73,63 +62,64 @@ class DscVerificationTestCase(unittest.TestCase):
self.test_bad() self.test_bad()
class LocalSourcePackageTestCase(unittest.TestCase): class LocalSourcePackageTestCase(BaseVerificationTestCase):
SourcePackage = ubuntutools.archive.UbuntuSourcePackage SourcePackage = ubuntutools.archive.UbuntuSourcePackage
def setUp(self): def setUp(self):
self.workdir = tempfile.mkdtemp(prefix='udt-test') super().setUp()
d = tempfile.TemporaryDirectory()
self.addCleanup(d.cleanup)
self.workdir = Path(d.name)
def tearDown(self): def pull(self, **kwargs):
shutil.rmtree(self.workdir) ''' Do the pull from pkg dir to the workdir, return the SourcePackage '''
srcpkg = self.SourcePackage(dscfile=self.pkg.dsc, workdir=self.workdir, **kwargs)
srcpkg.pull()
return srcpkg
def test_local_copy(self): def test_pull(self, **kwargs):
pkg = self.SourcePackage(package='example', srcpkg = self.pull(**kwargs)
version='1.0-1', self.assertTrue(filecmp.cmp(self.pkg.dsc, self.workdir / self.pkg.dsc.name))
component='main', self.assertTrue(filecmp.cmp(self.pkg.orig, self.workdir / self.pkg.orig.name))
dscfile='test-data/example_1.0-1.dsc', self.assertTrue(filecmp.cmp(self.pkg.debian, self.workdir / self.pkg.debian.name))
workdir=self.workdir, return srcpkg
verify_signature=False)
pkg.pull()
pkg.unpack()
def test_workdir_srcpkg_noinfo(self): def test_unpack(self, **kwargs):
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir) srcpkg = kwargs.get('srcpkg', self.pull(**kwargs))
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir) srcpkg.unpack()
shutil.copy2('test-data/example_1.0-1.debian.tar.xz', self.workdir) content = self.workdir / self.pkg.dirname / self.pkg.content_filename
self.assertEqual(self.pkg.content_text, content.read_text())
debian = self.workdir / self.pkg.dirname / 'debian'
self.assertTrue(debian.exists())
self.assertTrue(debian.is_dir())
pkg = self.SourcePackage(dscfile=os.path.join(self.workdir, def test_pull_and_unpack(self, **kwargs):
'example_1.0-1.dsc'), self.test_unpack(srcpkg=self.test_pull(**kwargs))
workdir=self.workdir,
verify_signature=False)
pkg.pull()
pkg.unpack()
def test_workdir_srcpkg_info(self): def test_with_package(self):
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir) self.test_pull_and_unpack(package=self.pkg.source)
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
shutil.copy2('test-data/example_1.0-1.debian.tar.xz', self.workdir)
pkg = self.SourcePackage(package='example', version='1.0-1', def test_with_package_version(self):
component='main', self.test_pull_and_unpack(package=self.pkg.source, version=self.pkg.version)
dscfile=os.path.join(self.workdir,
'example_1.0-1.dsc'), def test_with_package_version_component(self):
workdir=self.workdir, self.test_pull_and_unpack(package=self.pkg.source,
verify_signature=False) version=self.pkg.version,
pkg.pull() componet='main')
pkg.unpack()
def test_verification(self): def test_verification(self):
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir) corruption = b'CORRUPTION'
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
shutil.copy2('test-data/example_1.0-1.debian.tar.xz', self.workdir)
with open(os.path.join(self.workdir, 'example_1.0-1.debian.tar.xz'),
'r+b') as f:
f.write(b'CORRUPTION')
pkg = self.SourcePackage(package='example', self.pull()
version='1.0-1',
component='main', testfile = self.workdir / self.pkg.debian.name
dscfile='test-data/example_1.0-1.dsc', self.assertTrue(testfile.exists())
workdir=self.workdir, self.assertTrue(testfile.is_file())
verify_signature=False) self.assertNotEqual(testfile.read_bytes(), corruption)
pkg.pull() testfile.write_bytes(corruption)
self.assertEqual(testfile.read_bytes(), corruption)
self.test_pull()
self.assertTrue(testfile.exists())
self.assertTrue(testfile.is_file())
self.assertNotEqual(testfile.read_bytes(), corruption)