|
| 1 | +//go:build linux && amd64 |
| 2 | + |
| 3 | +package instances |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/kernel/hypeman/lib/forkvm" |
| 16 | + "github.com/kernel/hypeman/lib/guest" |
| 17 | + "github.com/kernel/hypeman/lib/hypervisor" |
| 18 | + "github.com/kernel/hypeman/lib/images" |
| 19 | + "github.com/kernel/hypeman/lib/paths" |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestWindowsNetworkingIntegration(t *testing.T) { |
| 25 | + fixture := os.Getenv("HYPEMAN_WINDOWS_TEST_AGENT_PERSONA") |
| 26 | + if fixture == "" { |
| 27 | + fixture = "/ci/windows/persona-agent.qcow2" |
| 28 | + } |
| 29 | + if _, err := os.Stat(fixture); err != nil { |
| 30 | + if os.Getenv("CI") == "true" { |
| 31 | + t.Fatalf("required Windows networking fixture is missing: %s", fixture) |
| 32 | + } |
| 33 | + t.Skipf("Windows networking fixture is unavailable: %s", fixture) |
| 34 | + } |
| 35 | + acquireHeavyIO(t) |
| 36 | + |
| 37 | + manager, dataDir := setupTestManagerForQEMU(t) |
| 38 | + p := paths.New(dataDir) |
| 39 | + const digestHex = "abababababababababababababababababababababababababababababababab" |
| 40 | + image := &images.Image{ |
| 41 | + Name: "registry.example/windows/persona:networking-integration", |
| 42 | + Digest: "sha256:" + digestHex, |
| 43 | + Platform: "windows/amd64", |
| 44 | + Status: images.StatusReady, |
| 45 | + Machine: &images.MachineImage{ |
| 46 | + Kind: images.MachineImageWindowsPersona, |
| 47 | + Base: "registry.example/windows/base@sha256:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", |
| 48 | + TPM: "2.0", |
| 49 | + SecureBoot: "required", |
| 50 | + VirtualSize: 80 << 30, |
| 51 | + }, |
| 52 | + } |
| 53 | + manager.imageManager = windowsFixtureImageManager{image: image} |
| 54 | + personaPath, err := images.GetMachineDiskPath(p, image.Name, image.Digest, image.Machine) |
| 55 | + require.NoError(t, err) |
| 56 | + require.NoError(t, forkvm.CopyRegularFile(fixture, personaPath)) |
| 57 | + require.NoError(t, os.Chmod(personaPath, 0444)) |
| 58 | + |
| 59 | + ctx := context.Background() |
| 60 | + instance, err := manager.CreateInstance(ctx, CreateInstanceRequest{ |
| 61 | + Name: "windows-networking-integration", |
| 62 | + Image: image.Name, |
| 63 | + Platform: "windows/amd64", |
| 64 | + Size: 8 << 30, |
| 65 | + Vcpus: 4, |
| 66 | + NetworkEnabled: true, |
| 67 | + Hypervisor: hypervisor.TypeQEMU, |
| 68 | + }) |
| 69 | + require.NoError(t, err) |
| 70 | + t.Cleanup(func() { _ = deleteTestInstanceNow(context.Background(), manager, instance.Id) }) |
| 71 | + require.NotEmpty(t, instance.IP) |
| 72 | + require.NotEmpty(t, instance.MAC) |
| 73 | + |
| 74 | + assertWindowsNetworkReady(t, ctx, manager, instance.Id, instance.IP) |
| 75 | +} |
| 76 | + |
| 77 | +func assertWindowsNetworkReady(t *testing.T, ctx context.Context, manager *manager, instanceID, expectedIP string) { |
| 78 | + t.Helper() |
| 79 | + require.Eventually(t, func() bool { |
| 80 | + current, err := manager.GetInstance(ctx, instanceID) |
| 81 | + return err == nil && current.State == StateRunning |
| 82 | + }, 4*time.Minute, time.Second) |
| 83 | + |
| 84 | + dialer, err := manager.GetVsockDialer(ctx, instanceID) |
| 85 | + require.NoError(t, err) |
| 86 | + var stdout, stderr bytes.Buffer |
| 87 | + command := fmt.Sprintf("$a=Get-NetIPAddress -AddressFamily IPv4 | Where-Object IPAddress -eq '%s'; if (-not $a) { exit 20 }; [System.Net.Dns]::GetHostAddresses('example.com') | Out-Null; [Console]::Out.Write($a.IPAddress)", expectedIP) |
| 88 | + exit, err := guest.ExecIntoInstance(ctx, dialer, guest.ExecOptions{ |
| 89 | + Command: []string{"powershell.exe", "-NoProfile", "-NonInteractive", "-Command", command}, |
| 90 | + Stdout: &stdout, |
| 91 | + Stderr: &stderr, |
| 92 | + Timeout: 30, |
| 93 | + }) |
| 94 | + require.NoError(t, err, stderr.String()) |
| 95 | + require.Equal(t, 0, exit.Code, stderr.String()) |
| 96 | + assert.Equal(t, expectedIP, stdout.String()) |
| 97 | + |
| 98 | + require.Eventually(t, func() bool { |
| 99 | + conn, err := net.DialTimeout("tcp", net.JoinHostPort(expectedIP, "3389"), time.Second) |
| 100 | + if err != nil { |
| 101 | + return false |
| 102 | + } |
| 103 | + _ = conn.Close() |
| 104 | + return true |
| 105 | + }, 2*time.Minute, time.Second, "RDP did not become reachable over the allocated network") |
| 106 | + |
| 107 | + ping := exec.Command("ping", "-c", "3", "-W", "2", expectedIP) |
| 108 | + require.NoError(t, ping.Run(), "allocated Windows IP did not answer ICMP") |
| 109 | +} |
0 commit comments