@@ -20,6 +20,7 @@ import (
2020 "testing"
2121
2222 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+ "k8s.io/utils/pointer"
2324
2425 "sigs.k8s.io/kubefed/pkg/controller/util"
2526)
@@ -85,3 +86,88 @@ func TestRetainClusterFields(t *testing.T) {
8586 })
8687 }
8788}
89+
90+ func TestRetainHealthCheckNodePortInServiceFields (t * testing.T ) {
91+ tests := []struct {
92+ name string
93+ desiredObj * unstructured.Unstructured
94+ clusterObj * unstructured.Unstructured
95+ retainSucceed bool
96+ expectedValue * int64
97+ }{
98+ {
99+ "cluster object has no healthCheckNodePort" ,
100+ & unstructured.Unstructured {
101+ Object : map [string ]interface {}{},
102+ },
103+ & unstructured.Unstructured {
104+ Object : map [string ]interface {}{},
105+ },
106+ true ,
107+ nil ,
108+ },
109+ {
110+ "cluster object has invalid healthCheckNodePort" ,
111+ & unstructured.Unstructured {
112+ Object : map [string ]interface {}{},
113+ },
114+ & unstructured.Unstructured {
115+ Object : map [string ]interface {}{
116+ "spec" : map [string ]interface {}{
117+ "healthCheckNodePort" : "invalid string" ,
118+ },
119+ },
120+ },
121+ false ,
122+ nil ,
123+ },
124+ {
125+ "cluster object has healthCheckNodePort 0" ,
126+ & unstructured.Unstructured {
127+ Object : map [string ]interface {}{},
128+ },
129+ & unstructured.Unstructured {
130+ Object : map [string ]interface {}{
131+ "spec" : map [string ]interface {}{
132+ "healthCheckNodePort" : int64 (0 ),
133+ },
134+ },
135+ },
136+ true ,
137+ nil ,
138+ },
139+ {
140+ "cluster object has healthCheckNodePort 1000" ,
141+ & unstructured.Unstructured {
142+ Object : map [string ]interface {}{},
143+ },
144+ & unstructured.Unstructured {
145+ Object : map [string ]interface {}{
146+ "spec" : map [string ]interface {}{
147+ "healthCheckNodePort" : int64 (1000 ),
148+ },
149+ },
150+ },
151+ true ,
152+ pointer .Int64Ptr (1000 ),
153+ },
154+ }
155+ for _ , test := range tests {
156+ t .Run (test .name , func (t * testing.T ) {
157+ if err := retainServiceFields (test .desiredObj , test .clusterObj ); (err == nil ) != test .retainSucceed {
158+ t .Fatalf ("test %s fails: unexpected returned error %v" , test .name , err )
159+ }
160+
161+ currentValue , ok , err := unstructured .NestedInt64 (test .desiredObj .Object , "spec" , "healthCheckNodePort" )
162+ if err != nil {
163+ t .Fatalf ("test %s fails: %v" , test .name , err )
164+ }
165+ if ! ok && test .expectedValue != nil {
166+ t .Fatalf ("test %s fails: expect specified healthCheckNodePort but not found" , test .name )
167+ }
168+ if ok && (test .expectedValue == nil || * test .expectedValue != currentValue ) {
169+ t .Fatalf ("test %s fails: unexpected current healthCheckNodePort %d" , test .name , currentValue )
170+ }
171+ })
172+ }
173+ }
0 commit comments