| 1 | |
| 2 | |
| 3 | |
| 4 | package cidata |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net" |
| 10 | "net/url" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "gotest.tools/v3/assert" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/iso9660util" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 18 | "github.com/lima-vm/lima/v2/pkg/networks" |
| 19 | ) |
| 20 | |
| 21 | func fakeLookupIP(_ string) []net.IP { |
| 22 | return []net.IP{net.IPv4(127, 0, 0, 0)} |
| 23 | } |
| 24 | |
| 25 | func TestSetupEnv(t *testing.T) { |
| 26 | netLookupIP = fakeLookupIP |
| 27 | urlMustParse := func(urlStr string) *url.URL { |
| 28 | u, err := url.Parse(urlStr) |
| 29 | assert.NilError(t, err) |
| 30 | return u |
| 31 | } |
| 32 | |
| 33 | httpProxyCases := []*url.URL{ |
| 34 | urlMustParse("http://127.0.0.1"), |
| 35 | urlMustParse("http://127.0.0.1:8080"), |
| 36 | urlMustParse("https://127.0.0.1:8080"), |
| 37 | urlMustParse("sock4://127.0.0.1:8080"), |
| 38 | urlMustParse("sock5://127.0.0.1:8080"), |
| 39 | urlMustParse("http://127.0.0.1:8080/"), |
| 40 | urlMustParse("http://127.0.0.1:8080/path"), |
| 41 | urlMustParse("http://localhost:8080"), |
| 42 | urlMustParse("http://localhost:8080/"), |
| 43 | urlMustParse("http://localhost:8080/path"), |
| 44 | urlMustParse("http://docker.for.mac.localhost:8080"), |
| 45 | urlMustParse("http://docker.for.mac.localhost:8080/"), |
| 46 | urlMustParse("http://docker.for.mac.localhost:8080/path"), |
| 47 | } |
| 48 | |
| 49 | for _, httpProxy := range httpProxyCases { |
| 50 | t.Run(httpProxy.Host, func(t *testing.T) { |
| 51 | envKey := "http_proxy" |
| 52 | envValue := httpProxy.String() |
| 53 | envs, err := setupEnv(map[string]string{envKey: envValue}, false, networks.SlirpGateway) |
| 54 | assert.NilError(t, err) |
| 55 | assert.Equal(t, envs[envKey], strings.ReplaceAll(envValue, httpProxy.Hostname(), networks.SlirpGateway)) |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestSetupInvalidEnv(t *testing.T) { |
| 61 | envKey := "http_proxy" |
| 62 | envValue := "://localhost:8080" |
| 63 | envs, err := setupEnv(map[string]string{envKey: envValue}, false, networks.SlirpGateway) |
| 64 | assert.NilError(t, err) |
| 65 | assert.Equal(t, envs[envKey], envValue) |
| 66 | } |
| 67 | |
| 68 | func TestAppendProvisionEntries(t *testing.T) { |
| 69 | scriptBoot := "Write-Host boot" |
| 70 | scriptSystem := "Write-Host system" |
| 71 | contentData := "hello" |
| 72 | exprYQ := ".foo=1" |
| 73 | provisions := []limatype.Provision{ |
| 74 | {Mode: limatype.ProvisionModeBoot, Script: &scriptBoot}, |
| 75 | {Mode: limatype.ProvisionModeSystem, Script: &scriptSystem}, |
| 76 | {Mode: limatype.ProvisionModeData, ProvisionData: limatype.ProvisionData{Content: &contentData}}, |
| 77 | {Mode: limatype.ProvisionModeYQ, Expression: &exprYQ}, |
| 78 | } |
| 79 | |
| 80 | layout, err := appendProvisionEntries(nil, provisions) |
| 81 | assert.NilError(t, err) |
| 82 | |
| 83 | assert.Assert(t, !hasEntryPath(layout, "provision.boot/00000000")) |
| 84 | assert.Assert(t, hasEntryPath(layout, "provision.data/00000002")) |
| 85 | assert.Assert(t, hasEntryPath(layout, "provision.yq/00000003")) |
| 86 | |
| 87 | gotData, err := readEntry(layout, "provision.data/00000002") |
| 88 | assert.NilError(t, err) |
| 89 | assert.Equal(t, gotData, contentData) |
| 90 | } |
| 91 | |
| 92 | func hasEntryPath(layout []iso9660util.Entry, path string) bool { |
| 93 | for _, e := range layout { |
| 94 | if e.Path == path { |
| 95 | return true |
| 96 | } |
| 97 | } |
| 98 | return false |
| 99 | } |
| 100 | |
| 101 | func readEntry(layout []iso9660util.Entry, path string) (string, error) { |
| 102 | for _, e := range layout { |
| 103 | if e.Path != path { |
| 104 | continue |
| 105 | } |
| 106 | b, err := io.ReadAll(e.Reader) |
| 107 | if err != nil { |
| 108 | return "", err |
| 109 | } |
| 110 | return string(b), nil |
| 111 | } |
| 112 | return "", fmt.Errorf("entry not found: %s", path) |
| 113 | } |