package TestDpkg; use base qw(DpkgTest::FunctionalTest); use Cwd; =item base_dpkg_cmd Returns the base command line for dpkg (path and basic options). =cut sub base_dpkg_cmd { my ($self, $pkg) = @_; $self->{program}." ".$self->dpkg_options; } =item dpkg_test_pkg($pkg_file) Returns the path for the given package file. =cut sub dpkg_test_pkg { my ($self, $pkg) = @_; "test-package-repo/$pkg"; } =item dpkg_root_dir Returns the directory used as root. Normally dpkg-root/root, but if we want to test the installed dpkg, it will be the real root directory (i.e., "/"). =cut sub dpkg_root_dir { my ($self) = @_; "dpkg-root/root" } =item execute_root_cmd($cmd) Executes the given command "as root" (with C or similar), and returns its return code. =cut sub execute_root_cmd { my ($self, $cmd) = @_; system("PATH=/usr/bin:/usr/sbin:/bin:/sbin fakeroot $cmd"); } =item dpkg_options Returns a string with the options that every test call to C should have. =cut sub dpkg_options { my ($self) = @_; my $rootdir = $self->dpkg_root_dir; my $logfile = "$rootdir/var/log/dpkg.log"; "--root $rootdir --admindir $rootdir/var/lib/dpkg --log $logfile"; } =item install_pkg($pkg_file, $extra_opts) Installs the given deb file. =cut sub install_pkg { my ($self, $pkg, $extra_opts) = @_; my @pkgs = ref($pkg) ? @$pkg : ($pkg); $extra_opts ||= ""; my $plain_pkgs = join(" ", map { $self->dpkg_test_pkg($_) } @pkgs); $self->execute_root_cmd($self->base_dpkg_cmd." -i ".$plain_pkgs." $extra_opts >/dev/null"); } sub is_pkg_installed { my ($self, $pkg) = @_; system($self->base_dpkg_cmd." -l $pkg 2>/dev/null | egrep -q ^ii") == 0 } =item assert_pkg_installed($pkg_name) Asserts that the given package is installed. =cut sub assert_pkg_installed { my ($self, $pkg) = @_; $self->assert($self->is_pkg_installed($pkg), "Package $pkg is installed"); } =item assert_pkg_not_installed($pkg_name) Asserts that the given package is B installed. =cut sub assert_pkg_not_installed { my ($self, $pkg) = @_; $self->assert(!$self->is_pkg_installed($pkg), "Package $pkg is not installed"); } sub set_up { my $self = shift; $self->{program} = $self->dpkg_binary('dpkg'); # Uncompress fresh dpkg root if necessary unless ($ENV{TEST_IN_REAL_SYSTEM}) { system("tar xfz dpkg-root/root.tar.gz -C dpkg-root"); } } sub test_pkg_install { my $self = shift; $self->install_pkg("most-basic_0.1_all.deb"); $self->assert_pkg_installed("most-basic"); $self->assert_equals(256, $self->install_pkg("some-garbage.deb", "2>/dev/null"), "Verify an empty file can't be installed"); $self->assert_pkg_not_installed("some-garbage"); } sub test_pkg_dep_install { my $self = shift; $self->install_pkg("test-dpkg-foobar_1.0_all.deb", "2>/dev/null"); $self->assert_pkg_not_installed("test-dpkg-foobar"); $self->install_pkg(["libtest-dpkg-foobar_1.0_all.deb", "test-dpkg-foobar_1.0_all.deb"]); $self->assert_pkg_installed("test-dpkg-foobar"); } sub test_pkg_conflicts { my $self = shift; $self->install_pkg("test-dpkg-conflicted_1.0_all.deb"); $self->assert_pkg_installed("test-dpkg-conflicted"); $self->install_pkg("test-dpkg-conflicting_1.0_all.deb", "2>/dev/null"); $self->assert_pkg_installed("test-dpkg-conflicting"); $self->assert_pkg_not_installed("test-dpkg-conflicted"); } sub tear_down { my $self = shift; # Wipe out dpkg root if necessary (check that the dir is a subir of the # current one, just in case) my $dir = $self->dpkg_root_dir; if (($dir !~ /^\// || $dir =~ /^cwd/) && not $ENV{TEST_IN_REAL_SYSTEM}) { system("rm -rf ".$dir); } } 1;