App-FuguVM

 view release on metacpan or  search on metacpan

t/fuguvm/guest.t  view on Meta::CPAN


	my $start = time;
	my $ret = $vm->_bounded(1, sub { sleep 5; return 'late' });
	is($ret, undef, '_bounded returns undef when the deadline elapses');
	ok(time - $start < 4, '_bounded aborts near the deadline, not later');
	ok($vm->{log}{warned}, '_bounded warns on timeout');
}

# The image cache follows the configured cache_dir, which
# App::FuguVM::Config::load_vm injects into the per-VM config.
{
	my $vm = App::FuguVM::Guest->new(config => { cache_dir => '/var/cache/fuguvm' });
	is($vm->_cache_dir, '/var/cache/fuguvm', 'configured cache_dir wins');
	is($vm->_image_cache->installed_dir, '/var/cache/fuguvm/installed',
	    'the image cache uses it too');

	local $ENV{HOME} = '/home/nobody';
	my $bare = App::FuguVM::Guest->new(config => {});
	is($bare->_cache_dir, '/home/nobody/.cache/fuguvm',
	    'a config without cache_dir falls back to the default');
}

# When the cache is off, `up` suppresses restore and save together.
# It derives no key at all. Thus it can neither look one up nor
# publish one.
{
	my $off = App::FuguVM::Guest->new(
		config => { cache_dir => '/var/cache/fuguvm', image_cache => 0 });
	is($off->_image_cache, undef, 'image_cache no disables the cache');

	my $flag = App::FuguVM::Guest->new(
		config   => { cache_dir => '/var/cache/fuguvm' },
		no_cache => 1,
	);
	is($flag->_image_cache, undef, '--no-cache disables the cache');

	my $on = App::FuguVM::Guest->new(
		config => { cache_dir => '/var/cache/fuguvm', image_cache => 1 });
	ok(defined $on->_image_cache, 'image_cache yes leaves it enabled');
}

# Installed-image cache: restore, chain verification, and reparenting.
# These are the parts of `up` that do not need a running QEMU.
SKIP: {
	my $has_qemu = `which qemu-img 2>/dev/null`;
	skip 'qemu-img not installed', 14 unless $has_qemu;

	require App::FuguVM::Disk;
	require App::FuguVM::DiskCache;
	require App::FuguVM::State;

	my $root = tempdir(CLEANUP => 1);
	my $cache = App::FuguVM::DiskCache->new("$root/cache");
	my $key = '7.8-arm64-11223344';

	# A stand-in for a freshly installed disk
	my $installed = "$root/installed.qcow2";
	system('qemu-img', 'create', '-f', 'qcow2', $installed, '64M') == 0
	    or skip 'cannot create a test disk image', 14;
	my $base = $cache->store($key, $installed,
	    { root_password => 'from-the-image' });
	ok(defined $base, 'a base image is available to restore from');

	my $state = App::FuguVM::State->new("$root/state", 'default');
	my $vm = App::FuguVM::Guest->new(
		config => { name => 'default', arch => 'arm64', cache_dir => "$root/cache" },
		state  => $state,
		log    => TestLog->new,
	);

	# Restore: the overlay plus the state that the installation
	# leaves behind
	ok($vm->_cache_restore($cache, $key), 'restore reports a cache hit');
	ok($state->disk_exists, 'the working disk exists after a restore');
	ok($state->is_installed, 'the restored VM is marked installed');
	is($state->get_root_password, 'from-the-image',
	    'the root password comes from the image, not a new install');
	is($state->data->{cached_from}, $key,
	    'state records which cached image it came from');

	my $disk = App::FuguVM::Disk->new("$root/state");
	is($disk->backing_file('default'), $base,
	    'the working disk is an overlay on the cached base');

	# A resolvable chain passes verification
	ok($vm->_verify_backing_chain, 'an intact backing chain verifies');

	# Reparenting a standalone disk onto a base
	my $other = App::FuguVM::State->new("$root/state2", 'default');
	my $vm2 = App::FuguVM::Guest->new(
		config => { name => 'default', arch => 'arm64', cache_dir => "$root/cache" },
		state  => $other,
		log    => TestLog->new,
	);
	App::FuguVM::Disk->new("$root/state2")->create('default', '64M');
	ok($other->disk_exists, 'a standalone disk to reparent');
	ok($vm2->_reparent_disk($base), 'reparent succeeds');
	is(App::FuguVM::Disk->new("$root/state2")->backing_file('default'), $base,
	    'the standalone disk became an overlay');
	ok(!-f $other->disk_path . '.replaced',
	    'no leftover copy of the replaced disk');

	# A missing base is a diagnosed failure, not silent corruption
	chmod 0700, $cache->entry_dir($key);
	unlink $base;
	my $log = TestLog->new;
	$vm->{log} = $log;
	ok(!$vm->_verify_backing_chain, 'a broken chain fails verification');
	like(join("\n", @{ $log->{errors} }), qr/fuguvm destroy/,
	    'and the error names the remedy');

	# A restore against the now-empty cache is a miss, not a crash
	my $fresh = App::FuguVM::State->new("$root/state3", 'default');
	my $vm3 = App::FuguVM::Guest->new(
		config => { name => 'default', arch => 'arm64', cache_dir => "$root/cache" },
		state  => $fresh,
		log    => TestLog->new,
	);
	ok(!$vm3->_cache_restore($cache, $key),
	    'restore misses once the base is gone');
}

# The install media arguments: -no-reboot for an autoinstall run
# with media, and for nothing else
{
	my $auto = App::FuguVM::Guest->new(
	    config => { arch => 'amd64', install_mode => 'autoinstall' });
	is_deeply([$auto->_media_args('/x/miniroot.img')],
	    ['-drive', 'file=/x/miniroot.img,format=raw,if=virtio,readonly=on',
		'-no-reboot'],
	    'an autoinstall run with install media adds -no-reboot');
	is_deeply([$auto->_media_args], [],
	    'the restart with no media adds nothing');

	my $expect = App::FuguVM::Guest->new(
	    config => { arch => 'amd64', install_mode => 'expect' });
	is_deeply([$expect->_media_args('/x/miniroot.img')],
	    ['-drive', 'file=/x/miniroot.img,format=raw,if=virtio,readonly=on'],
	    'the expect mode boots its media without -no-reboot');
}

# The root password of each mode
{
	my $root = tempdir(CLEANUP => 1);
	open my $fh, '>', "$root/password" or die $!;
	print $fh "s3cret\nsecond line\n";
	close $fh;
	chmod 0600, "$root/password" or die $!;

	my $imported = App::FuguVM::Guest->new(
		config => { install_mode => 'import',
		    root_password_file => "$root/password" },
		log => TestLog->new,
	);
	is($imported->_root_password, 's3cret',
	    '_root_password returns the first line, with no newline');

	my $bare = App::FuguVM::Guest->new(
		config => { install_mode => 'autoinstall' },
		log    => TestLog->new,
	);
	is($bare->_root_password, undef,
	    'no root_password_file means no password outside expect');

	my $expect = App::FuguVM::Guest->new(
		config => { install_mode => 'expect' },
		log    => TestLog->new,
	);
	my $generated = $expect->_root_password;
	is(length($generated), 32,
	    '_root_password generates a password in the expect mode');
	isnt($expect->_root_password, $generated, 'a fresh one each call');

	# A password file that the group or others can read gives a
	# warning
	chmod 0644, "$root/password" or die $!;
	my $log = TestLog->new;
	my $loud = App::FuguVM::Guest->new(
		config => { install_mode => 'import',
		    root_password_file => "$root/password" },
		log => $log,
	);
	is($loud->_root_password, 's3cret', 'the password still reads');
	ok($log->{warned}, 'and the tool warns about the file mode');
}

# The bind address and the connect address
{
	my $vm = App::FuguVM::Guest->new(
	    config => { bind_address => '10.0.0.1' });
	is($vm->bind_address, '10.0.0.1',
	    'bind_address returns the configured address');
	is($vm->connect_address, '10.0.0.1',
	    'connect_address returns the bind address');

	my $open = App::FuguVM::Guest->new(
	    config => { bind_address => '0.0.0.0' });
	is($open->connect_address, '127.0.0.1',
	    'connect_address is loopback for 0.0.0.0');

	my $bare = App::FuguVM::Guest->new(config => {});
	is($bare->bind_address, '127.0.0.1',
	    'a config without bind_address falls back to the default');
}

# The resolved ports: the record wins, the configured number serves
# without one, and auto with no record has no port
{
	require App::FuguVM::State;

	my $root = tempdir(CLEANUP => 1);
	my $state = App::FuguVM::State->new("$root/state", 'default');
	my $vm = App::FuguVM::Guest->new(
		config => { name => 'default', ssh_port => 2222,
		    console_port => 4444 },
		state => $state,
	);

	is($vm->ssh_port, 2222,
	    'ssh_port returns the configured number with no record');
	is($vm->console_port, 4444, 'console_port does the same');

	# The record serves while the guest runs
	$state->set_runtime(
	    accel => 'tcg', ssh_port => 2250, console_port => 4470);
	$state->vm_pidfile->write_pid($$);
	is($vm->ssh_port, 2250, 'the recorded port wins while running');
	is($vm->console_port, 4470, 'for both directives');

	# A record that a crash left behind must not read as a live
	# port: the guest is gone, so the configured value serves.
	$state->clear_vm_pid;
	is($vm->ssh_port, 2222,
	    'a stale record does not serve a stopped guest');

	my $fleet = App::FuguVM::Guest->new(
		config => { name => 'default', ssh_port => 'auto',
		    console_port => 'auto' },
		state => $state,
	);
	is($fleet->ssh_port, undef,
	    'auto reports no port for a stopped guest, stale record or not');
	is($fleet->console_port, undef, 'for both directives');

t/fuguvm/guest.t  view on Meta::CPAN

}

# The status report holds every key, for a stopped guest and for a
# guest with a record
{
	require App::FuguVM::State;

	my @keys = qw(accel arch bind_address console_port disk_exists
	    installed name pid proxy_url ssh_port state);

	my $root = tempdir(CLEANUP => 1);
	my $state = App::FuguVM::State->new("$root/state", 'default');
	my $vm = App::FuguVM::Guest->new(
		config => { name => 'default', arch => 'amd64',
		    ssh_port => 'auto', console_port => 'auto',
		    bind_address => '127.0.0.1',
		    cache_dir => "$root/cache" },
		state => $state,
	);

	my $status = $vm->status;
	for my $key (@keys) {
		ok(exists $status->{$key}, "a stopped guest reports $key");
	}
	is($status->{state}, 'stopped', 'the guest is stopped');
	is($status->{ssh_port}, undef, 'auto with no record has no port');
	is($status->{bind_address}, '127.0.0.1',
	    'status reports the bind address');
	is($status->{proxy_url}, undef,
	    'proxy_url is empty while the proxy does not run');

	$state->set_runtime(
	    accel => 'tcg', ssh_port => 2255, console_port => 4455);
	$state->vm_pidfile->write_pid($$);
	$status = $vm->status;
	for my $key (@keys) {
		ok(exists $status->{$key}, "a running guest reports $key");
	}
	is($status->{ssh_port}, 2255, 'status reports the recorded port');
	is($status->{console_port}, 4455, 'for both directives');
	is($status->{accel}, 'tcg', 'status reports the recorded accelerator');
	$state->clear_vm_pid;

	# status reports proxy_url from guest_url: the record of the
	# proxy plus a live child. This test process stands in for the
	# child.
	$state->store->set(proxy_port => 8123);
	$state->proxy_pidfile->write_pid($$);
	is($vm->status->{proxy_url}, 'http://10.0.2.2:8123',
	    'status reports proxy_url from guest_url');
	$state->proxy_pidfile->remove;
}

# The expect install carries the verify flag, as the word that the
# script reads by position
{
	my $on = App::FuguVM::Guest->new(
	    config => { name => 'default', arch => 'arm64' });
	my $config = $on->_install_config('secret', 'http://10.0.2.2:8080');
	is($config->{verify}, 'yes', 'verify defaults to yes');
	is($config->{root_password}, 'secret', 'the password rides along');
	is($config->{proxy_url}, 'http://10.0.2.2:8080', 'and the proxy URL');

	my $off = App::FuguVM::Guest->new(
	    config => { name => 'default', arch => 'arm64', verify => 0 });
	is($off->_install_config('secret', undef)->{verify}, 'no',
	    'verify 0 reads as the word no');
	is($off->_install_config('secret', undef)->{proxy_url}, 'none',
	    'and no proxy reads as none');
}

# up sets FUGUVM_DISTFILE_LIMIT from the configuration before it
# starts the proxy, so the spawned child inherits the cap
{
	require App::FuguVM::State;

	my $root = tempdir(CLEANUP => 1);
	my $state = App::FuguVM::State->new("$root/state", 'default');
	my $vm = App::FuguVM::Guest->new(
		config => { name => 'default', arch => 'arm64',
		    version => '7.8', cache_dir => "$root/cache",
		    distfile_cache => 4096 },
		state => $state,
		log   => TestLog->new,
	);

	# The stand-in start records the environment at spawn time and
	# starts nothing.
	my $seen;
	{
		no warnings 'redefine';
		local *Fugu::Proxy::start = sub ($self) {
			$seen = $ENV{FUGUVM_DISTFILE_LIMIT};
			return 8080;
		};
		local $ENV{FUGUVM_DISTFILE_LIMIT};
		ok(defined $vm->_ensure_proxy, '_ensure_proxy starts the proxy');
	}
	is($seen, 4096, 'the cap reaches the environment before the spawn');

	# The proxy cache of the guest carries the same cap
	is($vm->_proxy->cache->distfile_limit, 4096,
	    'the proxy cache of the guest carries the cap');

	# The mirror of the guest builds over the configured version
	# and architecture
	my $mirror = $vm->_mirror;
	is($mirror->url('miniroot78.img'),
	    'https://cdn.openbsd.org/pub/OpenBSD/7.8/arm64/miniroot78.img',
	    '_mirror builds over the configured version and architecture');

	# The mirror reports the fetch line of GST-MIRROR-5 through the
	# logger of the guest, so --quiet reaches that line. A mirror
	# without the argument would hold the process default instead.
	is($mirror->{log}, $vm->{log}, 'and it passes the logger of the guest');
}

done_testing();

# _flock_in_child($path):
#	Try a non-blocking exclusive flock on $path from a child



( run in 0.942 second using v1.01-cache-2.11-cpan-f5f21f1076c )