7
7
"testing"
8
8
"time"
9
9
10
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
10
"k8s.io/apimachinery/pkg/runtime"
12
11
controllerruntime "sigs.k8s.io/controller-runtime"
13
12
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -36,7 +35,6 @@ const (
36
35
)
37
36
38
37
func TestClientSettingsPoliciesTargetRefKind (t * testing.T ) {
39
-
40
38
tests := []struct {
41
39
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
42
40
name string
@@ -93,23 +91,12 @@ func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
93
91
94
92
for _ , tt := range tests {
95
93
t .Run (tt .name , func (t * testing.T ) {
96
- // Create a ClientSettingsPolicy with the targetRef from the test case.
97
- clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
98
- ObjectMeta : metav1.ObjectMeta {
99
- Name : "test-policy" ,
100
- Namespace : "default" ,
101
- },
102
- Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
103
- TargetRef : tt .policySpec .TargetRef ,
104
- },
105
- }
106
- validateClientSettingsPolicy (t , clientSettingsPolicy , tt .wantErrors )
94
+ validateClientSettingsPolicy (t , tt )
107
95
})
108
96
}
109
97
}
110
98
111
99
func TestClientSettingsPoliciesTargetRefGroup (t * testing.T ) {
112
-
113
100
tests := []struct {
114
101
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
115
102
name string
@@ -148,33 +135,31 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
148
135
149
136
for _ , tt := range tests {
150
137
t .Run (tt .name , func (t * testing.T ) {
151
- // Create a ClientSettingsPolicy with the targetRef from the test case.
152
- clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
153
- ObjectMeta : metav1.ObjectMeta {
154
- Name : "test-policy" ,
155
- Namespace : "default" ,
156
- },
157
- Spec : ngfAPIv1alpha1.ClientSettingsPolicySpec {
158
- TargetRef : tt .policySpec .TargetRef ,
159
- },
160
- }
161
- validateClientSettingsPolicy (t , clientSettingsPolicy , tt .wantErrors )
138
+ validateClientSettingsPolicy (t , tt )
162
139
})
163
140
}
164
141
}
165
142
166
- func validateClientSettingsPolicy (t * testing.T ,
167
- clientSettingsPolicy * ngfAPIv1alpha1.ClientSettingsPolicy ,
168
- wantErrors []string ,
143
+ func validateClientSettingsPolicy (t * testing.T , tt struct {
144
+ policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
145
+ name string
146
+ wantErrors []string
147
+ },
169
148
) {
170
149
t .Helper ()
171
150
172
151
// Get Kubernetes client from test framework
173
152
// This should be set up by your test framework to connect to a real cluster
174
153
k8sClient := getKubernetesClient (t )
175
154
176
- // Make policy name unique to avoid conflicts
177
- clientSettingsPolicy .Name = fmt .Sprintf ("%s-%d" , clientSettingsPolicy .Name , time .Now ().UnixNano ())
155
+ clientSettingsPolicy := & ngfAPIv1alpha1.ClientSettingsPolicy {
156
+ ObjectMeta : controllerruntime.ObjectMeta {
157
+ // Create a unique name and namespace for the policy to avoid conflicts
158
+ Name : fmt .Sprintf ("test-policy-%d" , time .Now ().UnixNano ()),
159
+ Namespace : "default" ,
160
+ },
161
+ Spec : tt .policySpec ,
162
+ }
178
163
179
164
err := k8sClient .Create (context .Background (), clientSettingsPolicy )
180
165
@@ -184,7 +169,7 @@ func validateClientSettingsPolicy(t *testing.T,
184
169
}()
185
170
186
171
// Check if we expected errors
187
- if len (wantErrors ) == 0 {
172
+ if len (tt . wantErrors ) == 0 {
188
173
if err != nil {
189
174
t .Errorf ("expected no error but got: %v" , err )
190
175
}
@@ -199,7 +184,7 @@ func validateClientSettingsPolicy(t *testing.T,
199
184
200
185
// Check that we got the expected error messages
201
186
var missingErrors []string
202
- for _ , wantError := range wantErrors {
187
+ for _ , wantError := range tt . wantErrors {
203
188
if ! strings .Contains (err .Error (), wantError ) {
204
189
missingErrors = append (missingErrors , wantError )
205
190
}
@@ -209,8 +194,9 @@ func validateClientSettingsPolicy(t *testing.T,
209
194
}
210
195
}
211
196
212
- // getKubernetesClient returns a client connected to a real Kubernetes cluster
197
+ // getKubernetesClient returns a client connected to a real Kubernetes cluster.
213
198
func getKubernetesClient (t * testing.T ) client.Client {
199
+ t .Helper ()
214
200
// Use controller-runtime to get cluster connection
215
201
k8sConfig , err := controllerruntime .GetConfig ()
216
202
if err != nil {
0 commit comments