Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/store/instance_test.go
2606 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package store
5
6
import (
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
19
const separator = string(filepath.Separator)
20
21
var (
22
vmtype = limatype.QEMU
23
goarch = limatype.NewArch(runtime.GOARCH)
24
space = strings.Repeat(" ", len(goarch)-4)
25
)
26
27
var 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
var table = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
37
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n"
38
39
var tableEmu = "NAME STATUS SSH ARCH CPUS MEMORY DISK DIR\n" +
40
"foo Stopped 127.0.0.1:0 unknown 0 0B 0B dir\n"
41
42
var tableHome = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
43
"foo Stopped 127.0.0.1:0 0 0B 0B ~" + separator + "dir\n"
44
45
var tableAll = "NAME STATUS SSH VMTYPE ARCH" + space + " CPUS MEMORY DISK DIR\n" +
46
"foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
47
48
// for width 60, everything is hidden
49
var table60 = "NAME STATUS SSH CPUS MEMORY DISK\n" +
50
"foo Stopped 127.0.0.1:0 0 0B 0B\n"
51
52
// for width 80, identical is hidden (type/arch)
53
var table80i = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" +
54
"foo Stopped 127.0.0.1:0 0 0B 0B dir\n"
55
56
// for width 80, different arch is still shown (not dir)
57
var table80d = "NAME STATUS SSH ARCH CPUS MEMORY DISK\n" +
58
"foo Stopped 127.0.0.1:0 unknown 0 0B 0B\n"
59
60
// for width 100, nothing is hidden
61
var table100 = "NAME STATUS SSH VMTYPE ARCH" + space + " CPUS MEMORY DISK DIR\n" +
62
"foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n"
63
64
// for width 80, directory is hidden (if not identical)
65
var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" +
66
"foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" +
67
"bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n"
68
69
func TestPrintInstanceTable(t *testing.T) {
70
var buf bytes.Buffer
71
instances := []*limatype.Instance{&instance}
72
err := PrintInstances(&buf, instances, "table", nil)
73
assert.NilError(t, err)
74
assert.Equal(t, table, buf.String())
75
}
76
77
func TestPrintInstanceTableEmu(t *testing.T) {
78
var buf bytes.Buffer
79
instance1 := instance
80
instance1.Arch = "unknown"
81
instances := []*limatype.Instance{&instance1}
82
err := PrintInstances(&buf, instances, "table", nil)
83
assert.NilError(t, err)
84
assert.Equal(t, tableEmu, buf.String())
85
}
86
87
func TestPrintInstanceTableHome(t *testing.T) {
88
var buf bytes.Buffer
89
homeDir, err := os.UserHomeDir()
90
assert.NilError(t, err)
91
instance1 := instance
92
instance1.Dir = filepath.Join(homeDir, "dir")
93
instances := []*limatype.Instance{&instance1}
94
err = PrintInstances(&buf, instances, "table", nil)
95
assert.NilError(t, err)
96
assert.Equal(t, tableHome, buf.String())
97
}
98
99
func TestPrintInstanceTable60(t *testing.T) {
100
var buf bytes.Buffer
101
instances := []*limatype.Instance{&instance}
102
options := PrintOptions{TerminalWidth: 60}
103
err := PrintInstances(&buf, instances, "table", &options)
104
assert.NilError(t, err)
105
assert.Equal(t, table60, buf.String())
106
}
107
108
func TestPrintInstanceTable80SameArch(t *testing.T) {
109
var buf bytes.Buffer
110
instances := []*limatype.Instance{&instance}
111
options := PrintOptions{TerminalWidth: 80}
112
err := PrintInstances(&buf, instances, "table", &options)
113
assert.NilError(t, err)
114
assert.Equal(t, table80i, buf.String())
115
}
116
117
func TestPrintInstanceTable80DiffArch(t *testing.T) {
118
var buf bytes.Buffer
119
instance1 := instance
120
instance1.Arch = limatype.NewArch("unknown")
121
instances := []*limatype.Instance{&instance1}
122
options := PrintOptions{TerminalWidth: 80}
123
err := PrintInstances(&buf, instances, "table", &options)
124
assert.NilError(t, err)
125
assert.Equal(t, table80d, buf.String())
126
}
127
128
func TestPrintInstanceTable100(t *testing.T) {
129
var buf bytes.Buffer
130
instances := []*limatype.Instance{&instance}
131
options := PrintOptions{TerminalWidth: 100}
132
err := PrintInstances(&buf, instances, "table", &options)
133
assert.NilError(t, err)
134
assert.Equal(t, table100, buf.String())
135
}
136
137
func TestPrintInstanceTableAll(t *testing.T) {
138
var buf bytes.Buffer
139
instances := []*limatype.Instance{&instance}
140
options := PrintOptions{TerminalWidth: 40, AllFields: true}
141
err := PrintInstances(&buf, instances, "table", &options)
142
assert.NilError(t, err)
143
assert.Equal(t, tableAll, buf.String())
144
}
145
146
func TestPrintInstanceTableTwo(t *testing.T) {
147
var buf bytes.Buffer
148
instance1 := instance
149
instance1.Name = "foo"
150
instance1.VMType = limatype.QEMU
151
instance1.Arch = limatype.X8664
152
instance2 := instance
153
instance2.Name = "bar"
154
instance2.VMType = limatype.VZ
155
instance2.Arch = limatype.AARCH64
156
instances := []*limatype.Instance{&instance1, &instance2}
157
options := PrintOptions{TerminalWidth: 80}
158
err := PrintInstances(&buf, instances, "table", &options)
159
assert.NilError(t, err)
160
assert.Equal(t, tableTwo, buf.String())
161
}
162
163