-
Notifications
You must be signed in to change notification settings - Fork 285
Add unit tests for computeagent #1182
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
Merged
katiewasnothere
merged 2 commits into
microsoft:master
from
katiewasnothere:add_compute_agent_unit_tests
Oct 1, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| package uvm | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/Microsoft/hcsshim/internal/computeagent" | ||
| hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2" | ||
| "github.com/Microsoft/hcsshim/internal/hns" | ||
| ) | ||
|
|
||
| type testUtilityVM struct{} | ||
|
|
||
| func (t *testUtilityVM) AddEndpointToNSWithID(ctx context.Context, nsID, nicID string, endpoint *hns.HNSEndpoint) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testUtilityVM) RemoveEndpointFromNS(ctx context.Context, id string, endpoint *hns.HNSEndpoint) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (t *testUtilityVM) UpdateNIC(ctx context.Context, id string, settings *hcsschema.NetworkAdapter) error { | ||
| return nil | ||
| } | ||
|
|
||
| func TestAddNIC(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| agent := &computeAgent{ | ||
| uvm: &testUtilityVM{}, | ||
| } | ||
|
|
||
| hnsGetHNSEndpointByName = func(endpointName string) (*hns.HNSEndpoint, error) { | ||
| return &hns.HNSEndpoint{ | ||
| Namespace: &hns.Namespace{ID: "test-namespace-ID"}, | ||
| }, nil | ||
| } | ||
|
|
||
| var ( | ||
| testNICID = "test-NIC-ID" | ||
| testEndpointName = "test-endpoint-name" | ||
| ) | ||
|
|
||
| type config struct { | ||
| name string | ||
| nicID string | ||
| endpointName string | ||
| errorExpected bool | ||
| } | ||
| tests := []config{ | ||
| { | ||
| name: "AddNIC returns no error", | ||
| nicID: testNICID, | ||
| endpointName: testEndpointName, | ||
| errorExpected: false, | ||
| }, | ||
| { | ||
| name: "AddNIC returns error with blank nic ID", | ||
| nicID: "", | ||
| endpointName: testEndpointName, | ||
| errorExpected: true, | ||
| }, | ||
| { | ||
| name: "AddNIC returns error with blank endpoint name", | ||
| nicID: testNICID, | ||
| endpointName: "", | ||
| errorExpected: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(_ *testing.T) { | ||
|
|
||
| req := &computeagent.AddNICInternalRequest{ | ||
| NicID: test.nicID, | ||
| EndpointName: test.endpointName, | ||
| } | ||
|
|
||
| _, err := agent.AddNIC(ctx, req) | ||
| if test.errorExpected && err == nil { | ||
| t.Fatalf("expected AddNIC to return an error") | ||
| } | ||
| if !test.errorExpected && err != nil { | ||
| t.Fatalf("expected AddNIC to return no error, instead got %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestModifyNIC(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| agent := &computeAgent{ | ||
| uvm: &testUtilityVM{}, | ||
| } | ||
|
|
||
| hnsGetHNSEndpointByName = func(endpointName string) (*hns.HNSEndpoint, error) { | ||
| return &hns.HNSEndpoint{ | ||
| Id: "test-ID", | ||
| MacAddress: "00-00-00-00-00-00", | ||
| }, nil | ||
| } | ||
|
|
||
| var ( | ||
| testNICID = "test-NIC-ID" | ||
| testEndpointName = "test-endpoint-name" | ||
| ) | ||
|
|
||
| iovSettingsOn := &computeagent.IovSettings{ | ||
| IovOffloadWeight: 100, | ||
| } | ||
|
|
||
| type config struct { | ||
| name string | ||
| nicID string | ||
| endpointName string | ||
| iovSettings *computeagent.IovSettings | ||
| errorExpected bool | ||
| } | ||
| tests := []config{ | ||
| { | ||
| name: "ModifyNIC returns no error", | ||
| nicID: testNICID, | ||
| endpointName: testEndpointName, | ||
| iovSettings: iovSettingsOn, | ||
| errorExpected: false, | ||
| }, | ||
| { | ||
| name: "ModifyNIC returns error with blank nic ID", | ||
| nicID: "", | ||
| endpointName: testEndpointName, | ||
| iovSettings: iovSettingsOn, | ||
| errorExpected: true, | ||
| }, | ||
| { | ||
| name: "ModifyNIC returns error with blank endpoint name", | ||
| nicID: testNICID, | ||
| endpointName: "", | ||
| iovSettings: iovSettingsOn, | ||
| errorExpected: true, | ||
| }, | ||
| { | ||
| name: "ModifyNIC returns error with nil IOV settings", | ||
| nicID: testNICID, | ||
| endpointName: testEndpointName, | ||
| iovSettings: nil, | ||
| errorExpected: true, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(_ *testing.T) { | ||
| req := &computeagent.ModifyNICInternalRequest{ | ||
| NicID: test.nicID, | ||
| EndpointName: test.endpointName, | ||
| IovPolicySettings: test.iovSettings, | ||
| } | ||
|
|
||
| _, err := agent.ModifyNIC(ctx, req) | ||
| if test.errorExpected && err == nil { | ||
|
anmaxvl marked this conversation as resolved.
|
||
| t.Fatalf("expected ModifyNIC to return an error") | ||
| } | ||
| if !test.errorExpected && err != nil { | ||
| t.Fatalf("expected ModifyNIC to return no error, instead got %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteNIC(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| agent := &computeAgent{ | ||
| uvm: &testUtilityVM{}, | ||
| } | ||
|
|
||
| hnsGetHNSEndpointByName = func(endpointName string) (*hns.HNSEndpoint, error) { | ||
| return &hns.HNSEndpoint{ | ||
| Namespace: &hns.Namespace{ID: "test-namespace-ID"}, | ||
| }, nil | ||
| } | ||
|
|
||
| var ( | ||
| testNICID = "test-NIC-ID" | ||
| testEndpointName = "test-endpoint-name" | ||
| ) | ||
|
|
||
| type config struct { | ||
| name string | ||
| nicID string | ||
| endpointName string | ||
| errorExpected bool | ||
| } | ||
| tests := []config{ | ||
| { | ||
| name: "DeleteNIC returns no error", | ||
| nicID: testNICID, | ||
| endpointName: testEndpointName, | ||
| errorExpected: false, | ||
| }, | ||
| { | ||
| name: "DeleteNIC returns error with blank nic ID", | ||
| nicID: "", | ||
| endpointName: testEndpointName, | ||
| errorExpected: true, | ||
| }, | ||
| { | ||
| name: "DeleteNIC returns error with blank endpoint name", | ||
| nicID: testNICID, | ||
| endpointName: "", | ||
| errorExpected: true, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(_ *testing.T) { | ||
| req := &computeagent.DeleteNICInternalRequest{ | ||
| NicID: test.nicID, | ||
| EndpointName: test.endpointName, | ||
| } | ||
|
|
||
| _, err := agent.DeleteNIC(ctx, req) | ||
| if test.errorExpected && err == nil { | ||
|
anmaxvl marked this conversation as resolved.
|
||
| t.Fatalf("expected DeleteNIC to return an error") | ||
| } | ||
| if !test.errorExpected && err != nil { | ||
| t.Fatalf("expected DeleteNIC to return no error, instead got %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
20 changes: 16 additions & 4 deletions
20
test/vendor/github.com/Microsoft/hcsshim/internal/uvm/computeagent.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.