| 1 | |
| 2 | |
| 3 | |
| 4 | package hcs |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "net" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "regexp" |
| 12 | "sync" |
| 13 | "testing" |
| 14 | |
| 15 | "gotest.tools/v3/assert" |
| 16 | ) |
| 17 | |
| 18 | const ( |
| 19 | testDiskPath = `C:\Users\lima\.lima\hcs\disk.vhdx` |
| 20 | testISOPath = `C:\Users\lima\.lima\hcs\cidata.iso` |
| 21 | testEndpointID = "11111111-2222-3333-4444-555555555555" |
| 22 | testMACAddress = "52-55-55-AA-BB-CC" |
| 23 | testSerialPipe = `\\.\pipe\lima-hcs-com1` |
| 24 | testCPUs = 2 |
| 25 | testMemoryMiB = 2048 |
| 26 | ) |
| 27 | |
| 28 | func TestNetworkAndEndpointName(t *testing.T) { |
| 29 | assert.Equal(t, networkName("test"), "lima-test-net") |
| 30 | assert.Equal(t, endpointName("test"), "lima-test-ep") |
| 31 | } |
| 32 | |
| 33 | func TestHNSMACAddress(t *testing.T) { |
| 34 | dir := t.TempDir() |
| 35 | got := hnsMACAddress(dir) |
| 36 | |
| 37 | re := regexp.MustCompile(`^52-55-55(-[0-9A-F]{2}){3}$`) |
| 38 | assert.Assert(t, re.MatchString(got), "unexpected MAC address %#q", got) |
| 39 | |
| 40 | assert.Equal(t, got, hnsMACAddress(dir), "MAC Address should be identical if input is the same.") |
| 41 | |
| 42 | assert.Assert(t, got != hnsMACAddress(t.TempDir())) |
| 43 | } |
| 44 | |
| 45 | func TestBuildVMConfig(t *testing.T) { |
| 46 | got, err := buildVMConfig(testDiskPath, testISOPath, testEndpointID, testMACAddress, testSerialPipe, testCPUs, testMemoryMiB) |
| 47 | assert.NilError(t, err) |
| 48 | |
| 49 | const want = `{ |
| 50 | "SchemaVersion": {"Major": 2, "Minor": 1}, |
| 51 | "Owner": "Lima", |
| 52 | "ShouldTerminateOnLastHandleClosed": true, |
| 53 | "VirtualMachine": { |
| 54 | "Chipset": { |
| 55 | "Uefi": { |
| 56 | "Console": "ComPort1", |
| 57 | "BootThis": {"DevicePath": "Primary disk", "DiskNumber": 0, "DeviceType": "ScsiDrive"} |
| 58 | } |
| 59 | }, |
| 60 | "ComputeTopology": { |
| 61 | "Memory": {"Backing": "Virtual", "SizeInMB": 2048}, |
| 62 | "Processor": {"Count": 2} |
| 63 | }, |
| 64 | "Devices": { |
| 65 | "Scsi": { |
| 66 | "Primary disk": { |
| 67 | "Attachments": {"0": {"Type": "VirtualDisk", "Path": "C:\\Users\\lima\\.lima\\hcs\\disk.vhdx"}} |
| 68 | }, |
| 69 | "cidata": { |
| 70 | "Attachments": {"0": {"Type": "Iso", "Path": "C:\\Users\\lima\\.lima\\hcs\\cidata.iso"}} |
| 71 | } |
| 72 | }, |
| 73 | "ComPorts": {"0": {"NamedPipe": "\\\\.\\pipe\\lima-hcs-com1"}}, |
| 74 | "NetworkAdapters": { |
| 75 | "11111111-2222-3333-4444-555555555555": { |
| 76 | "EndpointId": "11111111-2222-3333-4444-555555555555", |
| 77 | "MacAddress": "52-55-55-AA-BB-CC" |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | }` |
| 83 | |
| 84 | var gotDoc, wantDoc any |
| 85 | assert.NilError(t, json.Unmarshal([]byte(got), &gotDoc)) |
| 86 | assert.NilError(t, json.Unmarshal([]byte(want), &wantDoc)) |
| 87 | assert.DeepEqual(t, gotDoc, wantDoc) |
| 88 | } |
| 89 | |
| 90 | func TestBuildVMConfigSerialPortNumbering(t *testing.T) { |
| 91 | got, err := buildVMConfig(testDiskPath, testISOPath, testEndpointID, testMACAddress, testSerialPipe, testCPUs, testMemoryMiB) |
| 92 | assert.NilError(t, err) |
| 93 | |
| 94 | var doc struct { |
| 95 | VirtualMachine struct { |
| 96 | Chipset struct { |
| 97 | Uefi struct { |
| 98 | Console string |
| 99 | } |
| 100 | } |
| 101 | Devices struct { |
| 102 | ComPorts map[string]struct { |
| 103 | NamedPipe string |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | assert.NilError(t, json.Unmarshal([]byte(got), &doc)) |
| 109 | |
| 110 | assert.Equal(t, doc.VirtualMachine.Chipset.Uefi.Console, "ComPort1") |
| 111 | assert.Equal(t, doc.VirtualMachine.Devices.ComPorts["0"].NamedPipe, testSerialPipe) |
| 112 | } |
| 113 | |
| 114 | func TestServeSerial(t *testing.T) { |
| 115 | logPath := filepath.Join(t.TempDir(), "serial.log") |
| 116 | ln := newFakeListener() |
| 117 | |
| 118 | done := make(chan struct{}) |
| 119 | go func() { |
| 120 | serveSerial(ln, logPath) |
| 121 | close(done) |
| 122 | }() |
| 123 | |
| 124 | lines := []string{"[ 0.000000] Linux version 6.11\n", "lima login: \n"} |
| 125 | for _, line := range lines { |
| 126 | guest, host := net.Pipe() |
| 127 | ln.conns <- host |
| 128 | _, err := guest.Write([]byte(line)) |
| 129 | assert.NilError(t, err) |
| 130 | assert.NilError(t, guest.Close()) |
| 131 | } |
| 132 | |
| 133 | assert.NilError(t, ln.Close()) |
| 134 | <-done |
| 135 | |
| 136 | b, err := os.ReadFile(logPath) |
| 137 | assert.NilError(t, err) |
| 138 | assert.Equal(t, string(b), lines[0]+lines[1]) |
| 139 | } |
| 140 | |
| 141 | type fakeListener struct { |
| 142 | conns chan net.Conn |
| 143 | done chan struct{} |
| 144 | once sync.Once |
| 145 | } |
| 146 | |
| 147 | func newFakeListener() *fakeListener { |
| 148 | return &fakeListener{ |
| 149 | conns: make(chan net.Conn), |
| 150 | done: make(chan struct{}), |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func (ln *fakeListener) Accept() (net.Conn, error) { |
| 155 | select { |
| 156 | case conn := <-ln.conns: |
| 157 | return conn, nil |
| 158 | case <-ln.done: |
| 159 | return nil, net.ErrClosed |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func (ln *fakeListener) Close() error { |
| 164 | ln.once.Do(func() { close(ln.done) }) |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | func (ln *fakeListener) Addr() net.Addr { return fakeAddr{} } |
| 169 | |
| 170 | type fakeAddr struct{} |
| 171 | |
| 172 | func (fakeAddr) Network() string { return "fake" } |
| 173 | func (fakeAddr) String() string { return "fake" } |