|
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os/user" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/lima-vm/lima/pkg/limayaml" |
| 11 | + "gotest.tools/v3/assert" |
| 12 | +) |
| 13 | + |
| 14 | +const separator = string(filepath.Separator) |
| 15 | + |
| 16 | +var vmtype = limayaml.QEMU |
| 17 | +var goarch = limayaml.NewArch(runtime.GOARCH) |
| 18 | + |
| 19 | +var instance = Instance{ |
| 20 | + Name: "foo", |
| 21 | + Status: StatusStopped, |
| 22 | + VMType: vmtype, |
| 23 | + Arch: goarch, |
| 24 | + Dir: "dir", |
| 25 | +} |
| 26 | + |
| 27 | +var table = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" + |
| 28 | + "foo Stopped 127.0.0.1:0 0 0B 0B dir\n" |
| 29 | + |
| 30 | +var tableEmu = "NAME STATUS SSH ARCH CPUS MEMORY DISK DIR\n" + |
| 31 | + "foo Stopped 127.0.0.1:0 unknown 0 0B 0B dir\n" |
| 32 | + |
| 33 | +var tableHome = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" + |
| 34 | + "foo Stopped 127.0.0.1:0 0 0B 0B ~" + separator + "dir\n" |
| 35 | + |
| 36 | +var tableAll = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" + |
| 37 | + "foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n" |
| 38 | + |
| 39 | +// for width 60, everything is hidden |
| 40 | +var table60 = "NAME STATUS SSH CPUS MEMORY DISK\n" + |
| 41 | + "foo Stopped 127.0.0.1:0 0 0B 0B\n" |
| 42 | + |
| 43 | +// for width 80, identical is hidden (type/arch) |
| 44 | +var table80i = "NAME STATUS SSH CPUS MEMORY DISK DIR\n" + |
| 45 | + "foo Stopped 127.0.0.1:0 0 0B 0B dir\n" |
| 46 | + |
| 47 | +// for width 80, different arch is still shown (not dir) |
| 48 | +var table80d = "NAME STATUS SSH ARCH CPUS MEMORY DISK\n" + |
| 49 | + "foo Stopped 127.0.0.1:0 unknown 0 0B 0B\n" |
| 50 | + |
| 51 | +// for width 100, nothing is hidden |
| 52 | +var table100 = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK DIR\n" + |
| 53 | + "foo Stopped 127.0.0.1:0 " + vmtype + " " + goarch + " 0 0B 0B dir\n" |
| 54 | + |
| 55 | +// for width 80, directory is hidden (if not identical) |
| 56 | +var tableTwo = "NAME STATUS SSH VMTYPE ARCH CPUS MEMORY DISK\n" + |
| 57 | + "foo Stopped 127.0.0.1:0 qemu x86_64 0 0B 0B\n" + |
| 58 | + "bar Stopped 127.0.0.1:0 vz aarch64 0 0B 0B\n" |
| 59 | + |
| 60 | +func TestPrintInstanceTable(t *testing.T) { |
| 61 | + var buf bytes.Buffer |
| 62 | + instances := []*Instance{&instance} |
| 63 | + PrintInstances(&buf, instances, "table", nil) |
| 64 | + assert.Equal(t, table, buf.String()) |
| 65 | +} |
| 66 | + |
| 67 | +func TestPrintInstanceTableEmu(t *testing.T) { |
| 68 | + var buf bytes.Buffer |
| 69 | + instance1 := instance |
| 70 | + instance1.Arch = "unknown" |
| 71 | + instances := []*Instance{&instance1} |
| 72 | + PrintInstances(&buf, instances, "table", nil) |
| 73 | + assert.Equal(t, tableEmu, buf.String()) |
| 74 | +} |
| 75 | + |
| 76 | +func TestPrintInstanceTableHome(t *testing.T) { |
| 77 | + var buf bytes.Buffer |
| 78 | + u, err := user.Current() |
| 79 | + assert.NilError(t, err) |
| 80 | + instance1 := instance |
| 81 | + instance1.Dir = filepath.Join(u.HomeDir, "dir") |
| 82 | + instances := []*Instance{&instance1} |
| 83 | + PrintInstances(&buf, instances, "table", nil) |
| 84 | + assert.Equal(t, tableHome, buf.String()) |
| 85 | +} |
| 86 | + |
| 87 | +func TestPrintInstanceTable60(t *testing.T) { |
| 88 | + var buf bytes.Buffer |
| 89 | + instances := []*Instance{&instance} |
| 90 | + options := PrintOptions{TerminalWidth: 60} |
| 91 | + PrintInstances(&buf, instances, "table", &options) |
| 92 | + assert.Equal(t, table60, buf.String()) |
| 93 | +} |
| 94 | + |
| 95 | +func TestPrintInstanceTable80SameArch(t *testing.T) { |
| 96 | + var buf bytes.Buffer |
| 97 | + instances := []*Instance{&instance} |
| 98 | + options := PrintOptions{TerminalWidth: 80} |
| 99 | + PrintInstances(&buf, instances, "table", &options) |
| 100 | + assert.Equal(t, table80i, buf.String()) |
| 101 | +} |
| 102 | + |
| 103 | +func TestPrintInstanceTable80DiffArch(t *testing.T) { |
| 104 | + var buf bytes.Buffer |
| 105 | + instance1 := instance |
| 106 | + instance1.Arch = limayaml.NewArch("unknown") |
| 107 | + instances := []*Instance{&instance1} |
| 108 | + options := PrintOptions{TerminalWidth: 80} |
| 109 | + PrintInstances(&buf, instances, "table", &options) |
| 110 | + assert.Equal(t, table80d, buf.String()) |
| 111 | +} |
| 112 | + |
| 113 | +func TestPrintInstanceTable100(t *testing.T) { |
| 114 | + var buf bytes.Buffer |
| 115 | + instances := []*Instance{&instance} |
| 116 | + options := PrintOptions{TerminalWidth: 100} |
| 117 | + PrintInstances(&buf, instances, "table", &options) |
| 118 | + assert.Equal(t, table100, buf.String()) |
| 119 | +} |
| 120 | + |
| 121 | +func TestPrintInstanceTableAll(t *testing.T) { |
| 122 | + var buf bytes.Buffer |
| 123 | + instances := []*Instance{&instance} |
| 124 | + options := PrintOptions{TerminalWidth: 40, AllFields: true} |
| 125 | + PrintInstances(&buf, instances, "table", &options) |
| 126 | + assert.Equal(t, tableAll, buf.String()) |
| 127 | +} |
| 128 | + |
| 129 | +func TestPrintInstanceTableTwo(t *testing.T) { |
| 130 | + var buf bytes.Buffer |
| 131 | + instance1 := instance |
| 132 | + instance1.Name = "foo" |
| 133 | + instance1.VMType = limayaml.QEMU |
| 134 | + instance1.Arch = limayaml.X8664 |
| 135 | + instance2 := instance |
| 136 | + instance2.Name = "bar" |
| 137 | + instance2.VMType = limayaml.VZ |
| 138 | + instance2.Arch = limayaml.AARCH64 |
| 139 | + instances := []*Instance{&instance1, &instance2} |
| 140 | + options := PrintOptions{TerminalWidth: 80} |
| 141 | + PrintInstances(&buf, instances, "table", &options) |
| 142 | + assert.Equal(t, tableTwo, buf.String()) |
| 143 | +} |
0 commit comments