github.com/oam-dev/kubevela@v1.9.11/e2e/addon/mock/vela_addon_mock_server.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "embed" 21 "encoding/xml" 22 "fmt" 23 "html/template" 24 "io/fs" 25 "log" 26 "net/http" 27 "os" 28 "path" 29 "strings" 30 31 "github.com/oam-dev/kubevela/e2e/addon/mock/utils" 32 "github.com/oam-dev/kubevela/pkg/addon" 33 ) 34 35 var ( 36 //go:embed testdata 37 testData embed.FS 38 paths []struct { 39 path string 40 length int64 41 } 42 ) 43 44 func main() { 45 err := utils.ApplyMockServerConfig() 46 if err != nil { 47 log.Fatal(err) 48 } 49 http.HandleFunc("/", ossHandler) 50 http.HandleFunc("/helm/", helmHandler) 51 err = http.ListenAndServe(fmt.Sprintf(":%d", utils.Port), nil) 52 if err != nil { 53 log.Fatal("ListenAndServe: ", err) 54 } 55 } 56 57 var ossHandler http.HandlerFunc = func(rw http.ResponseWriter, req *http.Request) { 58 queryPath := strings.TrimPrefix(req.URL.Path, "/") 59 60 if strings.Contains(req.URL.RawQuery, "prefix") { 61 prefix := req.URL.Query().Get("prefix") 62 res := addon.ListBucketResult{ 63 Files: []addon.File{}, 64 Count: 0, 65 } 66 for _, p := range paths { 67 if strings.HasPrefix(p.path, prefix) { 68 res.Files = append(res.Files, addon.File{Name: p.path, Size: int(p.length)}) 69 res.Count++ 70 } 71 } 72 data, err := xml.Marshal(res) 73 error := map[string]error{"error": err} 74 // Make and parse the data 75 t, err := template.New("").Parse(string(data)) 76 if err != nil { 77 // Render the data 78 t.Execute(rw, error) 79 } 80 // Render the data 81 t.Execute(rw, data) 82 83 } else { 84 found := false 85 for _, p := range paths { 86 if queryPath == p.path { 87 file, err := testData.ReadFile(path.Join("testdata", queryPath)) 88 error := map[string]error{"error": err} 89 // Make and parse the data 90 t, err := template.New("").Parse(string(file)) 91 if err != nil { 92 // Render the data 93 t.Execute(rw, error) 94 } 95 found = true 96 t.Execute(rw, file) 97 break 98 } 99 } 100 if !found { 101 nf := "not found" 102 t, _ := template.New("").Parse(nf) 103 t.Execute(rw, nf) 104 } 105 } 106 } 107 108 var helmHandler http.HandlerFunc = func(rw http.ResponseWriter, req *http.Request) { 109 switch { 110 case strings.Contains(req.URL.Path, "index.yaml"): 111 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/index.yaml") 112 if err != nil { 113 _, _ = rw.Write([]byte(err.Error())) 114 } 115 rw.Write(file) 116 case strings.Contains(req.URL.Path, "fluxcd-test-version-1.0.0.tgz"): 117 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/fluxcd-test-version-1.0.0.tgz") 118 if err != nil { 119 _, _ = rw.Write([]byte(err.Error())) 120 } 121 rw.Write(file) 122 case strings.Contains(req.URL.Path, "fluxcd-test-version-2.0.0.tgz"): 123 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/fluxcd-test-version-2.0.0.tgz") 124 if err != nil { 125 _, _ = rw.Write([]byte(err.Error())) 126 } 127 rw.Write(file) 128 case strings.Contains(req.URL.Path, "vela-workflow-v0.3.5.tgz"): 129 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/vela-workflow-v0.3.5.tgz") 130 if err != nil { 131 _, _ = rw.Write([]byte(err.Error())) 132 } 133 rw.Write(file) 134 case strings.Contains(req.URL.Path, "foo-v1.0.0.tgz"): 135 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/foo-v1.0.0.tgz") 136 if err != nil { 137 _, _ = rw.Write([]byte(err.Error())) 138 } 139 rw.Write(file) 140 case strings.Contains(req.URL.Path, "bar-v1.0.0.tgz"): 141 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/bar-v1.0.0.tgz") 142 if err != nil { 143 _, _ = rw.Write([]byte(err.Error())) 144 } 145 rw.Write(file) 146 case strings.Contains(req.URL.Path, "bar-v2.0.0.tgz"): 147 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/bar-v2.0.0.tgz") 148 if err != nil { 149 _, _ = rw.Write([]byte(err.Error())) 150 } 151 rw.Write(file) 152 case strings.Contains(req.URL.Path, "mock-be-dep-addon-v1.0.0.tgz"): 153 file, err := os.ReadFile("./e2e/addon/mock/testrepo/helm-repo/mock-be-dep-addon-v1.0.0.tgz") 154 if err != nil { 155 _, _ = rw.Write([]byte(err.Error())) 156 } 157 rw.Write(file) 158 } 159 160 } 161 162 func init() { 163 _ = fs.WalkDir(testData, "testdata", func(path string, d fs.DirEntry, err error) error { 164 path = strings.TrimPrefix(path, "testdata/") 165 path = strings.TrimPrefix(path, "testdata") 166 167 info, _ := d.Info() 168 size := info.Size() 169 if path == "" { 170 return nil 171 } 172 if size == 0 { 173 path += "/" 174 } 175 paths = append(paths, struct { 176 path string 177 length int64 178 }{path: path, length: size}) 179 return nil 180 }) 181 }