|
| 1 | +package cidata |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "net/url" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/lima-vm/lima/pkg/limayaml" |
| 10 | + qemu "github.com/lima-vm/lima/pkg/qemu/const" |
| 11 | + "github.com/xorcare/pointer" |
| 12 | + "gotest.tools/v3/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func fakeLookupIP(host string) []net.IP { |
| 16 | + return []net.IP{net.IPv4(127, 0, 0, 0)} |
| 17 | +} |
| 18 | + |
| 19 | +func TestSetupEnv(t *testing.T) { |
| 20 | + netLookupIP = fakeLookupIP |
| 21 | + urlMustParse := func(urlStr string) *url.URL { |
| 22 | + u, err := url.Parse(urlStr) |
| 23 | + if err != nil { |
| 24 | + panic(err) |
| 25 | + } |
| 26 | + |
| 27 | + return u |
| 28 | + } |
| 29 | + |
| 30 | + httpProxyCases := []*url.URL{ |
| 31 | + urlMustParse("http://127.0.0.1"), |
| 32 | + urlMustParse("http://127.0.0.1:8080"), |
| 33 | + urlMustParse("https://127.0.0.1:8080"), |
| 34 | + urlMustParse("sock4://127.0.0.1:8080"), |
| 35 | + urlMustParse("sock5://127.0.0.1:8080"), |
| 36 | + urlMustParse("http://127.0.0.1:8080/"), |
| 37 | + urlMustParse("http://127.0.0.1:8080/path"), |
| 38 | + urlMustParse("http://localhost:8080"), |
| 39 | + urlMustParse("http://localhost:8080/"), |
| 40 | + urlMustParse("http://localhost:8080/path"), |
| 41 | + urlMustParse("http://docker.for.mac.localhost:8080"), |
| 42 | + urlMustParse("http://docker.for.mac.localhost:8080/"), |
| 43 | + urlMustParse("http://docker.for.mac.localhost:8080/path"), |
| 44 | + } |
| 45 | + |
| 46 | + for _, httpProxy := range httpProxyCases { |
| 47 | + t.Run(httpProxy.Host, func(t *testing.T) { |
| 48 | + envKey := "http_proxy" |
| 49 | + envValue := httpProxy.String() |
| 50 | + envs, err := setupEnv(&limayaml.LimaYAML{PropagateProxyEnv: pointer.Bool(false), Env: map[string]string{envKey: envValue}}) |
| 51 | + assert.NilError(t, err) |
| 52 | + assert.Equal(t, envs[envKey], strings.ReplaceAll(envValue, httpProxy.Hostname(), qemu.SlirpGateway)) |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestSetupInvalidEnv(t *testing.T) { |
| 58 | + envKey := "http_proxy" |
| 59 | + envValue := "://localhost:8080" |
| 60 | + envs, err := setupEnv(&limayaml.LimaYAML{PropagateProxyEnv: pointer.Bool(false), Env: map[string]string{envKey: envValue}}) |
| 61 | + assert.NilError(t, err) |
| 62 | + assert.Equal(t, envs[envKey], envValue) |
| 63 | +} |
0 commit comments