view test/test_packager.py @ 97:d85efd3276bd

revert previous change; it was committed accidentally
author Bernhard Herzog <bh@intevation.de>
date Mon, 26 Nov 2007 14:45:19 +0000
parents efbd66b13f51
children f7b9c7113c46
line wrap: on
line source
# Copyright (C) 2007 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

"""Tests for treepkg.packager"""

import sys
import os
import unittest

from treepkg.run import call
from treepkg.cmdexpand import cmdexpand
from treepkg.util import writefile
from treepkg.packager import PackagerGroup
import treepkg.subversion as subversion
import treepkg

from filesupport import FileTestMixin


def create_svn_repository(directory):
    baseurl = "file://" + directory
    call(cmdexpand("svnadmin create --fs-type fsfs $directory",
                   **locals()))
    call(cmdexpand("svn mkdir -q -m 'create directory structure'"
                   " $baseurl/trunk",
                   **locals()))
    return baseurl

def add_svn_files(workingcopy, filedesc, commitmsg):
    for name, contents in filedesc:
        writefile(os.path.join(workingcopy, name), contents)
        call(cmdexpand("svn add -q $name", **locals()),
             cwd=workingcopy)
    call(cmdexpand("svn commit -q -m $commitmsg", **locals()),
         cwd=workingcopy)


class SourcePackager(treepkg.packager.SourcePackager):

    pkg_basename = "testpkg"

    def do_package(self):
        pkgbaseversion, pkgbasedir = self.export_sources()

        pkgbasename = self.pkg_basename + "_" + pkgbaseversion
        origtargz = os.path.join(self.work_dir,
                                 pkgbasename + ".orig.tar.gz")
        self.create_tarball(origtargz, self.work_dir,
                            os.path.basename(pkgbasedir))

        changemsg = ("Update to SVN rev. %d" % (self.revision,))
        self.copy_debian_directory(pkgbasedir, pkgbaseversion,
                                   changemsg)

        self.create_source_package(pkgbasedir, origtargz)
        self.move_source_package(pkgbasename)


class RevisionPackager(treepkg.packager.RevisionPackager):

    source_packager_cls = SourcePackager


class PackageTrack(treepkg.packager.PackageTrack):

    revision_packager_cls = RevisionPackager


class PackagerTest(unittest.TestCase, FileTestMixin):

    revisions = []

    debian_files = [
        ("debian",
         [("control", """\
Source: testpkg
Priority: optional
Maintainer: TreePKG <treepkg@example.com>
Standards-Version: 3.7.2

Package: testpkg
Architecture: all
Description: Test package for treepkg testsGerman (de) internationalized (i18n) files for KDE
 This package contains the German internationalized (i18n) files for
 all KDE core applications.
"""),
          ("changelog", """\
testpkg (0-0) unstable; urgency=low

  * Initial version

 -- TreePKG <treepkg@example.com>  Thu,  8 Mar 2007 18:34:39 +0100
"""),
          ("rules", "binary: echo binary")])]


    def setUp(self):
        self.svndir = self.create_temp_dir(self.id() + "-svn")
        self.svnworkdir = self.create_temp_dir(self.id() + "-svnwork")
        self.trackdir = self.create_temp_dir(self.id() + "-track")
        self.create_files(self.trackdir, self.debian_files)
        self.svn_url = create_svn_repository(self.svndir) + "/trunk"
        subversion.checkout(self.svn_url, self.svnworkdir)
        for message, files in self.revisions:
            add_svn_files(self.svnworkdir, files, message)

    def runtest(self, debrevision, **extra_track_args):
        rootcmd = os.path.join(os.path.dirname(__file__), os.pardir, "test",
                               "mocksudopbuilder.py")
        track = PackageTrack(name="testpkg", base_dir=self.trackdir,
                             svn_url=self.svn_url, pbuilderrc="",
                             root_cmd=[sys.executable, rootcmd],
                             deb_email="treepkg@example.com",
                             deb_fullname="treepkg tester",
                             **extra_track_args)
        group = PackagerGroup([track], 1)
        group.check_package_tracks()

        # determine version that has been packaged.  This assumes that
        # check_package_tracks will leave the checkout in the revision
        # that was actually packaged.
        version = subversion.last_changed_revision(self.svn_url)

        # Now check the source and binary package files
        pkgdir = os.path.join(self.trackdir, "pkg", "%d-1" % version)
        self.assertEquals(sorted(os.listdir(os.path.join(pkgdir, "src"))),
                          [name % locals() for name in
                           ["testpkg_%(version)d-%(debrevision)s.diff.gz",
                            "testpkg_%(version)d-%(debrevision)s.dsc",
                            "testpkg_%(version)d.orig.tar.gz"]])
        self.assertEquals(sorted(os.listdir(os.path.join(pkgdir, "binary"))),
                          [name % locals() for name in
                           ["testpkg_%(version)d-%(debrevision)s_all.deb",
                           "testpkg_%(version)d-%(debrevision)s_i386.changes"]])


class TestPackager(PackagerTest):

    revisions = [
        ("Initial Revision",
         [("README", "and miles to go before I sleep")]),
        ]

    def test_default_debrevision_prefix(self):
        self.runtest("treepkg1")

    def test_custom_debrevision_prefix(self):
        self.runtest("kk1", debrevision_prefix="kk")

class StoppingPackager(treepkg.packager.RevisionPackager):

    def package(self):
        pass

class StoppingTrack(treepkg.packager.PackageTrack):

    def __init__(self, do_package, do_stop, instructions_file, name, trackdir):
        super(StoppingTrack, self).__init__(name, trackdir, "", "", "",
                                            "", "")
        self.do_package = do_package
        self.do_stop = do_stop
        self.instructions_file = instructions_file

    def package_if_updated(self, revision):
        if self.do_stop:
            writefile(self.instructions_file, "stop")
        if self.do_package:
            return StoppingPackager(self, 1)
        else:
            return None


class TestPackageGroupStop(unittest.TestCase, FileTestMixin):

    def setUp(self):
        self.trackdir = self.create_temp_dir(self.id() + "-track")
        self.instructions_file = os.path.join(self.trackdir, "instructions")

    def group(self, do_package=True, do_stop=True):
        return PackagerGroup([StoppingTrack(do_package, do_stop,
                                            self.instructions_file,
                                            "test", self.trackdir)],
                             1, instructions_file=self.instructions_file)

    def test_stop(self):
        group = self.group(do_package=True, do_stop=True)
        self.failUnless(group.check_package_tracks())

    def test_no_stop(self):
        group = self.group(do_package=True, do_stop=False)
        self.failIf(group.check_package_tracks())

    def test_instruction_removal(self):
        # run once with stopping
        group = self.group(do_package=True, do_stop=True)
        self.failUnless(group.check_package_tracks())

        # run again without stopping but using the same files.  The
        # instructions file should be removed automatically
        group = self.group(do_package=True, do_stop=False)
        self.failIf(group.check_package_tracks())

    def test_stopping_without_packaging(self):
        group = self.group(do_package=False, do_stop=True)
        self.failUnless(group.check_package_tracks())

    def test_stopping_between_checks(self):
        group = self.group(do_package=False, do_stop=False)
        # run check_package_tracks once
        self.failIf(group.check_package_tracks())

        # tell treepkg to stop
        writefile(self.instructions_file, "stop")

        # check again.  The check_package_tracks() may remove the
        # instructions file but it must do so only the first time
        self.failUnless(group.check_package_tracks())


class TestPackageTrack(unittest.TestCase, FileTestMixin):

    def setUp(self):
        self.trackdir = self.create_temp_dir(self.id() + "-track")

    def test_get_revision_numbers(self):
        # Note: The revisions in the pkg dir are not ordered so that we
        # can check whether get_revision_numbers returns a sorted list
        # of revisions
        self.create_files(self.trackdir,
                          [("pkg",
                            [("704195-1",
                              [("status", ""),
                               ("src", []),
                               ("binary", [])]),
                             ("702432-1",
                              [("status", ""),
                               ("src", []),
                               ("binary", [])])])])
        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
        self.assertEquals(track.get_revision_numbers(), [702432, 704195])

    def test_last_packaged_revision_with_no_revisions(self):
        self.create_files(self.trackdir, [("pkg", [])])
        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
        self.assertEquals(track.last_packaged_revision(), -1)

    def test_last_packaged_revision_several_revisions(self):
        self.create_files(self.trackdir,
                          [("pkg",
                            [("704195-1",
                              [("status", ""),
                               ("src", []),
                               ("binary", [])]),
                             ("702432-1",
                              [("status", ""),
                               ("src", []),
                               ("binary", [])])])])
        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
        self.assertEquals(track.last_packaged_revision(), 704195)


class TestRevisionPackager(unittest.TestCase, FileTestMixin):

    def setUp(self):
        self.trackdir = self.create_temp_dir(self.id() + "-track")

    def test_list_source_files(self):
        # Note: The revisions in the pkg dir are not ordered so that we
        # can check whether get_revision_numbers returns a sorted list
        # of revisions
        self.create_files(self.trackdir,
                          [("pkg",
                            [("704195-1",
                              [("status",
                                ("TreePackagerStatus 0.0\n"
                                 "status: binary_package_created\n"
                                 "start: 2007-09-10 17:16:48\n"
                                 "stop: 2007-09-11 00:07:36\n")),
                               ("src", [("test_1.0.orig.tar.gz", ""),
                                        ("test_1.0-1.diff.gz", ""),
                                        ("test_1.0-1.dsc", "")]),
                               ("binary", [])]),
                             ("702432-1",
                              [("status", ""),
                               ("src", []),
                               ("binary", [])])])])
        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
        revpkg = RevisionPackager(track, 704195)
        srcdir = os.path.join(self.trackdir, "pkg", "704195-1", "src")
        self.assertEquals(revpkg.list_source_files(),
                          [os.path.join(srcdir, filename)
                           for filename in ["test_1.0-1.diff.gz",
                                            "test_1.0-1.dsc",
                                            "test_1.0.orig.tar.gz"]])

    def test_list_binary_files(self):
        # Note: The revisions in the pkg dir are not ordered so that we
        # can check whether get_revision_numbers returns a sorted list
        # of revisions
        self.create_files(self.trackdir,
                          [("pkg",
                            [("704195-1",
                              [("status",
                                ("TreePackagerStatus 0.0\n"
                                 "status: binary_package_created\n"
                                 "start: 2007-09-10 17:16:48\n"
                                 "stop: 2007-09-11 00:07:36\n")),
                               ("src", []),
                               ("binary", [("test_1.0-1_i386.deb", ""),
                                           ("test_1.0-1_i386.changes", "")])]),
                             ("702432-1",
                              [("status", ""),
                               ("src", []),
                               ("binary", [])])])])
        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
        revpkg = RevisionPackager(track, 704195)
        srcdir = os.path.join(self.trackdir, "pkg", "704195-1", "binary")
        self.assertEquals(revpkg.list_binary_files(),
                          [os.path.join(srcdir, filename)
                           for filename in ["test_1.0-1_i386.changes",
                                            "test_1.0-1_i386.deb"]])
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)