App-FuguVM

 view release on metacpan or  search on metacpan

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

    ok(-f "$tmpdir/test/proxy.pid", 'proxy.pid is a separate file');

    $state->clear_vm_pid;
    is($state->proxy_pidfile->read_pid, 22222,
	'proxy PID unchanged after clearing VM PID');

    $state->proxy_pidfile->remove;
    ok(!-f "$tmpdir/test/proxy.pid", 'proxy.pid removed');
}

# The autoinstall responder rides on its own pid file too
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $state = App::FuguVM::State->new($tmpdir, 'test');

    isa_ok($state->autoinstall_pidfile, 'Fugu::Pidfile',
	'autoinstall_pidfile');
    like($state->autoinstall_pidfile->path, qr{\Q$tmpdir\E/test/},
	'the pid file lives under the state directory of the guest');
    isnt($state->autoinstall_pidfile->path, $state->proxy_pidfile->path,
	'and its path differs from the proxy pid file');

    $state->autoinstall_pidfile->write_pid(33333);
    is($state->autoinstall_pidfile->read_pid, 33333,
	'the responder pid file stores its own PID');
    ok(-f "$tmpdir/test/autoinstall.pid", 'autoinstall.pid is its own file');
    $state->autoinstall_pidfile->remove;
    ok(!-f "$tmpdir/test/autoinstall.pid", 'autoinstall.pid removed');
}

# Test installation state
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $state = App::FuguVM::State->new($tmpdir, 'test');

    ok(!$state->is_installed, 'Not installed initially');
    is($state->get_installed_arch, undef,
	'a fresh state records no architecture');

    $state->mark_installed('arm64');
    ok($state->is_installed, 'Installed after mark_installed');
    is($state->get_installed_arch, 'arm64',
	'mark_installed records the architecture');

    # Reload the state and make sure that the value persists
    my $state2 = App::FuguVM::State->new($tmpdir, 'test');
    ok($state2->is_installed, 'Installation state persisted');
    is($state2->get_installed_arch, 'arm64',
	'the architecture persists across a reload');
}

# Test disk paths
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $state = App::FuguVM::State->new($tmpdir, 'test');
    
    like($state->disk_path, qr/disk\.qcow2$/, 'disk_path ends with disk.qcow2');
    ok(!$state->disk_exists, 'disk_exists returns false when no disk');
}

# Test root password management
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $state = App::FuguVM::State->new($tmpdir, 'test');
    
    is($state->get_root_password, undef, 'No password initially');
    
    $state->set_root_password('testpass123');
    is($state->get_root_password, 'testpass123', 'Password stored and retrieved');
    
    # Reload the state and make sure that the value persists
    my $state2 = App::FuguVM::State->new($tmpdir, 'test');
    is($state2->get_root_password, 'testpass123', 'Password persisted');
}

# Test SSH key installation state
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $state = App::FuguVM::State->new($tmpdir, 'test');
    
    is($state->get_installed_ssh_pubkey, undef, 'No pubkey stored initially');
    
    my $test_pubkey = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... test@example';
    $state->mark_ssh_key_installed($test_pubkey);
    is($state->get_installed_ssh_pubkey, $test_pubkey, 'Pubkey stored correctly');
    
    # Reload the state and make sure that the value persists
    my $state2 = App::FuguVM::State->new($tmpdir, 'test');
    is($state2->get_installed_ssh_pubkey, $test_pubkey, 'Pubkey persisted correctly');
}

# The runtime record: the facts of one run of one guest
{
    my $tmpdir = tempdir(CLEANUP => 1);
    my $state = App::FuguVM::State->new($tmpdir, 'test');

    is_deeply($state->get_runtime, {},
	'get_runtime returns an empty hash reference for a fresh state');

    $state->set_runtime(
	accel        => 'kvm',
	ssh_port     => 2223,
	console_port => 4445,
    );
    is_deeply($state->get_runtime,
	{ accel => 'kvm', ssh_port => 2223, console_port => 4445 },
	'set_runtime and get_runtime round-trip the three facts');

    # The record persists across a reload
    my $state2 = App::FuguVM::State->new($tmpdir, 'test');
    is($state2->get_runtime->{ssh_port}, 2223,
	'the record survives a reload');

    # clear_runtime empties the record, and the store survives
    $state2->set_root_password('sentinel');
    $state2->clear_runtime;
    is_deeply($state2->get_runtime, {}, 'clear_runtime empties the record');

    my $state3 = App::FuguVM::State->new($tmpdir, 'test');
    is_deeply($state3->get_runtime, {}, 'and the clearing persists');
    is($state3->get_root_password, 'sentinel',
	'the rest of the store survives the clearing');
}

# ============================================================
# Robustness tests (from ROBUSTNESS-REPORT.md)
# ============================================================

# Issue 1: Extremely long VM name
{
    my $tmpdir = tempdir(CLEANUP => 1);
    
    # Suppress warnings for this test
    local $SIG{__WARN__} = sub {};
    
    my $long_name = 'x' x 10000;
    my $state = App::FuguVM::State->new($tmpdir, $long_name);
    is($state, undef, 'Long VM name (10000 chars) returns undef');
}

# Issue 1: a VM name at the boundary of 255 chars works
{
    my $tmpdir = tempdir(CLEANUP => 1);
    
    my $max_name = 'x' x 255;
    my $state = App::FuguVM::State->new($tmpdir, $max_name);
    ok(defined $state, 'VM name at 255 chars is accepted');
}

# Issue 1: a VM name over the boundary at 256 chars fails
{
    my $tmpdir = tempdir(CLEANUP => 1);
    
    local $SIG{__WARN__} = sub {};
    
    my $over_name = 'x' x 256;
    my $state = App::FuguVM::State->new($tmpdir, $over_name);
    is($state, undef, 'VM name at 256 chars returns undef');
}

# Issue 1: VM name with invalid characters (path separator)
{
    my $tmpdir = tempdir(CLEANUP => 1);
    
    local $SIG{__WARN__} = sub {};
    
    my $state = App::FuguVM::State->new($tmpdir, '../../../etc/passwd');
    is($state, undef, 'VM name with path traversal returns undef');
}

# Issue 3: File where directory expected
{
    my $tmpdir = tempdir(CLEANUP => 1);
    
    # Create a file where the VM state directory belongs
    my $file_path = "$tmpdir/testvm";
    open my $fh, '>', $file_path or die "Cannot create file: $!";
    print $fh "not a directory\n";
    close $fh;
    
    local $SIG{__WARN__} = sub {};



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