-
Notifications
You must be signed in to change notification settings - Fork 260
Add two API in CNS client for GET and POST all NCs #1632
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
Conversation
|
Looks pretty good @bohuini , but you should also add tests in |
|
Sure, thanks @timraymond . I'll add test and publish the PR when done. |
|
@bohuini Could you delete the merge commit and rebase this on master instead? I'd be happy to show you how. |
cns/client/client.go
Outdated
| return response, nil | ||
| } | ||
|
|
||
| func (c *Client) PostAllNCsToCns(ctx context.Context, createNcRequest cns.PostNetworkContainersRequest) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the ToCns part of each method name is just extra words. It's a CNS client, so it's implied that the request is to CNS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the name.
cns/client/client.go
Outdated
| var body bytes.Buffer | ||
| err := json.NewEncoder(&body).Encode(createNcRequest) | ||
| if err != nil { | ||
| return fmt.Errorf("postAllNetworkContainers failed with error: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be errors.Wrap instead. Also, the message should be what you were trying to do when it failed. In this instance, it should be just something like "encoding request JSON". The reason is that when all of those strings are combined, you get a nice faux stack trace that helps you track it down (and can be found using grep), for example: calling PostAllNCs: encoding request JSON: blah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Fixed.
cns/client/client.go
Outdated
| var response cns.PostNetworkContainersResponse | ||
| err = json.NewDecoder(resp.Body).Decode(&response) | ||
| if err != nil || response.Response.ReturnCode != types.Success { | ||
| return errors.Wrap(err, "decoding Post Network Containers response as JSON") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return errors.Wrap(err, "decoding Post Network Containers response as JSON") | |
| return errors.Wrap(err, "decoding PostNetworkContainersResponse as JSON") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed.
cns/client/client.go
Outdated
| var response cns.GetAllNetworkContainersResponse | ||
| err = json.NewDecoder(resp.Body).Decode(&response) | ||
| if err != nil || response.Response.ReturnCode != types.Success { | ||
| return cns.GetAllNetworkContainersResponse{}, errors.Wrap(err, "decoding Get all Network Containers response as JSON") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return cns.GetAllNetworkContainersResponse{}, errors.Wrap(err, "decoding Get all Network Containers response as JSON") | |
| return cns.GetAllNetworkContainersResponse{}, errors.Wrap(err, "decoding GetAllNetworkContainersResponse as JSON") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed.
| var ( | ||
| svc *restserver.HTTPRestService | ||
| dnsServers = []string{"8.8.8.8", "8.8.4.4"} | ||
| errBadRequest = errors.New("bad request") | ||
| createNcRequests = []cns.CreateNetworkContainerRequest{ | ||
| { | ||
| Version: "12345", | ||
| NetworkContainerType: "type1", | ||
| NetworkContainerid: "nc1", | ||
| OrchestratorContext: json.RawMessage("null"), | ||
| }, | ||
| { | ||
| Version: "12345", | ||
| NetworkContainerType: "blah", | ||
| NetworkContainerid: "nc2", | ||
| OrchestratorContext: json.RawMessage("null"), | ||
| }, | ||
| } | ||
| postAllNcsRequests = cns.PostNetworkContainersRequest{CreateNetworkContainerRequests: createNcRequests} | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these package variables should either be instantiated directly in the tests themselves or come from a factory function of some kind. Otherwise we end up with gnarly inter-test dependencies like we have in DNC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed.
cns/client/client_test.go
Outdated
| for _, test := range createNCTests { | ||
| test := test | ||
| t.Run(test.name, func(t *testing.T) { | ||
| t.Parallel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where there would be a hazard using an uncontrolled package variable for your test cases. Because the package variable is so easy and convenient to reuse, someone will eventually reuse it. If they write to it in a Parallel subtest, then we have a data race in the tests.
Keep the parallel subtests, because I don't think there's any reason why they can't be run in parallel (and fast tests are good). But we need to make sure that the data is sound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed t.Parallel() and the tests works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bohuini that's fine too... as I said, I do like using t.Parallel() wherever possible. Unit tests should be blazing fast (less than 10s for the entire suite IMO), and t.Parallel() helps a lot with this. I don't think it'll matter a ton here, so don't feel obligated to add it back. I just wanted to clarify my position.
| } | ||
|
|
||
| for i := 0; i < len(test.expReq.CreateNetworkContainerRequests); i++ { | ||
| assert.Equal(t, test.expReq.CreateNetworkContainerRequests[i], gotReq.CreateNetworkContainerRequests[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this stable against different orderings of the elements of the slice? You may have to sort here first...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order will affect the results. So it needs to be sorted at first.
39a725d to
0d9681a
Compare
|
@timraymond Rebased the branch and simplified the commits |
timraymond
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great 👍
0d9681a to
fdaeec8
Compare
fdaeec8 to
381ca77
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
381ca77 to
0a62854
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
0a62854 to
9468510
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
9468510 to
a0832fd
Compare
|
@bohuini you don't need to keep using |
Sure, @rbtr thanks for letting me know. |
Added two api in client and added unit test

Added two API in CNS client:
GetAllNCsFromCns()PostAllNCsToCns()