|
1 | 1 | package filter |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "testing" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 12 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend" |
| 13 | + backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics" |
| 14 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
9 | 15 | ) |
10 | 16 |
|
11 | 17 | func TestByLabelFactory(t *testing.T) { |
@@ -130,3 +136,126 @@ func TestByLabelFactoryInvalidJSON(t *testing.T) { |
130 | 136 | }) |
131 | 137 | } |
132 | 138 | } |
| 139 | + |
| 140 | +// Helper functions |
| 141 | +func createPod(nsn k8stypes.NamespacedName, ipaddr string, labels map[string]string) types.Pod { |
| 142 | + return &types.PodMetrics{ |
| 143 | + Pod: &backend.Pod{ |
| 144 | + NamespacedName: nsn, |
| 145 | + Address: ipaddr, |
| 146 | + Labels: labels, |
| 147 | + }, |
| 148 | + MetricsState: &backendmetrics.MetricsState{}, |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestByLabelFiltering(t *testing.T) { |
| 153 | + pods := []types.Pod{ |
| 154 | + createPod(k8stypes.NamespacedName{Namespace: "default", Name: "nginx-1"}, |
| 155 | + "10.0.0.1", |
| 156 | + map[string]string{ |
| 157 | + "app": "nginx", |
| 158 | + "version": "v1.0", |
| 159 | + "tier": "frontend", |
| 160 | + }), |
| 161 | + createPod(k8stypes.NamespacedName{Namespace: "default", Name: "nginx-2"}, |
| 162 | + "10.0.0.2", |
| 163 | + map[string]string{ |
| 164 | + "app": "nginx", |
| 165 | + "version": "v1.1", |
| 166 | + "tier": "frontend", |
| 167 | + }), |
| 168 | + createPod(k8stypes.NamespacedName{Namespace: "kube-system", Name: "coredns-1"}, |
| 169 | + "10.0.0.3", |
| 170 | + map[string]string{ |
| 171 | + "app": "coredns", |
| 172 | + "tier": "system", |
| 173 | + }), |
| 174 | + createPod(k8stypes.NamespacedName{Namespace: "default", Name: "redis-1"}, |
| 175 | + "10.0.0.4", |
| 176 | + map[string]string{ |
| 177 | + "app": "redis", |
| 178 | + "tier": "backend", |
| 179 | + "deprecated": "true", |
| 180 | + }), |
| 181 | + createPod(k8stypes.NamespacedName{Namespace: "default", Name: "web-1"}, |
| 182 | + "10.0.0.5", |
| 183 | + map[string]string{ |
| 184 | + "app": "web", |
| 185 | + "tier": "frontend", |
| 186 | + "environment": "production", |
| 187 | + }), |
| 188 | + createPod(k8stypes.NamespacedName{Namespace: "default", Name: "no-tier-pod"}, |
| 189 | + "10.0.0.6", |
| 190 | + map[string]string{ |
| 191 | + "app": "unknown", |
| 192 | + }), |
| 193 | + } |
| 194 | + |
| 195 | + tests := []struct { |
| 196 | + testName string |
| 197 | + label string |
| 198 | + validValues []string |
| 199 | + allowsNoLabel bool |
| 200 | + expectedPods []string // pod names that should match |
| 201 | + }{ |
| 202 | + { |
| 203 | + testName: "match app nginx", |
| 204 | + label: "app", |
| 205 | + validValues: []string{"nginx"}, |
| 206 | + expectedPods: []string{"nginx-1", "nginx-2"}, |
| 207 | + allowsNoLabel: false, |
| 208 | + }, |
| 209 | + { |
| 210 | + testName: "match exact version v1.0", |
| 211 | + label: "version", |
| 212 | + validValues: []string{"v1.0"}, |
| 213 | + expectedPods: []string{"nginx-1"}, |
| 214 | + allowsNoLabel: false, |
| 215 | + }, |
| 216 | + { |
| 217 | + testName: "allow pods without 'tier' label", |
| 218 | + label: "tier", |
| 219 | + allowsNoLabel: true, |
| 220 | + expectedPods: []string{"no-tier-pod"}, // only "no-tier-pod" doesn't have a "tier" label |
| 221 | + }, |
| 222 | + { |
| 223 | + testName: "allow all known tier values", |
| 224 | + label: "tier", |
| 225 | + validValues: []string{"frontend", "backend", "system"}, |
| 226 | + allowsNoLabel: false, |
| 227 | + expectedPods: []string{"nginx-1", "nginx-2", "coredns-1", "redis-1", "web-1"}, |
| 228 | + }, |
| 229 | + } |
| 230 | + |
| 231 | + for _, tt := range tests { |
| 232 | + t.Run(tt.testName, func(t *testing.T) { |
| 233 | + rawParams, err := json.Marshal(byLabelParameters{ |
| 234 | + Label: tt.label, |
| 235 | + ValidValues: tt.validValues, |
| 236 | + AllowsNoLabel: tt.allowsNoLabel, |
| 237 | + }) |
| 238 | + require.NoError(t, err) |
| 239 | + |
| 240 | + plugin, err := ByLabelFactory("test-label", rawParams, nil) |
| 241 | + require.NoError(t, err) |
| 242 | + require.NotNil(t, plugin) |
| 243 | + |
| 244 | + blf, ok := plugin.(*ByLabel) |
| 245 | + require.True(t, ok, "plugin should be of type *ByLabel") |
| 246 | + |
| 247 | + ctx := context.Background() |
| 248 | + filteredPods := blf.Filter(ctx, nil, nil, pods) |
| 249 | + |
| 250 | + var actualPodNames []string |
| 251 | + for _, pod := range filteredPods { |
| 252 | + actualPodNames = append(actualPodNames, pod.GetPod().NamespacedName.Name) |
| 253 | + } |
| 254 | + |
| 255 | + assert.ElementsMatch(t, tt.expectedPods, actualPodNames, |
| 256 | + "filtered pods should match expected pods") |
| 257 | + assert.Len(t, filteredPods, len(tt.expectedPods), |
| 258 | + "filtered pods count should match expected count") |
| 259 | + }) |
| 260 | + } |
| 261 | +} |
0 commit comments