|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + gohttp "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "kcl-lang.io/kpm/pkg/client" |
| 15 | +) |
| 16 | + |
| 17 | +func TestModCmdWithSkipTlsVerify(t *testing.T) { |
| 18 | + var buf bytes.Buffer |
| 19 | + |
| 20 | + mux := gohttp.NewServeMux() |
| 21 | + mux.HandleFunc("/", func(w gohttp.ResponseWriter, r *gohttp.Request) { |
| 22 | + buf.WriteString("Called Success\n") |
| 23 | + fmt.Fprintln(w, "Hello, client") |
| 24 | + }) |
| 25 | + |
| 26 | + mux.HandleFunc("/subpath/tags/list", func(w gohttp.ResponseWriter, r *gohttp.Request) { |
| 27 | + buf.WriteString("Called Success\n") |
| 28 | + fmt.Fprintln(w, "Hello, client") |
| 29 | + }) |
| 30 | + |
| 31 | + mux.HandleFunc("/subpath", func(w gohttp.ResponseWriter, r *gohttp.Request) { |
| 32 | + fmt.Fprintln(w, "Hello from subpath") |
| 33 | + }) |
| 34 | + |
| 35 | + ts := httptest.NewTLSServer(mux) |
| 36 | + defer ts.Close() |
| 37 | + |
| 38 | + fmt.Printf("ts.URL: %v\n", ts.URL) |
| 39 | + turl, err := url.Parse(ts.URL) |
| 40 | + assert.Equal(t, err, nil) |
| 41 | + turl.Scheme = "oci" |
| 42 | + turl.Path = filepath.Join(turl.Path, "subpath") |
| 43 | + fmt.Printf("turl.String(): %v\n", turl.String()) |
| 44 | + |
| 45 | + kpmcli, err := client.NewKpmClient() |
| 46 | + assert.Equal(t, err, nil) |
| 47 | + |
| 48 | + originalDir, err := os.Getwd() |
| 49 | + assert.NoError(t, err) |
| 50 | + testRootDir := filepath.Join(originalDir, "test_data") |
| 51 | + |
| 52 | + runTest := func(testDir string, testFunc func(), beforeTestFuncs []func()) { |
| 53 | + if testDir != "" { |
| 54 | + err = os.Chdir(filepath.Join(testRootDir, testDir)) |
| 55 | + assert.NoError(t, err) |
| 56 | + } |
| 57 | + |
| 58 | + for _, beforeTestFunc := range beforeTestFuncs { |
| 59 | + beforeTestFunc() |
| 60 | + } |
| 61 | + |
| 62 | + testFunc() |
| 63 | + assert.Equal(t, buf.String(), "Called Success\n") |
| 64 | + buf.Reset() |
| 65 | + defer func() { |
| 66 | + err := os.Chdir(originalDir) |
| 67 | + assert.NoError(t, err) |
| 68 | + }() |
| 69 | + } |
| 70 | + |
| 71 | + genKclModWithDep := func(depUrl string) { |
| 72 | + fmt.Println("Executing extra function for test_mod_graph") |
| 73 | + kclModContent := fmt.Sprintf(`[package] |
| 74 | +name = "test_mod" |
| 75 | +edition = "v0.10.0" |
| 76 | +version = "0.0.1" |
| 77 | +
|
| 78 | +[dependencies] |
| 79 | +dep1 = { oci = "%s"} |
| 80 | + `, depUrl) |
| 81 | + err := os.WriteFile("kcl.mod", []byte(kclModContent), 0644) |
| 82 | + assert.NoError(t, err) |
| 83 | + } |
| 84 | + |
| 85 | + runTest("", func() { |
| 86 | + fmt.Println("test_mod_pull") |
| 87 | + cmd := NewModPullCmd(kpmcli) |
| 88 | + cmd.SetArgs([]string{turl.String(), "--insecure-skip-tls-verify"}) |
| 89 | + _ = cmd.Execute() |
| 90 | + }, []func(){}) |
| 91 | + |
| 92 | + runTest("test_mod_push", func() { |
| 93 | + fmt.Println("test_mod_push") |
| 94 | + cmd := NewModPushCmd(kpmcli) |
| 95 | + cmd.SetArgs([]string{turl.String(), "--insecure-skip-tls-verify"}) |
| 96 | + _ = cmd.Execute() |
| 97 | + }, []func(){}) |
| 98 | + |
| 99 | + runTest("test_mod_add", func() { |
| 100 | + fmt.Println("test_mod_add") |
| 101 | + cmd := NewModAddCmd(kpmcli) |
| 102 | + cmd.SetArgs([]string{turl.String(), "--insecure-skip-tls-verify"}) |
| 103 | + _ = cmd.Execute() |
| 104 | + }, []func(){}) |
| 105 | + |
| 106 | + runTest("test_mod_graph", func() { |
| 107 | + fmt.Println("test_mod_graph") |
| 108 | + cmd := NewModGraphCmd(kpmcli) |
| 109 | + cmd.SetArgs([]string{"--insecure-skip-tls-verify"}) |
| 110 | + _ = cmd.Execute() |
| 111 | + }, []func(){ |
| 112 | + func() { genKclModWithDep(turl.String()) }, |
| 113 | + }) |
| 114 | + |
| 115 | + runTest("test_mod_metadata", func() { |
| 116 | + fmt.Println("test_mod_metadata") |
| 117 | + cmd := NewModMetadataCmd(kpmcli) |
| 118 | + cmd.SetArgs([]string{"--update", "--insecure-skip-tls-verify"}) |
| 119 | + _ = cmd.Execute() |
| 120 | + }, []func(){ |
| 121 | + func() { genKclModWithDep(turl.String()) }, |
| 122 | + }) |
| 123 | + |
| 124 | + runTest("test_mod_update", func() { |
| 125 | + fmt.Println("test_mod_update") |
| 126 | + cmd := NewModUpdateCmd(kpmcli) |
| 127 | + cmd.SetArgs([]string{"--insecure-skip-tls-verify"}) |
| 128 | + _ = cmd.Execute() |
| 129 | + }, []func(){ |
| 130 | + func() { genKclModWithDep(turl.String()) }, |
| 131 | + }) |
| 132 | +} |
0 commit comments