Skip to content

Commit

Permalink
Add unit tests for computeagent
Browse files Browse the repository at this point in the history
Signed-off-by: Kathryn Baldauf <[email protected]>
  • Loading branch information
katiewasnothere committed Sep 29, 2021
1 parent 057bebe commit 4a4e5f9
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 290 deletions.
2 changes: 0 additions & 2 deletions cmd/ncproxy/ncproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (s *grpcService) AddNIC(ctx context.Context, req *ncproxygrpc.AddNICRequest
}
if agent, ok := s.containerIDToComputeAgent.get(req.ContainerID); ok {
caReq := &computeagent.AddNICInternalRequest{
ContainerID: req.ContainerID,
NicID: req.NicID,
EndpointName: req.EndpointName,
}
Expand Down Expand Up @@ -190,7 +189,6 @@ func (s *grpcService) DeleteNIC(ctx context.Context, req *ncproxygrpc.DeleteNICR
}
if agent, ok := s.containerIDToComputeAgent.get(req.ContainerID); ok {
caReq := &computeagent.DeleteNICInternalRequest{
ContainerID: req.ContainerID,
NicID: req.NicID,
EndpointName: req.EndpointName,
}
Expand Down
170 changes: 40 additions & 130 deletions internal/computeagent/computeagent.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions internal/computeagent/computeagent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ service ComputeAgent{
}

message AddNICInternalRequest {
string container_id = 1;
string nic_id = 2;
string endpoint_name = 3;
string nic_id = 1;
string endpoint_name = 2;
}

message AddNICInternalResponse {}
Expand All @@ -27,9 +26,8 @@ message ModifyNICInternalRequest {
message ModifyNICInternalResponse {}

message DeleteNICInternalRequest {
string container_id = 1;
string nic_id = 2;
string endpoint_name = 3;
string nic_id = 1;
string endpoint_name = 2;
}

message DeleteNICInternalResponse {}
Expand Down
26 changes: 18 additions & 8 deletions internal/uvm/computeagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,38 @@ import (

const ComputeAgentAddrFmt = "\\\\.\\pipe\\computeagent-%s"

// create an interface here so we can mock out calls to the UtilityVM in our tests
type agentComputeSystem interface {
AddEndpointToNSWithID(context.Context, string, string, *hns.HNSEndpoint) error
UpdateNIC(context.Context, string, *hcsschema.NetworkAdapter) error
RemoveEndpointFromNS(context.Context, string, *hns.HNSEndpoint) error
}

var _ agentComputeSystem = &UtilityVM{}

// mock hcn function for tests
var hnsGetHNSEndpointByName = hns.GetHNSEndpointByName

// computeAgent implements the ComputeAgent ttrpc service for adding and deleting NICs to a
// Utility VM.
type computeAgent struct {
uvm *UtilityVM
uvm agentComputeSystem
}

var _ computeagent.ComputeAgentService = &computeAgent{}

// AddNIC will add a NIC to the computeagent services hosting UVM.
func (ca *computeAgent) AddNIC(ctx context.Context, req *computeagent.AddNICInternalRequest) (*computeagent.AddNICInternalResponse, error) {
log.G(ctx).WithFields(logrus.Fields{
"containerID": req.ContainerID,
"endpointID": req.EndpointName,
"nicID": req.NicID,
"endpointID": req.EndpointName,
"nicID": req.NicID,
}).Info("AddNIC request")

if req.NicID == "" || req.EndpointName == "" {
return nil, status.Error(codes.InvalidArgument, "received empty field in request")
}

endpoint, err := hns.GetHNSEndpointByName(req.EndpointName)
endpoint, err := hnsGetHNSEndpointByName(req.EndpointName)
if err != nil {
return nil, errors.Wrapf(err, "failed to get endpoint with name %q", req.EndpointName)
}
Expand All @@ -64,7 +75,7 @@ func (ca *computeAgent) ModifyNIC(ctx context.Context, req *computeagent.ModifyN
return nil, status.Error(codes.InvalidArgument, "received empty field in request")
}

endpoint, err := hns.GetHNSEndpointByName(req.EndpointName)
endpoint, err := hnsGetHNSEndpointByName(req.EndpointName)
if err != nil {
return nil, errors.Wrapf(err, "failed to get endpoint with name `%s`", req.EndpointName)
}
Expand Down Expand Up @@ -94,7 +105,6 @@ func (ca *computeAgent) ModifyNIC(ctx context.Context, req *computeagent.ModifyN
// DeleteNIC will delete a NIC from the computeagent services hosting UVM.
func (ca *computeAgent) DeleteNIC(ctx context.Context, req *computeagent.DeleteNICInternalRequest) (*computeagent.DeleteNICInternalResponse, error) {
log.G(ctx).WithFields(logrus.Fields{
"containerID": req.ContainerID,
"nicID": req.NicID,
"endpointName": req.EndpointName,
}).Info("DeleteNIC request")
Expand All @@ -103,7 +113,7 @@ func (ca *computeAgent) DeleteNIC(ctx context.Context, req *computeagent.DeleteN
return nil, status.Error(codes.InvalidArgument, "received empty field in request")
}

endpoint, err := hns.GetHNSEndpointByName(req.EndpointName)
endpoint, err := hnsGetHNSEndpointByName(req.EndpointName)
if err != nil {
return nil, errors.Wrapf(err, "failed to get endpoint with name %q", req.EndpointName)
}
Expand Down
Loading

0 comments on commit 4a4e5f9

Please sign in to comment.