From 3247dcca253002626cc42216dad8196ff6803beb Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Fri, 6 Nov 2020 14:29:10 +0200 Subject: [PATCH 1/3] [dtest] Fix coordinator API incompatibilities in docker harness --- .../dtest/docker/harness/resources/coordinator.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go index 00da24e3e1..dfcfea1059 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go +++ b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go @@ -31,7 +31,8 @@ import ( "github.com/m3db/m3/src/query/generated/proto/admin" - dockertest "github.com/ory/dockertest" + "github.com/gogo/protobuf/jsonpb" + "github.com/ory/dockertest" "go.uber.org/zap" ) @@ -112,7 +113,7 @@ func (c *coordinator) GetNamespace() (admin.NamespaceGetResponse, error) { return admin.NamespaceGetResponse{}, errClosed } - url := c.resource.getURL(7201, "api/v1/namespace") + url := c.resource.getURL(7201, "api/v1/services/m3db/namespace") logger := c.resource.logger.With( zapMethod("getNamespace"), zap.String("url", url)) @@ -135,7 +136,7 @@ func (c *coordinator) GetPlacement() (admin.PlacementGetResponse, error) { return admin.PlacementGetResponse{}, errClosed } - url := c.resource.getURL(7201, "api/v1/placement") + url := c.resource.getURL(7201, "api/v1/services/m3db/placement") logger := c.resource.logger.With( zapMethod("getPlacement"), zap.String("url", url)) @@ -273,13 +274,15 @@ func (c *coordinator) AddNamespace( zapMethod("addNamespace"), zap.String("url", url), zap.String("request", addRequest.String())) - b, err := json.Marshal(addRequest) + marshaller := jsonpb.Marshaler{EmitDefaults: true} + data := bytes.NewBuffer(nil) + err := marshaller.Marshal(data, &addRequest) if err != nil { logger.Error("failed to marshal", zap.Error(err)) return admin.NamespaceGetResponse{}, err } - resp, err := http.Post(url, "application/json", bytes.NewReader(b)) + resp, err := http.Post(url, "application/json", data) if err != nil { logger.Error("failed post", zap.Error(err)) return admin.NamespaceGetResponse{}, err From f867b1c1c9180d81cd90e10a9f154c95e1e4bcde Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Fri, 6 Nov 2020 15:49:56 +0200 Subject: [PATCH 2/3] fix linter errors --- .../docker/harness/resources/coordinator.go | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go index dfcfea1059..034c271ce2 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go +++ b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go @@ -22,7 +22,7 @@ package resources import ( "bytes" - "encoding/json" + "context" "fmt" "io/ioutil" "net" @@ -32,6 +32,7 @@ import ( "github.com/m3db/m3/src/query/generated/proto/admin" "github.com/gogo/protobuf/jsonpb" + "github.com/gogo/protobuf/proto" "github.com/ory/dockertest" "go.uber.org/zap" ) @@ -240,13 +241,7 @@ func (c *coordinator) CreateDatabase( zapMethod("createDatabase"), zap.String("url", url), zap.String("request", addRequest.String())) - b, err := json.Marshal(addRequest) - if err != nil { - logger.Error("failed to marshal", zap.Error(err)) - return admin.DatabaseCreateResponse{}, err - } - - resp, err := http.Post(url, "application/json", bytes.NewReader(b)) + resp, err := makePostRequest(logger, url, &addRequest) if err != nil { logger.Error("failed post", zap.Error(err)) return admin.DatabaseCreateResponse{}, err @@ -274,15 +269,7 @@ func (c *coordinator) AddNamespace( zapMethod("addNamespace"), zap.String("url", url), zap.String("request", addRequest.String())) - marshaller := jsonpb.Marshaler{EmitDefaults: true} - data := bytes.NewBuffer(nil) - err := marshaller.Marshal(data, &addRequest) - if err != nil { - logger.Error("failed to marshal", zap.Error(err)) - return admin.NamespaceGetResponse{}, err - } - - resp, err := http.Post(url, "application/json", data) + resp, err := makePostRequest(logger, url, &addRequest) if err != nil { logger.Error("failed post", zap.Error(err)) return admin.NamespaceGetResponse{}, err @@ -333,6 +320,26 @@ func (c *coordinator) WriteCarbon( // return nil } +func makePostRequest(logger *zap.Logger, url string, body proto.Message) (*http.Response, error) { + data := bytes.NewBuffer(nil) + if err := (&jsonpb.Marshaler{}).Marshal(data, body); err != nil { + logger.Error("failed to marshal", zap.Error(err)) + + return &http.Response{}, fmt.Errorf("failed to marshal: %w", err) + } + + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, data) + if err != nil { + logger.Error("failed to construct request", zap.Error(err)) + + return &http.Response{}, fmt.Errorf("failed to construct request: %w", err) + } + + req.Header.Add("Content-Type", "application/json") + + return http.DefaultClient.Do(req) +} + func (c *coordinator) query( verifier GoalStateVerifier, query string, ) error { From f537edccd8d8b4efa432e197d58f7a8d3f98a3c0 Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Mon, 16 Nov 2020 16:10:46 +0200 Subject: [PATCH 3/3] PR comments --- src/cmd/tools/dtest/docker/harness/resources/coordinator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go index 034c271ce2..0333519ecc 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go +++ b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go @@ -325,14 +325,14 @@ func makePostRequest(logger *zap.Logger, url string, body proto.Message) (*http. if err := (&jsonpb.Marshaler{}).Marshal(data, body); err != nil { logger.Error("failed to marshal", zap.Error(err)) - return &http.Response{}, fmt.Errorf("failed to marshal: %w", err) + return nil, fmt.Errorf("failed to marshal: %w", err) } req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, data) if err != nil { logger.Error("failed to construct request", zap.Error(err)) - return &http.Response{}, fmt.Errorf("failed to construct request: %w", err) + return nil, fmt.Errorf("failed to construct request: %w", err) } req.Header.Add("Content-Type", "application/json")