-
Notifications
You must be signed in to change notification settings - Fork 282
/
testing_utilities.go
66 lines (54 loc) · 1.46 KB
/
testing_utilities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2017 Yahoo Holdings, Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package devel
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/AthenZ/athenz/utils/zpe-updater/util"
"net"
)
func StartMockServer(endPoints map[string]string, metricEndPoints []string) string {
router := mux.NewRouter()
for key, val := range endPoints {
router.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, string(val))
}).Methods("GET")
}
for _, domain := range metricEndPoints {
router.HandleFunc(domain, func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("Could not read the body, error: %v", err)
}
io.WriteString(w, string(body))
}).Methods("POST")
}
listener, err := net.Listen("tcp", ":0")
if err != nil {
log.Panicln("Unable to serve on randomly assigned port")
}
s := &http.Server{Handler: router}
addr := listener.Addr().String()
go func() {
s.Serve(listener)
}()
return addr
}
func CreateFile(fileName, content string) error {
if util.Exists(fileName) {
err := os.Remove(fileName)
if err != nil {
return fmt.Errorf("Unable to remove file: %v, Error:%v", fileName, err)
}
}
err := ioutil.WriteFile(fileName, []byte(content), 0755)
if err != nil {
return fmt.Errorf("Unable to write file: %v, Error:%v", fileName, err)
}
return nil
}