|
| 1 | +//go:build windows |
| 2 | +// +build windows |
| 3 | + |
1 | 4 | package hnsclient |
2 | 5 |
|
3 | 6 | import ( |
@@ -33,3 +36,73 @@ func TestAdhocAdjustIPConfig(t *testing.T) { |
33 | 36 | }) |
34 | 37 | } |
35 | 38 | } |
| 39 | + |
| 40 | +func TestShouldCreateLoopbackAdapter(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + hostNCExists bool |
| 44 | + vEthernetHostNCExists bool |
| 45 | + expectedShouldCreate bool |
| 46 | + expectedLogMessagePrefix string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "should create when neither interface exists", |
| 50 | + hostNCExists: false, |
| 51 | + vEthernetHostNCExists: false, |
| 52 | + expectedShouldCreate: true, |
| 53 | + expectedLogMessagePrefix: "Creating loopback adapter", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "should not create when hostNCLoopbackAdapterName exists", |
| 57 | + hostNCExists: true, |
| 58 | + vEthernetHostNCExists: false, |
| 59 | + expectedShouldCreate: false, |
| 60 | + expectedLogMessagePrefix: "LoopbackAdapterHostNCConnectivity already created", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "should not create when vEthernethostNCLoopbackAdapterName exists", |
| 64 | + hostNCExists: false, |
| 65 | + vEthernetHostNCExists: true, |
| 66 | + expectedShouldCreate: false, |
| 67 | + expectedLogMessagePrefix: "vEthernet (LoopbackAdapterHostNCConnectivity) already created", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "should not create when both interfaces exist - prioritizes hostNCLoopbackAdapterName", |
| 71 | + hostNCExists: true, |
| 72 | + vEthernetHostNCExists: true, |
| 73 | + expectedShouldCreate: false, |
| 74 | + expectedLogMessagePrefix: "LoopbackAdapterHostNCConnectivity already created", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + tt := tt |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + // Create mock interface exists function |
| 82 | + mockInterfaceExists := func(interfaceName string) (bool, error) { |
| 83 | + switch interfaceName { |
| 84 | + case hostNCLoopbackAdapterName: |
| 85 | + return tt.hostNCExists, nil |
| 86 | + case vEthernethostNCLoopbackAdapterName: |
| 87 | + return tt.vEthernetHostNCExists, nil |
| 88 | + default: |
| 89 | + return false, nil |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + shouldCreate, logMessage := shouldCreateLoopbackAdapter(mockInterfaceExists) |
| 94 | + |
| 95 | + assert.Equal(t, tt.expectedShouldCreate, shouldCreate) |
| 96 | + assert.Contains(t, logMessage, tt.expectedLogMessagePrefix) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestConstants(t *testing.T) { |
| 102 | + // Test that the vEthernet constant is constructed correctly |
| 103 | + expectedVEthernetName := "vEthernet (LoopbackAdapterHostNCConnectivity)" |
| 104 | + assert.Equal(t, expectedVEthernetName, vEthernethostNCLoopbackAdapterName) |
| 105 | + |
| 106 | + // Test that the hostNCLoopbackAdapterName constant is as expected |
| 107 | + assert.Equal(t, "LoopbackAdapterHostNCConnectivity", hostNCLoopbackAdapterName) |
| 108 | +} |
0 commit comments