@@ -10,12 +10,18 @@ import (
1010 "net"
1111 "os"
1212 "reflect"
13+ "runtime"
1314 "strconv"
1415 "strings"
1516 "sync"
1617 "testing"
1718 "time"
18- "runtime"
19+
20+ "github.com/google/uuid"
21+ "github.com/pkg/errors"
22+ "github.com/stretchr/testify/assert"
23+ "github.com/stretchr/testify/require"
24+ "golang.org/x/exp/maps"
1925
2026 "github.com/Azure/azure-container-networking/cns"
2127 "github.com/Azure/azure-container-networking/cns/common"
@@ -26,11 +32,6 @@ import (
2632 "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
2733 nma "github.com/Azure/azure-container-networking/nmagent"
2834 "github.com/Azure/azure-container-networking/store"
29- "github.com/google/uuid"
30- "github.com/pkg/errors"
31- "github.com/stretchr/testify/assert"
32- "github.com/stretchr/testify/require"
33- "golang.org/x/exp/maps"
3435)
3536
3637const (
@@ -1684,19 +1685,19 @@ func setupIMDSMockAPIsWithCustomIDs(svc *HTTPRestService, interfaceIDs []string)
16841685
16851686// TestSyncHostNCVersionWithWindowsSwiftV2 tests SyncHostNCVersion and verifies it calls Windows SwiftV2 PrefixOnNic scenario
16861687func TestSyncHostNCVersionWithWindowsSwiftV2 (t * testing.T ) {
1687- svc := getTestService (cns .Kubernetes )
1688-
1688+ testSvc := getTestService (cns .Kubernetes )
1689+
16891690 // Set up test NCs with different scenarios
16901691 regularNCID := "regular-nc-id"
16911692 swiftV2NCID := "swift-v2-vnet-block-nc"
1692-
1693+
16931694 // Initialize ContainerStatus map if nil
1694- if svc .state .ContainerStatus == nil {
1695- svc .state .ContainerStatus = make (map [string ]containerstatus )
1695+ if testSvc .state .ContainerStatus == nil {
1696+ testSvc .state .ContainerStatus = make (map [string ]containerstatus )
16961697 }
1697-
1698+
16981699 // Add a regular NC
1699- svc .state .ContainerStatus [regularNCID ] = containerstatus {
1700+ testSvc .state .ContainerStatus [regularNCID ] = containerstatus {
17001701 ID : regularNCID ,
17011702 CreateNetworkContainerRequest : cns.CreateNetworkContainerRequest {
17021703 NetworkContainerid : regularNCID ,
@@ -1706,49 +1707,49 @@ func TestSyncHostNCVersionWithWindowsSwiftV2(t *testing.T) {
17061707 },
17071708 HostVersion : "1" ,
17081709 }
1709-
1710+
17101711 // Add a SwiftV2 VNETBlock NC that should trigger Windows registry operations
1711- svc .state .ContainerStatus [swiftV2NCID ] = containerstatus {
1712+ testSvc .state .ContainerStatus [swiftV2NCID ] = containerstatus {
17121713 ID : swiftV2NCID ,
17131714 CreateNetworkContainerRequest : cns.CreateNetworkContainerRequest {
17141715 NetworkContainerid : swiftV2NCID ,
1715- SwiftV2PrefixOnNic : true ,
1716+ SwiftV2PrefixOnNic : true ,
17161717 NetworkContainerType : cns .Docker ,
17171718 Version : "2" ,
17181719 },
17191720 HostVersion : "1" ,
17201721 }
1721-
1722+
17221723 // Set up mock NMAgent with NC versions
17231724 mockNMA := & fakes.NMAgentClientFake {}
1724- mockNMA .GetNCVersionListF = func (ctx context.Context ) (nma.NCVersionList , error ) {
1725+ mockNMA .GetNCVersionListF = func (_ context.Context ) (nma.NCVersionList , error ) {
17251726 return nma.NCVersionList {
17261727 Containers : []nma.NCVersion {
17271728 {
17281729 NetworkContainerID : regularNCID ,
1729- Version : "2" ,
1730+ Version : "2" ,
17301731 },
17311732 {
17321733 NetworkContainerID : swiftV2NCID ,
1733- Version : "2" ,
1734+ Version : "2" ,
17341735 },
17351736 },
17361737 }, nil
17371738 }
1738- svc .nma = mockNMA
1739-
1739+ testSvc .nma = mockNMA
1740+
17401741 // Set up mock IMDS client for Windows SwiftV2 scenario
17411742 mac1 , _ := net .ParseMAC ("AA:BB:CC:DD:EE:FF" )
17421743 mac2 , _ := net .ParseMAC ("11:22:33:44:55:66" )
1743-
1744+
17441745 interfaceMap := map [string ]imds.NetworkInterface {
17451746 "interface1" : {
17461747 InterfaceCompartmentID : "" , // Empty for Windows condition
1747- MacAddress : imds .HardwareAddr (mac1 ),
1748+ MacAddress : imds .HardwareAddr (mac1 ),
17481749 },
17491750 "interface2" : {
17501751 InterfaceCompartmentID : "nc-with-compartment-id" ,
1751- MacAddress : imds .HardwareAddr (mac2 ),
1752+ MacAddress : imds .HardwareAddr (mac2 ),
17521753 },
17531754 }
17541755 mockIMDS := & mockIMDSAdapter {
@@ -1770,46 +1771,44 @@ func TestSyncHostNCVersionWithWindowsSwiftV2(t *testing.T) {
17701771 },
17711772 },
17721773 }
1773-
1774+
17741775 // Replace the IMDS client
1775- originalIMDS := svc .imdsClient
1776- svc .imdsClient = mockIMDS
1777- defer func () { svc .imdsClient = originalIMDS }()
1778-
1776+ originalIMDS := testSvc .imdsClient
1777+ testSvc .imdsClient = mockIMDS
1778+ defer func () { testSvc .imdsClient = originalIMDS }()
1779+
17791780 // Verify preconditions
1780- assert .True (t , svc .isPrefixonNicSwiftV2 (), "isPrefixonNicSwiftV2() should return true" )
1781+ assert .True (t , testSvc .isPrefixonNicSwiftV2 (), "isPrefixonNicSwiftV2() should return true" )
17811782
17821783 ctx := context .Background ()
1783- svc .SyncHostNCVersion (ctx , cns .CRD )
1784-
1784+ testSvc .SyncHostNCVersion (ctx , cns .CRD )
1785+
17851786 // Verify that NC versions were updated
1786- updatedRegularNC := svc .state .ContainerStatus [regularNCID ]
1787- updatedSwiftV2NC := svc .state .ContainerStatus [swiftV2NCID ]
1788-
1787+ updatedRegularNC := testSvc .state .ContainerStatus [regularNCID ]
1788+ updatedSwiftV2NC := testSvc .state .ContainerStatus [swiftV2NCID ]
1789+
17891790 assert .Equal (t , "2" , updatedRegularNC .HostVersion , "Regular NC host version should be updated to 2" )
17901791 assert .Equal (t , "2" , updatedSwiftV2NC .HostVersion , "SwiftV2 NC host version should be updated to 2" )
1791-
1792- imdsNCs , err := svc .getIMDSNCs (ctx )
1792+
1793+ imdsNCs , err := testSvc .getIMDSNCs (ctx )
17931794 assert .NoError (t , err , "getIMDSNCs should not return error" )
1794-
1795+
17951796 // Verify IMDS results
17961797 assert .Contains (t , imdsNCs , "nc-with-compartment-id" , "NC with compartment ID should be in results" )
17971798 assert .Equal (t , PrefixOnNicNCVersion , imdsNCs ["nc-with-compartment-id" ], "NC should have expected version" )
1798-
1799+
17991800 // Log the conditions that would trigger Windows registry operations
18001801 isWindows := runtime .GOOS == "windows"
1801- hasSwiftV2PrefixOnNic := svc .isPrefixonNicSwiftV2 ()
1802-
1803- t .Logf ("Windows SwiftV2 PrefixOnNic conditions: (runtime.GOOS == 'windows' && service.isPrefixonNicSwiftV2()): %t" ,
1802+ hasSwiftV2PrefixOnNic := testSvc .isPrefixonNicSwiftV2 ()
1803+
1804+ t .Logf ("Windows SwiftV2 PrefixOnNic conditions: (runtime.GOOS == 'windows' && service.isPrefixonNicSwiftV2()): %t" ,
18041805 isWindows && hasSwiftV2PrefixOnNic )
1805-
1806-
1806+
18071807 // 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-
1808+ delete (testSvc .state .ContainerStatus , swiftV2NCID )
1809+ assert .False (t , testSvc .isPrefixonNicSwiftV2 (), "isPrefixonNicSwiftV2() should return false without SwiftV2 NCs" )
1810+
18111811 // Call getIMDSNCs again to verify condition is not triggered
1812- _ , err2 := svc .getIMDSNCs (ctx )
1812+ _ , err2 := testSvc .getIMDSNCs (ctx )
18131813 assert .NoError (t , err2 , "getIMDSNCs should not return error" )
18141814}
1815-
0 commit comments