App-FuguVM
view release on metacpan or search on metacpan
t/fuguvm/cli.t view on Meta::CPAN
like($out, qr{7\.8/ports\.tar\.gz\n$}, 'from the source scope');
($code, $out, $err) = _run_captured("--project=$project",
'mirror', 'fetch', 'nonsense.tgz');
is($code, 1, 'a name that no manifest holds is a refusal');
like($err, qr/nonsense\.tgz/, 'and the diagnostic names it');
is($out, '', 'and no path reaches standard output');
# verify removes a failed file in its own scope only, and it
# keeps an unknown file. The two scopes share the name
# base78.tgz: good in the release scope, tampered in the source
# scope.
$project = _signed_project($signer, 'base78.tgz');
my $good = _fake_mirror_file($project, '7.8/arm64/base78.tgz',
'set bytes');
my $bad = _fake_mirror_file($project, '7.8/base78.tgz',
'tampered bytes');
my $unknown = _fake_mirror_file($project, '7.8/arm64/index.txt',
'a listing');
($code, $out, $err) = _run_captured("--project=$project",
'mirror', 'verify');
is($code, 1, 'verify exits 1 when one file failed');
like($err, qr{source/base78\.tgz}, 'the failed line carries the scope');
ok(!-f $bad, 'verify removes the failed file');
ok(-f $good, 'and keeps the same-named file that verified');
ok(-f $unknown, 'and keeps an unknown file');
($code) = _run_captured("--project=$project", 'mirror', 'verify');
is($code, 0, 'a second run over the healed cache exits 0');
# The verify verb proves, whatever the verify directive says: a
# tampered manifest under 'verify no' still fails and leaves the
# cache.
$project = _signed_project($signer, undef, "verify no\n");
my $manifest =
"$project/cache/proxy/cdn.openbsd.org/pub/OpenBSD/7.8/arm64/SHA256";
open my $mh, '>>', $manifest or die $!;
print $mh "# one changed byte\n";
close $mh;
($code, $out, $err) = _run_captured("--project=$project",
'mirror', 'verify');
is($code, 1, 'verify proves under a verify no directive');
like($err, qr{release/SHA256}, 'and reports the failed manifest');
ok(!-f $manifest, 'and the poisoned manifest left the cache');
}
# clear refuses while a VM runs on a disk that the entry backs
SKIP: {
my $has_qemu = `which qemu-img 2>/dev/null`;
skip 'qemu-img not installed', 4 unless $has_qemu;
my $project = _cache_project();
my $cache = App::FuguVM::DiskCache->new("$project/cache");
my $key = '7.8-arm64-cafebabe';
my $source = "$project/source.qcow2";
system('qemu-img', 'create', '-f', 'qcow2', $source, '16M') == 0
or skip 'cannot create a test disk image', 4;
my $base = $cache->store($key, $source, { root_password => 'pw' });
# A working disk in this checkout, backed by the cached entry
my $state_dir = "$project/.fuguvm/state";
App::FuguVM::Disk->new($state_dir)->create('default', undef, $base);
# This test process stands in for a live QEMU
make_path("$state_dir/default");
open my $pidfh, '>', "$state_dir/default/vm.pid" or die $!;
print $pidfh "$$\n";
close $pidfh;
local $SIG{__WARN__} = sub {};
is(App::FuguVM::CLI->run("--project=$project", '--quiet', 'cache', 'clear'),
5, 'clear refuses with EXIT_VM_RUNNING while the VM runs');
ok(defined $cache->lookup($key), 'the entry survives the refusal');
# Once the VM stops, removal proceeds. A warning reports the orphan.
unlink "$state_dir/default/vm.pid";
is(App::FuguVM::CLI->run("--project=$project", '--quiet', 'cache', 'clear'),
0, 'clear proceeds once the VM is stopped');
is($cache->lookup($key), undef, 'the entry is gone');
}
# ============================================================
# Snapshot subcommand
# ============================================================
# Usage and name validation
{
my $project = _cache_project();
local $SIG{__WARN__} = sub {};
is(App::FuguVM::CLI->run("--project=$project", 'snapshot'), 2,
'snapshot without an action returns EXIT_INVALID_ARGS');
is(App::FuguVM::CLI->run("--project=$project", 'snapshot', 'frobnicate'), 2,
'unknown snapshot action returns EXIT_INVALID_ARGS');
is(App::FuguVM::CLI->run("--project=$project", 'snapshot', 'save'), 2,
'snapshot save without a name returns EXIT_INVALID_ARGS');
is(App::FuguVM::CLI->run("--project=$project", 'snapshot', 'save', '../x'),
2, 'a name with a path separator is rejected');
}
# Missing snapshots report a distinct, scriptable exit code. Thus
# callers can do 'snapshot restore || provision-from-scratch'.
{
my $project = _cache_project();
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'restore', 'deps'),
11, 'restoring a missing snapshot returns EXIT_SNAPSHOT_NOT_FOUND');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'rm', 'deps'),
11, 'removing a missing snapshot returns the same code');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'list'),
0, 'listing with nothing cached still succeeds');
}
# Save, restore, and the refusals, over a real backing chain
SKIP: {
my $has_qemu = `which qemu-img 2>/dev/null`;
skip 'qemu-img not installed', 12 unless $has_qemu;
my $project = _cache_project();
my $state_dir = "$project/.fuguvm/state";
my $cache = App::FuguVM::DiskCache->new("$project/cache");
my $key = $cache->key(App::FuguVM::Config->new($project)->load_vm('default'));
my $source = "$project/source.qcow2";
system('qemu-img', 'create', '-f', 'qcow2', $source, '16M') == 0
or skip 'cannot create a test disk image', 12;
# A snapshot of a standalone disk is not possible. Say so. Do
# not crash.
my $disk = App::FuguVM::Disk->new($state_dir);
$disk->create('default', '16M');
my $state = App::FuguVM::State->new($state_dir, 'default');
$state->mark_installed('arm64');
local $SIG{__WARN__} = sub {};
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'save', 'deps'),
1, 'a standalone disk is a diagnosed error, not a crash');
# Rebuild the disk as an overlay on a cached base
my $base = $cache->store($key, $source, { root_password => 'pw' });
unlink $disk->path('default');
$disk->create('default', undef, $base);
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'save', 'deps'),
0, 'save succeeds on a disk backed by a cached image');
ok(defined $cache->snapshot_lookup($key, 'deps'), 'the snapshot exists');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'list'),
0, 'list succeeds');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'restore', 'deps'),
0, 'restore succeeds');
is($disk->backing_file('default'),
$cache->snapshot_path($key, 'deps'),
'restore actually replaced the disk with an overlay on the snapshot');
# Restore from nothing. No disk and no state exist, as in a
# fresh checkout.
unlink $disk->path('default');
unlink "$state_dir/default/status";
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'restore', 'deps'),
0, 'restore works with no disk and no state');
my $reseeded = App::FuguVM::State->new($state_dir, 'default');
ok($reseeded->is_installed, 'restore reseeds installed state');
is($reseeded->get_root_password, 'pw',
'restore reseeds the root password from the base');
# A running VM refuses both save and restore
open my $pidfh, '>', "$state_dir/default/vm.pid" or die $!;
print $pidfh "$$\n";
close $pidfh;
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'save', 'deps'),
5, 'save refuses while the VM is running');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'restore', 'deps'),
5, 'restore refuses while the VM is running');
unlink "$state_dir/default/vm.pid";
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'rm', 'deps'),
0, 'rm succeeds');
}
# --names is the scriptable listing. It writes bare names on stdout,
# where a shell can read them. It does not go through the stderr logger.
SKIP: {
my $has_qemu = `which qemu-img 2>/dev/null`;
skip 'qemu-img not installed', 4 unless $has_qemu;
my $project = _cache_project();
my $cache = App::FuguVM::DiskCache->new("$project/cache");
my $key = $cache->key(App::FuguVM::Config->new($project)->load_vm('default'));
my $source = "$project/source.qcow2";
system('qemu-img', 'create', '-f', 'qcow2', $source, '16M') == 0
or skip 'cannot create a test disk image', 4;
my $base = $cache->store($key, $source, { root_password => 'pw' });
App::FuguVM::Disk->new("$project/.fuguvm/state")
->create('default', undef, $base);
App::FuguVM::State->new("$project/.fuguvm/state", 'default')->mark_installed('arm64');
is(_capture_stdout($project, 'snapshot', 'list', '--names'), '',
'nothing on stdout when there are no snapshots');
App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'save', 'deps-aaa');
App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'save', 'deps-bbb');
is(_capture_stdout($project, 'snapshot', 'list', '--names'),
"deps-aaa\ndeps-bbb\n", 'one bare name per line, sorted');
is(_capture_stdout($project, 'snapshot', 'list'), '',
'the human listing writes nothing to stdout');
local $SIG{__WARN__} = sub {};
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'snapshot', 'list', '--bogus'),
2, 'an unknown list option returns EXIT_INVALID_ARGS');
}
# ============================================================
# Image export
# ============================================================
# Usage errors match the style of the other subcommands
{
my $project = _cache_project();
local $SIG{__WARN__} = sub {};
is(App::FuguVM::CLI->run("--project=$project", '--quiet', 'image'), 2,
'image without an action returns EXIT_INVALID_ARGS');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'image', 'frobnicate'), 2,
'an unknown image action returns EXIT_INVALID_ARGS');
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'image', 'export'), 2,
'image export without a path returns EXIT_INVALID_ARGS');
my ($code, $out, $err) = _run_captured("--project=$project",
'image', 'export', "$project/out.qcow2", '--format=vmdk');
is($code, 2, 'an unknown --format value returns EXIT_INVALID_ARGS');
like($err, qr/qcow2.*raw/, 'and the diagnostic names both formats');
}
# The refusals and the export itself, over a real backing chain
SKIP: {
my $has_qemu = `which qemu-img 2>/dev/null`;
skip 'qemu-img not installed', 15 unless $has_qemu;
my $project = _cache_project();
my $state_dir = "$project/.fuguvm/state";
my $target = "$project/out.qcow2";
local $SIG{__WARN__} = sub {};
# A guest with no disk
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'image', 'export', $target),
1, 'a guest with no disk exits 1');
# A working disk on a cached base
my $cache = App::FuguVM::DiskCache->new("$project/cache");
my $key = $cache->key(
App::FuguVM::Config->new($project)->load_vm('default'));
my $source = "$project/source.qcow2";
system('qemu-img', 'create', '-f', 'qcow2', $source, '16M') == 0
or skip 'cannot create a test disk image', 14;
my $base = $cache->store($key, $source, { root_password => 'pw' });
App::FuguVM::Disk->new($state_dir)->create('default', undef, $base);
App::FuguVM::State->new($state_dir, 'default')->mark_installed('arm64');
# A running guest refuses with 5. The test writes its own
# process ID, so Fugu::Process->is_alive reports a live guest.
make_path("$state_dir/default");
open my $pidfh, '>', "$state_dir/default/vm.pid" or die $!;
print $pidfh "$$\n";
close $pidfh;
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'image', 'export', $target),
5, 'an export of a running guest exits 5');
unlink "$state_dir/default/vm.pid";
# The tool creates no directory for the operator
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'image', 'export', "$project/absent/out.qcow2"),
1, 'an absent parent directory exits 1');
# The export writes the base image and reports it
my ($code, $out) = _run_captured("--project=$project", '--quiet',
'image', 'export', $target);
is($code, 0, 'the export succeeds');
ok(-f $target, 'and the file exists');
like($out, qr/^bytes: [0-9]+$/m, 'the report holds the byte count');
like($out, qr/^format: qcow2$/m, 'and the default format');
like($out, qr/^key: \Q$key\E$/m, 'and the cache key of the source');
like($out, qr/^path: .*out\.qcow2$/m, 'and the written path');
like($out, qr/^source: \Q$base\E$/m, 'and the source image');
like(`qemu-img info "$target"`, qr/file format: qcow2/,
'the written file is a qcow2');
unlike(`qemu-img info "$target"`, qr/backing file/,
'with no backing file');
# An existing target stays unchanged
my $before = -s $target;
is(App::FuguVM::CLI->run("--project=$project", '--quiet',
'image', 'export', $target),
1, 'a second export onto the same path exits 1');
is(-s $target, $before, 'and the file stays unchanged');
# The raw form
($code) = _run_captured("--project=$project", '--quiet',
'image', 'export', "$project/out.raw", '--format=raw');
is($code, 0, 'a raw export succeeds');
like(`qemu-img info "$project/out.raw"`, qr/file format: raw/,
'and writes the raw format');
}
# A byte count for a person to read is presentation, so the CLI owns
# it. The cases came with the function from Fugu::Timeout.
subtest '_format_size' => sub {
my $f = \&App::FuguVM::CLI::_format_size;
is( $f->(0), '0B', 'zero' );
is( $f->(512), '512B', 'bytes' );
is( $f->(1023), '1023B', 'just under 1K' );
is( $f->(1024), '1.0K', 'one kilobyte' );
is( $f->(1536), '1.5K', 'one and a half' );
is( $f->(1024**2), '1.0M', 'one megabyte' );
t/fuguvm/cli.t view on Meta::CPAN
$signer->generate(comment => 'a cli test key',
public => "$project/keys/key.pub",
secret => "$project/keys/key.sec")
or die 'cannot generate a key pair: ', $signer->error, "\n";
rename "$project/keys/key.pub", "$project/keys/openbsd-78-base.pub"
or die "cannot rename the public key: $!\n";
my %manifest = (
'arm64/SHA256' => { 'base78.tgz' => 'set bytes' },
'SHA256' => {
'ports.tar.gz' => 'tree bytes',
(defined $source_extra ? ($source_extra => 'set bytes') : ()),
},
);
for my $rel (sort keys %manifest) {
my $lines = '';
my $files = $manifest{$rel};
for my $name (sort keys %$files) {
my $digest = Digest::SHA->new(256);
$digest->add($files->{$name});
$lines .= sprintf "SHA256 (%s) = %s\n", $name,
$digest->hexdigest;
}
my $path = _fake_mirror_file($project, "7.8/$rel", $lines);
$signer->sign(secret => "$project/keys/key.sec",
file => $path, signature => "$path.sig")
or die "cannot sign $path: ", $signer->error, "\n";
}
return $project;
}
# A cached distfile, seeded on disk like _fake_download
sub _fake_distfile
{
my ($project, $name) = @_;
my $path =
"$project/cache/proxy/cdn.openbsd.org/pub/OpenBSD/distfiles/$name";
$path =~ m{\A(.*)/} and make_path($1);
open my $fh, '>', $path or die $!;
print $fh 'not a real distfile';
close $fh;
return $path;
}
# A complete-looking cache entry without the cost of a real image
sub _fake_entry
{
my ($cache, $key) = @_;
my $dir = $cache->entry_dir($key);
make_path($dir);
open my $bh, '>', "$dir/base.qcow2" or die $!;
print $bh 'not a real image';
close $bh;
open my $mh, '>', "$dir/meta.json" or die $!;
print $mh qq({"key":"$key","created_at":1,"root_password":"pw"});
close $mh;
return $dir;
}
( run in 1.899 second using v1.01-cache-2.11-cpan-f5f21f1076c )