|
| 1 | +/* |
| 2 | +Copyright 2026 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 label |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer" |
| 28 | + fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 29 | + attrmetrics "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/metrics" |
| 30 | + attrstring "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/string" |
| 31 | +) |
| 32 | + |
| 33 | +const testLabel = "nvidia.com/gpu.product" |
| 34 | + |
| 35 | +type captureRegistrar struct { |
| 36 | + registration fwkdl.PendingRegistration |
| 37 | +} |
| 38 | + |
| 39 | +func (r *captureRegistrar) Register(reg fwkdl.PendingRegistration) error { |
| 40 | + r.registration = reg |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func newEndpoint(labels map[string]string) fwkdl.Endpoint { |
| 45 | + return fwkdl.NewEndpoint(&fwkdl.EndpointMetadata{Labels: labels}, nil) |
| 46 | +} |
| 47 | + |
| 48 | +func TestFactory(t *testing.T) { |
| 49 | + producer, err := Factory("gpu-product", fwkplugin.StrictDecoder(json.RawMessage(`{ |
| 50 | + "label": "nvidia.com/gpu.product", |
| 51 | + "attributeKey": "gpu.product", |
| 52 | + "valueType": "string" |
| 53 | + }`)), nil) |
| 54 | + |
| 55 | + require.NoError(t, err) |
| 56 | + assert.Equal(t, LabelProducerType, producer.TypedName().Type) |
| 57 | + assert.Equal(t, "gpu-product", producer.TypedName().Name) |
| 58 | +} |
| 59 | + |
| 60 | +func TestFactory_Validation(t *testing.T) { |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + parameters string |
| 64 | + wantErr string |
| 65 | + }{ |
| 66 | + {name: "missing label", parameters: `{"attributeKey":"gpu.product"}`, wantErr: "label"}, |
| 67 | + {name: "missing attribute key", parameters: `{"label":"gpu"}`, wantErr: "attributeKey"}, |
| 68 | + {name: "invalid value type", parameters: `{"label":"gpu","attributeKey":"gpu.product","valueType":"bool"}`, wantErr: "valueType"}, |
| 69 | + } |
| 70 | + |
| 71 | + for _, test := range tests { |
| 72 | + t.Run(test.name, func(t *testing.T) { |
| 73 | + producer, err := Factory("test", fwkplugin.StrictDecoder(json.RawMessage(test.parameters)), nil) |
| 74 | + require.Error(t, err) |
| 75 | + assert.Contains(t, err.Error(), test.wantErr) |
| 76 | + assert.Nil(t, producer) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestExtract_StringValue(t *testing.T) { |
| 82 | + producer, err := NewProducer("gpu-product", parameters{ |
| 83 | + Label: testLabel, |
| 84 | + AttributeKey: "gpu.product", |
| 85 | + ValueType: valueTypeString, |
| 86 | + }) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + endpoint := newEndpoint(map[string]string{testLabel: "H100"}) |
| 90 | + require.NoError(t, producer.Extract(context.Background(), fwkdl.EndpointEvent{ |
| 91 | + Type: fwkdl.EventAddOrUpdate, |
| 92 | + Endpoint: endpoint, |
| 93 | + })) |
| 94 | + |
| 95 | + value, ok := attrstring.ReadValue(endpoint.GetAttributes(), "gpu.product") |
| 96 | + require.True(t, ok) |
| 97 | + assert.Equal(t, attrstring.Value("H100"), value) |
| 98 | +} |
| 99 | + |
| 100 | +func TestExtract_NumberValue(t *testing.T) { |
| 101 | + producer, err := NewProducer("gpu-score", parameters{ |
| 102 | + Label: "gpu.score", |
| 103 | + AttributeKey: "gpu.score", |
| 104 | + ValueType: valueTypeNumber, |
| 105 | + }) |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + endpoint := newEndpoint(map[string]string{"gpu.score": "2.5"}) |
| 109 | + require.NoError(t, producer.Extract(context.Background(), fwkdl.EndpointEvent{ |
| 110 | + Type: fwkdl.EventAddOrUpdate, |
| 111 | + Endpoint: endpoint, |
| 112 | + })) |
| 113 | + |
| 114 | + value, ok := attrmetrics.ReadScalarMetricValue(endpoint.GetAttributes(), "gpu.score") |
| 115 | + require.True(t, ok) |
| 116 | + assert.Equal(t, attrmetrics.ScalarMetricValue(2.5), value) |
| 117 | +} |
| 118 | + |
| 119 | +func TestExtract_MissingLabelWritesFallbackValue(t *testing.T) { |
| 120 | + producer, err := NewProducer("gpu-product", parameters{ |
| 121 | + Label: testLabel, |
| 122 | + AttributeKey: "gpu.product", |
| 123 | + ValueType: valueTypeString, |
| 124 | + }) |
| 125 | + require.NoError(t, err) |
| 126 | + |
| 127 | + endpoint := newEndpoint(nil) |
| 128 | + require.NoError(t, producer.Extract(context.Background(), fwkdl.EndpointEvent{ |
| 129 | + Type: fwkdl.EventAddOrUpdate, |
| 130 | + Endpoint: endpoint, |
| 131 | + })) |
| 132 | + |
| 133 | + value, ok := attrstring.ReadValue(endpoint.GetAttributes(), "gpu.product") |
| 134 | + require.True(t, ok) |
| 135 | + assert.Empty(t, value) |
| 136 | +} |
| 137 | + |
| 138 | +func TestRegisterDependencies(t *testing.T) { |
| 139 | + producer, err := NewProducer("gpu-product", parameters{ |
| 140 | + Label: testLabel, |
| 141 | + AttributeKey: "gpu.product", |
| 142 | + }) |
| 143 | + require.NoError(t, err) |
| 144 | + |
| 145 | + var registrar captureRegistrar |
| 146 | + require.NoError(t, producer.RegisterDependencies(®istrar)) |
| 147 | + assert.Equal(t, "endpoint-notification-source", registrar.registration.SourceType) |
| 148 | + assert.Same(t, producer, registrar.registration.Extractor) |
| 149 | +} |
0 commit comments