77 "testing"
88 "time"
99
10- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110 "k8s.io/apimachinery/pkg/runtime"
1211 controllerruntime "sigs.k8s.io/controller-runtime"
1312 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -36,7 +35,6 @@ const (
3635)
3736
3837func TestClientSettingsPoliciesTargetRefKind (t * testing.T ) {
39-
4038 tests := []struct {
4139 policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
4240 name string
@@ -93,23 +91,12 @@ func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
9391
9492 for _ , tt := range tests {
9593 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 )
10795 })
10896 }
10997}
11098
11199func TestClientSettingsPoliciesTargetRefGroup (t * testing.T ) {
112-
113100 tests := []struct {
114101 policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
115102 name string
@@ -148,33 +135,31 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
148135
149136 for _ , tt := range tests {
150137 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 )
162139 })
163140 }
164141}
165142
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+ },
169148) {
170149 t .Helper ()
171150
172151 // Get Kubernetes client from test framework
173152 // This should be set up by your test framework to connect to a real cluster
174153 k8sClient := getKubernetesClient (t )
175154
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+ }
178163
179164 err := k8sClient .Create (context .Background (), clientSettingsPolicy )
180165
@@ -184,7 +169,7 @@ func validateClientSettingsPolicy(t *testing.T,
184169 }()
185170
186171 // Check if we expected errors
187- if len (wantErrors ) == 0 {
172+ if len (tt . wantErrors ) == 0 {
188173 if err != nil {
189174 t .Errorf ("expected no error but got: %v" , err )
190175 }
@@ -199,7 +184,7 @@ func validateClientSettingsPolicy(t *testing.T,
199184
200185 // Check that we got the expected error messages
201186 var missingErrors []string
202- for _ , wantError := range wantErrors {
187+ for _ , wantError := range tt . wantErrors {
203188 if ! strings .Contains (err .Error (), wantError ) {
204189 missingErrors = append (missingErrors , wantError )
205190 }
@@ -209,8 +194,9 @@ func validateClientSettingsPolicy(t *testing.T,
209194 }
210195}
211196
212- // getKubernetesClient returns a client connected to a real Kubernetes cluster
197+ // getKubernetesClient returns a client connected to a real Kubernetes cluster.
213198func getKubernetesClient (t * testing.T ) client.Client {
199+ t .Helper ()
214200 // Use controller-runtime to get cluster connection
215201 k8sConfig , err := controllerruntime .GetConfig ()
216202 if err != nil {
0 commit comments