Skip to content

Conversation

@bohuini
Copy link
Contributor

@bohuini bohuini commented Sep 27, 2022

Added two API in CNS client:

GetAllNCsFromCns()

PostAllNCsToCns()

@timraymond
Copy link
Member

Looks pretty good @bohuini , but you should also add tests in cns/client/client_test.go as well to keep the coverage up.

@bohuini
Copy link
Contributor Author

bohuini commented Sep 27, 2022

Sure, thanks @timraymond . I'll add test and publish the PR when done.

@bohuini bohuini marked this pull request as ready for review October 3, 2022 16:59
@bohuini bohuini requested a review from a team as a code owner October 3, 2022 16:59
@bohuini bohuini requested review from rsagasthya and removed request for a team October 3, 2022 16:59
@timraymond
Copy link
Member

@bohuini Could you delete the merge commit and rebase this on master instead? I'd be happy to show you how.

return response, nil
}

func (c *Client) PostAllNCsToCns(ctx context.Context, createNcRequest cns.PostNetworkContainersRequest) error {
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the name.

var body bytes.Buffer
err := json.NewEncoder(&body).Encode(createNcRequest)
if err != nil {
return fmt.Errorf("postAllNetworkContainers failed with error: %w", err)
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Fixed.

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

Choose a reason for hiding this comment

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

Suggested change
return errors.Wrap(err, "decoding Post Network Containers response as JSON")
return errors.Wrap(err, "decoding PostNetworkContainersResponse as JSON")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

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

Choose a reason for hiding this comment

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

Suggested change
return cns.GetAllNetworkContainersResponse{}, errors.Wrap(err, "decoding Get all Network Containers response as JSON")
return cns.GetAllNetworkContainersResponse{}, errors.Wrap(err, "decoding GetAllNetworkContainersResponse as JSON")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

Comment on lines 44 to 49
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}
)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

for _, test := range createNCTests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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])
Copy link
Member

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...

Copy link
Contributor Author

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.

@bohuini bohuini force-pushed the NC_refresh_optimization branch from 39a725d to 0d9681a Compare October 3, 2022 21:08
@bohuini
Copy link
Contributor Author

bohuini commented Oct 3, 2022

@timraymond Rebased the branch and simplified the commits

timraymond
timraymond previously approved these changes Oct 4, 2022
Copy link
Member

@timraymond timraymond left a comment

Choose a reason for hiding this comment

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

Looks great 👍

@bohuini bohuini force-pushed the NC_refresh_optimization branch from 0d9681a to fdaeec8 Compare October 4, 2022 20:19
@bohuini bohuini enabled auto-merge (squash) October 4, 2022 20:22
@bohuini bohuini force-pushed the NC_refresh_optimization branch from fdaeec8 to 381ca77 Compare October 5, 2022 17:52
timraymond
timraymond previously approved these changes Oct 5, 2022
@rbtr
Copy link
Collaborator

rbtr commented Oct 6, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 7, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 11, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

timraymond
timraymond previously approved these changes Oct 11, 2022
@bohuini bohuini force-pushed the NC_refresh_optimization branch from 0a62854 to 9468510 Compare October 11, 2022 20:06
@bohuini
Copy link
Contributor Author

bohuini commented Oct 11, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 12, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 12, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 12, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 12, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 12, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 13, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini
Copy link
Contributor Author

bohuini commented Oct 13, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bohuini bohuini force-pushed the NC_refresh_optimization branch from 9468510 to a0832fd Compare October 13, 2022 01:20
@rbtr
Copy link
Collaborator

rbtr commented Oct 13, 2022

@bohuini you don't need to keep using /azp run - it reruns the whole pipeline. If you have a check that failed you can rerun just that one by going to the checks tab:
image
or hitting "rerun failed" on the checks page or go to the azp page for this run by clicking "View more details on Azure Pipelines" and do it there.

@bohuini
Copy link
Contributor Author

bohuini commented Oct 13, 2022

@bohuini you don't need to keep using /azp run - it reruns the whole pipeline. If you have a check that failed you can rerun just that one by going to the checks tab: image or hitting "rerun failed" on the checks page or go to the azp page for this run by clicking "View more details on Azure Pipelines" and do it there.

Sure, @rbtr thanks for letting me know.

@bohuini bohuini disabled auto-merge October 13, 2022 17:23
@rbtr rbtr merged commit 48e5238 into Azure:master Oct 13, 2022
rjdenney pushed a commit to rjdenney/azure-container-networking that referenced this pull request Jan 19, 2023
Added two api in client and added unit test
@bohuini bohuini deleted the NC_refresh_optimization branch March 27, 2023 06:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants