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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#!/usr/bin/perl
# Copyright 2019 Simon McVittie
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
=head1 NAME
test_debi - unit test for debi
=head1 WARNING
This test requires root privileges, and installs and removes packages.
Please run it in an expendable environment, such as via
one of the B<autopkgtest-virt-*> autopkgtest backends.
=head1 DESCRIPTION
This test verifies that debi's B<--with-depends> and B<--upgrade> interact
as expected.
=cut
use autodie;
use strict;
use warnings;
use Cwd qw(getcwd);
use Digest::MD5;
use Digest::SHA;
use File::Temp qw(tempdir);
use IPC::Run qw(run);
use Test::More;
use Dpkg::Control;
my $srcdir = getcwd;
my $top_srcdir = getcwd . '/..';
my @debi = ("$top_srcdir/scripts/debi.pl", '--no-conf');
my $tmp;
if (defined $ARGV[0] && $ARGV[0] eq '--installed') {
$debi[0] = 'debi';
}
else {
$ENV{PATH} = "$top_srcdir/scripts:$ENV{PATH}";
}
sub verbose_run {
my $argv = shift;
diag("Running: @{$argv}");
my $ret = run($argv, @_);
if ($ret) {
diag("=> success");
} else {
diag("=> exit status $?");
}
return $ret;
}
sub capture {
my $output;
my $argv = shift;
ok(verbose_run($argv, '>', \$output), "@{$argv}");
chomp $output;
return $output;
}
sub make_deb {
my ($name, $version, $depends) = @_;
mkdir "$tmp/deb" unless -d "$tmp/deb";
mkdir "$tmp/deb/DEBIAN" unless -d "$tmp/deb/DEBIAN";
open my $fh, '>', "$tmp/deb/DEBIAN/control";
print {$fh} "Package: devscripts-test-$name\n";
print {$fh} "Section: misc\n";
print {$fh} "Priority: optional\n";
print {$fh} "Maintainer: nobody\n";
print {$fh} "Version: $version\n";
print {$fh} "Architecture: all\n";
print {$fh} "Depends: $depends\n";
print {$fh} "Description: a package\n";
close $fh;
my $deb = "$tmp/devscripts-test-${name}_${version}_all.deb";
if (!run(['dpkg-deb', '-b', "$tmp/deb", $deb])) {
BAIL_OUT("Failed to build $name package from $tmp/deb");
}
}
sub make_changes {
my @packages = @_;
my $changes = "$tmp/foo.changes";
my $ctrl = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
$ctrl->{Format} = '1.8';
$ctrl->{Source} = 'devscripts-test';
$ctrl->{Files} = "\n";
$ctrl->{'Checksums-Sha256'} = "\n";
foreach my $name (@packages) {
my $md5 = Digest::MD5->new;
my $sha256 = Digest::SHA->new(256);
open my $fh, '<', "$tmp/devscripts-test-${name}_1_all.deb";
binmode $fh;
$md5->addfile($fh);
seek $fh, 0, 0;
$sha256->addfile(*$fh);
close $fh;
my $hash = $md5->hexdigest;
my @stat = stat "$tmp/devscripts-test-${name}_1_all.deb";
my $size = $stat[7];
$ctrl->{Files}
.= "$hash $size misc optional devscripts-test-${name}_1_all.deb\n";
$hash = $sha256->hexdigest;
$ctrl->{'Checksums-Sha256'}
.= "$hash $size devscripts-test-${name}_1_all.deb\n";
}
diag $ctrl;
$ctrl->save($changes);
}
sub purge_packages {
ok(
verbose_run([
'dpkg',
'--purge',
'devscripts-test-already-installed',
'devscripts-test-dependency',
'devscripts-test-gains-dependency',
'devscripts-test-gains-local-dependency',
'devscripts-test-not-installed',
'hello',
]));
}
sub version_of {
my $output;
my $ignored;
run(['dpkg-query', '-W', '-f', '${Version}', shift],
'>', \$output, '2>', \$ignored);
chomp $output;
return $output;
}
sub status_of {
my $output;
my $ignored;
run(['dpkg-query', '-W', '-f', '${Status}', shift],
'>', \$output, '2>', \$ignored);
chomp $output;
return $output;
}
plan skip_all => 'not root' unless $< == 0 && $> == 0;
$tmp = tempdir(CLEANUP => 1);
open my $fh, '>', "$tmp/yes.conf";
print {$fh} qq{Apt::Get::Assume-Yes "true";\n};
print {$fh} qq{Apt::Get::allow-downgrades "true";\n};
close $fh;
$ENV{APT_CONFIG} = "$tmp/yes.conf";
make_deb('already-installed', '0', 'base-files');
make_deb('already-installed', '1', 'base-files');
make_deb('already-installed', '2', 'base-files');
make_deb('not-installed', '1', 'base-files');
make_deb('gains-local-dependency', '0', 'base-files');
make_deb('gains-local-dependency', '1', 'devscripts-test-dependency');
make_deb('dependency', '1', 'base-files');
make_deb('gains-dependency', '0', 'base-files');
make_deb('gains-dependency', '1', 'hello');
diag('debi foo.changes will upgrade existing packages and install new ones');
purge_packages();
ok(
verbose_run(
['dpkg', '-i', "$tmp/devscripts-test-already-installed_0_all.deb",]));
make_changes(qw(already-installed not-installed));
ok(verbose_run([@debi, "$tmp/foo.changes",]), 'plain debi succeeds');
is(version_of('devscripts-test-already-installed'),
'1', 'already installed package was upgraded');
is(version_of('devscripts-test-not-installed'),
'1', 'not-installed package was installed (regressed in #932640)');
diag('debi foo.changes will also downgrade existing packages');
purge_packages();
ok(
verbose_run(
['dpkg', '-i', "$tmp/devscripts-test-already-installed_2_all.deb",]));
make_changes(qw(already-installed));
ok(verbose_run([@debi, "$tmp/foo.changes",]), 'plain debi succeeds');
is(version_of('devscripts-test-already-installed'),
'1', 'already installed package was downgraded');
diag('debi --upgrade will upgrade/downgrade existing packages, only');
purge_packages();
ok(
verbose_run(
['dpkg', '-i', "$tmp/devscripts-test-already-installed_2_all.deb",]));
make_changes(qw(already-installed not-installed));
ok(verbose_run([@debi, '--upgrade', "$tmp/foo.changes",]),
'debi --upgrade succeeds');
is(version_of('devscripts-test-already-installed'),
'1', 'already installed package was downgraded');
is(version_of('devscripts-test-not-installed'),
'', 'not-installed package was not installed');
diag('it is OK if debi --upgrade does nothing');
purge_packages();
make_changes(qw(not-installed));
ok(verbose_run([@debi, '--upgrade', "$tmp/foo.changes",]),
'debi --upgrade succeeds');
is(version_of('devscripts-test-not-installed'),
'', 'not-installed package was not installed');
diag('debi without --with-depends does not try to satisfy dependencies');
purge_packages();
ok(
verbose_run(
['dpkg', '-i', "$tmp/devscripts-test-gains-dependency_0_all.deb",]));
make_changes(qw(gains-dependency));
ok(!verbose_run([@debi, "$tmp/foo.changes",]),
'debi without --with-depends does not install dependency');
# It's OK for it to either be unpacked but fail to configure, or be
# left at version 0.
isnt(
version_of('devscripts-test-gains-dependency') . "::"
. status_of('devscripts-test-gains-dependency'),
'1::install ok installed',
'package with a dependency was not installed'
);
is(version_of('hello'), '', 'third party dependency was not installed');
diag('debi --with-depends does satisfy dependencies');
purge_packages();
make_changes(qw(gains-dependency));
ok(verbose_run([@debi, '--with-depends', "$tmp/foo.changes",]),
'debi --with-depends succeeds');
is(version_of('devscripts-test-gains-dependency'),
'1', 'package with a dependency was installed');
isnt(version_of('hello'), '', 'third party dependency was installed');
diag('debi --upgrade --with-depends does satisfy new dependencies');
purge_packages();
ok(
verbose_run(
['dpkg', '-i', "$tmp/devscripts-test-gains-dependency_0_all.deb",]));
make_changes(qw(gains-dependency not-installed));
ok(verbose_run([@debi, '--with-depends', '--upgrade', "$tmp/foo.changes",]),
'debi --with-depends --upgrade succeeds');
is(version_of('devscripts-test-gains-dependency'),
'1', 'package with a dependency was installed');
isnt(version_of('hello'), '', 'third party dependency was installed');
is(version_of('devscripts-test-not-installed'),
'', 'not-installed package was not installed (#932963)');
purge_packages();
verbose_run(
['dpkg', '-i', "$tmp/devscripts-test-gains-local-dependency_0_all.deb",]);
make_changes(qw(dependency gains-local-dependency));
ok(
verbose_run([@debi, '--upgrade', '--with-depends', "$tmp/foo.changes",]),
'corner case from #932963: debi --upgrade --with-depends can cope with '
. 'a new dependency on a binary from the same source'
);
is(version_of('devscripts-test-gains-local-dependency'),
'1', 'the package we wanted to upgrade is upgraded');
is(version_of('devscripts-test-dependency'),
'1',
'the new dependency of the package we wanted to upgrade is installed');
purge_packages();
done_testing;
|