Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dtest] Fix API incompatibilities in docker harness #2849

Merged
merged 7 commits into from
Nov 17, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json.Marshal() serializes protobuf.BoolValue as a object rather than a boolean, which is expected when deserializing.

Also, when I changed the marshaling, linter kicked in and forced me to make some additional refactorings.

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 &http.Response{}, fmt.Errorf("failed to marshal: %w", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just return nil in these error cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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 {
Expand Down