@@ -11,6 +11,7 @@ import (
1111 "os"
1212 "reflect"
1313 "strconv"
14+ "strings"
1415 "sync"
1516 "testing"
1617 "time"
@@ -330,6 +331,86 @@ func createNCReqeustForSyncHostNCVersion(t *testing.T) cns.CreateNetworkContaine
330331 return req
331332}
332333
334+ func TestSyncHostNCVersionErrorMessages (t * testing.T ) {
335+ tests := []struct {
336+ name string
337+ ncVersionInDNC string
338+ nmaResponse nma.NCVersionList
339+ expectedErrorSubstring string
340+ }{
341+ {
342+ name : "missing NC from NMAgent response" ,
343+ ncVersionInDNC : "2" ,
344+ nmaResponse : nma.NCVersionList {
345+ Containers : []nma.NCVersion {}, // Empty response - NC is missing
346+ },
347+ expectedErrorSubstring : "missing NCs from NMAgent response" ,
348+ },
349+ {
350+ name : "outdated NC in NMAgent response" ,
351+ ncVersionInDNC : "2" ,
352+ nmaResponse : nma.NCVersionList {
353+ Containers : []nma.NCVersion {
354+ {
355+ NetworkContainerID : ncID ,
356+ Version : "1" , // Outdated version compared to DNC version 2
357+ },
358+ },
359+ },
360+ expectedErrorSubstring : "outdated NCs in NMAgent response" ,
361+ },
362+ }
363+
364+ for _ , tt := range tests {
365+ t .Run (tt .name , func (t * testing.T ) {
366+ // Create NC request with specified DNC version
367+ restartService ()
368+ setEnv (t )
369+ setOrchestratorTypeInternal (cns .KubernetesCRD )
370+
371+ secondaryIPConfigs := make (map [string ]cns.SecondaryIPConfig )
372+ ipAddress := "10.0.0.16"
373+ secIPConfig := newSecondaryIPConfig (ipAddress , 0 )
374+ ipID := uuid .New ()
375+ secondaryIPConfigs [ipID .String ()] = secIPConfig
376+ _ = createNCReqInternal (t , secondaryIPConfigs , ncID , tt .ncVersionInDNC )
377+
378+ // Set up mock NMAgent with the specified response
379+ mnma := & fakes.NMAgentClientFake {
380+ GetNCVersionListF : func (_ context.Context ) (nma.NCVersionList , error ) {
381+ return tt .nmaResponse , nil
382+ },
383+ }
384+ cleanup := setMockNMAgent (svc , mnma )
385+ defer cleanup ()
386+
387+ // Call syncHostNCVersion and verify it returns the expected error
388+ programmedCount , err := svc .syncHostNCVersion (context .Background (), cns .KubernetesCRD )
389+
390+ t .Logf ("Test case: %s, programmedCount: %d, error: %v" , tt .name , programmedCount , err )
391+
392+ if err == nil {
393+ t .Errorf ("Expected error but got none" )
394+ return
395+ }
396+
397+ if ! strings .Contains (err .Error (), tt .expectedErrorSubstring ) {
398+ t .Errorf ("Expected error message to contain '%s', but got: %s" , tt .expectedErrorSubstring , err .Error ())
399+ }
400+
401+ // In the outdated case, NMAgent has a version of the NC so it counts as programmed, just not up-to-date
402+ expectedProgrammedCount := 0
403+ if tt .name == "outdated NC in NMAgent response" {
404+ expectedProgrammedCount = 1 // NMAgent has version 1, so it's programmed to some version
405+ }
406+
407+ if programmedCount != expectedProgrammedCount {
408+ t .Errorf ("Expected programmedCount to be %d, but got %d" , expectedProgrammedCount , programmedCount )
409+ }
410+ })
411+ }
412+ }
413+
333414func TestReconcileNCWithEmptyState (t * testing.T ) {
334415 restartService ()
335416 setEnv (t )
0 commit comments