Skip to content

Commit 8f74214

Browse files
committed
Add Makefile targets to run CEL test and re format test
1 parent aa4a6b2 commit 8f74214

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

tests/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ STANDARD_CONFORMANCE_PROFILES = GATEWAY-HTTP,GATEWAY-GRPC
1717
EXPERIMENTAL_CONFORMANCE_PROFILES = GATEWAY-TLS
1818
CONFORMANCE_PROFILES = $(STANDARD_CONFORMANCE_PROFILES) # by default we use the standard conformance profiles. If experimental is enabled we override this and add the experimental profiles.
1919
SKIP_TESTS =
20+
CEL_TEST_TARGET =
2021

2122
# Check if ENABLE_EXPERIMENTAL is true
2223
ifeq ($(ENABLE_EXPERIMENTAL),true)
@@ -186,3 +187,22 @@ uninstall-ngf: ## Uninstall NGF on configured kind cluster
186187
-make uninstall-gateway-crds
187188
-kubectl delete namespace nginx-gateway
188189
-kubectl kustomize ../config/crd | kubectl delete -f -
190+
191+
# Run CEL validation integration tests against a real cluster
192+
.PHONY: test-cel-validation
193+
test-cel-validation:
194+
@if [ -z "$(CEL_TEST_TARGET)" ]; then \
195+
echo "Running all tests in ./cel"; \
196+
go test -v ./cel; \
197+
else \
198+
echo "Running test: $(CEL_TEST_TARGET) in ./cel"; \
199+
go test -v ./cel -run "$(CEL_TEST_TARGET)"; \
200+
fi
201+
202+
203+
# Set up a kind cluster, install NGF CRDs, and run CEL validation tests
204+
.PHONY: setup-and-test-cel-validation
205+
setup-and-test-cel-validation:
206+
kind create cluster --name $(CLUSTER_NAME) || true
207+
kubectl kustomize ../config/crd | kubectl apply -f -
208+
$(MAKE) test-cel-validation

tests/cel/clientsettingspolicy_test.go

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
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

3837
func 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

11199
func 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.
213198
func 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

Comments
 (0)