@@ -20,13 +20,168 @@ import (
20
20
"testing"
21
21
22
22
. "github.com/onsi/gomega"
23
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23
24
)
24
25
25
26
func TestInfrastructureCluster (t * testing.T ) {
26
- t .Run ("Has ignore paths" , func (t * testing.T ) {
27
- g := NewWithT (t )
28
- g .Expect (InfrastructureCluster ().IgnorePaths ()).To (Equal ([]Path {
29
- {"spec" , "controlPlaneEndpoint" },
30
- }))
31
- })
27
+ tests := []struct {
28
+ name string
29
+ infrastructureCluster * unstructured.Unstructured
30
+ want []Path
31
+ expectErr bool
32
+ }{
33
+ {
34
+ name : "No ignore paths when controlPlaneEndpoint is not set" ,
35
+ infrastructureCluster : & unstructured.Unstructured {
36
+ Object : map [string ]interface {}{
37
+ "spec" : map [string ]interface {}{
38
+ "server" : "1.2.3.4" ,
39
+ },
40
+ },
41
+ },
42
+ want : nil ,
43
+ },
44
+ {
45
+ name : "No ignore paths when controlPlaneEndpoint is nil" ,
46
+ infrastructureCluster : & unstructured.Unstructured {
47
+ Object : map [string ]interface {}{
48
+ "spec" : map [string ]interface {}{
49
+ "controlPlaneEndpoint" : nil ,
50
+ },
51
+ },
52
+ },
53
+ want : nil ,
54
+ },
55
+ {
56
+ name : "No ignore paths when controlPlaneEndpoint is an empty object" ,
57
+ infrastructureCluster : & unstructured.Unstructured {
58
+ Object : map [string ]interface {}{
59
+ "spec" : map [string ]interface {}{
60
+ "controlPlaneEndpoint" : map [string ]interface {}{},
61
+ },
62
+ },
63
+ },
64
+ want : nil ,
65
+ },
66
+ {
67
+ name : "Don't ignore host when controlPlaneEndpoint.host is set" ,
68
+ infrastructureCluster : & unstructured.Unstructured {
69
+ Object : map [string ]interface {}{
70
+ "spec" : map [string ]interface {}{
71
+ "controlPlaneEndpoint" : map [string ]interface {}{
72
+ "host" : "example.com" ,
73
+ },
74
+ },
75
+ },
76
+ },
77
+ want : nil ,
78
+ },
79
+ {
80
+ name : "Ignore host when controlPlaneEndpoint.host is set to its zero value" ,
81
+ infrastructureCluster : & unstructured.Unstructured {
82
+ Object : map [string ]interface {}{
83
+ "spec" : map [string ]interface {}{
84
+ "controlPlaneEndpoint" : map [string ]interface {}{
85
+ "host" : "" ,
86
+ },
87
+ },
88
+ },
89
+ },
90
+ want : []Path {
91
+ {"spec" , "controlPlaneEndpoint" , "host" },
92
+ },
93
+ },
94
+ {
95
+ name : "Don't ignore port when controlPlaneEndpoint.port is set" ,
96
+ infrastructureCluster : & unstructured.Unstructured {
97
+ Object : map [string ]interface {}{
98
+ "spec" : map [string ]interface {}{
99
+ "controlPlaneEndpoint" : map [string ]interface {}{
100
+ "port" : int64 (6443 ),
101
+ },
102
+ },
103
+ },
104
+ },
105
+ want : nil ,
106
+ },
107
+ {
108
+ name : "Ignore port when controlPlaneEndpoint.port is set to its zero value" ,
109
+ infrastructureCluster : & unstructured.Unstructured {
110
+ Object : map [string ]interface {}{
111
+ "spec" : map [string ]interface {}{
112
+ "controlPlaneEndpoint" : map [string ]interface {}{
113
+ "port" : int64 (0 ),
114
+ },
115
+ },
116
+ },
117
+ },
118
+ want : []Path {
119
+ {"spec" , "controlPlaneEndpoint" , "port" },
120
+ },
121
+ },
122
+ {
123
+ name : "Ignore host and port when controlPlaneEndpoint host and port are set to their zero values" ,
124
+ infrastructureCluster : & unstructured.Unstructured {
125
+ Object : map [string ]interface {}{
126
+ "spec" : map [string ]interface {}{
127
+ "controlPlaneEndpoint" : map [string ]interface {}{
128
+ "host" : "" ,
129
+ "port" : int64 (0 ),
130
+ },
131
+ },
132
+ },
133
+ },
134
+ want : []Path {
135
+ {"spec" , "controlPlaneEndpoint" , "host" },
136
+ {"spec" , "controlPlaneEndpoint" , "port" },
137
+ },
138
+ },
139
+ {
140
+ name : "Ignore host when controlPlaneEndpoint host is to its zero values, even if port is set" ,
141
+ infrastructureCluster : & unstructured.Unstructured {
142
+ Object : map [string ]interface {}{
143
+ "spec" : map [string ]interface {}{
144
+ "controlPlaneEndpoint" : map [string ]interface {}{
145
+ "host" : "" ,
146
+ "port" : int64 (6443 ),
147
+ },
148
+ },
149
+ },
150
+ },
151
+ want : []Path {
152
+ {"spec" , "controlPlaneEndpoint" , "host" },
153
+ },
154
+ },
155
+ {
156
+ name : "Ignore port when controlPlaneEndpoint port is to its zero values, even if host is set" ,
157
+ infrastructureCluster : & unstructured.Unstructured {
158
+ Object : map [string ]interface {}{
159
+ "spec" : map [string ]interface {}{
160
+ "controlPlaneEndpoint" : map [string ]interface {}{
161
+ "host" : "example.com" ,
162
+ "port" : int64 (0 ),
163
+ },
164
+ },
165
+ },
166
+ },
167
+ want : []Path {
168
+ {"spec" , "controlPlaneEndpoint" , "port" },
169
+ },
170
+ },
171
+ }
172
+
173
+ for _ , tt := range tests {
174
+ t .Run (tt .name , func (t * testing.T ) {
175
+ g := NewWithT (t )
176
+
177
+ got , err := InfrastructureCluster ().IgnorePaths (tt .infrastructureCluster )
178
+
179
+ if tt .expectErr {
180
+ g .Expect (err ).To (HaveOccurred ())
181
+ return
182
+ }
183
+ g .Expect (err ).ToNot (HaveOccurred ())
184
+ g .Expect (got ).To (Equal (tt .want ))
185
+ })
186
+ }
32
187
}
0 commit comments