File: TestLib.pm

package info (click to toggle)
postgresql-common 71
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 584 kB
  • ctags: 96
  • sloc: perl: 2,158; sh: 215; makefile: 12
file content (172 lines) | stat: -rw-r--r-- 5,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Common functionality for postgresql-common selftests
# (c) 2005 Martin Pitt <mpitt@debian.org>

package TestLib;
use strict;
use Exporter;
use Test::More;

our $VERSION = 1.00;
our @ISA = ('Exporter');
our @EXPORT = qw/ps ok_dir exec_as deb_installed is_program_out
    like_program_out unlike_program_out pidof pid_env check_clean @MAJORS/;

use lib '/usr/share/postgresql-common';
use PgCommon qw/get_versions change_ugid/;
our @MAJORS = sort (get_versions());

# called if a test fails; spawn a shell if the environment variable
# FAILURE=shell is set
sub fail_debug { 
    if ($ENV{'FAILURE'} eq 'shell') {
	system 'bash';
    }
}

# Return whether a given deb is installed.
# Arguments: <deb name>
sub deb_installed {
    open (DPKG, "dpkg -s $_[0] 2>/dev/null|") or die "call dpkg: $!";
    while (<DPKG>) {
	return 1 if /^Version:/;
    }

    return 0;
}

# Return the user, group, and command line of running processes for the given
# program.
sub ps {
    return `ps h -o user,group,args -C $_[0] | grep '$_[0]' | sort -u`;
}

# Return array of pids that match the given command line
sub pidof {
    open F, '-|', 'ps', 'h', '-C', $_[0], '-o', 'pid,cmd' or die "open: $!";
    my @pids;
    while (<F>) {
        if ((index $_, $_[0]) >= 0 && (index $_, '/') >= 0) {
            push @pids, (split)[0];
        }
    }
    close F;
    return @pids;
}

# Return an reference to an array of all entries but . and .. of the given directory.
sub dircontent {
    opendir D, $_[0] or die "opendir: $!";
    my @e = grep { $_ ne '.' && $_ ne '..' } readdir (D);
    closedir D;
    return \@e;
}

# Return environment of given PID
sub pid_env {
    my $path = "/proc/$_[0]/environ";
    my @lines;
    open E, $path or die "open $path: $!";
    {
        local $/;
        @lines = split '\0', <E>;
    }
    close E;
    my %env;
    foreach (@lines) {
        my ($k, $v) = (split '=');
        $env{$k} = $v;
    }
    return %env;
}

# Check the contents of a directory.
# Arguments: <directory name> <ref to expected dir content> <test description>
sub ok_dir {
    my $content = dircontent $_[0];
    if (eq_set $content, $_[1]) {
	pass $_[2];
    } else {
	diag "Expected directory contents: [@{$_[1]}], actual contents: [@$content]\n";
	fail $_[2];
    }
}

# Execute a command as a different user and return the output. Prints the
# output of the command if exit code differs from expected one.
# Arguments: <user> <system command> <ref to output> [<expected exit code>]
# Returns: Program exit code
sub exec_as {
    my $uid;
    if ($_[0] =~ /\d+/) {
	$uid = int($_[0]);
    } else {
	$uid = getpwnam $_[0] or die "TestLib::exec_as: target user '$_[0]' does not exist";
    }
    change_ugid ($uid, (getpwuid $uid)[3]);
    die "changing euid: $!" if $> != $uid;
    my $out = `$_[1] 2>&1`;
    my $result = $? >> 8;
    $< = $> = 0;
    $( = $) = 0;
    die "changing euid back to root: $!" if $> != 0;
    $_[2] = \$out;

    if (defined $_[3] && $_[3] != $result) {
        print "command '$_[1]' did not exit with expected code $_[3]:\n";
        print $out;
    }
    return $result;
}

# Execute a command as a particular user, and check the exit code and output
# (merged stdout/stderr).
# Arguments: <user> <command> <expected exit code> <expected output> [<description>]
sub is_program_out {
    my $outref;
    my $result = exec_as $_[0], $_[1], $outref;
    is $result, $_[2], $_[1] or fail_debug;
    is ($$outref, $_[3], (defined $_[4] ? $_[4] : "correct output of $_[1]")) or fail_debug;
}

# Execute a command as a particular user, and check the exit code and output
# against a regular expression (merged stdout/stderr).
# Arguments: <user> <command> <expected exit code> <expected output re> [<description>]
sub like_program_out {
    my $outref;
    my $result = exec_as $_[0], $_[1], $outref;
    is $result, $_[2], $_[1] or fail_debug;
    like ($$outref, $_[3], (defined $_[4] ? $_[4] : "correct output of $_[1]")) or fail_debug;
}

# Execute a command as a particular user, check the exit code, and check that
# the output does not match a regular expression (merged stdout/stderr).
# Arguments: <user> <command> <expected exit code> <expected output re> [<description>]
sub unlike_program_out {
    my $outref;
    my $result = exec_as $_[0], $_[1], $outref;
    is $result, $_[2], $_[1] or fail_debug;
    unlike ($$outref, $_[3], (defined $_[4] ? $_[4] : "correct output of $_[1]")) or fail_debug;
}

# Check that all PostgreSQL related directories are empty and no
# postmaster/pg_autovacuum processes are running. Should be called at the end
# of all tests. Does 10 tests.
sub check_clean {
    is (`pg_lsclusters -h`, '', 'No existing clusters');
    is ((ps 'postmaster'), '', 'No postmaster processes left behind');
    is ((ps 'postgres'), '', 'No postgres processes left behind');
    is ((ps 'pg_autovacuum'), '', 'No pg_autovacuum processes left behind');

    my @check_dirs = ('/etc/postgresql', '/var/lib/postgresql',
        '/var/run/postgresql', '/var/log/postgresql');
    foreach (@check_dirs) {
        if (-d) {
            ok_dir $_, [], "No files in $_ left behind";
        } else {
            pass "Directory $_ does not exist";
        }
    }

    is_program_out 0, 'netstat -avptn | grep ":543[2-9]\\b"', 1, '',
	'PostgreSQL TCP ports are closed';
}