Basic test suite for ubuntutools.archive

This commit is contained in:
Stefano Rivera 2010-12-31 17:37:24 +02:00
parent b78922b14a
commit 531aaba3a0
4 changed files with 152 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,17 @@
Format: 3.0 (quilt)
Source: example
Binary: example
Architecture: all
Version: 1.0-1
Maintainer: Stefano Rivera <stefanor@ubuntu.com>
Standards-Version: 3.9.1
Build-Depends: debhelper (>= 7.0.50~)
Checksums-Sha1:
13c309f70155ab5be50553ce72f3344c48851013 170 example_1.0.orig.tar.gz
ae9dcd225000d24128b8eccbdc472abcfa9b60ee 1170 example_1.0-1.debian.tar.gz
Checksums-Sha256:
4908cb2ce47c9835fb74476ab5dba0298da7a56a389328b845d07575f5ea6276 170 example_1.0.orig.tar.gz
ca366dc1ae0efa1df6537f6882e87134a0409e1589ca18daf56d065beb9cfce3 1170 example_1.0-1.debian.tar.gz
Files:
8054fdf3653160e13d9539ca36c2e311 170 example_1.0.orig.tar.gz
8538e8b6cb8162d5c6886b18c132a1c8 1170 example_1.0-1.debian.tar.gz

Binary file not shown.

View File

@ -0,0 +1,135 @@
# test_archive.py - Test suite for ubuntutools.archive
#
# Copyright (C) 2010, Stefano Rivera <stefanor@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
from __future__ import with_statement
import __builtin__
import os.path
import shutil
import StringIO
import subprocess
import sys
import tempfile
import urllib2
import urlparse
import mox
from ubuntutools.archive import Dsc, DebianSourcePackage, UbuntuSourcePackage
from ubuntutools.logger import Logger
from ubuntutools.test import unittest
class DscVerificationTestCase(mox.MoxTestBase, unittest.TestCase):
def setUp(self):
super(DscVerificationTestCase, self).setUp()
with open('test-data/example_1.0-1.dsc', 'rb') as f:
self.dsc = Dsc(f.read())
def tearDown(self):
super(DscVerificationTestCase, self).tearDown()
def test_good(self):
self.assertTrue(self.dsc.verify_file(
'test-data/example_1.0.orig.tar.gz'))
self.assertTrue(self.dsc.verify_file(
'test-data/example_1.0-1.debian.tar.gz'))
def test_missing(self):
self.assertFalse(self.dsc.verify_file(
'test-data/does.not.exist'))
def test_bad(self):
fn = 'test-data/example_1.0.orig.tar.gz'
with open(fn, 'rb') as f:
data = f.read()
data = data[:-1] + chr(ord(data[-1]) ^ 8)
self.mox.StubOutWithMock(__builtin__, 'open')
open(fn, 'rb').AndReturn(StringIO.StringIO(data))
self.mox.ReplayAll()
self.assertFalse(self.dsc.verify_file(fn))
def test_sha1(self):
del self.dsc['Checksums-Sha256']
self.test_good()
self.test_bad()
def test_md5(self):
del self.dsc['Checksums-Sha256']
del self.dsc['Checksums-Sha1']
self.test_good()
self.test_bad()
class LocalSourcePackageTestCase(mox.MoxTestBase, unittest.TestCase):
SourcePackage = DebianSourcePackage
def setUp(self):
super(LocalSourcePackageTestCase, self).setUp()
self.workdir = tempfile.mkdtemp(prefix='udt-test')
for funcname in ('ubuntutools.archive.Distribution',
'ubuntutools.archive.rmadison',
'urllib2.urlopen',
):
mod, func = funcname.rsplit('.', 1)
setattr(self, 'o_' + func, getattr(sys.modules[mod], func))
self.mox.StubOutWithMock(sys.modules[mod], func)
self.mox.StubOutWithMock(Logger, 'stdout')
def tearDown(self):
super(LocalSourcePackageTestCase, self).tearDown()
shutil.rmtree(self.workdir)
def test_local_copy(self):
urllib2.urlopen(mox.Regex('^file://.*\.dsc$')
).WithSideEffects(self.o_urlopen)
urllib2.urlopen(mox.Regex('^file://.*\.orig\.tar\.gz$')
).WithSideEffects(self.o_urlopen)
urllib2.urlopen(mox.Regex('^file://.*\.debian\.tar\.gz$')
).WithSideEffects(self.o_urlopen)
Logger.stdout.write(mox.IsA(basestring)).MultipleTimes()
Logger.stdout.flush().MultipleTimes()
self.mox.ReplayAll()
pkg = self.SourcePackage('example', '1.0-1', 'main',
dscfile='test-data/example_1.0-1.dsc',
workdir=self.workdir)
pkg.pull()
pkg.unpack()
def test_verification(self):
shutil.copy2('test-data/example_1.0-1.dsc', self.workdir)
shutil.copy2('test-data/example_1.0.orig.tar.gz', self.workdir)
shutil.copy2('test-data/example_1.0-1.debian.tar.gz', self.workdir)
with open(os.path.join(self.workdir, 'example_1.0-1.debian.tar.gz'),
'r+b') as f:
f.write('CORRUPTION')
urllib2.urlopen(mox.Regex('^file://.*\.dsc$')
).WithSideEffects(self.o_urlopen)
urllib2.urlopen(mox.Regex('^file://.*\.debian\.tar\.gz$')
).WithSideEffects(self.o_urlopen)
Logger.stdout.write(mox.IsA(basestring)).MultipleTimes()
Logger.stdout.flush().MultipleTimes()
self.mox.ReplayAll()
pkg = self.SourcePackage('example', '1.0-1', 'main',
dscfile='test-data/example_1.0-1.dsc',
workdir=self.workdir)
pkg.pull()
pkg.unpack()
class UbuntuLocalSourcePackageTestCase(LocalSourcePackageTestCase):
SourcePackage = UbuntuSourcePackage