App-FuguVM

 view release on metacpan or  search on metacpan

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

    like($config->error, qr/qemu_version/, 'and error names the directive');
}

# The fixed ports of every VM declaration, for the port probe
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $homedir = tempdir(CLEANUP => 1);
    local $ENV{HOME} = $homedir;
    make_path("$tmpdir/.fuguvm/vms");

    open my $gh, '>', "$homedir/.fuguvmrc";
    print $gh "vm \"global\" {\n";
    print $gh "    ssh_port     2400\n";
    print $gh "    console_port 4600\n";
    print $gh "}\n";
    close $gh;

    open my $fh, '>', "$tmpdir/.fuguvmrc";
    print $fh "vm \"pinned\" {\n";
    print $fh "    ssh_port     2300\n";
    print $fh "    console_port 4500\n";
    print $fh "}\n";
    print $fh "vm \"bare\" {\n}\n";
    print $fh "vm \"fleet\" {\n";
    print $fh "    ssh_port     auto\n";
    print $fh "    console_port auto\n";
    print $fh "}\n";
    close $fh;

    open my $vh, '>', "$tmpdir/.fuguvm/vms/filed.conf";
    print $vh "ssh_port = 2500\n";
    close $vh;

    my $config = App::FuguVM::Config->new($tmpdir);
    my $ports = $config->declared_ports;

    ok($ports->{2300} && $ports->{4500},
	'declared_ports holds the fixed ports of a project block');
    ok($ports->{2400} && $ports->{4600},
	'and the fixed ports of a global block');
    ok($ports->{2222} && $ports->{4444},
	'a declaration that omits a directive holds the default port');
    ok($ports->{2500}, 'a vms/ file counts too');
    ok(!$ports->{auto}, 'the word auto is not a port');

    is_deeply($config->load_vm('fleet')->{declared_ports}, $ports,
	'load_vm folds the set into the per-VM configuration');
}

# The three image-lifecycle directives: the resolution, the derived
# install_mode, and each refusal at the configuration boundary
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $homedir = tempdir(CLEANUP => 1);
    local $ENV{HOME} = $homedir;
    make_path("$tmpdir/.fuguvm/vms");

    # The files that the directives point at
    for my $seed (["$tmpdir/install.conf", "System hostname = image\n"],
	["$tmpdir/base.qcow2", 'not a real image'],
	["$homedir/password", "secret\n"]) {
	open my $fh, '>', $seed->[0] or die $!;
	print $fh $seed->[1];
	close $fh;
    }

    my $write = sub ($body) {
	open my $fh, '>', "$tmpdir/.fuguvmrc" or die $!;
	print $fh $body;
	close $fh;
	return App::FuguVM::Config->new($tmpdir);
    };

    # install_mode derives from the directives; there is no
    # install_mode directive
    my $config = $write->("vm \"plain\" {\n}\n");
    is($config->load_vm('plain')->{install_mode}, 'expect',
	'no directive derives the expect mode');

    $config = $write->("vm \"auto\" {\n    autoinstall install.conf\n}\n");
    my $vm = $config->load_vm('auto');
    is($vm->{install_mode}, 'autoinstall',
	'autoinstall derives the autoinstall mode');
    is($vm->{autoinstall}, "$tmpdir/install.conf",
	'a relative path resolves against the project root');

    $config = $write->("vm \"import\" {\n    base_disk base.qcow2\n}\n");
    $vm = $config->load_vm('import');
    is($vm->{install_mode}, 'import', 'base_disk derives the import mode');
    is($vm->{base_disk}, "$tmpdir/base.qcow2",
	'and its path resolves the same way');

    $config = $write->(
	"vm \"tilde\" {\n    root_password_file ~/password\n}\n");
    is($config->load_vm('tilde')->{root_password_file},
	"$homedir/password", 'a leading tilde expands');

    # An absent file is a refusal that names the resolved path
    $config = $write->("vm \"gone\" {\n    autoinstall absent.conf\n}\n");
    is($config->load_vm('gone'), undef,
	'an absent autoinstall file makes load_vm return undef');
    like($config->error, qr{\Q$tmpdir/absent.conf\E},
	'and error names the resolved path');

    $config = $write->("vm \"gone\" {\n    base_disk absent.qcow2\n}\n");
    is($config->load_vm('gone'), undef,
	'an absent base_disk file behaves the same way');
    like($config->error, qr{\Q$tmpdir/absent.qcow2\E},
	'and error names the resolved path');

    # Two origins contradict each other
    $config = $write->("vm \"both\" {\n"
	. "    autoinstall install.conf\n"
	. "    base_disk base.qcow2\n"
	. "}\n");
    is($config->load_vm('both'), undef,
	'autoinstall with base_disk makes load_vm return undef');
    like($config->error, qr/autoinstall/, 'error names one directive');
    like($config->error, qr/base_disk/, 'and the other');

    # An imported base lives in the cache, so the cache must be on
    $config = $write->("vm \"import\" {\n"
	. "    base_disk base.qcow2\n"
	. "    image_cache no\n"
	. "}\n");
    is($config->load_vm('import'), undef,
	'base_disk with image_cache no makes load_vm return undef');
    like($config->error, qr/image cache/, 'and error names the cause');

    # ssh_pubkey without root_password_file refuses outside the
    # expect mode, because the tool cannot authenticate
    for my $origin ('autoinstall install.conf', 'base_disk base.qcow2') {
	$config = $write->("ssh_pubkey ssh-ed25519 KEY test\@host\n"
	    . "vm \"keyed\" {\n    $origin\n}\n");
	is($config->load_vm('keyed'), undef,
	    "ssh_pubkey with no root_password_file refuses ($origin)");
	like($config->error, qr/root_password_file/,
	    'and error names one remedy');
	like($config->error, qr/ssh_pubkey/, 'and the other');
    }

    $config = $write->("ssh_pubkey ssh-ed25519 KEY test\@host\n"
	. "vm \"keyed\" {\n"
	. "    autoinstall install.conf\n"
	. "    root_password_file ~/password\n"
	. "}\n");
    ok(defined $config->load_vm('keyed'),
	'root_password_file satisfies the refusal');

    $config = $write->("ssh_pubkey ssh-ed25519 KEY test\@host\n"
	. "vm \"keyed\" {\n}\n");
    ok(defined $config->load_vm('keyed'),
	'the expect mode needs no password file');
    is($config->error, undef, 'and error returns undef after it');
}

# Test VM block with unquoted name
{
    my $tmpdir = tempdir(CLEANUP => 1);
    make_path("$tmpdir/.fuguvm/vms");
    
    open my $fh, '>', "$tmpdir/.fuguvmrc";
    print $fh "vm simple {\n";
    print $fh "    memory 512\n";
    print $fh "    version 7.8\n";
    print $fh "}\n";
    close $fh;
    
    my $config = App::FuguVM::Config->new($tmpdir);
    my $vm = $config->load_vm('simple');
    
    ok(defined $vm, 'VM with unquoted name loaded');
    is($vm->{memory}, 512, 'VM memory correct');
    is($vm->{name}, 'simple', 'VM name set from unquoted block name');
}

done_testing();



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