跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

instance_test.go

210 行 · 7.4 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package store
5
6import (
7 "bytes"
8 "os"
9 "path/filepath"
10 "runtime"
11 "strings"
12 "testing"
13
14 "gotest.tools/v3/assert"
15
16 "github.com/lima-vm/lima/v2/pkg/limatype"
17)
18
19const separator = string(filepath.Separator)
20
21var (
22 vmtype = limatype.QEMU
23 goarch = limatype.NewArch(runtime.GOARCH)
24 space = strings.Repeat(" ", len(goarch)-4)
25)
26
27var instance = limatype.Instance{
28 Name: "foo",
29 Status: limatype.StatusStopped,
30 VMType: vmtype,
31 Arch: goarch,
32 Dir: "dir",
33 SSHAddress: "127.0.0.1",
34}
35
36// width 0 means the terminal width is unknown (stdout is not a TTY), so all columns are shown.
37var table = "NAME STATUS SSH VMTYPE ARCH" + space + " CPUS MEMORY DISK DIR\n" +
38 "foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
39
40var tableEmu = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" +
41 "foo Stopped 127.0.0.1:0 " + vmtype + " unknown 0 0B 0B dir\n"
42
43var tableHome = "NAME STATUS SSH VMTYPE ARCH" + space + " CPUS MEMORY DISK DIR\n" +
44 "foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B ~" + separator + "dir\n"
45
46var tableAll = "NAME STATUS SSH VMTYPE ARCH" + space + " CPUS MEMORY DISK DIR\n" +
47 "foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
48
49// for width 60, everything is hidden
50var table60 = "NAME STATUS SSH CPUS MEMORY DISK\n" +
51 "foo Stopped 127.0.0.1:0 0 0B 0B\n"
52
53// for width 80, identical is hidden (type/arch)
54var table80i = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
55 "foo Stopped 127.0.0.1:0 0 0B 0B dir\n"
56
57// for width 80, different arch is still shown (not dir)
58var table80d = "NAME STATUS SSH ARCH CPUS MEMORY DISK\n" +
59 "foo Stopped 127.0.0.1:0 unknown 0 0B 0B\n"
60
61// for width 100, nothing is hidden
62var table100 = "NAME STATUS SSH VMTYPE ARCH" + space + " CPUS MEMORY DISK DIR\n" +
63 "foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
64
65// for width 80, directory is hidden (if not identical)
66var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" +
67 "foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" +
68 "bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n"
69
70func TestSSHPortFromConfig(t *testing.T) {
71 t.Run("valid", func(t *testing.T) {
72 f := filepath.Join(t.TempDir(), "ssh.config")
73 content := "Host lima-default\n Hostname 127.0.0.1\n Port 58786\n User foo\n"
74 assert.NilError(t, os.WriteFile(f, []byte(content), 0o644))
75 port, err := sshPortFromConfig(f)
76 assert.NilError(t, err)
77 assert.Equal(t, 58786, port)
78 })
79 t.Run("missing file", func(t *testing.T) {
80 _, err := sshPortFromConfig(filepath.Join(t.TempDir(), "nonexistent"))
81 assert.ErrorIs(t, err, os.ErrNotExist)
82 })
83 t.Run("no port line", func(t *testing.T) {
84 f := filepath.Join(t.TempDir(), "ssh.config")
85 assert.NilError(t, os.WriteFile(f, []byte("Host lima-default\n Hostname 127.0.0.1\n"), 0o644))
86 _, err := sshPortFromConfig(f)
87 assert.ErrorContains(t, err, "port not found")
88 })
89 t.Run("invalid port", func(t *testing.T) {
90 f := filepath.Join(t.TempDir(), "ssh.config")
91 assert.NilError(t, os.WriteFile(f, []byte("Host lima-default\n Port abc\n"), 0o644))
92 _, err := sshPortFromConfig(f)
93 assert.ErrorContains(t, err, "invalid syntax")
94 })
95}
96
97func TestPrintInstanceTable(t *testing.T) {
98 var buf bytes.Buffer
99 instances := []*limatype.Instance{&instance}
100 err := PrintInstances(&buf, instances, "table", nil)
101 assert.NilError(t, err)
102 assert.Equal(t, table, buf.String())
103}
104
105func TestPrintInstanceTableEmu(t *testing.T) {
106 var buf bytes.Buffer
107 instance1 := instance
108 instance1.Arch = "unknown"
109 instances := []*limatype.Instance{&instance1}
110 err := PrintInstances(&buf, instances, "table", nil)
111 assert.NilError(t, err)
112 assert.Equal(t, tableEmu, buf.String())
113}
114
115func TestPrintInstanceTableHome(t *testing.T) {
116 var buf bytes.Buffer
117 homeDir, err := os.UserHomeDir()
118 assert.NilError(t, err)
119 instance1 := instance
120 instance1.Dir = filepath.Join(homeDir, "dir")
121 instances := []*limatype.Instance{&instance1}
122 err = PrintInstances(&buf, instances, "table", nil)
123 assert.NilError(t, err)
124 assert.Equal(t, tableHome, buf.String())
125}
126
127func TestPrintInstanceTable60(t *testing.T) {
128 var buf bytes.Buffer
129 instances := []*limatype.Instance{&instance}
130 options := PrintOptions{TerminalWidth: 60}
131 err := PrintInstances(&buf, instances, "table", &options)
132 assert.NilError(t, err)
133 assert.Equal(t, table60, buf.String())
134}
135
136func TestPrintInstanceTable80SameArch(t *testing.T) {
137 var buf bytes.Buffer
138 instances := []*limatype.Instance{&instance}
139 options := PrintOptions{TerminalWidth: 80}
140 err := PrintInstances(&buf, instances, "table", &options)
141 assert.NilError(t, err)
142 assert.Equal(t, table80i, buf.String())
143}
144
145func TestPrintInstanceTable80DiffArch(t *testing.T) {
146 var buf bytes.Buffer
147 instance1 := instance
148 instance1.Arch = limatype.NewArch("unknown")
149 instances := []*limatype.Instance{&instance1}
150 options := PrintOptions{TerminalWidth: 80}
151 err := PrintInstances(&buf, instances, "table", &options)
152 assert.NilError(t, err)
153 assert.Equal(t, table80d, buf.String())
154}
155
156func TestPrintInstanceTable100(t *testing.T) {
157 var buf bytes.Buffer
158 instances := []*limatype.Instance{&instance}
159 options := PrintOptions{TerminalWidth: 100}
160 err := PrintInstances(&buf, instances, "table", &options)
161 assert.NilError(t, err)
162 assert.Equal(t, table100, buf.String())
163}
164
165func TestPrintInstanceTableAll(t *testing.T) {
166 var buf bytes.Buffer
167 instances := []*limatype.Instance{&instance}
168 options := PrintOptions{TerminalWidth: 40, AllFields: true}
169 err := PrintInstances(&buf, instances, "table", &options)
170 assert.NilError(t, err)
171 assert.Equal(t, tableAll, buf.String())
172}
173
174// Regression test for https://github.com/lima-vm/lima/issues/3986:
175// when stdout is not a TTY (TerminalWidth == 0), all columns must be shown,
176// regardless of whether --all-fields was passed.
177func TestPrintInstanceTableNonTTY(t *testing.T) {
178 instances := []*limatype.Instance{&instance}
179 t.Run("AllFields", func(t *testing.T) {
180 var buf bytes.Buffer
181 options := PrintOptions{AllFields: true}
182 err := PrintInstances(&buf, instances, "table", &options)
183 assert.NilError(t, err)
184 assert.Equal(t, tableAll, buf.String())
185 })
186 t.Run("Default", func(t *testing.T) {
187 var buf bytes.Buffer
188 options := PrintOptions{}
189 err := PrintInstances(&buf, instances, "table", &options)
190 assert.NilError(t, err)
191 assert.Equal(t, tableAll, buf.String())
192 })
193}
194
195func TestPrintInstanceTableTwo(t *testing.T) {
196 var buf bytes.Buffer
197 instance1 := instance
198 instance1.Name = "foo"
199 instance1.VMType = limatype.QEMU
200 instance1.Arch = limatype.X8664
201 instance2 := instance
202 instance2.Name = "bar"
203 instance2.VMType = limatype.VZ
204 instance2.Arch = limatype.AARCH64
205 instances := []*limatype.Instance{&instance1, &instance2}
206 options := PrintOptions{TerminalWidth: 80}
207 err := PrintInstances(&buf, instances, "table", &options)
208 assert.NilError(t, err)
209 assert.Equal(t, tableTwo, buf.String())
210}