|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + dto "github.com/prometheus/client_model/go" |
| 25 | + "k8s.io/utils/ptr" |
| 26 | + |
| 27 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // use hardcoded values - importing causes cycle |
| 32 | + defaultTotalQueuedRequestsMetric = "vllm:num_requests_waiting" |
| 33 | +) |
| 34 | + |
| 35 | +func TestExtractorExtract(t *testing.T) { |
| 36 | + ctx := context.Background() |
| 37 | + |
| 38 | + extractor, err := NewExtractor(defaultTotalQueuedRequestsMetric, "", "", "") |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("failed to create extractor: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + ep := datalayer.NewEndpoint(nil, nil) |
| 44 | + if ep == nil { |
| 45 | + t.Fatal("expected non-nil endpoint") |
| 46 | + } |
| 47 | + |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + data any |
| 51 | + wantErr bool |
| 52 | + updated bool // whether metrics are expected to change |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "nil data", |
| 56 | + data: nil, |
| 57 | + wantErr: true, |
| 58 | + updated: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "empty PrometheusMetricMap", |
| 62 | + data: PrometheusMetricMap{}, |
| 63 | + wantErr: true, // errors when metrics are missing |
| 64 | + updated: false, // and also not updated... |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "single valid metric", |
| 68 | + data: PrometheusMetricMap{ |
| 69 | + defaultTotalQueuedRequestsMetric: &dto.MetricFamily{ |
| 70 | + Type: dto.MetricType_GAUGE.Enum(), |
| 71 | + Metric: []*dto.Metric{ |
| 72 | + { |
| 73 | + Gauge: &dto.Gauge{Value: ptr.To(5.0)}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + wantErr: true, // missing metrics can return an error |
| 79 | + updated: true, // but should still update |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + defer func() { |
| 86 | + if r := recover(); r != nil { |
| 87 | + t.Errorf("Extract panicked: %v", r) |
| 88 | + } |
| 89 | + }() |
| 90 | + |
| 91 | + before := ep.GetMetrics().Clone() |
| 92 | + err := extractor.Extract(ctx, tt.data, ep) |
| 93 | + after := ep.GetMetrics() |
| 94 | + |
| 95 | + if tt.wantErr && err == nil { |
| 96 | + t.Errorf("expected error but got nil") |
| 97 | + } |
| 98 | + if !tt.wantErr && err != nil { |
| 99 | + t.Errorf("unexpected error: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + if tt.updated { |
| 103 | + if diff := cmp.Diff(before, after); diff == "" { |
| 104 | + t.Errorf("expected metrics to be updated, but no change detected") |
| 105 | + } |
| 106 | + } else { |
| 107 | + if diff := cmp.Diff(before, after); diff != "" { |
| 108 | + t.Errorf("expected no metrics update, but got changes:\n%s", diff) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments