view test/test_builder.py @ 126:68d829cac3ff

New parameter for PBuilder.build: extra_env, a mapping with extra environment varaibles. Also add a test case.
author Bernhard Herzog <bh@intevation.de>
date Thu, 22 May 2008 18:53:14 +0000
parents 6e34fc4ebe39
children bfcb2bbf9a52
line wrap: on
line source
# Copyright (C) 2007, 2008 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.builder"""

import sys
import os
import unittest

from treepkg.builder import PBuilder
from treepkg.run import call

from filesupport import FileTestMixin

# helper program to dump the command line arguments into a file so that
# test cases can check them.  Also if the environment variable
# TREEPKG_TEST is set, create the file named by it.
dump_command_line_py = """
import sys, os
open(sys.argv[1], 'w').write(repr(sys.argv[2:]))
value = os.environ.get('TREEPKG_TEST')
if value:
    open(value, 'w').close()
"""

class PBuilderTests(unittest.TestCase, FileTestMixin):

    def setUp(self):
        self.dump_command_line = self.create_temp_file("dump_command_line.py",
                                                       dump_command_line_py)
        self.command_line_file = self.temp_file_name("command_line")
        self.root_command = [sys.executable, self.dump_command_line,
                             self.command_line_file]

    def check_command_line(self, args):
        self.checkFileContents(self.command_line_file, repr(args))


class TestPBuilder(PBuilderTests):

    def test_build(self):
        """Tests the PBuilder.build method.
        The test checks whether the build method creates the binary_dir
        and then checks the arguments with which the root command is
        called.  The test is a little too strict because it expects a
        specific order of the arguments even though the order of some of
        the arguments doesn't matter.

        A more thorough test of the build method is implicity done by
        the packager tests.
        """
        binary_dir_name = self.temp_file_name("binary")
        if os.path.exists(binary_dir_name):
            os.rmdir(binary_dir_name)
        # sanity check: the binary directory must not exist yet.
        self.failIf(os.path.exists(binary_dir_name))

        builder = PBuilder("my_pbuilderrc", self.root_command)
        builder.build("my_dsc_file", binary_dir_name, "the_logfile")
        self.check_command_line(['/usr/sbin/pbuilder', 'build',
                                 '--configfile', 'my_pbuilderrc',
                                 '--logfile', 'the_logfile',
                                 '--buildresult', binary_dir_name,
                                 'my_dsc_file'])
        self.failUnless(os.path.isdir(binary_dir_name))

    def test_build_with_bindmounts(self):
        """Tests the PBuilder.build method with the bindmounts parameter"""
        binary_dir_name = self.temp_file_name("binary")
        if os.path.exists(binary_dir_name):
            os.rmdir(binary_dir_name)
        # sanity check: the binary directory must not exist yet.
        self.failIf(os.path.exists(binary_dir_name))

        builder = PBuilder("my_pbuilderrc", self.root_command)
        builder.build("my_dsc_file", binary_dir_name, "the_logfile",
                      bindmounts=["/home/builder/tracks",
                                  "/home/builder/pbuilder"])
        self.check_command_line(['/usr/sbin/pbuilder', 'build',
                                 '--configfile', 'my_pbuilderrc',
                                 '--bindmounts', "/home/builder/tracks",
                                 '--bindmounts', "/home/builder/pbuilder",
                                 '--logfile', 'the_logfile',
                                 '--buildresult', binary_dir_name,
                                 'my_dsc_file'])
        self.failUnless(os.path.isdir(binary_dir_name))

    def test_build_with_extra_packages(self):
        """Tests the PBuilder.build method with the extra_packages parameter"""
        binary_dir_name = self.temp_file_name("binary")
        if os.path.exists(binary_dir_name):
            os.rmdir(binary_dir_name)
        # sanity check: the binary directory must not exist yet.
        self.failIf(os.path.exists(binary_dir_name))

        builder = PBuilder("my_pbuilderrc", self.root_command)
        builder.build("my_dsc_file", binary_dir_name, "the_logfile",
                      extra_packages=["subversion", "texinfo"])
        self.check_command_line(['/usr/sbin/pbuilder', 'build',
                                 '--configfile', 'my_pbuilderrc',
                                 '--extrapackages', "subversion",
                                 '--extrapackages', "texinfo",
                                 '--logfile', 'the_logfile',
                                 '--buildresult', binary_dir_name,
                                 'my_dsc_file'])
        self.failUnless(os.path.isdir(binary_dir_name))

    def test_build_with_extra_env(self):
        """Tests the PBuilder.build method with the extra_env parameter"""
        binary_dir_name = self.temp_file_name("binary")
        if os.path.exists(binary_dir_name):
            os.rmdir(binary_dir_name)
        env_test_file = self.temp_file_name(self.id() + "_envtest", remove=True)
        # sanity check: the binary directory must not exist yet.
        self.failIf(os.path.exists(binary_dir_name))
        # sanity check: the environment variable TREEPKG_TEST must not
        # be set yet
        self.failIf("TREEPKG_TEST" in os.environ)

        builder = PBuilder("my_pbuilderrc", self.root_command)
        builder.build("my_dsc_file", binary_dir_name, "the_logfile",
                      extra_env=dict(TREEPKG_TEST=env_test_file))
        self.check_command_line(['/usr/sbin/pbuilder', 'build',
                                 '--configfile', 'my_pbuilderrc',
                                 '--logfile', 'the_logfile',
                                 '--buildresult', binary_dir_name,
                                 'my_dsc_file'])
        self.failUnless(os.path.isdir(binary_dir_name))
        self.failUnless(os.path.exists(env_test_file))

    def test_run_script(self):
        builder = PBuilder("my_pbuilderrc", self.root_command)
        builder.run_script("my_script", logfile="the_logfile")
        self.check_command_line(['/usr/sbin/pbuilder', 'execute',
                                 '--configfile', 'my_pbuilderrc',
                                 '--logfile', 'the_logfile',
                                 'my_script'])

    def test_run_script_with_bindmounts(self):
        builder = PBuilder("my_pbuilderrc", self.root_command)
        builder.run_script("my_script", logfile="the_logfile",
                           bindmounts=("/home/builder/foo",
                                       "/home/builder/treepkg"))
        self.check_command_line(['/usr/sbin/pbuilder', 'execute',
                                 '--configfile', 'my_pbuilderrc',
                                 '--logfile', 'the_logfile',
                                 '--bindmounts', '/home/builder/foo',
                                 '--bindmounts', '/home/builder/treepkg',
                                 'my_script'])


class TestPBuilderWithBinaryPackage(PBuilderTests):

    minimal_package = [
        ("debian",
         [("control", """\
Source: minimal
Section: utils
Priority: optional
Maintainer: Bernhard Herzog <bh@intevation.de>
Standards-Version: 3.7.2.2
Build-Depends: debhelper

Package: minimal
Architecture: any
Depends: ${shlibs:Depends}
Description: Minimal package for test purposes
"""),
          ("rules", 0755, """\
#!/usr/bin/make -f

build: build-stamp
build-stamp:
	dh_testdir
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	dh_clean

# Build architecture-dependent files here.
binary-arch:
	dh_testdir
	dh_testroot
	dh_installdocs README
	dh_installchangelogs
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch
.PHONY: build build-stamp clean binary-arch binary
"""),
          ("changelog", """\
minimal (1.0-1) unstable; urgency=low

  * Newly packaged

 -- Bernhard Herzog <bh@intevation.de>  Wed, 21 May 2008 16:10:29 +0200
""")]),
        ("README", """\
This is a minimal debian package for test purposes
"""),
        ]

    pbuilder_files = [("pbuilderrc", ""),
                      ("extra-pkg", [])]

    def setUp(self):
        PBuilderTests.setUp(self)
        self.temp_base_dir = self.create_temp_dir(self.id())
        self.minimal_packge_dir = os.path.join(self.temp_base_dir,
                                               "minimal-1.0")
        self.create_files(self.minimal_packge_dir, self.minimal_package)
        call(["dpkg-buildpackage", "-rfakeroot", "-b", "-uc"],
             cwd=self.minimal_packge_dir, suppress_output=True)
        self.minimal_package_deb = os.path.join(self.temp_base_dir,
                                                "minimal_1.0-1_i386.deb")
        self.pbuilder_basedir = os.path.join(self.temp_base_dir, "pbuilder")
        self.create_files(self.pbuilder_basedir, self.pbuilder_files)
        self.extra_pkg_dir = os.path.join(self.pbuilder_basedir, "extra-pkg")
        self.pbuilderrc = os.path.join(self.pbuilder_basedir, "pbuilderrc")

    def test_add_binaries_to_extra_pkg(self):
        """Tests the PBuilder.add_binaries_to_extra_pkg method"""
        builder = PBuilder(self.pbuilderrc, self.root_command)
        # sanity check: the extra-pkg directory should be empty now
        self.assertEquals(os.listdir(self.extra_pkg_dir), [])

        builder.add_binaries_to_extra_pkg([self.minimal_package_deb])

        self.assertEquals(sorted(os.listdir(self.extra_pkg_dir)),
                          ["Packages", "minimal_1.0-1_i386.deb"])
        self.check_command_line(['/usr/sbin/pbuilder', 'update',
                                 '--configfile', self.pbuilderrc])
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)