Skip to content

Commit

Permalink
[dtest] Fix API incompatibilities in docker harness (#2849)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis authored Nov 17, 2020
1 parent 153f7b5 commit e346a94
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/cmd/tools/dtest/docker/harness/resources/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package resources

import (
"bytes"
"encoding/json"
"context"
"fmt"
"io/ioutil"
"net"
Expand All @@ -31,7 +31,9 @@ import (

"github.com/m3db/m3/src/query/generated/proto/admin"

dockertest "github.com/ory/dockertest"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/ory/dockertest"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -112,7 +114,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))

Expand All @@ -135,7 +137,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))

Expand Down Expand Up @@ -239,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
Expand Down Expand Up @@ -273,13 +269,7 @@ func (c *coordinator) AddNamespace(
zapMethod("addNamespace"), 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.NamespaceGetResponse{}, 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.NamespaceGetResponse{}, err
Expand Down Expand Up @@ -330,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 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 nil, 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 {
Expand Down

0 comments on commit e346a94

Please sign in to comment.