Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/limatmpl/locator_test.go
2621 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package limatmpl_test
5
6
import (
7
"fmt"
8
"runtime"
9
"testing"
10
11
"gotest.tools/v3/assert"
12
13
"github.com/lima-vm/lima/v2/pkg/limatmpl"
14
"github.com/lima-vm/lima/v2/pkg/limatype"
15
)
16
17
func TestInstNameFromImageURL(t *testing.T) {
18
t.Run("strips image format and compression method", func(t *testing.T) {
19
name := limatmpl.InstNameFromImageURL("linux.iso.bz2", "unknown")
20
assert.Equal(t, name, "linux")
21
})
22
t.Run("removes generic tags", func(t *testing.T) {
23
name := limatmpl.InstNameFromImageURL("linux-linux_cloudimg.base-x86_64.raw", "unknown")
24
assert.Equal(t, name, "linux-x86_64")
25
})
26
t.Run("removes Alpine `nocloud_` prefix", func(t *testing.T) {
27
name := limatmpl.InstNameFromImageURL("nocloud_linux-x86_64.raw", "unknown")
28
assert.Equal(t, name, "linux-x86_64")
29
})
30
t.Run("removes date tag", func(t *testing.T) {
31
name := limatmpl.InstNameFromImageURL("linux-20250101.raw", "unknown")
32
assert.Equal(t, name, "linux")
33
})
34
t.Run("removes date tag including time", func(t *testing.T) {
35
name := limatmpl.InstNameFromImageURL("linux-20250101-2000.raw", "unknown")
36
assert.Equal(t, name, "linux")
37
})
38
t.Run("removes date tag including zero time", func(t *testing.T) {
39
name := limatmpl.InstNameFromImageURL("linux-20250101.0.raw", "unknown")
40
assert.Equal(t, name, "linux")
41
})
42
t.Run("replace arch with archlinux", func(t *testing.T) {
43
name := limatmpl.InstNameFromImageURL("arch-aarch64.raw", "unknown")
44
assert.Equal(t, name, "archlinux-aarch64")
45
})
46
t.Run("don't replace arch in the middle of the name", func(t *testing.T) {
47
name := limatmpl.InstNameFromImageURL("my-arch-aarch64.raw", "unknown")
48
assert.Equal(t, name, "my-arch-aarch64")
49
})
50
t.Run("removes native arch", func(t *testing.T) {
51
arch := limatype.NewArch(runtime.GOARCH)
52
image := fmt.Sprintf("linux_cloudimg.base-%s.qcow2.gz", arch)
53
name := limatmpl.InstNameFromImageURL(image, arch)
54
assert.Equal(t, name, "linux")
55
})
56
t.Run("removes redundant major version", func(t *testing.T) {
57
name := limatmpl.InstNameFromImageURL("rocky-8-8.10.raw", "unknown")
58
assert.Equal(t, name, "rocky-8.10")
59
})
60
t.Run("don't remove non-redundant major version", func(t *testing.T) {
61
name := limatmpl.InstNameFromImageURL("rocky-8-9.10.raw", "unknown")
62
assert.Equal(t, name, "rocky-8-9.10")
63
})
64
}
65
66
func TestSeemsTemplateURL(t *testing.T) {
67
arg := "template:foo/bar"
68
t.Run(arg, func(t *testing.T) {
69
is, name := limatmpl.SeemsTemplateURL(arg)
70
assert.Equal(t, is, true)
71
assert.Equal(t, name, "foo/bar")
72
})
73
notTemplateURLs := []string{
74
"file:///foo",
75
"http://foo",
76
"https://foo",
77
"foo",
78
}
79
for _, arg := range notTemplateURLs {
80
t.Run(arg, func(t *testing.T) {
81
is, _ := limatmpl.SeemsTemplateURL(arg)
82
assert.Equal(t, is, false)
83
})
84
}
85
}
86
87
func TestSeemsHTTPURL(t *testing.T) {
88
httpURLs := []string{
89
"http://foo/",
90
"https://foo/",
91
}
92
for _, arg := range httpURLs {
93
t.Run(arg, func(t *testing.T) {
94
assert.Equal(t, limatmpl.SeemsHTTPURL(arg), true)
95
})
96
}
97
notHTTPURLs := []string{
98
"file:///foo",
99
"template:foo",
100
"foo",
101
}
102
for _, arg := range notHTTPURLs {
103
t.Run(arg, func(t *testing.T) {
104
assert.Equal(t, limatmpl.SeemsHTTPURL(arg), false)
105
})
106
}
107
}
108
109
func TestSeemsFileURL(t *testing.T) {
110
arg := "file:///foo"
111
t.Run(arg, func(t *testing.T) {
112
assert.Equal(t, limatmpl.SeemsFileURL(arg), true)
113
})
114
notFileURLs := []string{
115
"http://foo",
116
"https://foo",
117
"template:foo",
118
"foo",
119
}
120
for _, arg := range notFileURLs {
121
t.Run(arg, func(t *testing.T) {
122
assert.Equal(t, limatmpl.SeemsFileURL(arg), false)
123
})
124
}
125
}
126
127