oceanbase

Форк
0
/
observer_handler_test.go 
286 строк · 9.0 Кб
1
/**
2
 * Copyright (c) 2021 OceanBase
3
 * OceanBase CE is licensed under Mulan PubL v2.
4
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
5
 * You may obtain a copy of Mulan PubL v2 at:
6
 *          http://license.coscl.org.cn/MulanPubL-2.0
7
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
 * See the Mulan PubL v2 for more details.
11
 */
12

13
package server
14

15
import (
16
	"bytes"
17
	"context"
18
	"net/http"
19
	"net/http/httptest"
20
	"testing"
21

22
	"github.com/gin-gonic/gin"
23
	"github.com/stretchr/testify/require"
24

25
	"github.com/oceanbase/configserver/config"
26
	"github.com/oceanbase/configserver/ent"
27
)
28

29
const testRootServiceJson = "{\"Type\":\"PRIMARY\",\"ObClusterId\":1,\"ObRegionId\":1,\"ObCluster\":\"c1\",\"ObRegion\":\"c1\",\"ReadonlyRsList\":[],\"RsList\":[{\"address\":\"1.1.1.1:2882\",\"role\":\"LEADER\",\"sql_port\":2881}],\"timestamp\":1649435362283000}"
30

31
func TestGetRootServiceInfoParamOldVersion(t *testing.T) {
32
	gin.SetMode(gin.TestMode)
33
	w := httptest.NewRecorder()
34
	c, _ := gin.CreateTestContext(w)
35

36
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObRegion=c1&ObRegionId=1", nil)
37

38
	obRootServiceInfoParam, err := getCommonParam(c)
39
	require.Equal(t, "c1", obRootServiceInfoParam.ObCluster)
40
	require.Equal(t, int64(1), obRootServiceInfoParam.ObClusterId)
41
	require.Equal(t, 0, obRootServiceInfoParam.Version)
42
	require.True(t, err == nil)
43

44
}
45

46
func TestGetRootServiceInfoParamVersion2(t *testing.T) {
47
	gin.SetMode(gin.TestMode)
48
	w := httptest.NewRecorder()
49
	c, _ := gin.CreateTestContext(w)
50

51
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", nil)
52

53
	obRootServiceInfoParam, err := getCommonParam(c)
54
	require.Equal(t, "c1", obRootServiceInfoParam.ObCluster)
55
	require.Equal(t, int64(1), obRootServiceInfoParam.ObClusterId)
56
	require.Equal(t, 2, obRootServiceInfoParam.Version)
57
	require.True(t, err == nil)
58

59
}
60

61
func TestGetObRootServiceInfo(t *testing.T) {
62
	// test gin
63
	gin.SetMode(gin.TestMode)
64
	w := httptest.NewRecorder()
65
	c, _ := gin.CreateTestContext(w)
66
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1", nil)
67

68
	// mock db client
69
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
70
	client.Schema.Create(context.Background())
71

72
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
73
	configServer = &ConfigServer{
74
		Config: configServerConfig,
75
		Client: client,
76
	}
77

78
	client.ObCluster.
79
		Create().
80
		SetName("c1").
81
		SetObClusterID(1).
82
		SetType("PRIMARY").
83
		SetRootserviceJSON(testRootServiceJson).
84
		OnConflict().
85
		SetRootserviceJSON(testRootServiceJson).
86
		Exec(context.Background())
87

88
	response := getObRootServiceInfo(context.Background(), c)
89
	require.Equal(t, http.StatusOK, response.Code)
90
}
91

92
func TestGetObRootServiceInfoV2(t *testing.T) {
93
	// test gin
94
	gin.SetMode(gin.TestMode)
95
	w := httptest.NewRecorder()
96
	c, _ := gin.CreateTestContext(w)
97
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&version=2", nil)
98

99
	// mock db client
100
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
101
	client.Schema.Create(context.Background())
102

103
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
104
	configServer = &ConfigServer{
105
		Config: configServerConfig,
106
		Client: client,
107
	}
108
	client.ObCluster.
109
		Create().
110
		SetName("c1").
111
		SetObClusterID(1).
112
		SetType("PRIMARY").
113
		SetRootserviceJSON(testRootServiceJson).
114
		OnConflict().
115
		SetRootserviceJSON(testRootServiceJson).
116
		Exec(context.Background())
117

118
	response := getObRootServiceInfo(context.Background(), c)
119
	require.Equal(t, http.StatusOK, response.Code)
120
}
121

122
func TestGetObRootServiceInfoV2WithObClusterId(t *testing.T) {
123
	// test gin
124
	gin.SetMode(gin.TestMode)
125
	w := httptest.NewRecorder()
126
	c, _ := gin.CreateTestContext(w)
127
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", nil)
128

129
	// mock db client
130
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
131
	client.Schema.Create(context.Background())
132

133
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
134
	configServer = &ConfigServer{
135
		Config: configServerConfig,
136
		Client: client,
137
	}
138

139
	client.ObCluster.
140
		Create().
141
		SetName("c1").
142
		SetObClusterID(1).
143
		SetType("PRIMARY").
144
		SetRootserviceJSON(testRootServiceJson).
145
		OnConflict().
146
		SetRootserviceJSON(testRootServiceJson).
147
		Exec(context.Background())
148

149
	response := getObRootServiceInfo(context.Background(), c)
150
	require.Equal(t, http.StatusOK, response.Code)
151
}
152

153
func TestGetObRootServiceInfoNoResult(t *testing.T) {
154
	// test gin
155
	gin.SetMode(gin.TestMode)
156
	w := httptest.NewRecorder()
157
	c, _ := gin.CreateTestContext(w)
158
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c2&ObClusterId=2&version=2", nil)
159

160
	// mock db client
161
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
162
	client.Schema.Create(context.Background())
163

164
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
165
	configServer = &ConfigServer{
166
		Config: configServerConfig,
167
		Client: client,
168
	}
169

170
	response := getObRootServiceInfo(context.Background(), c)
171
	require.Equal(t, http.StatusNotFound, response.Code)
172
}
173

174
func TestCreateOrUpdateObRootServiceInfo(t *testing.T) {
175
	// test gin
176
	gin.SetMode(gin.TestMode)
177
	w := httptest.NewRecorder()
178
	c, _ := gin.CreateTestContext(w)
179
	c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", bytes.NewBuffer([]byte(testRootServiceJson)))
180

181
	// mock db client
182
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
183
	client.Schema.Create(context.Background())
184

185
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
186
	configServer = &ConfigServer{
187
		Config: configServerConfig,
188
		Client: client,
189
	}
190

191
	response := createOrUpdateObRootServiceInfo(context.Background(), c)
192
	require.Equal(t, http.StatusOK, response.Code)
193
}
194

195
func TestDeleteObRootServiceInfo(t *testing.T) {
196
	// test gin
197
	gin.SetMode(gin.TestMode)
198
	w := httptest.NewRecorder()
199
	c, _ := gin.CreateTestContext(w)
200
	c.Request, _ = http.NewRequest("DELETE", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", nil)
201

202
	// mock db client
203
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
204
	client.Schema.Create(context.Background())
205

206
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
207
	configServer = &ConfigServer{
208
		Config: configServerConfig,
209
		Client: client,
210
	}
211

212
	client.ObCluster.
213
		Create().
214
		SetName("c1").
215
		SetObClusterID(1).
216
		SetType("PRIMARY").
217
		SetRootserviceJSON(testRootServiceJson).
218
		OnConflict().
219
		SetRootserviceJSON(testRootServiceJson).
220
		Exec(context.Background())
221

222
	response := deleteObRootServiceInfo(context.Background(), c)
223
	require.Equal(t, http.StatusOK, response.Code)
224
}
225

226
func TestDeleteObRootServiceInfoVersion1(t *testing.T) {
227
	// test gin
228
	gin.SetMode(gin.TestMode)
229
	w := httptest.NewRecorder()
230
	c, _ := gin.CreateTestContext(w)
231
	c.Request, _ = http.NewRequest("DELETE", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1", nil)
232

233
	// mock db client
234
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
235
	client.Schema.Create(context.Background())
236

237
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
238
	configServer = &ConfigServer{
239
		Config: configServerConfig,
240
		Client: client,
241
	}
242

243
	client.ObCluster.
244
		Create().
245
		SetName("c1").
246
		SetObClusterID(1).
247
		SetType("PRIMARY").
248
		SetRootserviceJSON(testRootServiceJson).
249
		OnConflict().
250
		SetRootserviceJSON(testRootServiceJson).
251
		Exec(context.Background())
252

253
	response := deleteObRootServiceInfo(context.Background(), c)
254
	require.Equal(t, http.StatusBadRequest, response.Code)
255
}
256

257
func TestDeleteObRootServiceInfoWithoutClusterId(t *testing.T) {
258
	// test gin
259
	gin.SetMode(gin.TestMode)
260
	w := httptest.NewRecorder()
261
	c, _ := gin.CreateTestContext(w)
262
	c.Request, _ = http.NewRequest("DELETE", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&version=2", nil)
263

264
	// mock db client
265
	client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
266
	client.Schema.Create(context.Background())
267

268
	configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")
269
	configServer = &ConfigServer{
270
		Config: configServerConfig,
271
		Client: client,
272
	}
273

274
	client.ObCluster.
275
		Create().
276
		SetName("c1").
277
		SetObClusterID(1).
278
		SetType("PRIMARY").
279
		SetRootserviceJSON(testRootServiceJson).
280
		OnConflict().
281
		SetRootserviceJSON(testRootServiceJson).
282
		Exec(context.Background())
283

284
	response := deleteObRootServiceInfo(context.Background(), c)
285
	require.Equal(t, http.StatusBadRequest, response.Code)
286
}
287

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

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

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

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