Skip to content

Commit

Permalink
misc: refactor topo enums and pd fixture API (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish authored Jan 20, 2022
1 parent 2431655 commit f9c5bac
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 196 deletions.
2 changes: 1 addition & 1 deletion pkg/apiserver/debugapi/endpoint/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// APIDefinition can be "resolved" to become a request when its parameter values are given via RequestPayload.
type APIDefinition struct {
ID string `json:"id"`
Component topo.ComponentKind `json:"component"`
Component topo.Kind `json:"component"`
Path string `json:"path"`
Method string `json:"method"`
PathParams []APIParamDefinition `json:"path_params"` // e.g. /stats/dump/{db}/{table} -> db, table
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/debugapi/endpoint/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type HTTPClients struct {
TiFlashStatusClient *tiflashclient.StatusClient
}

func (c HTTPClients) GetHTTPClientByNodeKind(kind topo.ComponentKind) *httpclient.Client {
func (c HTTPClients) GetHTTPClientByNodeKind(kind topo.Kind) *httpclient.Client {
switch kind {
case topo.KindPD:
if c.PDAPIClient == nil {
Expand Down
33 changes: 14 additions & 19 deletions util/client/pdclient/fixture/pd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
package fixture

import (
"strings"

"github.com/jarcoal/httpmock"

"github.com/pingcap/tidb-dashboard/util/client/httpclient"
"github.com/pingcap/tidb-dashboard/util/client/pdclient"
"github.com/pingcap/tidb-dashboard/util/testutil/httpmockutil"
)

func newResponder(body string) httpmock.Responder {
return httpmock.NewStringResponder(200, strings.TrimSpace(body))
}
const BaseURL = "http://172.16.6.171:2379"

func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string) {
baseURL = "http://172.16.6.171:2379"
func NewPDServerFixture() (mockTransport *httpmock.MockTransport) {
mockTransport = httpmock.NewMockTransport()
mockTransport.RegisterResponder("GET", "http://172.16.6.171:2379/pd/api/v1/status",
newResponder(`
httpmockutil.StringResponder(`
{
"build_ts": "2021-07-17 05:37:05",
"version": "v4.0.14",
Expand All @@ -28,7 +24,7 @@ func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string
}
`))
mockTransport.RegisterResponder("GET", "http://172.16.6.171:2379/pd/api/v1/health",
newResponder(`
httpmockutil.StringResponder(`
[
{
"name": "pd-172.16.6.170-2379",
Expand Down Expand Up @@ -57,7 +53,7 @@ func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string
]
`))
mockTransport.RegisterResponder("GET", "http://172.16.6.171:2379/pd/api/v1/members",
newResponder(`
httpmockutil.StringResponder(`
{
"header": {
"cluster_id": 6973530669239952773
Expand Down Expand Up @@ -129,7 +125,7 @@ func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string
}
`))
mockTransport.RegisterResponder("GET", "http://172.16.6.171:2379/pd/api/v1/stores",
newResponder(`
httpmockutil.StringResponder(`
{
"count": 3,
"stores": [
Expand Down Expand Up @@ -224,7 +220,7 @@ func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string
}
`))
mockTransport.RegisterResponder("GET", "http://172.16.6.171:2379/pd/api/v1/config",
newResponder(`
httpmockutil.StringResponder(`
{
"client-urls": "http://0.0.0.0:2379",
"peer-urls": "http://0.0.0.0:2380",
Expand Down Expand Up @@ -418,7 +414,7 @@ func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string
}
`))
mockTransport.RegisterResponder("GET", "http://172.16.6.171:2379/pd/api/v1/config/replicate",
newResponder(`
httpmockutil.StringResponder(`
{
"max-replicas": 3,
"location-labels": "",
Expand All @@ -429,12 +425,11 @@ func NewPDServerFixture() (mockTransport *httpmock.MockTransport, baseURL string
return
}

func NewAPIAPIClientFixture() *pdclient.APIClient {
mockTransport, baseURL := NewPDServerFixture()
// NewAPIClientFixture returns a PD client whose default Base URL is pointing to a mock PD server.
func NewAPIClientFixture() *pdclient.APIClient {
mockTransport := NewPDServerFixture()
apiClient := pdclient.NewAPIClient(httpclient.Config{})
apiClient.
SetDefaultTransport(mockTransport).
SetDefaultBaseURL(baseURL)

apiClient.SetDefaultBaseURL(BaseURL)
apiClient.SetDefaultTransport(mockTransport)
return apiClient
}
14 changes: 8 additions & 6 deletions util/client/pdclient/pd_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

package pdclient

import "context"
import (
"context"
)

// TODO: Switch to use swagger.

Expand All @@ -13,7 +15,7 @@ type GetStatusResponse struct {
}

// GetStatus returns the content from /status PD API.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) GetStatus(ctx context.Context) (resp *GetStatusResponse, err error) {
_, err = api.LR().SetContext(ctx).Get(APIPrefix + "/status").ReadBodyAsJSON(&resp)
return
Expand All @@ -27,7 +29,7 @@ type GetHealthResponseMember struct {
type GetHealthResponse []GetHealthResponseMember

// GetHealth returns the content from /health PD API.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) GetHealth(ctx context.Context) (resp *GetHealthResponse, err error) {
_, err = api.LR().SetContext(ctx).Get(APIPrefix + "/health").ReadBodyAsJSON(&resp)
return
Expand All @@ -46,7 +48,7 @@ type GetMembersResponse struct {
}

// GetMembers returns the content from /members PD API.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) GetMembers(ctx context.Context) (resp *GetMembersResponse, err error) {
_, err = api.LR().SetContext(ctx).Get(APIPrefix + "/members").ReadBodyAsJSON(&resp)
return
Expand All @@ -57,7 +59,7 @@ type GetConfigReplicateResponse struct {
}

// GetConfigReplicate returns the content from /config/replicate PD API.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) GetConfigReplicate(ctx context.Context) (resp *GetConfigReplicateResponse, err error) {
_, err = api.LR().SetContext(ctx).Get(APIPrefix + "/config/replicate").ReadBodyAsJSON(&resp)
return
Expand Down Expand Up @@ -89,7 +91,7 @@ type GetStoresResponse struct {
}

// GetStores returns the content from /stores PD API.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) GetStores(ctx context.Context) (resp *GetStoresResponse, err error) {
_, err = api.LR().SetContext(ctx).Get(APIPrefix + "/stores").ReadBodyAsJSON(&resp)
return
Expand Down
6 changes: 4 additions & 2 deletions util/client/pdclient/pd_api_highlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// HLGetStores returns all stores in PD in order.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) HLGetStores(ctx context.Context) ([]GetStoresResponseStore, error) {
resp, err := api.GetStores(ctx)
if err != nil {
Expand All @@ -28,7 +28,7 @@ func (api *APIClient) HLGetStores(ctx context.Context) ([]GetStoresResponseStore
}

// HLGetLocationLabels returns the location label config in PD.
// An optional ctx can be passed in to override the default context. To keep the default context, pass nil.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) HLGetLocationLabels(ctx context.Context) ([]string, error) {
resp, err := api.GetConfigReplicate(ctx)
if err != nil {
Expand All @@ -51,6 +51,8 @@ type StoreLocations struct {
Stores []StoreLabels `json:"stores"`
}

// HLGetStoreLocations returns the stores and their locations.
// You must specify the base URL by calling SetDefaultBaseURL() before using this function.
func (api *APIClient) HLGetStoreLocations(ctx context.Context) (*StoreLocations, error) {
locationLabels, err := api.HLGetLocationLabels(ctx)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions util/client/pdclient/pd_api_highlevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

func TestAPIClient_HLGetLocationLabels(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.HLGetLocationLabels(context.Background())
require.NoError(t, err)
require.Equal(t, []string{}, resp)
}

func TestAPIClient_HLGetStoreLocations(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.HLGetStoreLocations(context.Background())
require.NoError(t, err)
require.Equal(t, &pdclient.StoreLocations{
Expand All @@ -34,7 +34,7 @@ func TestAPIClient_HLGetStoreLocations(t *testing.T) {
}

func TestAPIClient_HLGetStores(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.HLGetStores(context.Background())
require.NoError(t, err)
require.Equal(t, []pdclient.GetStoresResponseStore{
Expand Down
10 changes: 5 additions & 5 deletions util/client/pdclient/pd_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestAPIClient_GetConfigReplicate(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.GetConfigReplicate(context.Background())
require.NoError(t, err)
require.Equal(t, &pdclient.GetConfigReplicateResponse{
Expand All @@ -22,7 +22,7 @@ func TestAPIClient_GetConfigReplicate(t *testing.T) {
}

func TestAPIClient_GetHealth(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.GetHealth(context.Background())
require.NoError(t, err)
require.Equal(t, &pdclient.GetHealthResponse{
Expand All @@ -33,7 +33,7 @@ func TestAPIClient_GetHealth(t *testing.T) {
}

func TestAPIClient_GetMembers(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.GetMembers(context.Background())
require.NoError(t, err)
require.Equal(t, &pdclient.GetMembersResponse{
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestAPIClient_GetMembers(t *testing.T) {
}

func TestAPIClient_GetStatus(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.GetStatus(context.Background())
require.NoError(t, err)
require.Equal(t, &pdclient.GetStatusResponse{
Expand All @@ -71,7 +71,7 @@ func TestAPIClient_GetStatus(t *testing.T) {
}

func TestAPIClient_GetStores(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
apiClient := fixture.NewAPIClientFixture()
resp, err := apiClient.GetStores(context.Background())
require.NoError(t, err)
require.Equal(t, &pdclient.GetStoresResponse{
Expand Down
Loading

0 comments on commit f9c5bac

Please sign in to comment.