oceanbase
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
13package server14
15import (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
29const 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
31func TestGetRootServiceInfoParamOldVersion(t *testing.T) {32gin.SetMode(gin.TestMode)33w := httptest.NewRecorder()34c, _ := gin.CreateTestContext(w)35
36c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObRegion=c1&ObRegionId=1", nil)37
38obRootServiceInfoParam, err := getCommonParam(c)39require.Equal(t, "c1", obRootServiceInfoParam.ObCluster)40require.Equal(t, int64(1), obRootServiceInfoParam.ObClusterId)41require.Equal(t, 0, obRootServiceInfoParam.Version)42require.True(t, err == nil)43
44}
45
46func TestGetRootServiceInfoParamVersion2(t *testing.T) {47gin.SetMode(gin.TestMode)48w := httptest.NewRecorder()49c, _ := gin.CreateTestContext(w)50
51c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", nil)52
53obRootServiceInfoParam, err := getCommonParam(c)54require.Equal(t, "c1", obRootServiceInfoParam.ObCluster)55require.Equal(t, int64(1), obRootServiceInfoParam.ObClusterId)56require.Equal(t, 2, obRootServiceInfoParam.Version)57require.True(t, err == nil)58
59}
60
61func TestGetObRootServiceInfo(t *testing.T) {62// test gin63gin.SetMode(gin.TestMode)64w := httptest.NewRecorder()65c, _ := gin.CreateTestContext(w)66c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1", nil)67
68// mock db client69client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")70client.Schema.Create(context.Background())71
72configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")73configServer = &ConfigServer{74Config: configServerConfig,75Client: client,76}77
78client.ObCluster.79Create().80SetName("c1").81SetObClusterID(1).82SetType("PRIMARY").83SetRootserviceJSON(testRootServiceJson).84OnConflict().85SetRootserviceJSON(testRootServiceJson).86Exec(context.Background())87
88response := getObRootServiceInfo(context.Background(), c)89require.Equal(t, http.StatusOK, response.Code)90}
91
92func TestGetObRootServiceInfoV2(t *testing.T) {93// test gin94gin.SetMode(gin.TestMode)95w := httptest.NewRecorder()96c, _ := gin.CreateTestContext(w)97c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&version=2", nil)98
99// mock db client100client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")101client.Schema.Create(context.Background())102
103configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")104configServer = &ConfigServer{105Config: configServerConfig,106Client: client,107}108client.ObCluster.109Create().110SetName("c1").111SetObClusterID(1).112SetType("PRIMARY").113SetRootserviceJSON(testRootServiceJson).114OnConflict().115SetRootserviceJSON(testRootServiceJson).116Exec(context.Background())117
118response := getObRootServiceInfo(context.Background(), c)119require.Equal(t, http.StatusOK, response.Code)120}
121
122func TestGetObRootServiceInfoV2WithObClusterId(t *testing.T) {123// test gin124gin.SetMode(gin.TestMode)125w := httptest.NewRecorder()126c, _ := gin.CreateTestContext(w)127c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", nil)128
129// mock db client130client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")131client.Schema.Create(context.Background())132
133configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")134configServer = &ConfigServer{135Config: configServerConfig,136Client: client,137}138
139client.ObCluster.140Create().141SetName("c1").142SetObClusterID(1).143SetType("PRIMARY").144SetRootserviceJSON(testRootServiceJson).145OnConflict().146SetRootserviceJSON(testRootServiceJson).147Exec(context.Background())148
149response := getObRootServiceInfo(context.Background(), c)150require.Equal(t, http.StatusOK, response.Code)151}
152
153func TestGetObRootServiceInfoNoResult(t *testing.T) {154// test gin155gin.SetMode(gin.TestMode)156w := httptest.NewRecorder()157c, _ := gin.CreateTestContext(w)158c.Request, _ = http.NewRequest("GET", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c2&ObClusterId=2&version=2", nil)159
160// mock db client161client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")162client.Schema.Create(context.Background())163
164configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")165configServer = &ConfigServer{166Config: configServerConfig,167Client: client,168}169
170response := getObRootServiceInfo(context.Background(), c)171require.Equal(t, http.StatusNotFound, response.Code)172}
173
174func TestCreateOrUpdateObRootServiceInfo(t *testing.T) {175// test gin176gin.SetMode(gin.TestMode)177w := httptest.NewRecorder()178c, _ := gin.CreateTestContext(w)179c.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 client182client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")183client.Schema.Create(context.Background())184
185configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")186configServer = &ConfigServer{187Config: configServerConfig,188Client: client,189}190
191response := createOrUpdateObRootServiceInfo(context.Background(), c)192require.Equal(t, http.StatusOK, response.Code)193}
194
195func TestDeleteObRootServiceInfo(t *testing.T) {196// test gin197gin.SetMode(gin.TestMode)198w := httptest.NewRecorder()199c, _ := gin.CreateTestContext(w)200c.Request, _ = http.NewRequest("DELETE", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1&version=2", nil)201
202// mock db client203client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")204client.Schema.Create(context.Background())205
206configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")207configServer = &ConfigServer{208Config: configServerConfig,209Client: client,210}211
212client.ObCluster.213Create().214SetName("c1").215SetObClusterID(1).216SetType("PRIMARY").217SetRootserviceJSON(testRootServiceJson).218OnConflict().219SetRootserviceJSON(testRootServiceJson).220Exec(context.Background())221
222response := deleteObRootServiceInfo(context.Background(), c)223require.Equal(t, http.StatusOK, response.Code)224}
225
226func TestDeleteObRootServiceInfoVersion1(t *testing.T) {227// test gin228gin.SetMode(gin.TestMode)229w := httptest.NewRecorder()230c, _ := gin.CreateTestContext(w)231c.Request, _ = http.NewRequest("DELETE", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&ObClusterId=1", nil)232
233// mock db client234client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")235client.Schema.Create(context.Background())236
237configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")238configServer = &ConfigServer{239Config: configServerConfig,240Client: client,241}242
243client.ObCluster.244Create().245SetName("c1").246SetObClusterID(1).247SetType("PRIMARY").248SetRootserviceJSON(testRootServiceJson).249OnConflict().250SetRootserviceJSON(testRootServiceJson).251Exec(context.Background())252
253response := deleteObRootServiceInfo(context.Background(), c)254require.Equal(t, http.StatusBadRequest, response.Code)255}
256
257func TestDeleteObRootServiceInfoWithoutClusterId(t *testing.T) {258// test gin259gin.SetMode(gin.TestMode)260w := httptest.NewRecorder()261c, _ := gin.CreateTestContext(w)262c.Request, _ = http.NewRequest("DELETE", "http://1.1.1.1:8080/services?Action=ObRootServiceInfo&ObCluster=c1&version=2", nil)263
264// mock db client265client, _ := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")266client.Schema.Create(context.Background())267
268configServerConfig, _ := config.ParseConfigServerConfig("../etc/config.yaml")269configServer = &ConfigServer{270Config: configServerConfig,271Client: client,272}273
274client.ObCluster.275Create().276SetName("c1").277SetObClusterID(1).278SetType("PRIMARY").279SetRootserviceJSON(testRootServiceJson).280OnConflict().281SetRootserviceJSON(testRootServiceJson).282Exec(context.Background())283
284response := deleteObRootServiceInfo(context.Background(), c)285require.Equal(t, http.StatusBadRequest, response.Code)286}
287