|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/onkernel/hypeman/cmd/api/config" |
| 12 | + "github.com/onkernel/hypeman/lib/devices" |
| 13 | + "github.com/onkernel/hypeman/lib/guest" |
| 14 | + "github.com/onkernel/hypeman/lib/hypervisor" |
| 15 | + "github.com/onkernel/hypeman/lib/images" |
| 16 | + "github.com/onkernel/hypeman/lib/instances" |
| 17 | + "github.com/onkernel/hypeman/lib/network" |
| 18 | + "github.com/onkernel/hypeman/lib/paths" |
| 19 | + "github.com/onkernel/hypeman/lib/system" |
| 20 | + "github.com/onkernel/hypeman/lib/volumes" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +// TestSystemdMode verifies that hypeman correctly detects and runs |
| 26 | +// systemd-based images with systemd as PID 1. |
| 27 | +// |
| 28 | +// This test uses the jrei/systemd-ubuntu image from Docker Hub which runs |
| 29 | +// systemd as its CMD. The test verifies that hypeman auto-detects this and: |
| 30 | +// - Uses systemd mode (chroot to container rootfs) |
| 31 | +// - Starts systemd as PID 1 |
| 32 | +// - Injects and starts the hypeman-agent.service |
| 33 | +func TestSystemdMode(t *testing.T) { |
| 34 | + if testing.Short() { |
| 35 | + t.Skip("skipping integration test in short mode") |
| 36 | + } |
| 37 | + |
| 38 | + // Skip if KVM is not available |
| 39 | + if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { |
| 40 | + t.Skip("/dev/kvm not available") |
| 41 | + } |
| 42 | + |
| 43 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) |
| 44 | + defer cancel() |
| 45 | + |
| 46 | + // Set up test environment |
| 47 | + tmpDir := t.TempDir() |
| 48 | + p := paths.New(tmpDir) |
| 49 | + |
| 50 | + cfg := &config.Config{ |
| 51 | + DataDir: tmpDir, |
| 52 | + BridgeName: "vmbr0", |
| 53 | + SubnetCIDR: "10.100.0.0/16", |
| 54 | + DNSServer: "1.1.1.1", |
| 55 | + } |
| 56 | + |
| 57 | + // Create managers |
| 58 | + imageManager, err := images.NewManager(p, 1, nil) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + systemManager := system.NewManager(p) |
| 62 | + networkManager := network.NewManager(p, cfg, nil) |
| 63 | + deviceManager := devices.NewManager(p) |
| 64 | + volumeManager := volumes.NewManager(p, 0, nil) |
| 65 | + |
| 66 | + limits := instances.ResourceLimits{ |
| 67 | + MaxOverlaySize: 100 * 1024 * 1024 * 1024, |
| 68 | + MaxVcpusPerInstance: 0, |
| 69 | + MaxMemoryPerInstance: 0, |
| 70 | + MaxTotalVcpus: 0, |
| 71 | + MaxTotalMemory: 0, |
| 72 | + } |
| 73 | + |
| 74 | + instanceManager := instances.NewManager(p, imageManager, systemManager, networkManager, deviceManager, volumeManager, limits, "", nil, nil) |
| 75 | + |
| 76 | + // Cleanup any orphaned instances |
| 77 | + t.Cleanup(func() { |
| 78 | + instanceManager.DeleteInstance(ctx, "systemd-test") |
| 79 | + }) |
| 80 | + |
| 81 | + imageName := "docker.io/jrei/systemd-ubuntu:22.04" |
| 82 | + |
| 83 | + // Pull the systemd image |
| 84 | + t.Log("Pulling systemd image:", imageName) |
| 85 | + _, err = imageManager.CreateImage(ctx, images.CreateImageRequest{ |
| 86 | + Name: imageName, |
| 87 | + }) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + // Wait for image to be ready |
| 91 | + t.Log("Waiting for image build...") |
| 92 | + var img *images.Image |
| 93 | + for i := 0; i < 120; i++ { |
| 94 | + img, err = imageManager.GetImage(ctx, imageName) |
| 95 | + if err == nil && img.Status == images.StatusReady { |
| 96 | + break |
| 97 | + } |
| 98 | + time.Sleep(1 * time.Second) |
| 99 | + } |
| 100 | + require.Equal(t, images.StatusReady, img.Status, "image should be ready") |
| 101 | + |
| 102 | + // Verify systemd detection |
| 103 | + t.Run("IsSystemdImage", func(t *testing.T) { |
| 104 | + isSystemd := images.IsSystemdImage(img.Entrypoint, img.Cmd) |
| 105 | + assert.True(t, isSystemd, "image should be detected as systemd, entrypoint=%v cmd=%v", img.Entrypoint, img.Cmd) |
| 106 | + }) |
| 107 | + |
| 108 | + // Ensure system files (kernel, initrd) |
| 109 | + t.Log("Ensuring system files...") |
| 110 | + err = systemManager.EnsureSystemFiles(ctx) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + // Create the systemd instance |
| 114 | + t.Log("Creating systemd instance...") |
| 115 | + inst, err := instanceManager.CreateInstance(ctx, instances.CreateInstanceRequest{ |
| 116 | + Name: "systemd-test", |
| 117 | + Image: imageName, |
| 118 | + Size: 2 * 1024 * 1024 * 1024, // 2GB |
| 119 | + HotplugSize: 512 * 1024 * 1024, |
| 120 | + OverlaySize: 1024 * 1024 * 1024, |
| 121 | + Vcpus: 2, |
| 122 | + NetworkEnabled: false, // No network needed for this test |
| 123 | + }) |
| 124 | + require.NoError(t, err) |
| 125 | + t.Logf("Instance created: %s", inst.Id) |
| 126 | + |
| 127 | + // Wait for guest agent to be ready |
| 128 | + t.Log("Waiting for guest agent...") |
| 129 | + err = waitForGuestAgent(ctx, instanceManager, inst.Id, 60*time.Second) |
| 130 | + require.NoError(t, err, "guest agent should be ready") |
| 131 | + |
| 132 | + // Test: Verify systemd is PID 1 |
| 133 | + t.Run("SystemdIsPID1", func(t *testing.T) { |
| 134 | + output, exitCode, err := execInInstance(ctx, inst, "cat", "/proc/1/comm") |
| 135 | + require.NoError(t, err, "exec should work") |
| 136 | + require.Equal(t, 0, exitCode, "command should succeed") |
| 137 | + |
| 138 | + pid1Name := strings.TrimSpace(output) |
| 139 | + assert.Equal(t, "systemd", pid1Name, "PID 1 should be systemd") |
| 140 | + t.Logf("PID 1 is: %s", pid1Name) |
| 141 | + }) |
| 142 | + |
| 143 | + // Test: Verify guest-agent binary exists |
| 144 | + t.Run("GuestAgentExists", func(t *testing.T) { |
| 145 | + output, exitCode, err := execInInstance(ctx, inst, "test", "-x", "/opt/hypeman/guest-agent") |
| 146 | + require.NoError(t, err, "exec should work") |
| 147 | + assert.Equal(t, 0, exitCode, "guest-agent binary should exist at /opt/hypeman/guest-agent, output: %s", output) |
| 148 | + }) |
| 149 | + |
| 150 | + // Test: Verify hypeman-agent.service is active |
| 151 | + t.Run("AgentServiceActive", func(t *testing.T) { |
| 152 | + output, exitCode, err := execInInstance(ctx, inst, "systemctl", "is-active", "hypeman-agent") |
| 153 | + require.NoError(t, err, "exec should work") |
| 154 | + status := strings.TrimSpace(output) |
| 155 | + assert.Equal(t, 0, exitCode, "hypeman-agent service should be active, status: %s", status) |
| 156 | + assert.Equal(t, "active", status, "service status should be 'active'") |
| 157 | + t.Logf("hypeman-agent service status: %s", status) |
| 158 | + }) |
| 159 | + |
| 160 | + // Test: Verify we can view agent logs via journalctl |
| 161 | + t.Run("AgentLogsAccessible", func(t *testing.T) { |
| 162 | + output, exitCode, err := execInInstance(ctx, inst, "journalctl", "-u", "hypeman-agent", "--no-pager", "-n", "5") |
| 163 | + require.NoError(t, err, "exec should work") |
| 164 | + assert.Equal(t, 0, exitCode, "journalctl should succeed") |
| 165 | + t.Logf("Agent logs (last 5 lines):\n%s", output) |
| 166 | + }) |
| 167 | + |
| 168 | + t.Log("All systemd mode tests passed!") |
| 169 | +} |
| 170 | + |
| 171 | +// waitForGuestAgent polls until the guest agent is ready |
| 172 | +func waitForGuestAgent(ctx context.Context, mgr instances.Manager, instanceID string, timeout time.Duration) error { |
| 173 | + inst, err := mgr.GetInstance(ctx, instanceID) |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + dialer, err := hypervisor.NewVsockDialer(inst.HypervisorType, inst.VsockSocket, inst.VsockCID) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + // Use WaitForAgent to wait for the agent to be ready |
| 184 | + var stdout bytes.Buffer |
| 185 | + _, err = guest.ExecIntoInstance(ctx, dialer, guest.ExecOptions{ |
| 186 | + Command: []string{"echo", "ready"}, |
| 187 | + Stdout: &stdout, |
| 188 | + TTY: false, |
| 189 | + WaitForAgent: timeout, |
| 190 | + }) |
| 191 | + return err |
| 192 | +} |
| 193 | + |
| 194 | +// execInInstance executes a command in the instance |
| 195 | +func execInInstance(ctx context.Context, inst *instances.Instance, command ...string) (string, int, error) { |
| 196 | + dialer, err := hypervisor.NewVsockDialer(inst.HypervisorType, inst.VsockSocket, inst.VsockCID) |
| 197 | + if err != nil { |
| 198 | + return "", -1, err |
| 199 | + } |
| 200 | + |
| 201 | + var stdout, stderr bytes.Buffer |
| 202 | + exit, err := guest.ExecIntoInstance(ctx, dialer, guest.ExecOptions{ |
| 203 | + Command: command, |
| 204 | + Stdout: &stdout, |
| 205 | + Stderr: &stderr, |
| 206 | + TTY: false, |
| 207 | + }) |
| 208 | + if err != nil { |
| 209 | + return stderr.String(), -1, err |
| 210 | + } |
| 211 | + |
| 212 | + return stdout.String(), exit.Code, nil |
| 213 | +} |
0 commit comments