Debian Codegraph @

lib/t/DebianLinux.t

raw
1 use strict;
2 use warnings;
3 use Test;
4 use Text::Glob ();
5
6 # Mock the glob function used in image_list()
7 my @glob_filenames;
8 BEGIN {
9 no strict 'refs';
10
11 *CORE::GLOBAL::glob = sub : prototype(_;) {
12 if ($#glob_filenames >= 0) {
13 my $pattern = ($#_ >= 0 ? $_[0] : $_);
14 return Text::Glob::match_glob($pattern, @glob_filenames);
15 } else {
16 return CORE::glob(@_);
17 }
18 }
19 }
20
21 use DebianLinux qw(version_cmp read_kernelimg_conf image_list);
22
23 BEGIN {
24 plan test => 45;
25 }
26
27 ## version_cmp
28
29 # Simple numeric comparison
30 ok(version_cmp('2', '2'), 0);
31 ok(version_cmp('2', '3'), -1);
32 ok(version_cmp('3', '2'), 1);
33 # Multiple components
34 ok(version_cmp('2.6.32', '2.6.32'), 0);
35 ok(version_cmp('2.6.32', '2.6.33'), -1);
36 ok(version_cmp('2.6.33', '2.6.32'), 1);
37 # Extra components (non-numeric, non-pre-release) > null
38 ok(version_cmp('2.6.32-local', '2.6.32-local'), 0);
39 ok(version_cmp('2.6.32', '2.6.32-local'), -1);
40 ok(version_cmp('2.6.32-local', '2.6.32'), 1);
41 # Extra numeric components > null
42 ok(version_cmp('2.6.32', '2.6.32.1'), -1);
43 ok(version_cmp('2.6.32.1', '2.6.32'), 1);
44 ok(version_cmp('2.6.32', '2.6.32-1'), -1);
45 ok(version_cmp('2.6.32-1', '2.6.32'), 1);
46 # Extra pre-release components < null
47 ok(version_cmp('2.6.33-rc1', '2.6.33-rc1'), 0);
48 ok(version_cmp('2.6.33-rc1', '2.6.33'), -1);
49 ok(version_cmp('2.6.33', '2.6.33-rc1'), 1);
50 ok(version_cmp('2.6.33-trunk', '2.6.33-trunk'), 0);
51 ok(version_cmp('2.6.33-rc1', '2.6.33-trunk'), -1);
52 ok(version_cmp('2.6.33-trunk', '2.6.33'), -1);
53 # Pre-release < numeric
54 ok(version_cmp('2.6.32-1', '2.6.32-trunk'), 1);
55 ok(version_cmp('2.6.32-trunk', '2.6.32-1'), -1);
56 # Pre-release < non-numeric non-pre-release
57 ok(version_cmp('2.6.32-local', '2.6.32-trunk'), 1);
58 ok(version_cmp('2.6.32-trunk', '2.6.32-local'), -1);
59 # Pre-release cases including flavour (#761614)
60 ok(version_cmp('2.6.33-trunk-flavour', '2.6.33-trunk-flavour'), 0);
61 ok(version_cmp('2.6.33-rc1', '2.6.33-trunk-flavour'), -1);
62 ok(version_cmp('2.6.33-rc1-flavour', '2.6.33-trunk-flavour'), -1);
63 ok(version_cmp('2.6.32-1-flavour', '2.6.32-trunk-flavour'), 1);
64 ok(version_cmp('2.6.32-trunk-flavour', '2.6.32-1-flavour'), -1);
65 ok(version_cmp('2.6.32-local', '2.6.32-trunk-flavour'), 1);
66 ok(version_cmp('2.6.32-trunk-flavour', '2.6.32-local'), -1);
67 # Numeric < non-numeric non-pre-release
68 ok(version_cmp('2.6.32-1', '2.6.32-local'), -1);
69 ok(version_cmp('2.6.32-local', '2.6.32-1'), 1);
70 # Hyphen < dot
71 ok(version_cmp('2.6.32-2', '2.6.32.1'), -1);
72 ok(version_cmp('2.6.32.1', '2.6.32-2'), 1);
73
74 ## read_kernelimg_conf
75
76 sub read_kernelimg_conf_str {
77 use File::Temp ();
78
79 my $str = shift;
80
81 my $fh = File::Temp->new() or die "$!";
82 $fh->print($str) or die "$!";
83 $fh->close();
84
85 return read_kernelimg_conf($fh->filename);
86 }
87
88 sub hash_equal {
89 my ($left, $right) = @_;
90
91 # 'Smart equality' only compares keys
92 return 0 unless %$left ~~ %$right;
93
94 for my $key (keys(%$left)) {
95 die "hash is too complex" unless (ref($left->{$key}) eq '' &&
96 ref($right->{$key}) eq '');
97 return 0 unless $left->{$key} eq $right->{$key};
98 }
99
100 return 1;
101 }
102
103 # Empty config
104 ok(hash_equal(read_kernelimg_conf_str(''),
105 {
106 do_symlinks => 1,
107 image_dest => '/',
108 }));
109 # Sample config
110 ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
111 # This is a sample /etc/kernel-img.conf file
112 # See kernel-img.conf(5) for details
113
114 # If you want the symbolic link (or image, if move_image is set) to be
115 # stored elsewhere than / set this variable to the dir where you
116 # want the symbolic link. Please note that this is not a Boolean
117 # variable. This may be of help to loadlin users, who may set both
118 # this and move_image. Defaults to /. This can be used in conjunction
119 # with all above options except link_in_boot, which would not make
120 # sense. (If both image_dest and link_in_boot are set, link_in_boot
121 # overrides).
122 image_dest = /
123
124 # This option manipulates the build link created by recent kernels. If
125 # the link is a dangling link, and if a the corresponding kernel
126 # headers appear to have been installed on the system, a new symlink
127 # shall be created to point to them.
128 #relink_build_link = YES
129
130 # If set, the preinst shall silently try to move /lib/modules/version
131 # out of the way if it is the same version as the image being
132 # installed. Use at your own risk.
133 #clobber_modules = NO
134
135 # If set, does not prompt to continue after a depmod problem in the
136 # postinstall script. This facilitates automated installs, though it
137 # may mask a problem with the kernel image. A diag‐ nostic is still
138 # issued. This is unset be default.
139 # ignore_depmod_err = NO
140
141 # These setting are for legacy postinst scripts only. newer postinst
142 # scripts from the kenrel-package do not use them
143 do_symlinks = yes
144 do_bootloader = no
145 do_initrd=yes
146 link_in_boot=no
147 EOT
148 {
149 do_symlinks => 1,
150 image_dest => '/',
151 }));
152 # Slightly different spacing and value syntax
153 ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
154 image_dest = foo bar
155 relink_build_link = yes
156 do_symlinks = 0
157 link_in_boot= false
158 no_symlinks=1
159 EOT
160 {
161 do_symlinks => 0,
162 image_dest => 'foo',
163 }));
164 # Check that 'false' and 'no' also work
165 ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
166 do_symlinks = false
167 EOT
168 {
169 do_symlinks => 0,
170 image_dest => '/',
171 }));
172 ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
173 do_symlinks = no
174 EOT
175 {
176 do_symlinks => 0,
177 image_dest => '/',
178 }));
179 # Check that invalid values have no effect
180 ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
181 do_symlinks=
182 link_in_boot yes
183 link_in_boot 1
184 EOT
185 {
186 do_symlinks => 1,
187 image_dest => '/',
188 }));
189 # Check link_in_boot dominates image_dest
190 ok(hash_equal(read_kernelimg_conf_str(<< 'EOT'),
191 image_dest = /local
192 link_in_boot = true
193 EOT
194 {
195 do_symlinks => 1,
196 image_dest => '/boot',
197 }));
198
199 ## image_list
200
201 @glob_filenames = qw(/boot/ipxe.efi /boot/initrd.img-4.19.0-4-amd64
202 /boot/lost+found /boot/.. /boot/System.map-4.19.0-4-amd64
203 /boot/config-4.19.0-3-amd64 /boot/vmlinuz-4.19.0-4-amd64
204 /boot/initrd.img-4.19.0-3-amd64 /boot/grub
205 /boot/vmlinuz-4.19.0-3-amd64 /boot/efi /boot/ipxe.lkrn
206 /boot/. /boot/config-4.19.0-4-amd64
207 /boot/System.map-4.19.0-3-amd64);
208 ok([image_list()] ~~
209 [['4.19.0-4-amd64', '/boot/vmlinuz-4.19.0-4-amd64'],
210 ['4.19.0-3-amd64', '/boot/vmlinuz-4.19.0-3-amd64']]);
211 @glob_filenames = qw(/boot/.. /boot/vmlinux /boot/vmlinux.old
212 /boot/initrd.img-4.9.0-7-powerpc64le
213 /boot/vmlinux-4.9.0-7-powerpc64le
214 /boot/System.map-4.9.0-7-powerpc64le /boot/initrd.img.old
215 /boot/System.map-4.9.0-6-powerpc64le /boot/.
216 /boot/config-4.9.0-7-powerpc64le
217 /boot/System.map-4.9.0-8-powerpc64le
218 /boot/vmlinux-4.9.0-8-powerpc64le
219 /boot/initrd.img-4.9.0-6-powerpc64le
220 /boot/vmlinux-4.9.0-6-powerpc64le
221 /boot/initrd.img-4.9.0-8-powerpc64le
222 /boot/config-4.9.0-8-powerpc64le /boot/grub /boot/initrd.img
223 /boot/config-4.9.0-6-powerpc64le);
224 ok([image_list()] ~~
225 [['4.9.0-7-powerpc64le', '/boot/vmlinux-4.9.0-7-powerpc64le'],
226 ['4.9.0-8-powerpc64le', '/boot/vmlinux-4.9.0-8-powerpc64le'],
227 ['4.9.0-6-powerpc64le', '/boot/vmlinux-4.9.0-6-powerpc64le']]);
228 @glob_filenames = qw(/boot/vmlinux-4.19.0-3-m68k
229 /boot/vmlinux-4.19.0-4-m68k /boot/vmlinuz-4.1.0-2-m68k
230 /boot/config-4.19.0-3-m68k /boot/config-4.19.0-4-m68k
231 /boot/config-4.1.0-2-m68k /boot/initrd.img-4.19.0-3-m68k
232 /boot/initrd.img-4.19.0-4-m68k /boot/initrd.img-4.1.0-2-m68k
233 /boot/System.map-4.19.0-3-m68k /boot/System.map-4.19.0-4-m68k
234 /boot/System.map-4.1.0-2-m68k);
235 ok([image_list()] ~~
236 [['4.19.0-3-m68k', '/boot/vmlinux-4.19.0-3-m68k'],
237 ['4.19.0-4-m68k', '/boot/vmlinux-4.19.0-4-m68k'],
238 ['4.1.0-2-m68k', '/boot/vmlinuz-4.1.0-2-m68k']]);
239 @glob_filenames = qw(/boot/config-4.16.0-2-amd64 /boot/config-4.17.0-1-amd64
240 /boot/initrd.img-4.16.0-2-amd64 /boot/initrd.img-4.17.0-1-amd64
241 /boot/System.map-4.16.0-2-amd64 /boot/System.map-4.17.0-1-amd64
242 /boot/vmlinuz-4.16.0-2-amd64 /boot/vmlinuz-4.16.0-2-amd64.sig
243 /boot/vmlinuz-4.17.0-1-amd64 /boot/vmlinuz-4.17.0-1-amd64.sig);
244 ok([image_list()] ~~
245 [['4.16.0-2-amd64', '/boot/vmlinuz-4.16.0-2-amd64'],
246 ['4.17.0-1-amd64', '/boot/vmlinuz-4.17.0-1-amd64']]);
247
248 # Disable mocking
249 @glob_filenames = ();