bh@118: # Copyright (C) 2007, 2008 by Intevation GmbH bh@118: # Authors: bh@118: # Bernhard Herzog bh@118: # bh@118: # This program is free software under the GPL (>=v2) bh@118: # Read the file COPYING coming with the software for details. bh@118: bh@118: """Tests for treepkg.builder""" bh@118: bh@118: import sys bh@118: import os bh@118: import unittest bh@118: bh@118: from treepkg.builder import PBuilder bh@121: from treepkg.run import call bh@118: bh@118: from filesupport import FileTestMixin bh@118: bh@118: # helper program to dump the command line arguments into a file so that bh@126: # test cases can check them. Also if the environment variable bh@126: # TREEPKG_TEST is set, create the file named by it. bh@118: dump_command_line_py = """ bh@126: import sys, os bh@118: open(sys.argv[1], 'w').write(repr(sys.argv[2:])) bh@126: value = os.environ.get('TREEPKG_TEST') bh@126: if value: bh@126: open(value, 'w').close() bh@118: """ bh@118: bh@121: class PBuilderTests(unittest.TestCase, FileTestMixin): bh@118: bh@118: def setUp(self): bh@118: self.dump_command_line = self.create_temp_file("dump_command_line.py", bh@118: dump_command_line_py) bh@118: self.command_line_file = self.temp_file_name("command_line") bh@118: self.root_command = [sys.executable, self.dump_command_line, bh@118: self.command_line_file] bh@118: bh@118: def check_command_line(self, args): bh@118: self.checkFileContents(self.command_line_file, repr(args)) bh@118: bh@121: bh@121: class TestPBuilder(PBuilderTests): bh@121: bh@118: def test_build(self): bh@118: """Tests the PBuilder.build method. bh@118: The test checks whether the build method creates the binary_dir bh@118: and then checks the arguments with which the root command is bh@118: called. The test is a little too strict because it expects a bh@118: specific order of the arguments even though the order of some of bh@118: the arguments doesn't matter. bh@118: bh@118: A more thorough test of the build method is implicity done by bh@118: the packager tests. bh@118: """ bh@118: binary_dir_name = self.temp_file_name("binary") bh@118: if os.path.exists(binary_dir_name): bh@118: os.rmdir(binary_dir_name) bh@118: # sanity check: the binary directory must not exist yet. bh@118: self.failIf(os.path.exists(binary_dir_name)) bh@118: bh@118: builder = PBuilder("my_pbuilderrc", self.root_command) bh@118: builder.build("my_dsc_file", binary_dir_name, "the_logfile") bh@118: self.check_command_line(['/usr/sbin/pbuilder', 'build', bh@118: '--configfile', 'my_pbuilderrc', bh@118: '--logfile', 'the_logfile', bh@118: '--buildresult', binary_dir_name, bh@118: 'my_dsc_file']) bh@118: self.failUnless(os.path.isdir(binary_dir_name)) bh@119: bh@122: def test_build_with_bindmounts(self): bh@122: """Tests the PBuilder.build method with the bindmounts parameter""" bh@122: binary_dir_name = self.temp_file_name("binary") bh@122: if os.path.exists(binary_dir_name): bh@122: os.rmdir(binary_dir_name) bh@122: # sanity check: the binary directory must not exist yet. bh@122: self.failIf(os.path.exists(binary_dir_name)) bh@122: bh@122: builder = PBuilder("my_pbuilderrc", self.root_command) bh@122: builder.build("my_dsc_file", binary_dir_name, "the_logfile", bh@122: bindmounts=["/home/builder/tracks", bh@122: "/home/builder/pbuilder"]) bh@122: self.check_command_line(['/usr/sbin/pbuilder', 'build', bh@122: '--configfile', 'my_pbuilderrc', bh@122: '--bindmounts', "/home/builder/tracks", bh@122: '--bindmounts', "/home/builder/pbuilder", bh@122: '--logfile', 'the_logfile', bh@122: '--buildresult', binary_dir_name, bh@122: 'my_dsc_file']) bh@122: self.failUnless(os.path.isdir(binary_dir_name)) bh@122: bh@122: def test_build_with_extra_packages(self): bh@122: """Tests the PBuilder.build method with the extra_packages parameter""" bh@122: binary_dir_name = self.temp_file_name("binary") bh@122: if os.path.exists(binary_dir_name): bh@122: os.rmdir(binary_dir_name) bh@122: # sanity check: the binary directory must not exist yet. bh@122: self.failIf(os.path.exists(binary_dir_name)) bh@122: bh@122: builder = PBuilder("my_pbuilderrc", self.root_command) bh@122: builder.build("my_dsc_file", binary_dir_name, "the_logfile", bh@122: extra_packages=["subversion", "texinfo"]) bh@122: self.check_command_line(['/usr/sbin/pbuilder', 'build', bh@122: '--configfile', 'my_pbuilderrc', bh@122: '--extrapackages', "subversion", bh@122: '--extrapackages', "texinfo", bh@122: '--logfile', 'the_logfile', bh@122: '--buildresult', binary_dir_name, bh@122: 'my_dsc_file']) bh@122: self.failUnless(os.path.isdir(binary_dir_name)) bh@122: bh@126: def test_build_with_extra_env(self): bh@126: """Tests the PBuilder.build method with the extra_env parameter""" bh@126: binary_dir_name = self.temp_file_name("binary") bh@126: if os.path.exists(binary_dir_name): bh@126: os.rmdir(binary_dir_name) bh@126: env_test_file = self.temp_file_name(self.id() + "_envtest", remove=True) bh@126: # sanity check: the binary directory must not exist yet. bh@126: self.failIf(os.path.exists(binary_dir_name)) bh@126: # sanity check: the environment variable TREEPKG_TEST must not bh@126: # be set yet bh@126: self.failIf("TREEPKG_TEST" in os.environ) bh@126: bh@126: builder = PBuilder("my_pbuilderrc", self.root_command) bh@126: builder.build("my_dsc_file", binary_dir_name, "the_logfile", bh@126: extra_env=dict(TREEPKG_TEST=env_test_file)) bh@126: self.check_command_line(['/usr/sbin/pbuilder', 'build', bh@126: '--configfile', 'my_pbuilderrc', bh@126: '--logfile', 'the_logfile', bh@126: '--buildresult', binary_dir_name, bh@126: 'my_dsc_file']) bh@126: self.failUnless(os.path.isdir(binary_dir_name)) bh@126: self.failUnless(os.path.exists(env_test_file)) bh@126: bh@119: def test_run_script(self): bh@119: builder = PBuilder("my_pbuilderrc", self.root_command) bh@119: builder.run_script("my_script", logfile="the_logfile") bh@119: self.check_command_line(['/usr/sbin/pbuilder', 'execute', bh@119: '--configfile', 'my_pbuilderrc', bh@119: '--logfile', 'the_logfile', bh@119: 'my_script']) bh@119: bh@119: def test_run_script_with_bindmounts(self): bh@119: builder = PBuilder("my_pbuilderrc", self.root_command) bh@119: builder.run_script("my_script", logfile="the_logfile", bh@119: bindmounts=("/home/builder/foo", bh@119: "/home/builder/treepkg")) bh@119: self.check_command_line(['/usr/sbin/pbuilder', 'execute', bh@119: '--configfile', 'my_pbuilderrc', bh@119: '--logfile', 'the_logfile', bh@119: '--bindmounts', '/home/builder/foo', bh@119: '--bindmounts', '/home/builder/treepkg', bh@119: 'my_script']) bh@121: bh@121: bh@121: class TestPBuilderWithBinaryPackage(PBuilderTests): bh@121: bh@121: minimal_package = [ bh@121: ("debian", bh@121: [("control", """\ bh@121: Source: minimal bh@121: Section: utils bh@121: Priority: optional bh@121: Maintainer: Bernhard Herzog bh@121: Standards-Version: 3.7.2.2 bh@121: Build-Depends: debhelper bh@121: bh@121: Package: minimal bh@121: Architecture: any bh@121: Depends: ${shlibs:Depends} bh@121: Description: Minimal package for test purposes bh@121: """), bh@121: ("rules", 0755, """\ bh@121: #!/usr/bin/make -f bh@121: bh@121: build: build-stamp bh@121: build-stamp: bh@121: dh_testdir bh@121: touch build-stamp bh@121: bh@121: clean: bh@121: dh_testdir bh@121: dh_testroot bh@121: rm -f build-stamp bh@121: dh_clean bh@121: bh@121: # Build architecture-dependent files here. bh@121: binary-arch: bh@121: dh_testdir bh@121: dh_testroot bh@121: dh_installdocs README bh@121: dh_installchangelogs bh@121: dh_fixperms bh@121: dh_installdeb bh@121: dh_gencontrol bh@121: dh_md5sums bh@121: dh_builddeb bh@121: bh@121: binary: binary-arch bh@121: .PHONY: build build-stamp clean binary-arch binary bh@121: """), bh@121: ("changelog", """\ bh@121: minimal (1.0-1) unstable; urgency=low bh@121: bh@121: * Newly packaged bh@121: bh@121: -- Bernhard Herzog Wed, 21 May 2008 16:10:29 +0200 bh@121: """)]), bh@121: ("README", """\ bh@121: This is a minimal debian package for test purposes bh@121: """), bh@121: ] bh@121: bh@121: pbuilder_files = [("pbuilderrc", ""), bh@121: ("extra-pkg", [])] bh@121: bh@121: def setUp(self): bh@121: PBuilderTests.setUp(self) bh@121: self.temp_base_dir = self.create_temp_dir(self.id()) bh@121: self.minimal_packge_dir = os.path.join(self.temp_base_dir, bh@121: "minimal-1.0") bh@121: self.create_files(self.minimal_packge_dir, self.minimal_package) bh@121: call(["dpkg-buildpackage", "-rfakeroot", "-b", "-uc"], bh@121: cwd=self.minimal_packge_dir, suppress_output=True) bh@121: self.minimal_package_deb = os.path.join(self.temp_base_dir, bh@121: "minimal_1.0-1_i386.deb") bh@121: self.pbuilder_basedir = os.path.join(self.temp_base_dir, "pbuilder") bh@121: self.create_files(self.pbuilder_basedir, self.pbuilder_files) bh@121: self.extra_pkg_dir = os.path.join(self.pbuilder_basedir, "extra-pkg") bh@121: self.pbuilderrc = os.path.join(self.pbuilder_basedir, "pbuilderrc") bh@121: bh@121: def test_add_binaries_to_extra_pkg(self): bh@121: """Tests the PBuilder.add_binaries_to_extra_pkg method""" bh@121: builder = PBuilder(self.pbuilderrc, self.root_command) bh@121: # sanity check: the extra-pkg directory should be empty now bh@121: self.assertEquals(os.listdir(self.extra_pkg_dir), []) bh@121: bh@121: builder.add_binaries_to_extra_pkg([self.minimal_package_deb]) bh@121: bh@121: self.assertEquals(sorted(os.listdir(self.extra_pkg_dir)), bh@121: ["Packages", "minimal_1.0-1_i386.deb"]) bh@121: self.check_command_line(['/usr/sbin/pbuilder', 'update', bh@121: '--configfile', self.pbuilderrc])