diff --git a/xCAT-test/unit/imgutils_debootstrap_finished.t b/xCAT-test/unit/imgutils_debootstrap_finished.t new file mode 100644 index 0000000000..bd7138308a --- /dev/null +++ b/xCAT-test/unit/imgutils_debootstrap_finished.t @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +use File::Slurper qw(write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; +use imgutils; + +sub image_root { + my (@files) = @_; + my $root = tempdir(CLEANUP => 1) . '/rootimg'; + make_path($root); + for my $file (@files) { + if ($file =~ m{/$}) { + make_path("$root/$file"); + } else { + make_path("$root/" . ($file =~ s{/[^/]+$}{}r)); + write_text("$root/$file", ''); + } + } + return $root; +} + +is(imgutils::debootstrap_finished(tempdir(CLEANUP => 1) . '/rootimg'), 0, + 'a missing root image needs debootstrap'); +is(imgutils::debootstrap_finished(image_root('etc/fstab')), 0, + 'a root image with only the dummy fstab of genimage needs debootstrap'); +is(imgutils::debootstrap_finished(image_root('var/lib/dpkg/')), 0, + 'a root image without a dpkg status file needs debootstrap'); +is(imgutils::debootstrap_finished(image_root('etc/fstab', 'var/lib/dpkg/status')), 1, + 'a root image that debootstrap finished is reused'); + +for my $case ( + [ 'debootstrap/debootstrap.log', 'var/lib/dpkg/status' ], + [ 'debootstrap/' ], +) { + my $root = image_root(@$case); + my $finished = eval { imgutils::debootstrap_finished($root) }; + is($finished, undef, "an unfinished root image with @$case stops genimage"); + like($@, qr/^Error: \Q$root\E holds an unfinished debootstrap\. Remove \Q$root\E /, + "the error for a root image with @$case names the root image to remove"); +} + +done_testing(); diff --git a/xCAT-test/unit/imgutils_host_resolver.t b/xCAT-test/unit/imgutils_host_resolver.t new file mode 100644 index 0000000000..761e46024e --- /dev/null +++ b/xCAT-test/unit/imgutils_host_resolver.t @@ -0,0 +1,77 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; +use imgutils; + +plan skip_all => 'the build host has no /etc/hosts or /etc/resolv.conf' + unless -r '/etc/hosts' && -r '/etc/resolv.conf'; + +my %host = map { $_ => read_text("/etc/$_") } qw(hosts resolv.conf); +my $stub = '../run/systemd/resolve/stub-resolv.conf'; +my $dummy = '#Dummy resolv.conf to make boot cleaner'; + +# A root image as an earlier genimage run leaves it: resolv.conf links to the stub that holds the dummy. +sub image_root { + my $root = tempdir(CLEANUP => 1); + make_path("$root/etc", "$root/run/systemd/resolve"); + write_text("$root/etc/hosts", "127.0.0.1 localhost\n"); + write_text("$root/run/systemd/resolve/stub-resolv.conf", $dummy); + symlink($stub, "$root/etc/resolv.conf") or die "symlink: $!"; + return $root; +} + +sub has_host_files { + my ($root, $label) = @_; + for my $file (qw(hosts resolv.conf)) { + ok(!-l "$root/etc/$file", "$label: etc/$file is a file, not a link"); + is(read_text("$root/etc/$file"), $host{$file}, "$label: etc/$file holds the file of the build host"); + } + return; +} + +{ + my $root = image_root(); + for my $pass (1, 2) { + imgutils::use_host_resolver($root); + has_host_files($root, "pass $pass"); + is(read_text("$root/etc/hosts.bak"), "127.0.0.1 localhost\n", "pass $pass: hosts.bak holds the hosts of the image"); + is(readlink("$root/etc/resolv.conf.bak"), $stub, "pass $pass: resolv.conf.bak is the link of the image"); + } + is(read_text("$root/run/systemd/resolve/stub-resolv.conf"), $dummy, 'the stub that the link names is not written'); +} + +{ + my $root = image_root(); + my $outside = tempdir(CLEANUP => 1) . '/stub-resolv.conf'; + write_text($outside, "nameserver 192.0.2.1\n"); + unlink("$root/etc/resolv.conf"); + symlink($outside, "$root/etc/resolv.conf") or die "symlink: $!"; + imgutils::use_host_resolver($root); + is(readlink("$root/etc/resolv.conf.bak"), $outside, 'an absolute link is moved aside, not followed'); + + # A package script can turn resolv.conf back into a link while the .bak files exist. + unlink("$root/etc/resolv.conf"); + symlink($outside, "$root/etc/resolv.conf") or die "symlink: $!"; + imgutils::use_host_resolver($root); + has_host_files($root, 'after resolv.conf is linked again'); + is(readlink("$root/etc/resolv.conf.bak"), $outside, 'after resolv.conf is linked again: resolv.conf.bak still holds the link of the image'); + is(read_text($outside), "nameserver 192.0.2.1\n", 'a file outside the image is never written through a link'); +} + +{ + my $root = image_root(); + unlink("$root/etc/hosts"); + imgutils::use_host_resolver($root); + has_host_files($root, 'an image without hosts'); + ok(!-e "$root/etc/hosts.bak", 'an image without hosts gets no hosts.bak'); +} + +done_testing(); diff --git a/xCAT-test/unit/imgutils_reused_root.t b/xCAT-test/unit/imgutils_reused_root.t new file mode 100644 index 0000000000..dc7971e962 --- /dev/null +++ b/xCAT-test/unit/imgutils_reused_root.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; +use imgutils; + +plan skip_all => 'the build host has no /etc/hosts or /etc/resolv.conf' + unless -r '/etc/hosts' && -r '/etc/resolv.conf'; + +sub sources { + my ($dir) = @_; + opendir(my $dh, $dir) or die "opendir $dir: $!"; + my @files = sort grep { !/^\./ } readdir($dh); + closedir($dh); + return \@files; +} + +my $root = tempdir(CLEANUP => 1); +my $sources = "$root/etc/apt/sources.list.d"; +my $site = "deb http://192.0.2.2/site ./\n"; +make_path($sources); +write_text("$sources/$_", "deb http://192.0.2.1/retired ./\n") for qw(genimage.apt.list genimage1.apt.list); +write_text("$sources/site.list", $site); +write_text("$root/etc/hosts", "127.0.0.1 localhost\n"); +write_text("$root/etc/resolv.conf", '#Dummy resolv.conf to make boot cleaner'); + +for my $pass (1, 2) { + imgutils::prepare_reused_root($root); + is_deeply(sources($sources), ['site.list'], "pass $pass: only the apt sources that genimage writes are removed"); + is(read_text("$sources/site.list"), $site, "pass $pass: a site source is kept as it is"); + is(read_text("$root/etc/resolv.conf"), read_text('/etc/resolv.conf'), + "pass $pass: the image gets the resolv.conf of the build host"); + is(read_text("$root/etc/resolv.conf.bak"), '#Dummy resolv.conf to make boot cleaner', + "pass $pass: resolv.conf.bak holds the resolv.conf of the image"); +} + +{ + my $bare = tempdir(CLEANUP => 1); + make_path("$bare/etc"); + imgutils::prepare_reused_root($bare); + is(read_text("$bare/etc/hosts"), read_text('/etc/hosts'), 'an image without apt sources still gets the hosts of the build host'); +} + +done_testing(); diff --git a/xCAT-test/unit/imgutils_reused_root_release.t b/xCAT-test/unit/imgutils_reused_root_release.t new file mode 100644 index 0000000000..04276341e6 --- /dev/null +++ b/xCAT-test/unit/imgutils_reused_root_release.t @@ -0,0 +1,105 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use FindBin; +use File::Find; +use File::Path qw(make_path); +use File::Slurper qw(write_text); +use File::Temp qw(tempdir); +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use lib "$FindBin::Bin/../../xCAT-server/share/xcat/netboot/imgutils"; +use Test::More; +use imgutils; + +# A finished root image with the os-release and the dpkg status that Ubuntu writes. +sub image_root { + my (%overrides) = @_; + my %image = (codename => 'resolute', arch => 'riscv64', field => 'VERSION_CODENAME', %overrides); + my $root = tempdir(CLEANUP => 1); + make_path("$root/usr/lib", "$root/etc", "$root/var/lib/dpkg"); + write_text("$root/usr/lib/os-release", "NAME=\"Ubuntu\"\nVERSION_ID=\"26.04\"\n$image{field}=$image{codename}\nID=ubuntu\n") + if defined $image{codename}; + symlink('../usr/lib/os-release', "$root/etc/os-release") or die "symlink: $!"; + write_text("$root/var/lib/dpkg/status", + "Package: debconf\nStatus: install ok installed\nArchitecture: all\n\n" + . "Package: dpkg\nEssential: yes\nStatus: install ok installed\nArchitecture: $image{arch}\nVersion: 1.23.5ubuntu1\n\n" + . "Package: dpkg-dev\nStatus: install ok installed\nArchitecture: all\n"); + return $root; +} + +sub snapshot { + my ($root) = @_; + my @entries; + find({ wanted => sub { push @entries, join(' ', $File::Find::name, (lstat)[1, 2, 7, 9]) }, no_chdir => 1 }, $root); + return [sort @entries]; +} + +for my $case ( + [ 'resolute', 'riscv64', 'the release and architecture that built the image' ], + [ 'resolute-updates', 'riscv64', 'the updates pocket of that release' ], + [ 'resolute-security', 'riscv64', 'the security pocket of that release' ], +) { + my ($suite, $arch, $label) = @$case; + is(eval { imgutils::check_reused_root(image_root(), $suite, $arch) }, 1, "a root image is reused for $label") or diag($@); +} + +{ + my $root = image_root(codename => '"resolute"'); + is(eval { imgutils::check_reused_root($root, 'resolute', 'riscv64') }, 1, 'a quoted VERSION_CODENAME is read') or diag($@); +} + +{ + my $root = image_root(codename => 'xenial', arch => 'amd64', field => 'UBUNTU_CODENAME'); + is(eval { imgutils::check_reused_root($root, 'xenial', 'amd64') }, 1, 'an Ubuntu 16.04 image with only UBUNTU_CODENAME is reused') or diag($@); +} + +{ + # Ubuntu 14.04 names its release only in lsb-release. + my $root = image_root(codename => undef, arch => 'amd64'); + unlink("$root/etc/os-release"); + write_text("$root/etc/os-release", "NAME=\"Ubuntu\"\nVERSION_ID=\"14.04\"\nID=ubuntu\n"); + write_text("$root/etc/lsb-release", "DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=14.04\nDISTRIB_CODENAME=trusty\n"); + is(eval { imgutils::check_reused_root($root, 'trusty', 'amd64') }, 1, 'an Ubuntu 14.04 image with only DISTRIB_CODENAME is reused') or diag($@); + is(eval { imgutils::check_reused_root($root, 'xenial', 'amd64') }, undef, 'an Ubuntu 14.04 image is not reused for Ubuntu 16.04'); + like($@, qr/holds Ubuntu trusty for amd64, not xenial for amd64/, 'the error names the release of the Ubuntu 14.04 image'); +} + +for my $case ( + [ [], 'noble', 'riscv64', qr/holds Ubuntu resolute for riscv64, not noble for riscv64\. Remove /, 'another release' ], + [ [], 'resolute', 'amd64', qr/holds Ubuntu resolute for riscv64, not resolute for amd64\. Remove /, 'another architecture' ], + [ [ codename => undef ], 'resolute', 'riscv64', qr/cannot tell the Ubuntu release and architecture/, 'no os-release' ], + [ [ arch => '' ], 'resolute', 'riscv64', qr/cannot tell the Ubuntu release and architecture/, 'no dpkg architecture' ], +) { + my ($image, $suite, $arch, $error, $label) = @$case; + my $root = image_root(@$image); + my $before = snapshot($root); + my $result = eval { imgutils::check_reused_root($root, $suite, $arch) }; + is($result, undef, "a root image is not reused for $label"); + like($@, $error, "the error for $label names the mismatch"); + is_deeply(snapshot($root), $before, "a root image refused for $label is left as it was"); +} + +{ + my $root = image_root(); + make_path("$root/bin"); + write_text("$root/bin/uname-binary", "uname\n"); + my $before = snapshot($root); + is(eval { imgutils::check_reused_root($root, 'resolute', 'riscv64') }, undef, + 'a root image with the uname backup of an interrupted run is not reused'); + like($@, qr/holds bin\/uname-binary, which an interrupted genimage run left\. Restore bin\/uname from it or remove /, + 'the error says how to recover the uname of the image'); + is_deeply(snapshot($root), $before, 'a root image with the uname backup is left as it was'); +} + +{ + # An absolute link would read the os-release of the build host, which can name the same release. + my $root = image_root(codename => undef); + my $host = tempdir(CLEANUP => 1) . '/os-release'; + write_text($host, "VERSION_CODENAME=resolute\n"); + unlink("$root/etc/os-release"); + symlink($host, "$root/etc/os-release") or die "symlink: $!"; + is(eval { imgutils::check_reused_root($root, 'resolute', 'riscv64') }, undef, 'an os-release link that leaves the image is not followed'); +} + +done_testing();