Skip to content

Commit

Permalink
[CNS] Add unit tests to Standalone SwiftV2 scenario (#2932)
Browse files Browse the repository at this point in the history
* chore: update comments to include field keys

* test: refactor to pass in orchestrator type for CNS mock

* chore: add swiftv2 standalone context with NMAgent

* test: add swiftv2 standalone happy path tests

* test: add comments

* test: add validation tests

* feat!: remove middleware error response overriding to unexpected error

* test: fix missing argument to refactored getTestService
  • Loading branch information
jackieluc authored Aug 27, 2024
1 parent e10c61d commit 0a47e17
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 35 deletions.
4 changes: 1 addition & 3 deletions cns/middlewares/standaloneSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/pkg/errors"
)

Expand All @@ -16,8 +15,7 @@ func (m *StandaloneSWIFTv2Middleware) IPConfigsRequestHandlerWrapper(ipRequestHa
return func(ctx context.Context, req cns.IPConfigsRequest) (*cns.IPConfigsResponse, error) {
ipConfigsResp, err := ipRequestHandler(ctx, req)
if err != nil {
ipConfigsResp.Response.ReturnCode = types.UnexpectedError
return ipConfigsResp, errors.Wrapf(err, "Failed to requestIPConfigs for Standalone-SwiftV2 from IPConfigsRequest %v", req)
return ipConfigsResp, errors.Wrapf(err, "Failed to requestIPConfigs for Standalone SwiftV2 from IPConfigsRequest %+v", req)
}

return ipConfigsResp, nil
Expand Down
9 changes: 7 additions & 2 deletions cns/restserver/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,22 @@ func (service *HTTPRestService) requestIPConfigHandlerHelperStandalone(ctx conte
return &cns.IPConfigsResponse{}, fmt.Errorf("error getting orchestrator context from PodInfo %w", err)
}
cnsRequest := cns.GetNetworkContainerRequest{OrchestratorContext: orchestratorContext}

// IMPORTANT: although SwiftV2 reuses the concept of NCs, NMAgent doesn't program NCs for SwiftV2, but
// instead programs NICs. When getting SwiftV2 NCs, we want the NIC type and MAC address of the NCs.
// TODO: we need another way to verify and sync NMAgent's NIC programming status. pending new NMAgent API or NIC programming status to be passed in the SwiftV2 create NC request.
resp := service.getAllNetworkContainerResponses(cnsRequest) //nolint:contextcheck // not passed in any methods, appease linter
// return err if returned list has no NCs
if len(resp) == 0 {
return &cns.IPConfigsResponse{
Response: cns.Response{
ReturnCode: types.FailedToAllocateIPConfig,
Message: fmt.Sprintf("AllocateIPConfig failed due to not getting NC Response from statefile, IP config request is %v", ipconfigsRequest),
Message: fmt.Sprintf("AllocateIPConfig failed due to not getting NC Response from statefile, IP config request is %+v", ipconfigsRequest),
},
}, ErrGetAllNCResponseEmpty
}

// assign NICType and MAC Address for SwiftV2. we assume that there won't be any SwiftV1 NCs here
podIPInfoList := make([]cns.PodIpInfo, 0, len(resp))
for i := range resp {
podIPInfo := cns.PodIpInfo{
Expand All @@ -166,7 +171,7 @@ func (service *HTTPRestService) requestIPConfigHandlerHelperStandalone(ctx conte
return &cns.IPConfigsResponse{
Response: cns.Response{
ReturnCode: types.FailedToAllocateIPConfig,
Message: fmt.Sprintf("AllocateIPConfig failed while updating pod with interfaces: %v, IP config request is %v", err, ipconfigsRequest),
Message: fmt.Sprintf("AllocateIPConfig failed while updating pod with interfaces: %v, IP config request is %+v", err, ipconfigsRequest),
},
}, err
}
Expand Down
Loading

0 comments on commit 0a47e17

Please sign in to comment.