summaryrefslogtreecommitdiffstats
path: root/t/dh_installsystemd/dh_installsystemd.t
blob: bbb4eb0d12086d835e9f6da3dfceea478dbabc37 (plain)
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
#!/usr/bin/perl
use strict;
use Test::More;

use File::Basename qw(dirname);
use lib dirname(dirname(__FILE__));
use Test::DH;
use File::Path qw(remove_tree make_path);
use Debian::Debhelper::Dh_Lib qw(!dirname);

plan(tests => 4);

sub write_file {
	my ($path, $content) = @_;

	my $dir = dirname($path);
	mkdirs($dir);

	open(my $fd, '>>', $path) or error("open($path) failed: $!");
	print {$fd} $content . '\n';
	close($fd) or error("close($path) failed: $!");
}

sub unit_is_enabled {
	my ($package, $unit, $num_enables) = @_;
	my @output;
	my $matches;
	my @postinst_snippets = find_script($package, 'postinst');
	@output=`cat @postinst_snippets` if @postinst_snippets;
	# Match exactly one tab; the "dont-enable" script has an "enable"
	# line for re-enabling the service if the admin had it enabled.
	# But we do not want to include that in our count.
	$matches = grep { m{^\tif deb-systemd-helper .* was-enabled .*'\Q$unit\E\.service'} } @output;
	ok($matches == $num_enables) or diag("$unit appears to have been enabled $matches times (expected $num_enables)");
}

sub unit_is_started {
	my ($package, $unit, $num_starts, $num_stops) = @_;
	my @output;
	my $matches;
	$num_stops = $num_stops // $num_starts;
	my @postinst_snippets = find_script($package, 'postinst');
	@output=`cat @postinst_snippets` if @postinst_snippets;
	$matches = grep { m{deb-systemd-invoke \$_dh_action .*'\Q$unit\E.service'} } @output;
	ok($matches == $num_starts) or diag("$unit appears to have been started $matches times (expected $num_starts)");
	my @prerm_snippets = find_script($package, 'prerm');
	@output=`cat @prerm_snippets` if @prerm_snippets;
	$matches = grep { m{deb-systemd-invoke stop .*'\Q$unit\E.service'} } @output;
	ok($matches == $num_stops) or diag("$unit appears to have been stopped $matches times (expected $num_stops)");
}


#
# Test a simple source package defining a single binary package
#
our $TEST_DH_FIXTURE_DIR = 'simple';
our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
    debian/changelog
    debian/control
    debian/foo.service
));

each_compat_subtest {
	ok(run_dh_tool('dh_installsystemd'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	ok(run_dh_tool('dh_clean'));

	ok(run_dh_tool('dh_installsystemd', '--no-start'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 0, 0);
	ok(run_dh_tool('dh_clean'));

	ok(run_dh_tool('dh_installsystemd', '--no-start', 'foo.service'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 0, 0);
	ok(run_dh_tool('dh_clean'));
};


#
# Test a more complex source package defining three binary packages
#
$TEST_DH_FIXTURE_DIR = '.';
@TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
    debian/changelog
    debian/control
    debian/foo.service
    debian/foo2.service
));

each_compat_subtest {
	ok(run_dh_tool( 'dh_installsystemd'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	unit_is_enabled('foo', 'foo2', 0);
	unit_is_started('foo', 'foo2', 0);
	ok(run_dh_tool('dh_clean'));

	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	ok(run_dh_tool('dh_installsystemd'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 1);
	ok(run_dh_tool('dh_clean'));


	# lib -> usr/lib (if we ever support that)
	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	ok(run_dh_tool('dh_installsystemd'));
	ok(-e 'debian/foo/lib/systemd/system/foo2.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 1);
	ok(run_dh_tool('dh_clean'));

	# lib -> usr/lib with both no-empty (if we ever suport that)
	make_path('debian/foo/lib/systemd/system/');
	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/bar.service');
	ok(run_dh_tool('dh_installsystemd'));
	ok(-e 'debian/foo/lib/systemd/system/bar.service');
	ok(-e 'debian/foo/lib/systemd/system/foo2.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 1);
	ok(run_dh_tool('dh_clean'));

	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	ok(run_dh_tool('dh_installsystemd', '--no-start'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 0, 0);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 0, 0);
	ok(run_dh_tool('dh_clean'));

	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	ok(run_dh_tool('dh_installsystemd', '-p', 'foo', '--no-start', 'foo.service'));
	ok(run_dh_tool('dh_installsystemd', '-p', 'foo', 'foo2.service'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 0, 0);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 1);
	ok(run_dh_tool('dh_clean'));

	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	ok(run_dh_tool('dh_installsystemd', '-p', 'foo', '--no-enable', 'foo.service'));
	ok(run_dh_tool('dh_installsystemd', '-p', 'foo', 'foo2.service'));
	ok(-e 'debian/foo/lib/systemd/system/foo.service');
	ok(find_script('foo', 'postinst'));
	unit_is_enabled('foo', 'foo', 0);
	unit_is_started('foo', 'foo', 1, 1);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 1);
	ok(run_dh_tool('dh_clean'));

	make_path('debian/foo/lib/systemd/system/');
	ok(run_dh_tool('dh_installsystemd', '--no-restart-after-upgrade'));
	my @foo_postinst = find_script('foo', 'postinst');
	ok(@foo_postinst);
	my $matches = @foo_postinst ? grep { m{deb-systemd-invoke start .*foo.service} } `cat @foo_postinst` : -1;
	ok($matches == 1);
	ok(run_dh_tool('dh_clean'));

	# Quoting #764730
	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service');
	ok(run_dh_tool('dh_installsystemd'));
	unit_is_enabled('foo', 'foo\x2dfuse', 1);
	unit_is_started('foo', 'foo\x2dfuse', 1);
	ok(run_dh_tool('dh_clean'));

	# --name flag #870768
	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
	ok(run_dh_tool('dh_installsystemd', '--name=foo'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	unit_is_enabled('foo', 'foo2', 0);
	unit_is_started('foo', 'foo2', 0);
	ok(run_dh_tool('dh_installsystemd', '--name=foo2'));
	unit_is_enabled('foo', 'foo', 1);
	unit_is_started('foo', 'foo', 1);
	unit_is_enabled('foo', 'foo2', 1);
	unit_is_started('foo', 'foo2', 1);
	ok(run_dh_tool('dh_clean'));

	make_path('debian/foo/lib/systemd/system/');
	copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service');
	make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service');
	ok(run_dh_tool('dh_installsystemd'));
	unit_is_enabled('foo', 'foo', 1);
	# Alias= realized by symlinks are not enabled in maintaner scripts
	unit_is_enabled('foo', 'source', 0);
	unit_is_enabled('foo', 'target', 1);
	ok(run_dh_tool('dh_clean'));
};

each_compat_up_to_and_incl_subtest(11, sub {
	make_path('debian/foo/lib/systemd/system/');
	make_path('debian/foo/etc/init.d/');
	copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service');
	make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service');
	write_file('debian/foo/etc/init.d/source', '# something');
	ok(run_dh_tool('dh_installsystemd'));
	unit_is_enabled('foo', 'foo', 1);
	# Alias= realized by symlinks are not enabled in maintaner scripts
	unit_is_enabled('foo', 'source', 0);
	unit_is_enabled('foo', 'target', 1);
	# The presence of a sysvinit script for the alias unit inhibits start of both
	unit_is_started('foo', 'source', 0);
	unit_is_started('foo', 'target', 0);
	ok(run_dh_tool('dh_clean'));
});

each_compat_from_and_above_subtest(12, sub {
	make_path('debian/foo/lib/systemd/system/');
	make_path('debian/foo/etc/init.d/');
	copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service');
	make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service');
	write_file('debian/foo/etc/init.d/source', '# something');
	ok(run_dh_tool('dh_installsystemd'));
	unit_is_enabled('foo', 'foo', 1);
	# Alias= realized by symlinks are not enabled in maintaner scripts
	unit_is_enabled('foo', 'source', 0);
	unit_is_enabled('foo', 'target', 1);
	unit_is_started('foo', 'source', 0);
	unit_is_started('foo', 'target', 1);
	ok(run_dh_tool('dh_clean'));
});