|
| 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_Snippets(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 empty snippet value is not allowed", |
| 48 | + wantErrors: []string{"Invalid value: \"\""}, |
| 49 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 50 | + Snippets: []ngfAPIv1alpha1.Snippet{ |
| 51 | + { |
| 52 | + Context: ngfAPIv1alpha1.NginxContextHTTP, |
| 53 | + Value: "", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Validate no snippets is not allowed", |
| 60 | + wantErrors: []string{"Invalid value: 0"}, |
| 61 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 62 | + Snippets: []ngfAPIv1alpha1.Snippet{}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Validate maximum number of snippets is allowed", |
| 67 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 68 | + Snippets: []ngfAPIv1alpha1.Snippet{ |
| 69 | + { |
| 70 | + Context: ngfAPIv1alpha1.NginxContextMain, |
| 71 | + Value: "worker_processes 4;", |
| 72 | + }, |
| 73 | + { |
| 74 | + Context: ngfAPIv1alpha1.NginxContextHTTP, |
| 75 | + Value: "limit_req zone=one burst=5 nodelay;", |
| 76 | + }, |
| 77 | + { |
| 78 | + Context: ngfAPIv1alpha1.NginxContextHTTPServer, |
| 79 | + Value: "server_name example.com;", |
| 80 | + }, |
| 81 | + { |
| 82 | + Context: ngfAPIv1alpha1.NginxContextHTTPServerLocation, |
| 83 | + Value: "deny all;", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "Validate more than maximum number of snippets is not allowed", |
| 90 | + wantErrors: []string{"Too many: 5: must have at most 4 items"}, |
| 91 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 92 | + Snippets: []ngfAPIv1alpha1.Snippet{ |
| 93 | + { |
| 94 | + Context: ngfAPIv1alpha1.NginxContextMain, |
| 95 | + Value: "worker_processes 4;", |
| 96 | + }, |
| 97 | + { |
| 98 | + Context: ngfAPIv1alpha1.NginxContextHTTP, |
| 99 | + Value: "limit_req zone=one burst=5 nodelay;", |
| 100 | + }, |
| 101 | + { |
| 102 | + Context: ngfAPIv1alpha1.NginxContextHTTPServer, |
| 103 | + Value: "server_name example.com;", |
| 104 | + }, |
| 105 | + { |
| 106 | + Context: ngfAPIv1alpha1.NginxContextHTTPServerLocation, |
| 107 | + Value: "deny all;", |
| 108 | + }, |
| 109 | + { |
| 110 | + Context: ngfAPIv1alpha1.NginxContextHTTPServerLocation, |
| 111 | + Value: "extra configuration", |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + snippetsFilter := &ngfAPIv1alpha1.SnippetsFilter{ |
| 122 | + ObjectMeta: controllerruntime.ObjectMeta{ |
| 123 | + Name: uniqueResourceName(testResourceName), |
| 124 | + Namespace: defaultNamespace, |
| 125 | + }, |
| 126 | + Spec: tt.spec, |
| 127 | + } |
| 128 | + validateCrd(t, tt.wantErrors, snippetsFilter, k8sClient) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestSnippetsFilterValidation_Context(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + k8sClient := getKubernetesClient(t) |
| 136 | + |
| 137 | + tests := []struct { |
| 138 | + name string |
| 139 | + wantErrors []string |
| 140 | + spec ngfAPIv1alpha1.SnippetsFilterSpec |
| 141 | + }{ |
| 142 | + { |
| 143 | + name: "Validate empty context is not allowed", |
| 144 | + wantErrors: []string{"Unsupported value: \"\""}, |
| 145 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 146 | + Snippets: []ngfAPIv1alpha1.Snippet{ |
| 147 | + { |
| 148 | + Context: "", |
| 149 | + Value: "empty context configuration", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "Validate incorrect context is not allowed", |
| 156 | + wantErrors: []string{"Unsupported value: \"invalid.context\""}, |
| 157 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 158 | + Snippets: []ngfAPIv1alpha1.Snippet{ |
| 159 | + { |
| 160 | + Context: "invalid.context", |
| 161 | + Value: "invalid context configuration", |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "Validate duplicate contexts are not allowed", |
| 168 | + wantErrors: []string{"Only one snippet allowed per context"}, |
| 169 | + spec: ngfAPIv1alpha1.SnippetsFilterSpec{ |
| 170 | + Snippets: []ngfAPIv1alpha1.Snippet{ |
| 171 | + { |
| 172 | + Context: ngfAPIv1alpha1.NginxContextHTTP, |
| 173 | + Value: "limit_req zone=one burst=5 nodelay;", |
| 174 | + }, |
| 175 | + { |
| 176 | + Context: ngfAPIv1alpha1.NginxContextHTTP, |
| 177 | + Value: "sendfile on;", |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + for _, tt := range tests { |
| 185 | + t.Run(tt.name, func(t *testing.T) { |
| 186 | + t.Parallel() |
| 187 | + snippetsFilter := &ngfAPIv1alpha1.SnippetsFilter{ |
| 188 | + ObjectMeta: controllerruntime.ObjectMeta{ |
| 189 | + Name: uniqueResourceName(testResourceName), |
| 190 | + Namespace: defaultNamespace, |
| 191 | + }, |
| 192 | + Spec: tt.spec, |
| 193 | + } |
| 194 | + validateCrd(t, tt.wantErrors, snippetsFilter, k8sClient) |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
0 commit comments