From dc62cc289805c61142f28a46b6a9e840d7b6d5c3 Mon Sep 17 00:00:00 2001 From: Boyang Lyu Date: Fri, 9 Aug 2024 15:46:54 +0800 Subject: [PATCH] add requirePDClient to cobra command Signed-off-by: Boyang Lyu --- client/http/api.go | 2 +- tests/integrations/client/http_client_test.go | 3 +++ .../pd-ctl/pdctl/command/gc_safepoint_command.go | 16 +++++++++------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/client/http/api.go b/client/http/api.go index 97d155fe04a..d1bce99f4f9 100644 --- a/client/http/api.go +++ b/client/http/api.go @@ -219,5 +219,5 @@ func GetKeyspaceMetaByNameURL(keyspaceName string) string { // GetDeleteSafePointURI returns the URI for delete safepoint service func GetDeleteSafePointURI(serviceID string) string { - return safepoint + "/" + serviceID + return fmt.Sprintf("%s/%s", safepoint, serviceID) } diff --git a/tests/integrations/client/http_client_test.go b/tests/integrations/client/http_client_test.go index bb8049181e0..8e261e1555b 100644 --- a/tests/integrations/client/http_client_test.go +++ b/tests/integrations/client/http_client_test.go @@ -844,6 +844,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { ctx, cancel := context.WithCancel(suite.ctx) defer cancel() + // adding some safepoints to the server list := &api.ListServiceGCSafepoint{ ServiceGCSafepoints: []*endpoint.ServiceSafePoint{ { @@ -873,6 +874,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { } storage.SaveGCSafePoint(1) + // get the safepoints and start testing l, err := client.GetGCSafePoint(ctx) re.NoError(err) @@ -880,6 +882,7 @@ func (suite *httpClientTestSuite) TestGetGCSafePoint() { re.Equal(uint64(1), l.MinServiceGcSafepoint) re.Len(l.ServiceGCSafepoints, 3) + // TODO : add some sorting to preserve order for i, val := range l.ServiceGCSafepoints { re.Equal(list.ServiceGCSafepoints[i].ServiceID, val.ServiceID) re.Equal(list.ServiceGCSafepoints[i].SafePoint, val.SafePoint) diff --git a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go index 8849eb2c848..f35677d5b34 100644 --- a/tools/pd-ctl/pdctl/command/gc_safepoint_command.go +++ b/tools/pd-ctl/pdctl/command/gc_safepoint_command.go @@ -23,9 +23,10 @@ import ( // NewServiceGCSafepointCommand return a service gc safepoint subcommand of rootCmd func NewServiceGCSafepointCommand() *cobra.Command { l := &cobra.Command{ - Use: "service-gc-safepoint", - Short: "show all service gc safepoint", - Run: showSSPs, + Use: "service-gc-safepoint", + Short: "show all service gc safepoint", + PersistentPreRunE: requirePDClient, + Run: showSSPs, } l.AddCommand(NewDeleteServiceGCSafepointCommand()) return l @@ -34,10 +35,11 @@ func NewServiceGCSafepointCommand() *cobra.Command { // NewDeleteServiceGCSafepointCommand return a subcommand to delete service gc safepoint func NewDeleteServiceGCSafepointCommand() *cobra.Command { l := &cobra.Command{ - Use: "delete ", - Short: "delete a service gc safepoint", - Run: deleteSSP, - Hidden: true, + Use: "delete ", + Short: "delete a service gc safepoint", + PersistentPreRunE: requirePDClient, + Run: deleteSSP, + Hidden: true, } return l }