|  | 
|  | 1 | +package cel | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"testing" | 
|  | 5 | + | 
|  | 6 | +	controllerruntime "sigs.k8s.io/controller-runtime" | 
|  | 7 | + | 
|  | 8 | +	ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1" | 
|  | 9 | +) | 
|  | 10 | + | 
|  | 11 | +func TestSnippetsFilterValidation(t *testing.T) { | 
|  | 12 | +	t.Parallel() | 
|  | 13 | +	k8sClient := getKubernetesClient(t) | 
|  | 14 | + | 
|  | 15 | +	tests := []struct { | 
|  | 16 | +		name       string | 
|  | 17 | +		wantErrors []string | 
|  | 18 | +		spec       ngfAPIv1alpha1.SnippetsFilterSpec | 
|  | 19 | +	}{ | 
|  | 20 | +		{ | 
|  | 21 | +			name: "Validate single snippet with valid context", | 
|  | 22 | +			spec: ngfAPIv1alpha1.SnippetsFilterSpec{ | 
|  | 23 | +				Snippets: []ngfAPIv1alpha1.Snippet{ | 
|  | 24 | +					{ | 
|  | 25 | +						Context: ngfAPIv1alpha1.NginxContextHTTP, | 
|  | 26 | +						Value:   "limit_req zone=one burst=5 nodelay;", | 
|  | 27 | +					}, | 
|  | 28 | +				}, | 
|  | 29 | +			}, | 
|  | 30 | +		}, | 
|  | 31 | +		{ | 
|  | 32 | +			name: "Validate multiple snippets with unique contexts", | 
|  | 33 | +			spec: ngfAPIv1alpha1.SnippetsFilterSpec{ | 
|  | 34 | +				Snippets: []ngfAPIv1alpha1.Snippet{ | 
|  | 35 | +					{ | 
|  | 36 | +						Context: ngfAPIv1alpha1.NginxContextMain, | 
|  | 37 | +						Value:   "worker_processes 4;", | 
|  | 38 | +					}, | 
|  | 39 | +					{ | 
|  | 40 | +						Context: ngfAPIv1alpha1.NginxContextHTTPServer, | 
|  | 41 | +						Value:   "server_name example.com;", | 
|  | 42 | +					}, | 
|  | 43 | +				}, | 
|  | 44 | +			}, | 
|  | 45 | +		}, | 
|  | 46 | +		{ | 
|  | 47 | +			name:       "Validate duplicate contexts are not allowed", | 
|  | 48 | +			wantErrors: []string{expectedSnippetsFilterContextError}, | 
|  | 49 | +			spec: ngfAPIv1alpha1.SnippetsFilterSpec{ | 
|  | 50 | +				Snippets: []ngfAPIv1alpha1.Snippet{ | 
|  | 51 | +					{ | 
|  | 52 | +						Context: ngfAPIv1alpha1.NginxContextHTTP, | 
|  | 53 | +						Value:   "limit_req zone=one burst=5 nodelay;", | 
|  | 54 | +					}, | 
|  | 55 | +					{ | 
|  | 56 | +						Context: ngfAPIv1alpha1.NginxContextHTTP, | 
|  | 57 | +						Value:   "sendfile on;", | 
|  | 58 | +					}, | 
|  | 59 | +				}, | 
|  | 60 | +			}, | 
|  | 61 | +		}, | 
|  | 62 | +	} | 
|  | 63 | + | 
|  | 64 | +	for _, tt := range tests { | 
|  | 65 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 66 | +			t.Parallel() | 
|  | 67 | +			snippetsFilter := &ngfAPIv1alpha1.SnippetsFilter{ | 
|  | 68 | +				ObjectMeta: controllerruntime.ObjectMeta{ | 
|  | 69 | +					Name:      uniqueResourceName(testResourceName), | 
|  | 70 | +					Namespace: defaultNamespace, | 
|  | 71 | +				}, | 
|  | 72 | +				Spec: tt.spec, | 
|  | 73 | +			} | 
|  | 74 | +			validateCrd(t, tt.wantErrors, snippetsFilter, k8sClient) | 
|  | 75 | +		}) | 
|  | 76 | +	} | 
|  | 77 | +} | 
0 commit comments