|
| 1 | +package platform |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/Azure/azure-container-networking/platform/windows/adapter/mocks" |
| 8 | + "github.com/golang/mock/gomock" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +var errTestFailure = errors.New("test failure") |
| 13 | + |
| 14 | +// Test if hasNetworkAdapter returns false on actual error or empty adapter name(an error) |
| 15 | +func TestHasNetworkAdapterReturnsError(t *testing.T) { |
| 16 | + ctrl := gomock.NewController(t) |
| 17 | + defer ctrl.Finish() |
| 18 | + |
| 19 | + mockNetworkAdapter := mocks.NewMockNetworkAdapter(ctrl) |
| 20 | + mockNetworkAdapter.EXPECT().GetAdapterName().Return("", errTestFailure) |
| 21 | + |
| 22 | + result := hasNetworkAdapter(mockNetworkAdapter) |
| 23 | + assert.False(t, result) |
| 24 | +} |
| 25 | + |
| 26 | +// Test if hasNetworkAdapter returns false on actual error or empty adapter name(an error) |
| 27 | +func TestHasNetworkAdapterAdapterReturnsEmptyAdapterName(t *testing.T) { |
| 28 | + ctrl := gomock.NewController(t) |
| 29 | + defer ctrl.Finish() |
| 30 | + |
| 31 | + mockNetworkAdapter := mocks.NewMockNetworkAdapter(ctrl) |
| 32 | + mockNetworkAdapter.EXPECT().GetAdapterName().Return("Ethernet 3", nil) |
| 33 | + |
| 34 | + result := hasNetworkAdapter(mockNetworkAdapter) |
| 35 | + assert.True(t, result) |
| 36 | +} |
| 37 | + |
| 38 | +// Test if updatePriorityVLANTagIfRequired returns error on getting error on calling getpriorityvlantag |
| 39 | +func TestUpdatePriorityVLANTagIfRequiredReturnsError(t *testing.T) { |
| 40 | + ctrl := gomock.NewController(t) |
| 41 | + defer ctrl.Finish() |
| 42 | + |
| 43 | + mockNetworkAdapter := mocks.NewMockNetworkAdapter(ctrl) |
| 44 | + mockNetworkAdapter.EXPECT().GetPriorityVLANTag().Return(0, errTestFailure) |
| 45 | + result := updatePriorityVLANTagIfRequired(mockNetworkAdapter, 3) |
| 46 | + assert.EqualError(t, result, "error while getting Priority VLAN Tag value: test failure") |
| 47 | +} |
| 48 | + |
| 49 | +// Test if updatePriorityVLANTagIfRequired returns nil if currentval == desiredvalue (SetPriorityVLANTag not being called) |
| 50 | +func TestUpdatePriorityVLANTagIfRequiredIfCurrentValEqualDesiredValue(t *testing.T) { |
| 51 | + ctrl := gomock.NewController(t) |
| 52 | + defer ctrl.Finish() |
| 53 | + |
| 54 | + mockNetworkAdapter := mocks.NewMockNetworkAdapter(ctrl) |
| 55 | + mockNetworkAdapter.EXPECT().GetPriorityVLANTag().Return(4, nil) |
| 56 | + result := updatePriorityVLANTagIfRequired(mockNetworkAdapter, 4) |
| 57 | + assert.NoError(t, result) |
| 58 | +} |
| 59 | + |
| 60 | +// Test if updatePriorityVLANTagIfRequired returns nil if SetPriorityVLANTag being called to set value |
| 61 | +func TestUpdatePriorityVLANTagIfRequiredIfCurrentValNotEqualDesiredValAndSetReturnsNoError(t *testing.T) { |
| 62 | + ctrl := gomock.NewController(t) |
| 63 | + defer ctrl.Finish() |
| 64 | + |
| 65 | + mockNetworkAdapter := mocks.NewMockNetworkAdapter(ctrl) |
| 66 | + mockNetworkAdapter.EXPECT().GetPriorityVLANTag().Return(1, nil) |
| 67 | + mockNetworkAdapter.EXPECT().SetPriorityVLANTag(2).Return(nil) |
| 68 | + result := updatePriorityVLANTagIfRequired(mockNetworkAdapter, 2) |
| 69 | + assert.NoError(t, result) |
| 70 | +} |
| 71 | + |
| 72 | +// Test if updatePriorityVLANTagIfRequired returns error if SetPriorityVLANTag throwing error |
| 73 | + |
| 74 | +func TestUpdatePriorityVLANTagIfRequiredIfCurrentValNotEqualDesiredValAndSetReturnsError(t *testing.T) { |
| 75 | + ctrl := gomock.NewController(t) |
| 76 | + defer ctrl.Finish() |
| 77 | + |
| 78 | + mockNetworkAdapter := mocks.NewMockNetworkAdapter(ctrl) |
| 79 | + mockNetworkAdapter.EXPECT().GetPriorityVLANTag().Return(1, nil) |
| 80 | + mockNetworkAdapter.EXPECT().SetPriorityVLANTag(5).Return(errTestFailure) |
| 81 | + result := updatePriorityVLANTagIfRequired(mockNetworkAdapter, 5) |
| 82 | + assert.EqualError(t, result, "error while setting Priority VLAN Tag value: test failure") |
| 83 | +} |
0 commit comments