Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/cidata/cidata_test.go
2614 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package cidata
5
6
import (
7
"net"
8
"net/url"
9
"strings"
10
"testing"
11
12
"gotest.tools/v3/assert"
13
14
"github.com/lima-vm/lima/v2/pkg/networks"
15
)
16
17
func fakeLookupIP(_ string) []net.IP {
18
return []net.IP{net.IPv4(127, 0, 0, 0)}
19
}
20
21
func TestSetupEnv(t *testing.T) {
22
netLookupIP = fakeLookupIP
23
urlMustParse := func(urlStr string) *url.URL {
24
u, err := url.Parse(urlStr)
25
assert.NilError(t, err)
26
return u
27
}
28
29
httpProxyCases := []*url.URL{
30
urlMustParse("http://127.0.0.1"),
31
urlMustParse("http://127.0.0.1:8080"),
32
urlMustParse("https://127.0.0.1:8080"),
33
urlMustParse("sock4://127.0.0.1:8080"),
34
urlMustParse("sock5://127.0.0.1:8080"),
35
urlMustParse("http://127.0.0.1:8080/"),
36
urlMustParse("http://127.0.0.1:8080/path"),
37
urlMustParse("http://localhost:8080"),
38
urlMustParse("http://localhost:8080/"),
39
urlMustParse("http://localhost:8080/path"),
40
urlMustParse("http://docker.for.mac.localhost:8080"),
41
urlMustParse("http://docker.for.mac.localhost:8080/"),
42
urlMustParse("http://docker.for.mac.localhost:8080/path"),
43
}
44
45
for _, httpProxy := range httpProxyCases {
46
t.Run(httpProxy.Host, func(t *testing.T) {
47
envKey := "http_proxy"
48
envValue := httpProxy.String()
49
envs, err := setupEnv(map[string]string{envKey: envValue}, false, networks.SlirpGateway)
50
assert.NilError(t, err)
51
assert.Equal(t, envs[envKey], strings.ReplaceAll(envValue, httpProxy.Hostname(), networks.SlirpGateway))
52
})
53
}
54
}
55
56
func TestSetupInvalidEnv(t *testing.T) {
57
envKey := "http_proxy"
58
envValue := "://localhost:8080"
59
envs, err := setupEnv(map[string]string{envKey: envValue}, false, networks.SlirpGateway)
60
assert.NilError(t, err)
61
assert.Equal(t, envs[envKey], envValue)
62
}
63
64