@@ -531,6 +531,232 @@ func TestPopulateStatusFields(t *testing.T) {
531531 },
532532 },
533533 },
534+ {
535+ name : "nested identifiers and additional status fields" ,
536+ clientInfo : & getter.Info {
537+ Resource : getter.Resource {
538+ Identifiers : []string {"metadata.uid" },
539+ AdditionalStatusFields : []string {"spec.host" , "status.phase" },
540+ },
541+ },
542+ mg : & unstructured.Unstructured {
543+ Object : map [string ]interface {}{},
544+ },
545+ body : map [string ]interface {}{
546+ "metadata" : map [string ]interface {}{
547+ "uid" : "xyz-123" ,
548+ },
549+ "spec" : map [string ]interface {}{
550+ "host" : "example.com" ,
551+ },
552+ "status" : map [string ]interface {}{
553+ "phase" : "Running" ,
554+ },
555+ },
556+ wantErr : false ,
557+ expected : map [string ]interface {}{
558+ "status" : map [string ]interface {}{
559+ "metadata" : map [string ]interface {}{
560+ "uid" : "xyz-123" ,
561+ },
562+ "spec" : map [string ]interface {}{
563+ "host" : "example.com" ,
564+ },
565+ "status" : map [string ]interface {}{
566+ "phase" : "Running" ,
567+ },
568+ },
569+ },
570+ },
571+ {
572+ name : "mixed top-level and nested fields" ,
573+ clientInfo : & getter.Info {
574+ Resource : getter.Resource {
575+ Identifiers : []string {"id" },
576+ AdditionalStatusFields : []string {"metadata.name" },
577+ },
578+ },
579+ mg : & unstructured.Unstructured {
580+ Object : map [string ]interface {}{},
581+ },
582+ body : map [string ]interface {}{
583+ "id" : "abc-456" ,
584+ "metadata" : map [string ]interface {}{
585+ "name" : "my-resource" ,
586+ },
587+ },
588+ wantErr : false ,
589+ expected : map [string ]interface {}{
590+ "status" : map [string ]interface {}{
591+ "id" : "abc-456" ,
592+ "metadata" : map [string ]interface {}{
593+ "name" : "my-resource" ,
594+ },
595+ },
596+ },
597+ },
598+ {
599+ name : "nested field not found in body" ,
600+ clientInfo : & getter.Info {
601+ Resource : getter.Resource {
602+ AdditionalStatusFields : []string {"spec.nonexistent" },
603+ },
604+ },
605+ mg : & unstructured.Unstructured {
606+ Object : map [string ]interface {}{},
607+ },
608+ body : map [string ]interface {}{
609+ "spec" : map [string ]interface {}{
610+ "host" : "example.com" ,
611+ },
612+ },
613+ wantErr : false ,
614+ expected : map [string ]interface {}{},
615+ },
616+ {
617+ name : "complex nested object" ,
618+ clientInfo : & getter.Info {
619+ Resource : getter.Resource {
620+ AdditionalStatusFields : []string {"data.config.spec" },
621+ },
622+ },
623+ mg : & unstructured.Unstructured {
624+ Object : map [string ]interface {}{},
625+ },
626+ body : map [string ]interface {}{
627+ "data" : map [string ]interface {}{
628+ "config" : map [string ]interface {}{
629+ "spec" : map [string ]interface {}{
630+ "key" : "value" ,
631+ "num" : float64 (123 ),
632+ },
633+ },
634+ },
635+ },
636+ wantErr : false ,
637+ expected : map [string ]interface {}{
638+ "status" : map [string ]interface {}{
639+ "data" : map [string ]interface {}{
640+ "config" : map [string ]interface {}{
641+ "spec" : map [string ]interface {}{
642+ "key" : "value" ,
643+ "num" : int64 (123 ),
644+ },
645+ },
646+ },
647+ },
648+ },
649+ },
650+ {
651+ name : "slice of strings" ,
652+ clientInfo : & getter.Info {
653+ Resource : getter.Resource {
654+ AdditionalStatusFields : []string {"spec.tags" },
655+ },
656+ },
657+ mg : & unstructured.Unstructured {
658+ Object : map [string ]interface {}{},
659+ },
660+ body : map [string ]interface {}{
661+ "spec" : map [string ]interface {}{
662+ "tags" : []interface {}{"tag1" , "tag2" },
663+ },
664+ },
665+ wantErr : false ,
666+ expected : map [string ]interface {}{
667+ "status" : map [string ]interface {}{
668+ "spec" : map [string ]interface {}{
669+ "tags" : []interface {}{"tag1" , "tag2" },
670+ },
671+ },
672+ },
673+ },
674+ {
675+ name : "slice of objects with mixed numeric types" ,
676+ clientInfo : & getter.Info {
677+ Resource : getter.Resource {
678+ AdditionalStatusFields : []string {"spec.ports" },
679+ },
680+ },
681+ mg : & unstructured.Unstructured {
682+ Object : map [string ]interface {}{},
683+ },
684+ body : map [string ]interface {}{
685+ "spec" : map [string ]interface {}{
686+ "ports" : []interface {}{
687+ map [string ]interface {}{"name" : "http" , "port" : 80 },
688+ map [string ]interface {}{"name" : "https" , "port" : float32 (443 )},
689+ },
690+ },
691+ },
692+ wantErr : false ,
693+ expected : map [string ]interface {}{
694+ "status" : map [string ]interface {}{
695+ "spec" : map [string ]interface {}{
696+ "ports" : []interface {}{
697+ map [string ]interface {}{"name" : "http" , "port" : int64 (80 )},
698+ map [string ]interface {}{"name" : "https" , "port" : int64 (443 )},
699+ },
700+ },
701+ },
702+ },
703+ },
704+ {
705+ name : "object with nil value" ,
706+ clientInfo : & getter.Info {
707+ Resource : getter.Resource {
708+ AdditionalStatusFields : []string {"config" },
709+ },
710+ },
711+ mg : & unstructured.Unstructured {
712+ Object : map [string ]interface {}{},
713+ },
714+ body : map [string ]interface {}{
715+ "config" : map [string ]interface {}{
716+ "settingA" : "valueA" ,
717+ "settingB" : nil ,
718+ },
719+ },
720+ wantErr : false ,
721+ expected : map [string ]interface {}{
722+ "status" : map [string ]interface {}{
723+ "config" : map [string ]interface {}{
724+ "settingA" : "valueA" ,
725+ "settingB" : nil ,
726+ },
727+ },
728+ },
729+ },
730+ {
731+ name : "slice of objects with mixed numeric types" ,
732+ clientInfo : & getter.Info {
733+ Resource : getter.Resource {
734+ AdditionalStatusFields : []string {"spec.ports" },
735+ },
736+ },
737+ mg : & unstructured.Unstructured {
738+ Object : map [string ]interface {}{},
739+ },
740+ body : map [string ]interface {}{
741+ "spec" : map [string ]interface {}{
742+ "ports" : []interface {}{
743+ map [string ]interface {}{"name" : "http" , "port" : 80 },
744+ map [string ]interface {}{"name" : "https" , "port" : float32 (443 )},
745+ },
746+ },
747+ },
748+ wantErr : false ,
749+ expected : map [string ]interface {}{
750+ "status" : map [string ]interface {}{
751+ "spec" : map [string ]interface {}{
752+ "ports" : []interface {}{
753+ map [string ]interface {}{"name" : "http" , "port" : int64 (80 )},
754+ map [string ]interface {}{"name" : "https" , "port" : int64 (443 )},
755+ },
756+ },
757+ },
758+ },
759+ },
534760 }
535761
536762 for _ , tt := range tests {
@@ -542,52 +768,8 @@ func TestPopulateStatusFields(t *testing.T) {
542768 }
543769
544770 if ! tt .wantErr {
545- // Validate the results
546- if len (tt .expected ) == 0 {
547- // No status should be created or should be empty
548- status , exists , _ := unstructured .NestedMap (tt .mg .Object , "status" )
549- if exists && len (status ) > 0 {
550- // Check if there were pre-existing status fields ("existing" field in status)
551- hasPreExisting := false
552- for _ , test := range tests {
553- if test .name == tt .name {
554- if statusObj , ok := test .mg .Object ["status" ]; ok {
555- if statusMap , ok := statusObj .(map [string ]interface {}); ok && len (statusMap ) > 0 {
556- hasPreExisting = true
557- break
558- }
559- }
560- }
561- }
562- if ! hasPreExisting {
563- t .Errorf ("populateStatusFields() unexpected status field created: %v while expected is empty" , status )
564- }
565- }
566- } else {
567- // Validate expected status fields
568- status , exists , _ := unstructured .NestedMap (tt .mg .Object , "status" )
569- if ! exists {
570- t .Errorf ("populateStatusFields() status field not created while length of expected is %d" , len (tt .expected ))
571- return
572- }
573-
574- expectedStatus := tt .expected ["status" ].(map [string ]interface {})
575-
576- // Check that all expected fields are present with correct values
577- for k , expectedVal := range expectedStatus {
578- if actualVal , ok := status [k ]; ! ok {
579- t .Errorf ("populateStatusFields() status.%s not found" , k )
580- } else if actualVal != expectedVal {
581- t .Errorf ("populateStatusFields() status.%s = %v, want %v" , k , actualVal , expectedVal )
582- }
583- }
584-
585- // For tests with existing status, ensure they're preserved
586- if tt .name == "existing status fields should be preserved" {
587- if status ["existing" ] != "existingValue" {
588- t .Errorf ("populateStatusFields() existing status field not preserved" )
589- }
590- }
771+ if diff := cmp .Diff (tt .expected , tt .mg .Object ); diff != "" {
772+ t .Errorf ("populateStatusFields() mismatch (-want +got):\n %s" , diff )
591773 }
592774 }
593775 })
0 commit comments