bh@84: # Copyright (C) 2007 by Intevation GmbH bh@84: # Authors: bh@84: # Bernhard Herzog bh@84: # bh@84: # This program is free software under the GPL (>=v2) bh@84: # Read the file COPYING coming with the software for details. bh@84: bh@84: """Tests for treepkg.packager""" bh@84: bh@84: import sys bh@84: import os bh@84: import unittest bh@84: bh@84: from treepkg.run import call bh@84: from treepkg.cmdexpand import cmdexpand bh@84: from treepkg.util import writefile bh@84: from treepkg.packager import PackagerGroup bh@84: import treepkg.subversion as subversion bh@84: import treepkg bh@84: bh@84: from filesupport import FileTestMixin bh@84: bh@84: bh@84: def create_svn_repository(directory): bh@84: baseurl = "file://" + directory bh@84: call(cmdexpand("svnadmin create --fs-type fsfs $directory", bh@84: **locals())) bh@84: call(cmdexpand("svn mkdir -q -m 'create directory structure'" bh@84: " $baseurl/trunk", bh@84: **locals())) bh@84: return baseurl bh@84: bh@84: def add_svn_files(workingcopy, filedesc, commitmsg): bh@84: for name, contents in filedesc: bh@84: writefile(os.path.join(workingcopy, name), contents) bh@84: call(cmdexpand("svn add -q $name", **locals()), bh@84: cwd=workingcopy) bh@84: call(cmdexpand("svn commit -q -m $commitmsg", **locals()), bh@84: cwd=workingcopy) bh@84: bh@84: bh@84: class SourcePackager(treepkg.packager.SourcePackager): bh@84: bh@84: pkg_basename = "testpkg" bh@84: bh@84: def do_package(self): bh@84: pkgbaseversion, pkgbasedir = self.export_sources() bh@84: bh@84: pkgbasename = self.pkg_basename + "_" + pkgbaseversion bh@84: origtargz = os.path.join(self.work_dir, bh@84: pkgbasename + ".orig.tar.gz") bh@84: self.create_tarball(origtargz, self.work_dir, bh@84: os.path.basename(pkgbasedir)) bh@84: bh@84: changemsg = ("Update to SVN rev. %d" % (self.revision,)) bh@84: self.copy_debian_directory(pkgbasedir, pkgbaseversion, bh@84: changemsg) bh@84: bh@84: self.create_source_package(pkgbasedir, origtargz) bh@84: self.move_source_package(pkgbasename) bh@84: bh@84: bh@84: class RevisionPackager(treepkg.packager.RevisionPackager): bh@84: bh@84: source_packager_cls = SourcePackager bh@84: bh@84: bh@84: class PackageTrack(treepkg.packager.PackageTrack): bh@84: bh@84: revision_packager_cls = RevisionPackager bh@84: bh@84: bh@84: class TestPackager(unittest.TestCase, FileTestMixin): bh@84: bh@84: initial_files = [ bh@84: ("README", "and miles to go before I sleep"), bh@84: ] bh@84: bh@84: debian_files = [ bh@84: ("debian", bh@84: [("control", """\ bh@84: Source: testpkg bh@84: Priority: optional bh@84: Maintainer: TreePKG bh@84: Standards-Version: 3.7.2 bh@84: bh@84: Package: testpkg bh@84: Architecture: all bh@84: Description: Test package for treepkg testsGerman (de) internationalized (i18n) files for KDE bh@84: This package contains the German internationalized (i18n) files for bh@84: all KDE core applications. bh@84: """), bh@84: ("changelog", """\ bh@84: testpkg (0-0) unstable; urgency=low bh@84: bh@84: * Initial version bh@84: bh@84: -- TreePKG Thu, 8 Mar 2007 18:34:39 +0100 bh@84: """), bh@84: ("rules", "binary: echo binary")])] bh@84: bh@84: def setUp(self): bh@84: self.svndir = self.create_temp_dir(self.id() + "-svn") bh@84: self.svnworkdir = self.create_temp_dir(self.id() + "-svnwork") bh@84: self.trackdir = self.create_temp_dir(self.id() + "-track") bh@84: self.create_files(self.trackdir, self.debian_files) bh@84: bh@84: def test(self): bh@84: baseurl = create_svn_repository(self.svndir) bh@84: svn_url = baseurl + "/trunk" bh@84: subversion.checkout(svn_url, self.svnworkdir) bh@84: add_svn_files(self.svnworkdir, self.initial_files, bh@84: "Initial version") bh@84: rootcmd = os.path.join(os.path.dirname(__file__), os.pardir, "test", bh@84: "mocksudopbuilder.py") bh@84: track = PackageTrack(name="testpkg", base_dir=self.trackdir, bh@84: svn_url=svn_url, pbuilderrc="", bh@84: root_cmd=[sys.executable, rootcmd], bh@84: deb_email="treepkg@example.com", bh@84: deb_fullname="treepkg tester") bh@84: group = PackagerGroup([track], 1) bh@84: group.check_package_tracks() bh@84: bh@84: # Now check the source and binary package files bh@84: pkgdir = os.path.join(self.trackdir, "pkg", "2-1") bh@84: self.assertEquals(sorted(os.listdir(os.path.join(pkgdir, "src"))), bh@84: ["testpkg_2-kk1.diff.gz", bh@84: "testpkg_2-kk1.dsc", bh@84: "testpkg_2.orig.tar.gz"]) bh@84: self.assertEquals(sorted(os.listdir(os.path.join(pkgdir, "binary"))), bh@84: ["testpkg_2-kk1_all.deb", bh@84: "testpkg_2-kk1_i386.changes"]) bh@85: bh@91: class StoppingPackager(treepkg.packager.RevisionPackager): bh@91: bh@91: def package(self): bh@91: pass bh@91: bh@91: class StoppingTrack(treepkg.packager.PackageTrack): bh@91: bh@91: def __init__(self, do_package, do_stop, instructions_file, name, trackdir): bh@91: super(StoppingTrack, self).__init__(name, trackdir, "", "", "", bh@91: "", "") bh@91: self.do_package = do_package bh@91: self.do_stop = do_stop bh@91: self.instructions_file = instructions_file bh@91: bh@91: def package_if_updated(self, revision): bh@91: if self.do_stop: bh@91: writefile(self.instructions_file, "stop") bh@91: if self.do_package: bh@91: return StoppingPackager(self, 1) bh@91: else: bh@91: return None bh@91: bh@91: bh@91: class TestPackageGroupStop(unittest.TestCase, FileTestMixin): bh@91: bh@91: def setUp(self): bh@91: self.trackdir = self.create_temp_dir(self.id() + "-track") bh@91: self.instructions_file = os.path.join(self.trackdir, "instructions") bh@91: bh@91: def group(self, do_package=True, do_stop=True): bh@91: return PackagerGroup([StoppingTrack(do_package, do_stop, bh@91: self.instructions_file, bh@91: "test", self.trackdir)], bh@91: 1, instructions_file=self.instructions_file) bh@91: bh@91: def test_stop(self): bh@91: group = self.group(do_package=True, do_stop=True) bh@91: self.failUnless(group.check_package_tracks()) bh@91: bh@91: def test_no_stop(self): bh@91: group = self.group(do_package=True, do_stop=False) bh@91: self.failIf(group.check_package_tracks()) bh@91: bh@91: def test_instruction_removal(self): bh@91: # run once with stopping bh@91: group = self.group(do_package=True, do_stop=True) bh@91: self.failUnless(group.check_package_tracks()) bh@91: bh@91: # run again without stopping but using the same files. The bh@91: # instructions file should be removed automatically bh@91: group = self.group(do_package=True, do_stop=False) bh@91: self.failIf(group.check_package_tracks()) bh@91: bh@91: def test_stopping_without_packaging(self): bh@91: group = self.group(do_package=False, do_stop=True) bh@91: self.failUnless(group.check_package_tracks()) bh@91: bh@91: def test_stopping_between_checks(self): bh@91: group = self.group(do_package=False, do_stop=False) bh@91: # run check_package_tracks once bh@91: self.failIf(group.check_package_tracks()) bh@91: bh@91: # tell treepkg to stop bh@91: writefile(self.instructions_file, "stop") bh@91: bh@91: # check again. The check_package_tracks() may remove the bh@91: # instructions file but it must do so only the first time bh@91: self.failUnless(group.check_package_tracks()) bh@91: bh@85: bh@85: class TestPackageTrack(unittest.TestCase, FileTestMixin): bh@85: bh@85: def setUp(self): bh@85: self.trackdir = self.create_temp_dir(self.id() + "-track") bh@85: bh@85: def test_get_revision_numbers(self): bh@86: # Note: The revisions in the pkg dir are not ordered so that we bh@86: # can check whether get_revision_numbers returns a sorted list bh@86: # of revisions bh@86: self.create_files(self.trackdir, bh@86: [("pkg", bh@86: [("704195-1", bh@86: [("status", ""), bh@86: ("src", []), bh@86: ("binary", [])]), bh@86: ("702432-1", bh@86: [("status", ""), bh@86: ("src", []), bh@86: ("binary", [])])])]) bh@85: track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "") bh@85: self.assertEquals(track.get_revision_numbers(), [702432, 704195]) bh@87: bh@87: def test_last_packaged_revision_with_no_revisions(self): bh@87: self.create_files(self.trackdir, [("pkg", [])]) bh@87: track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "") bh@87: self.assertEquals(track.last_packaged_revision(), -1) bh@87: bh@87: def test_last_packaged_revision_several_revisions(self): bh@87: self.create_files(self.trackdir, bh@87: [("pkg", bh@87: [("704195-1", bh@87: [("status", ""), bh@87: ("src", []), bh@87: ("binary", [])]), bh@87: ("702432-1", bh@87: [("status", ""), bh@87: ("src", []), bh@87: ("binary", [])])])]) bh@87: track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "") bh@87: self.assertEquals(track.last_packaged_revision(), 704195) bh@88: bh@88: bh@88: class TestRevisionPackager(unittest.TestCase, FileTestMixin): bh@88: bh@88: def setUp(self): bh@88: self.trackdir = self.create_temp_dir(self.id() + "-track") bh@88: bh@88: def test_list_source_files(self): bh@88: # Note: The revisions in the pkg dir are not ordered so that we bh@88: # can check whether get_revision_numbers returns a sorted list bh@88: # of revisions bh@88: self.create_files(self.trackdir, bh@88: [("pkg", bh@88: [("704195-1", bh@88: [("status", bh@88: ("TreePackagerStatus 0.0\n" bh@88: "status: binary_package_created\n" bh@88: "start: 2007-09-10 17:16:48\n" bh@88: "stop: 2007-09-11 00:07:36\n")), bh@88: ("src", [("test_1.0.orig.tar.gz", ""), bh@88: ("test_1.0-1.diff.gz", ""), bh@88: ("test_1.0-1.dsc", "")]), bh@88: ("binary", [])]), bh@88: ("702432-1", bh@88: [("status", ""), bh@88: ("src", []), bh@88: ("binary", [])])])]) bh@88: track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "") bh@88: revpkg = RevisionPackager(track, 704195) bh@88: srcdir = os.path.join(self.trackdir, "pkg", "704195-1", "src") bh@88: self.assertEquals(revpkg.list_source_files(), bh@88: [os.path.join(srcdir, filename) bh@88: for filename in ["test_1.0-1.diff.gz", bh@88: "test_1.0-1.dsc", bh@88: "test_1.0.orig.tar.gz"]]) bh@88: bh@88: def test_list_binary_files(self): bh@88: # Note: The revisions in the pkg dir are not ordered so that we bh@88: # can check whether get_revision_numbers returns a sorted list bh@88: # of revisions bh@88: self.create_files(self.trackdir, bh@88: [("pkg", bh@88: [("704195-1", bh@88: [("status", bh@88: ("TreePackagerStatus 0.0\n" bh@88: "status: binary_package_created\n" bh@88: "start: 2007-09-10 17:16:48\n" bh@88: "stop: 2007-09-11 00:07:36\n")), bh@88: ("src", []), bh@88: ("binary", [("test_1.0-1_i386.deb", ""), bh@88: ("test_1.0-1_i386.changes", "")])]), bh@88: ("702432-1", bh@88: [("status", ""), bh@88: ("src", []), bh@88: ("binary", [])])])]) bh@88: track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "") bh@88: revpkg = RevisionPackager(track, 704195) bh@88: srcdir = os.path.join(self.trackdir, "pkg", "704195-1", "binary") bh@88: self.assertEquals(revpkg.list_binary_files(), bh@88: [os.path.join(srcdir, filename) bh@88: for filename in ["test_1.0-1_i386.changes", bh@88: "test_1.0-1_i386.deb"]])