-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests_common.go
154 lines (136 loc) · 3.84 KB
/
tests_common.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package config
import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/viper"
"go.uber.org/goleak"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func StringToCfgForTests(t *testing.T, yaml []byte) (Config, Config) {
tmp := C
C = Config{}
viper.Reset()
viper.SetConfigType("yaml")
if err := viper.ReadConfig(bytes.NewBuffer(yaml)); err != nil {
t.Errorf("Can't read config: %s\ngot %s", yaml, err)
}
err := viper.Unmarshal(&C)
if err != nil {
t.Errorf("unable to decode into struct, %v", err)
}
if C.ClientId == string("") {
t.Errorf("Expected C.ClientId not empty got %v\n", C)
}
if C.ConfigVersion == string("") {
t.Errorf("Expected C.ConfigVersion not empty got %v\n", C)
}
return C, tmp
}
func LoadCfgForTests(t *testing.T, CfgFile string) (Config, Config) {
tmp := C
viper.Reset()
C = Config{}
viper.SetConfigFile(CfgFile)
// t.Logf("Loaded: %v", CfgFile)
if err := viper.ReadInConfig(); err != nil {
t.Errorf("Can't read config: %v", err)
}
err := viper.Unmarshal(&C)
if err != nil {
t.Errorf("unable to decode into struct, %v", err)
}
if C.ClientId == string("") {
t.Errorf("Expected C.ClientId not empty got %v\n", C)
}
if C.ConfigVersion == string("") {
t.Errorf("Expected C.ConfigVersion not empty got %v\n", C)
}
return C, tmp
}
func NewTestServer(t *testing.T, in func() interface{}, out func(string)) *httptest.Server {
// Response server.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := in()
js, err := json.Marshal(&c)
if err != nil {
log.Warningf("Failed to marshal for '%v' due %v", c, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if _, errWrite := w.Write(js); errWrite != nil {
t.Errorf("Can't w.Write %v due %v\n", js, err)
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("ReadAll %s", err)
}
out(fmt.Sprintf("%s", b))
}))
return srv
}
func NewFlakyTestServer(t *testing.T, out <-chan string, isFlakyNow <-chan bool, maxTimeout time.Duration) *httptest.Server {
//s1 := rand.NewSource(time.Now().UnixNano())
//r1 := rand.New(s1)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-isFlakyNow:
http.Error(w, "Flaky response ", http.StatusInternalServerError)
return
default:
}
//if r1.Intn(2) != 0 {
// t.Log("Flaky response")
// http.Error(w, "Flaky response ", http.StatusInternalServerError)
// return
//}
var response []byte
var gotBody map[string]interface{}
var expectInBody map[string]interface{}
select {
case data := <-out:
response = []byte(data)
//t.Logf("Reading out %v", data)
case <-time.After(maxTimeout):
t.Fatalf("timed out")
}
byt, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatalf("ReadAll %s", err)
}
if err := json.Unmarshal(byt, &gotBody); err != nil {
t.Fatalf("Cannot unmarsharl to gotBody %v got %v", byt, err)
}
if err := json.Unmarshal(response, &expectInBody); err != nil {
t.Fatalf("Cannot unmarsharl to expectInBody %v got %v", byt, err)
}
for k, v := range expectInBody {
valInBody, ok := gotBody[k]
if !ok {
t.Fatalf("Key '%s' not exists in body", k)
} else if fmt.Sprintf("%v", valInBody) != fmt.Sprintf("%v", v) {
t.Fatalf("Body '%v' != expected '%v'", valInBody, v)
}
}
js, err := json.Marshal(&response)
if err != nil {
log.Errorf("Failed to marshal for '%v' due %v", response, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, errWrite := w.Write(response); errWrite != nil {
t.Fatalf("Can't w.Write %v due %v\n", js, err)
}
w.WriteHeader(http.StatusOK)
}))
return srv
}