github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/framework/e2e/curl.go (about) 1 // Copyright 2021 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package e2e 16 17 import ( 18 "fmt" 19 "math/rand" 20 "strings" 21 ) 22 23 type CURLReq struct { 24 Username string 25 Password string 26 27 IsTLS bool 28 Timeout int 29 30 Endpoint string 31 32 Value string 33 Expected string 34 Header string 35 36 MetricsURLScheme string 37 38 Ciphers string 39 } 40 41 // CURLPrefixArgs builds the beginning of a curl command for a given key 42 // addressed to a random URL in the given cluster. 43 func CURLPrefixArgs(cfg *EtcdProcessClusterConfig, member EtcdProcess, method string, req CURLReq) []string { 44 var ( 45 cmdArgs = []string{"curl"} 46 acurl = member.Config().Acurl 47 ) 48 if req.MetricsURLScheme != "https" { 49 if req.IsTLS { 50 if cfg.ClientTLS != ClientTLSAndNonTLS { 51 panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS") 52 } 53 cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath) 54 acurl = ToTLS(member.Config().Acurl) 55 } else if cfg.ClientTLS == ClientTLS { 56 if !cfg.NoCN { 57 cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath) 58 } else { 59 cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath3, "--key", PrivateKeyPath3) 60 } 61 } 62 } 63 if req.MetricsURLScheme != "" { 64 acurl = member.EndpointsMetrics()[0] 65 } 66 ep := acurl + req.Endpoint 67 68 if req.Username != "" || req.Password != "" { 69 cmdArgs = append(cmdArgs, "-L", "-u", fmt.Sprintf("%s:%s", req.Username, req.Password), ep) 70 } else { 71 cmdArgs = append(cmdArgs, "-L", ep) 72 } 73 if req.Timeout != 0 { 74 cmdArgs = append(cmdArgs, "-m", fmt.Sprintf("%d", req.Timeout)) 75 } 76 77 if req.Header != "" { 78 cmdArgs = append(cmdArgs, "-H", req.Header) 79 } 80 81 if req.Ciphers != "" { 82 cmdArgs = append(cmdArgs, "--ciphers", req.Ciphers) 83 } 84 85 switch method { 86 case "POST", "PUT": 87 dt := req.Value 88 if !strings.HasPrefix(dt, "{") { // for non-JSON value 89 dt = "value=" + dt 90 } 91 cmdArgs = append(cmdArgs, "-X", method, "-d", dt) 92 } 93 return cmdArgs 94 } 95 96 func CURLPost(clus *EtcdProcessCluster, req CURLReq) error { 97 return SpawnWithExpect(CURLPrefixArgs(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "POST", req), req.Expected) 98 } 99 100 func CURLPut(clus *EtcdProcessCluster, req CURLReq) error { 101 return SpawnWithExpect(CURLPrefixArgs(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "PUT", req), req.Expected) 102 } 103 104 func CURLGet(clus *EtcdProcessCluster, req CURLReq) error { 105 return SpawnWithExpect(CURLPrefixArgs(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "GET", req), req.Expected) 106 }