bh@246: # Copyright (C) 2009 by Intevation GmbH bh@246: # Authors: bh@246: # Bernhard Herzog bh@246: # bh@246: # This program is free software under the GPL (>=v2) bh@246: # Read the file COPYING coming with the software for details. bh@246: bh@246: bh@246: import sys bh@246: import os bh@246: import difflib bh@246: import unittest bh@251: from datetime import datetime, timedelta bh@246: bh@246: from filesupport import FileTestMixin bh@246: bh@246: from treepkg.run import call, capture_output, SubprocessError bh@251: from treepkg.status import RevisionStatus bh@246: bh@246: bh@246: def package_track_files(track, version, binaries, revisions): bh@246: src_templates = ["%s_%s.%%s%s" % (track, version, suffix) bh@246: for suffix in [".orig.tar.gz", "-1.diff.gz", "-1.dsc"]] bh@246: bin_templates = ["%s_%s.%%s-1_i386.deb" % (basename, version) bh@246: for basename in binaries] bh@251: bh@251: start_time = datetime.now() bh@251: build_duration = timedelta(minutes=30) bh@251: check_interval = timedelta(minutes=60) bh@251: bh@251: package_dirs = [] bh@251: for revision in revisions: bh@251: status = RevisionStatus(None) bh@252: if len(revision) == 2: bh@252: rev, rules_rev = revision bh@252: status.binary_package_created() bh@252: else: bh@252: rev, rules_rev, status_name = revision bh@252: getattr(status, status_name)() bh@252: bh@251: status.start = start_time bh@251: status.stop = start_time + build_duration bh@251: start_time += check_interval bh@251: bh@252: if status.status.finished and not status.status.error: bh@252: src_files = [(template % (rev,), "") for template in src_templates] bh@252: bin_files = [(template % (rev,), "") for template in bin_templates] bh@252: else: bh@252: src_files = [] bh@252: bin_files = [] bh@252: bh@251: package_dirs.append(("%s-%s" % (rev, rules_rev), bh@251: [("status", "".join(status.serialize())), bh@252: ("src", src_files), bh@252: ("binary", bin_files)])) bh@246: return (track, bh@251: [("pkg", bh@251: package_dirs)]) bh@251: bh@246: bh@246: class ListPackagesTest(unittest.TestCase, FileTestMixin): bh@246: bh@246: treepkg_cfg_template = """\ bh@246: [DEFAULT] bh@246: tracks_dir: %(tracksdir)s bh@246: root_cmd: true bh@246: pbuilderrc: bh@246: deb_email: packager@example.com bh@246: deb_fullname: Sample Packager bh@246: debrevision_prefix: treepkg bh@246: handle_dependencies: False bh@246: bh@246: [treepkg] bh@246: check_interval: 3600 bh@246: instructions_file: bh@246: bh@246: [pkg_kdepim] bh@246: svn_url: svn://anonsvn.kde.org/home/kde/branches/kdepim/enterprise/kdepim bh@246: base_dir: %%(tracks_dir)s/kdepim bh@246: packager_class: recipes.kde_enterprise_3_5.kdepim bh@246: bh@246: [pkg_kdepimlibs] bh@246: svn_url: svn://anonsvn.kde.org/home/kde/branches/kdepim/enterprise/kdepimlibs bh@246: base_dir: %%(tracks_dir)s/kdepimlibs bh@246: packager_class: recipes.kde_enterprise_3_5.kdepim bh@246: """ bh@246: bh@246: track_files = [] bh@246: bh@246: def setUp(self): bh@246: self.bindir = os.path.join(os.path.dirname(__file__), bh@246: os.pardir, "bin") bh@246: self.listpackages = os.path.join(self.bindir, "listpackages.py") bh@246: self.tracksdir = self.create_files("tracks", self.track_files) bh@246: config = self.treepkg_cfg_template % dict(tracksdir=self.tracksdir) bh@246: self.config_file = self.create_temp_file("treepkg.cfg", config) bh@246: self.base_command = [sys.executable, self.listpackages, bh@246: "--config-file=" + self.config_file] bh@246: bh@246: def run_command(self, extra_args, expected_exit_code=0): bh@246: cmd = self.base_command + list(extra_args) bh@246: try: bh@246: output = capture_output(cmd) bh@246: except SubprocessError, exc: bh@246: if expected_exit_code != exc.returncode: bh@246: raise bh@246: output = exc.output bh@246: return output bh@246: bh@246: def check_cmd(self, args, expected): bh@246: expected_lines = [os.path.join(self.tracksdir, filename) + "\n" bh@246: for filename in expected.splitlines()] bh@246: try: bh@246: output_lines = [line + "\n" bh@246: for line in self.run_command(args).splitlines()] bh@246: except SubprocessError, exc: bh@246: print >>sys.stderr, "Unexpected output:", exc.output bh@246: raise bh@246: bh@246: # the order of the listed packages doesn't matter, so sort the bh@246: # lists before comparing them bh@246: expected_lines.sort() bh@246: output_lines.sort() bh@246: bh@246: if expected_lines != output_lines: bh@246: diff = difflib.unified_diff(expected_lines, output_lines, bh@246: "expected", "found") bh@246: raise self.failureException("\n" + "".join(diff)) bh@246: bh@246: bh@248: class TestListPackagesNoPackages(ListPackagesTest): bh@248: bh@248: track_files = [] bh@248: bh@248: def test_listpackages_noargs(self): bh@248: # if nothing has been packaged yet, the output is simply empty bh@248: self.check_cmd(["--source"], "") bh@248: bh@248: def test_listpackages_revision(self): bh@248: # if nothing has been packaged yet, the output is simply empty bh@248: self.assertEquals(self.run_command(["--source", "--revision=12345", bh@248: "--track=kdepim"], bh@248: expected_exit_code=1), bh@248: "No revision 12345\n") bh@248: bh@248: def test_listpackages_revision_rulesrev(self): bh@248: # if nothing has been packaged yet, the output is simply empty bh@248: self.assertEquals(self.run_command(["--source", "--revision=12345-321", bh@248: "--track=kdepim"], bh@248: expected_exit_code=1), bh@248: "No revision 12345-321\n") bh@248: bh@246: class TestListPackagesTrackNoRevision(ListPackagesTest): bh@246: bh@246: track_files = [package_track_files("kdepim", "1.0.1", bh@246: ["kdepim", "kleopatra"], bh@246: [(704195, 31), (702432, 47)])] bh@246: bh@246: def test_listpackages_source(self): bh@246: self.check_cmd(["--track=kdepim", "--source"], bh@246: """\ bh@251: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432-1.diff.gz bh@251: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432-1.dsc bh@251: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432.orig.tar.gz bh@246: """) bh@246: bh@246: def test_listpackages_binary(self): bh@246: self.check_cmd(["--track=kdepim", "--binary"], bh@246: """\ bh@251: kdepim/pkg/702432-47/binary/kdepim_1.0.1.702432-1_i386.deb bh@251: kdepim/pkg/702432-47/binary/kleopatra_1.0.1.702432-1_i386.deb bh@246: """) bh@246: bh@246: bh@252: class TestListPackagesTrackNoRevisionWithIncompleteBuilds(ListPackagesTest): bh@252: bh@252: track_files = [package_track_files("kdepim", "1.0.1", bh@252: ["kdepim", "kleopatra"], bh@252: [(702432, 47), bh@252: (704195, 31, "error")])] bh@252: bh@252: def test_listpackages_source(self): bh@252: self.check_cmd(["--track=kdepim", "--source"], bh@252: """\ bh@252: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432-1.diff.gz bh@252: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432-1.dsc bh@252: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432.orig.tar.gz bh@252: """) bh@252: bh@252: def test_listpackages_binary(self): bh@252: self.check_cmd(["--track=kdepim", "--binary"], bh@252: """\ bh@252: kdepim/pkg/702432-47/binary/kdepim_1.0.1.702432-1_i386.deb bh@252: kdepim/pkg/702432-47/binary/kleopatra_1.0.1.702432-1_i386.deb bh@252: """) bh@252: bh@252: bh@246: class TestListPackagesNoTrackNoRevision(ListPackagesTest): bh@246: bh@246: track_files = [package_track_files("kdepim", "1.0.1", bh@246: ["kdepim", "kleopatra"], bh@251: [(702432, 47), bh@251: (704195, 31)]), bh@246: package_track_files("kdepimlibs", "1.0.1", bh@246: ["kdepimlibs"], bh@251: [(803691, 4321), bh@251: (803692, 4322)]), bh@246: ] bh@246: bh@246: def test_listpackages_source(self): bh@246: self.check_cmd(["--source"], bh@246: """\ bh@246: kdepim/pkg/704195-31/src/kdepim_1.0.1.704195-1.diff.gz bh@246: kdepim/pkg/704195-31/src/kdepim_1.0.1.704195-1.dsc bh@246: kdepim/pkg/704195-31/src/kdepim_1.0.1.704195.orig.tar.gz bh@246: kdepimlibs/pkg/803692-4322/src/kdepimlibs_1.0.1.803692-1.diff.gz bh@246: kdepimlibs/pkg/803692-4322/src/kdepimlibs_1.0.1.803692-1.dsc bh@246: kdepimlibs/pkg/803692-4322/src/kdepimlibs_1.0.1.803692.orig.tar.gz""") bh@246: bh@246: def test_listpackages_binary(self): bh@246: self.check_cmd(["--binary"], bh@246: """\ bh@246: kdepim/pkg/704195-31/binary/kdepim_1.0.1.704195-1_i386.deb bh@246: kdepim/pkg/704195-31/binary/kleopatra_1.0.1.704195-1_i386.deb bh@246: kdepimlibs/pkg/803692-4322/binary/kdepimlibs_1.0.1.803692-1_i386.deb""") bh@246: bh@246: bh@253: class TestListPackagesNNewest(ListPackagesTest): bh@253: bh@253: track_files = [package_track_files("kdepim", "1.0.1", bh@253: ["kdepim", "kleopatra"], bh@253: [(701865, 28), bh@253: (700345, 28), bh@253: (702432, 28), bh@253: (704195, 31, "error")]), bh@253: package_track_files("kdepimlibs", "1.0.1", bh@253: ["kdepimlibs"], bh@253: [(700298, 34), bh@253: (700345, 34), bh@253: (701866, 34, "error"), bh@253: (701866, 35), bh@253: (702436, 35)]), bh@253: ] bh@253: bh@253: def test_listpackages_source(self): bh@253: self.check_cmd(["--source", "--newest=2"], bh@253: """\ bh@253: kdepim/pkg/700345-28/src/kdepim_1.0.1.700345-1.diff.gz bh@253: kdepim/pkg/700345-28/src/kdepim_1.0.1.700345-1.dsc bh@253: kdepim/pkg/700345-28/src/kdepim_1.0.1.700345.orig.tar.gz bh@253: kdepim/pkg/702432-28/src/kdepim_1.0.1.702432-1.diff.gz bh@253: kdepim/pkg/702432-28/src/kdepim_1.0.1.702432-1.dsc bh@253: kdepim/pkg/702432-28/src/kdepim_1.0.1.702432.orig.tar.gz bh@253: kdepimlibs/pkg/701866-35/src/kdepimlibs_1.0.1.701866-1.diff.gz bh@253: kdepimlibs/pkg/701866-35/src/kdepimlibs_1.0.1.701866-1.dsc bh@253: kdepimlibs/pkg/701866-35/src/kdepimlibs_1.0.1.701866.orig.tar.gz bh@253: kdepimlibs/pkg/702436-35/src/kdepimlibs_1.0.1.702436-1.diff.gz bh@253: kdepimlibs/pkg/702436-35/src/kdepimlibs_1.0.1.702436-1.dsc bh@253: kdepimlibs/pkg/702436-35/src/kdepimlibs_1.0.1.702436.orig.tar.gz""") bh@253: bh@253: def test_listpackages_binary(self): bh@253: self.check_cmd(["--binary", "--newest=3"], bh@253: """\ bh@253: kdepim/pkg/700345-28/binary/kdepim_1.0.1.700345-1_i386.deb bh@253: kdepim/pkg/700345-28/binary/kleopatra_1.0.1.700345-1_i386.deb bh@253: kdepim/pkg/701865-28/binary/kdepim_1.0.1.701865-1_i386.deb bh@253: kdepim/pkg/701865-28/binary/kleopatra_1.0.1.701865-1_i386.deb bh@253: kdepim/pkg/702432-28/binary/kdepim_1.0.1.702432-1_i386.deb bh@253: kdepim/pkg/702432-28/binary/kleopatra_1.0.1.702432-1_i386.deb bh@253: kdepimlibs/pkg/700345-34/binary/kdepimlibs_1.0.1.700345-1_i386.deb bh@253: kdepimlibs/pkg/701866-35/binary/kdepimlibs_1.0.1.701866-1_i386.deb bh@253: kdepimlibs/pkg/702436-35/binary/kdepimlibs_1.0.1.702436-1_i386.deb""") bh@253: bh@253: bh@246: class TestListPackagesTrackRevision(ListPackagesTest): bh@246: bh@246: track_files = [package_track_files("kdepim", "1.0.1", bh@246: ["kdepim", "kleopatra"], bh@246: [(704195, 31), (702432, 47)]), bh@246: package_track_files("kdepimlibs", "1.0.1", bh@246: ["kdepimlibs"], bh@246: [(803691, 4321), (803692, 4322)]), bh@246: ] bh@246: bh@246: def test_listpackages_source(self): bh@246: self.check_cmd(["--source", "--track=kdepim", "--revision=702432"], bh@246: """\ bh@246: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432-1.diff.gz bh@246: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432-1.dsc bh@246: kdepim/pkg/702432-47/src/kdepim_1.0.1.702432.orig.tar.gz""") bh@246: bh@246: def test_listpackages_binary(self): bh@246: self.check_cmd(["--binary", "--track=kdepim", "--revision=702432"], bh@246: """\ bh@246: kdepim/pkg/702432-47/binary/kdepim_1.0.1.702432-1_i386.deb bh@246: kdepim/pkg/702432-47/binary/kleopatra_1.0.1.702432-1_i386.deb""") bh@246: bh@246: bh@246: bh@246: class TestListPackagesMultipleRulesRevisions(ListPackagesTest): bh@246: bh@246: track_files = [package_track_files("kdepim", "1.0.1", bh@246: ["kdepim", "kleopatra"], bh@251: [(702432, 47), bh@251: (704195, 31), bh@251: (704195, 33)]), bh@246: package_track_files("kdepimlibs", "1.0.1", bh@246: ["kdepimlibs"], bh@251: [(803691, 4321), bh@251: (803692, 4322)]), bh@246: ] bh@246: bh@246: def test_listpackages_source_no_rev(self): bh@246: self.check_cmd(["--source", "--track=kdepim"], bh@246: """\ bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195-1.diff.gz bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195-1.dsc bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195.orig.tar.gz""") bh@246: bh@246: def test_listpackages_binary_no_rev(self): bh@246: self.check_cmd(["--binary", "--track=kdepim"], bh@246: """\ bh@246: kdepim/pkg/704195-33/binary/kdepim_1.0.1.704195-1_i386.deb bh@246: kdepim/pkg/704195-33/binary/kleopatra_1.0.1.704195-1_i386.deb""") bh@246: bh@246: def test_listpackages_source_and_binary_no_rev(self): bh@246: self.check_cmd(["--source", "--track=kdepim", "--binary"], bh@246: """\ bh@246: kdepim/pkg/704195-33/binary/kdepim_1.0.1.704195-1_i386.deb bh@246: kdepim/pkg/704195-33/binary/kleopatra_1.0.1.704195-1_i386.deb bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195-1.diff.gz bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195-1.dsc bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195.orig.tar.gz bh@246: """) bh@246: bh@246: def test_listpackages_source_no_rulesrev(self): bh@246: self.check_cmd(["--source", "--track=kdepim", "--revision=704195"], bh@246: """\ bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195-1.diff.gz bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195-1.dsc bh@246: kdepim/pkg/704195-33/src/kdepim_1.0.1.704195.orig.tar.gz""") bh@246: bh@246: def test_listpackages_binary_no_rulesrev(self): bh@246: self.check_cmd(["--binary", "--track=kdepim", "--revision=704195"], bh@246: """\ bh@246: kdepim/pkg/704195-33/binary/kdepim_1.0.1.704195-1_i386.deb bh@246: kdepim/pkg/704195-33/binary/kleopatra_1.0.1.704195-1_i386.deb""") bh@246: bh@246: def test_listpackages_source_rulesrev(self): bh@246: self.check_cmd(["--source", "--track=kdepim", "--revision=704195-31"], bh@246: """\ bh@246: kdepim/pkg/704195-31/src/kdepim_1.0.1.704195-1.diff.gz bh@246: kdepim/pkg/704195-31/src/kdepim_1.0.1.704195-1.dsc bh@246: kdepim/pkg/704195-31/src/kdepim_1.0.1.704195.orig.tar.gz""") bh@246: bh@246: def test_listpackages_binary_rulesrev(self): bh@246: self.check_cmd(["--binary", "--track=kdepim", "--revision=704195-31"], bh@246: """\ bh@246: kdepim/pkg/704195-31/binary/kdepim_1.0.1.704195-1_i386.deb bh@246: kdepim/pkg/704195-31/binary/kleopatra_1.0.1.704195-1_i386.deb""") bh@246: bh@246: