github.com/midokura/kubeedge@v1.2.0-mido.0/tests/e2e/utils/context.go (about) 1 /* 2 Copyright 2019 The KubeEdge 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 package utils 17 18 import ( 19 "crypto/tls" 20 "io" 21 "net/http" 22 "net/url" 23 "sort" 24 "strings" 25 "time" 26 ) 27 28 //Test context struct 29 type TestContext struct { 30 Cfg Config 31 } 32 33 //NewTestContext function to build testcontext with provided config. 34 func NewTestContext(cfg Config) *TestContext { 35 return &TestContext{ 36 Cfg: cfg, 37 } 38 } 39 40 //SendHttpRequest Function to prepare the http req and send 41 func SendHttpRequest(method, reqApi string) (error, *http.Response) { 42 var body io.Reader 43 var resp *http.Response 44 45 tr := &http.Transport{ 46 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, 47 } 48 client := &http.Client{ 49 Transport: tr, 50 } 51 req, err := http.NewRequest(method, reqApi, body) 52 if err != nil { 53 // handle error 54 Fatalf("Frame HTTP request failed: %v", err) 55 return err, resp 56 } 57 req.Header.Set("Content-Type", "application/json") 58 t := time.Now() 59 resp, err = client.Do(req) 60 if err != nil { 61 // handle error 62 Fatalf("HTTP request is failed :%v", err) 63 return err, resp 64 } 65 Infof("%s %s %v in %v", req.Method, req.URL, resp.Status, time.Now().Sub(t)) 66 return nil, resp 67 } 68 69 //MapLabels function add label selector 70 func MapLabels(ls map[string]string) string { 71 selector := make([]string, 0, len(ls)) 72 for key, value := range ls { 73 selector = append(selector, key+"="+value) 74 } 75 sort.StringSlice(selector).Sort() 76 return url.QueryEscape(strings.Join(selector, ",")) 77 }