Skip to content

Commit 942e890

Browse files
update tests
1 parent 01e4ea0 commit 942e890

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

cns/restserver/internalapi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
230230
programmedNCs[service.state.ContainerStatus[idx].ID] = struct{}{}
231231
}
232232
}
233-
if len(outdatedNCs) != 0 {
233+
if len(outdatedNCs) == 0 {
234234
return len(programmedNCs), nil
235235
}
236236

@@ -240,7 +240,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
240240
}
241241

242242
// Get IMDS NC versions for delegated NIC scenarios
243-
imdsNCVersions, err := service.GetIMDSNCs(ctx)
243+
imdsNCVersions, err := service.getIMDSNCs(ctx)
244244
if err != nil {
245245
// If any of the NMA API check calls, imds calls fails assume that nma build doesn't have the latest changes and create empty map
246246
imdsNCVersions = make(map[string]string)
@@ -696,7 +696,7 @@ func (service *HTTPRestService) isNCDetailsAPIExists(ctx context.Context) bool {
696696
}
697697

698698
// GetIMDSNCs gets NC versions from IMDS and returns them as a map
699-
func (service *HTTPRestService) GetIMDSNCs(ctx context.Context) (map[string]string, error) {
699+
func (service *HTTPRestService) getIMDSNCs(ctx context.Context) (map[string]string, error) {
700700
imdsClient := service.imdsClient
701701
if imdsClient == nil {
702702
//nolint:staticcheck // SA1019: suppress deprecated logger.Printf usage. Todo: legacy logger usage is consistent in cns repo. Migrates when all logger usage is migrated

cns/restserver/internalapi_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sync"
1616
"testing"
1717
"time"
18+
"runtime"
1819

1920
"github.com/Azure/azure-container-networking/cns"
2021
"github.com/Azure/azure-container-networking/cns/common"
@@ -1680,3 +1681,135 @@ func setupIMDSMockAPIsWithCustomIDs(svc *HTTPRestService, interfaceIDs []string)
16801681
// Return cleanup function
16811682
return func() { svc.imdsClient = originalIMDS }
16821683
}
1684+
1685+
// TestSyncHostNCVersionWithWindowsSwiftV2 tests SyncHostNCVersion and verifies it calls Windows SwiftV2 PrefixOnNic scenario
1686+
func TestSyncHostNCVersionWithWindowsSwiftV2(t *testing.T) {
1687+
svc := getTestService(cns.Kubernetes)
1688+
1689+
// Set up test NCs with different scenarios
1690+
regularNCID := "regular-nc-id"
1691+
swiftV2NCID := "swift-v2-vnet-block-nc"
1692+
1693+
// Initialize ContainerStatus map if nil
1694+
if svc.state.ContainerStatus == nil {
1695+
svc.state.ContainerStatus = make(map[string]containerstatus)
1696+
}
1697+
1698+
// Add a regular NC
1699+
svc.state.ContainerStatus[regularNCID] = containerstatus{
1700+
ID: regularNCID,
1701+
CreateNetworkContainerRequest: cns.CreateNetworkContainerRequest{
1702+
NetworkContainerid: regularNCID,
1703+
SwiftV2PrefixOnNic: false,
1704+
NetworkContainerType: cns.Docker,
1705+
Version: "2",
1706+
},
1707+
HostVersion: "1",
1708+
}
1709+
1710+
// Add a SwiftV2 VNETBlock NC that should trigger Windows registry operations
1711+
svc.state.ContainerStatus[swiftV2NCID] = containerstatus{
1712+
ID: swiftV2NCID,
1713+
CreateNetworkContainerRequest: cns.CreateNetworkContainerRequest{
1714+
NetworkContainerid: swiftV2NCID,
1715+
SwiftV2PrefixOnNic: true,
1716+
NetworkContainerType: cns.Docker,
1717+
Version: "2",
1718+
},
1719+
HostVersion: "1",
1720+
}
1721+
1722+
// Set up mock NMAgent with NC versions
1723+
mockNMA := &fakes.NMAgentClientFake{}
1724+
mockNMA.GetNCVersionListF = func(ctx context.Context) (nma.NCVersionList, error) {
1725+
return nma.NCVersionList{
1726+
Containers: []nma.NCVersion{
1727+
{
1728+
NetworkContainerID: regularNCID,
1729+
Version: "2",
1730+
},
1731+
{
1732+
NetworkContainerID: swiftV2NCID,
1733+
Version: "2",
1734+
},
1735+
},
1736+
}, nil
1737+
}
1738+
svc.nma = mockNMA
1739+
1740+
// Set up mock IMDS client for Windows SwiftV2 scenario
1741+
mac1, _ := net.ParseMAC("AA:BB:CC:DD:EE:FF")
1742+
mac2, _ := net.ParseMAC("11:22:33:44:55:66")
1743+
1744+
interfaceMap := map[string]imds.NetworkInterface{
1745+
"interface1": {
1746+
InterfaceCompartmentID: "", // Empty for Windows condition
1747+
MacAddress: imds.HardwareAddr(mac1),
1748+
},
1749+
"interface2": {
1750+
InterfaceCompartmentID: "nc-with-compartment-id",
1751+
MacAddress: imds.HardwareAddr(mac2),
1752+
},
1753+
}
1754+
mockIMDS := &mockIMDSAdapter{
1755+
mock: &struct {
1756+
networkInterfaces func(_ context.Context) ([]imds.NetworkInterface, error)
1757+
imdsVersions func(_ context.Context) (*imds.APIVersionsResponse, error)
1758+
}{
1759+
networkInterfaces: func(_ context.Context) ([]imds.NetworkInterface, error) {
1760+
var interfaces []imds.NetworkInterface
1761+
for _, iface := range interfaceMap {
1762+
interfaces = append(interfaces, iface)
1763+
}
1764+
return interfaces, nil
1765+
},
1766+
imdsVersions: func(_ context.Context) (*imds.APIVersionsResponse, error) {
1767+
return &imds.APIVersionsResponse{
1768+
APIVersions: []string{expectedIMDSAPIVersion},
1769+
}, nil
1770+
},
1771+
},
1772+
}
1773+
1774+
// Replace the IMDS client
1775+
originalIMDS := svc.imdsClient
1776+
svc.imdsClient = mockIMDS
1777+
defer func() { svc.imdsClient = originalIMDS }()
1778+
1779+
// Verify preconditions
1780+
assert.True(t, svc.isPrefixonNicSwiftV2(), "isPrefixonNicSwiftV2() should return true")
1781+
1782+
ctx := context.Background()
1783+
svc.SyncHostNCVersion(ctx, cns.CRD)
1784+
1785+
// Verify that NC versions were updated
1786+
updatedRegularNC := svc.state.ContainerStatus[regularNCID]
1787+
updatedSwiftV2NC := svc.state.ContainerStatus[swiftV2NCID]
1788+
1789+
assert.Equal(t, "2", updatedRegularNC.HostVersion, "Regular NC host version should be updated to 2")
1790+
assert.Equal(t, "2", updatedSwiftV2NC.HostVersion, "SwiftV2 NC host version should be updated to 2")
1791+
1792+
imdsNCs, err := svc.getIMDSNCs(ctx)
1793+
assert.NoError(t, err, "getIMDSNCs should not return error")
1794+
1795+
// Verify IMDS results
1796+
assert.Contains(t, imdsNCs, "nc-with-compartment-id", "NC with compartment ID should be in results")
1797+
assert.Equal(t, PrefixOnNicNCVersion, imdsNCs["nc-with-compartment-id"], "NC should have expected version")
1798+
1799+
// Log the conditions that would trigger Windows registry operations
1800+
isWindows := runtime.GOOS == "windows"
1801+
hasSwiftV2PrefixOnNic := svc.isPrefixonNicSwiftV2()
1802+
1803+
t.Logf("Windows SwiftV2 PrefixOnNic conditions: (runtime.GOOS == 'windows' && service.isPrefixonNicSwiftV2()): %t",
1804+
isWindows && hasSwiftV2PrefixOnNic)
1805+
1806+
1807+
// Test with no SwiftV2 NCs
1808+
delete(svc.state.ContainerStatus, swiftV2NCID)
1809+
assert.False(t, svc.isPrefixonNicSwiftV2(), "isPrefixonNicSwiftV2() should return false without SwiftV2 NCs")
1810+
1811+
// Call getIMDSNCs again to verify condition is not triggered
1812+
_, err2 := svc.getIMDSNCs(ctx)
1813+
assert.NoError(t, err2, "getIMDSNCs should not return error")
1814+
}
1815+

0 commit comments

Comments
 (0)