mosn

Форк
0
/
http_healthcheck_test.go 
282 строки · 11.1 Кб
1
//go:build MOSNTest
2
// +build MOSNTest
3

4
package healthcheck
5

6
import (
7
	"testing"
8
	"time"
9

10
	"mosn.io/mosn/test/lib"
11
	"mosn.io/mosn/test/lib/http"
12

13
	. "mosn.io/mosn/test/framework"
14
)
15

16
func TestHttpHealthCheck(t *testing.T) {
17
	Scenario(t, "http health checker without config", func() {
18
		_, _ = lib.InitMosn(ConfigHttpHealthCluster, lib.CreateConfig(MockHttpCheckServerConfig))
19
		Case("http health checker fallback to tcp dail", func() {
20
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
21
				TargetAddr: "127.0.0.1:2046",
22
				Request: &http.RequestConfig{
23
					Header: map[string][]string{
24
						"x-test-route": []string{"paas"},
25
					},
26
				},
27
				Verify: &http.VerifyConfig{
28
					ExpectedStatusCode: 200,
29
				},
30
			})
31
			time.Sleep(time.Second * 3)
32
			Verify(client.SyncCall(), Equal, true)
33
		})
34
	})
35
}
36

37
func TestHttpHealthCheckWithConfigFailed(t *testing.T) {
38
	Scenario(t, "http health checker with config error", func() {
39
		_, _ = lib.InitMosn(ConfigHttpHealthCluster2, lib.CreateConfig(MockHttpCheckServerConfig))
40
		Case("http health checker fallback to tcp dail", func() {
41
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
42
				TargetAddr: "127.0.0.1:2046",
43
				Request: &http.RequestConfig{
44
					Header: map[string][]string{
45
						"x-test-route": []string{"paas"},
46
					},
47
				},
48
				Verify: &http.VerifyConfig{
49
					ExpectedStatusCode: 502,
50
				},
51
			})
52
			time.Sleep(time.Second * 3)
53
			Verify(client.SyncCall(), Equal, true)
54
		})
55
	})
56
}
57

58
func TestHttpHealthCheckWithConfigSuccess(t *testing.T) {
59
	Scenario(t, "http health checker with config right", func() {
60
		_, _ = lib.InitMosn(ConfigHttpHealthCluster3, lib.CreateConfig(MockHttpCheckServerConfig))
61
		Case("http health checker fallback to tcp dail", func() {
62
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
63
				TargetAddr: "127.0.0.1:2046",
64
				Request: &http.RequestConfig{
65
					Header: map[string][]string{
66
						"x-test-route": []string{"paas"},
67
					},
68
				},
69
				Verify: &http.VerifyConfig{
70
					ExpectedStatusCode: 200,
71
				},
72
			})
73
			time.Sleep(time.Second * 3)
74
			Verify(client.SyncCall(), Equal, true)
75
		})
76
	})
77
}
78

79
const ConfigHttpHealthCluster = `{
80
		"servers":[
81
                {
82
                        "default_log_path":"stdout",
83
                        "default_log_level": "ERROR",
84
                        "routers": [
85
                                {
86
                                        "router_config_name":"router_to_server",
87
                                        "virtual_hosts":[{
88
                                                "name":"server_hosts",
89
                                                "domains": ["*"],
90
                                                "routers": [
91
                                                        {
92
                                                                "match":{"headers":[{"name":"x-test-route","value":"paas"}]},
93
                                                                "route":{"cluster_name":"server_cluster"}
94
                                                        }
95
                                                ]
96
                                        }]
97
                                }
98
                        ],
99
                        "listeners":[
100
                                {
101
                                        "address":"127.0.0.1:2046",
102
                                        "bind_port": true,
103
                                        "filter_chains": [{
104
                                                "filters": [
105
                                                        {
106
                                                                "type": "proxy",
107
                                                                "config": {
108
                                                                        "downstream_protocol": "Http1",
109
                                                                        "upstream_protocol": "Http1",
110
                                                                        "router_config_name":"router_to_server"
111
                                                                }
112
                                                        }
113
                                                ]
114
                                        }]
115
                                }
116
                        ]
117
                }
118
        ],
119
        "cluster_manager":{
120
                "clusters":[
121
                        {
122
                                "name": "server_cluster",
123
                                "type": "SIMPLE",
124
                                "lb_type": "LB_RANDOM",
125
                                "hosts":[
126
                                        {"address":"127.0.0.1:8080"}
127
                                ],
128
								"health_check": {
129
									  "protocol": "Http1",
130
									  "service_name": "local_service",
131
									  "timeout": "1s",
132
									  "interval": "1s",
133
									  "healthy_threshold": 1,
134
									  "unhealthy_threshold": 1
135
									}
136
                        }
137
                ]
138
        }
139
}`
140

141
const ConfigHttpHealthCluster2 = `{
142
		"servers":[
143
                {
144
                        "default_log_path":"stdout",
145
                        "default_log_level": "ERROR",
146
                        "routers": [
147
                                {
148
                                        "router_config_name":"router_to_server",
149
                                        "virtual_hosts":[{
150
                                                "name":"server_hosts",
151
                                                "domains": ["*"],
152
                                                "routers": [
153
                                                        {
154
                                                                "match":{"headers":[{"name":"x-test-route","value":"paas"}]},
155
                                                                "route":{"cluster_name":"server_cluster"}
156
                                                        }
157
                                                ]
158
                                        }]
159
                                }
160
                        ],
161
                        "listeners":[
162
                                {
163
                                        "address":"127.0.0.1:2046",
164
                                        "bind_port": true,
165
                                        "filter_chains": [{
166
                                                "filters": [
167
                                                        {
168
                                                                "type": "proxy",
169
                                                                "config": {
170
                                                                        "downstream_protocol": "Http1",
171
                                                                        "upstream_protocol": "Http1",
172
                                                                        "router_config_name":"router_to_server"
173
                                                                }
174
                                                        }
175
                                                ]
176
                                        }]
177
                                }
178
                        ]
179
                }
180
        ],
181
        "cluster_manager":{
182
                "clusters":[
183
                        {
184
                                "name": "server_cluster",
185
                                "type": "SIMPLE",
186
                                "lb_type": "LB_RANDOM",
187
                                "hosts":[
188
                                        {"address":"127.0.0.1:8080"}
189
                                ],
190
								"health_check": {
191
									  "protocol": "Http1",
192
									  "service_name": "local_service",
193
									  "timeout": "1s",
194
									  "interval": "1s",
195
									  "healthy_threshold": 1,
196
									  "unhealthy_threshold": 1,
197
									  "check_config": {
198
											"http_check_config":{
199
												"port":12133
200
											}
201
										}
202
									}
203
                        }
204
                ]
205
        }
206
}`
207

208
const ConfigHttpHealthCluster3 = `{
209
		"servers":[
210
                {
211
                        "default_log_path":"stdout",
212
                        "default_log_level": "ERROR",
213
                        "routers": [
214
                                {
215
                                        "router_config_name":"router_to_server",
216
                                        "virtual_hosts":[{
217
                                                "name":"server_hosts",
218
                                                "domains": ["*"],
219
                                                "routers": [
220
                                                        {
221
                                                                "match":{"headers":[{"name":"x-test-route","value":"paas"}]},
222
                                                                "route":{"cluster_name":"server_cluster"}
223
                                                        }
224
                                                ]
225
                                        }]
226
                                }
227
                        ],
228
                        "listeners":[
229
                                {
230
                                        "address":"127.0.0.1:2046",
231
                                        "bind_port": true,
232
                                        "filter_chains": [{
233
                                                "filters": [
234
                                                        {
235
                                                                "type": "proxy",
236
                                                                "config": {
237
                                                                        "downstream_protocol": "Http1",
238
                                                                        "upstream_protocol": "Http1",
239
                                                                        "router_config_name":"router_to_server"
240
                                                                }
241
                                                        }
242
                                                ]
243
                                        }]
244
                                }
245
                        ]
246
                }
247
        ],
248
        "cluster_manager":{
249
                "clusters":[
250
                        {
251
                                "name": "server_cluster",
252
                                "type": "SIMPLE",
253
                                "lb_type": "LB_RANDOM",
254
                                "hosts":[
255
                                        {"address":"127.0.0.1:8080"}
256
                                ],
257
								"health_check": {
258
									  "protocol": "Http1",
259
									  "service_name": "local_service",
260
									  "timeout": "1s",
261
									  "interval": "1s",
262
									  "healthy_threshold": 1,
263
									  "unhealthy_threshold": 1,
264
									  "check_config": {
265
											"http_check_config":{
266
												"port":8080,
267
												"timeout": "30s",
268
												"path": ""
269
											}
270
										}
271
									}
272
                        }
273
                ]
274
        }
275
}`
276

277
const MockHttpCheckServerConfig = `{
278
	"protocol":"Http1",
279
	"config": {
280
		"address": "127.0.0.1:8080"
281
	}
282
}`
283

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.