|
| 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 requestcontrol |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" |
| 26 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 27 | +) |
| 28 | + |
| 29 | +type mockPrepareDataPlugin struct { |
| 30 | + name string |
| 31 | + produces map[string]any |
| 32 | + consumes map[string]any |
| 33 | +} |
| 34 | + |
| 35 | +func (m *mockPrepareDataPlugin) TypedName() plugins.TypedName { |
| 36 | + return plugins.TypedName{Name: m.name, Type: "mock"} |
| 37 | +} |
| 38 | + |
| 39 | +func (m *mockPrepareDataPlugin) Produces() map[string]any { |
| 40 | + return m.produces |
| 41 | +} |
| 42 | + |
| 43 | +func (m *mockPrepareDataPlugin) Consumes() map[string]any { |
| 44 | + return m.consumes |
| 45 | +} |
| 46 | + |
| 47 | +func (m *mockPrepareDataPlugin) PrepareRequestData(ctx context.Context, request *types.LLMRequest, pods []types.Pod) { |
| 48 | + pods[0].Put(mockProducedDataKey, mockProducedDataType{value: 42}) |
| 49 | +} |
| 50 | + |
| 51 | +func TestPrepareDataGraph(t *testing.T) { |
| 52 | + pluginA := &mockPrepareDataPlugin{name: "A", produces: map[string]any{"keyA": nil}} |
| 53 | + pluginB := &mockPrepareDataPlugin{name: "B", consumes: map[string]any{"keyA": nil}, produces: map[string]any{"keyB": nil}} |
| 54 | + pluginC := &mockPrepareDataPlugin{name: "C", consumes: map[string]any{"keyB": nil}} |
| 55 | + pluginD := &mockPrepareDataPlugin{name: "D", consumes: map[string]any{"keyA": nil}} |
| 56 | + pluginE := &mockPrepareDataPlugin{name: "E"} // No dependencies |
| 57 | + |
| 58 | + // Cycle plugins |
| 59 | + pluginX := &mockPrepareDataPlugin{name: "X", produces: map[string]any{"keyX": nil}, consumes: map[string]any{"keyY": nil}} |
| 60 | + pluginY := &mockPrepareDataPlugin{name: "Y", produces: map[string]any{"keyY": nil}, consumes: map[string]any{"keyX": nil}} |
| 61 | + |
| 62 | + testCases := []struct { |
| 63 | + name string |
| 64 | + plugins []PrepareDataPlugin |
| 65 | + expectedDAG map[string][]string |
| 66 | + expectError bool |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "No plugins", |
| 70 | + plugins: []PrepareDataPlugin{}, |
| 71 | + expectedDAG: map[string][]string{}, |
| 72 | + expectError: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Plugins with no dependencies", |
| 76 | + plugins: []PrepareDataPlugin{pluginA, pluginE}, |
| 77 | + expectedDAG: map[string][]string{ |
| 78 | + "A/mock": {}, |
| 79 | + "E/mock": {}, |
| 80 | + }, |
| 81 | + expectError: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Simple linear dependency (A -> B -> C)", |
| 85 | + plugins: []PrepareDataPlugin{pluginA, pluginB, pluginC}, |
| 86 | + expectedDAG: map[string][]string{ |
| 87 | + "A/mock": {}, |
| 88 | + "B/mock": {"A/mock"}, |
| 89 | + "C/mock": {"B/mock"}, |
| 90 | + }, |
| 91 | + expectError: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "DAG with multiple dependencies (A -> B, A -> D)", |
| 95 | + plugins: []PrepareDataPlugin{pluginA, pluginB, pluginD, pluginE}, |
| 96 | + expectedDAG: map[string][]string{ |
| 97 | + "A/mock": {}, |
| 98 | + "B/mock": {"A/mock"}, |
| 99 | + "D/mock": {"A/mock"}, |
| 100 | + "E/mock": {}, |
| 101 | + }, |
| 102 | + expectError: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "Graph with a cycle (X -> Y, Y -> X)", |
| 106 | + plugins: []PrepareDataPlugin{pluginX, pluginY}, |
| 107 | + expectedDAG: nil, |
| 108 | + expectError: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "Complex graph with a cycle", |
| 112 | + plugins: []PrepareDataPlugin{pluginA, pluginB, pluginX, pluginY}, |
| 113 | + expectedDAG: nil, |
| 114 | + expectError: true, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tc := range testCases { |
| 119 | + t.Run(tc.name, func(t *testing.T) { |
| 120 | + dag, err := prepareDataGraph(tc.plugins) |
| 121 | + |
| 122 | + if tc.expectError { |
| 123 | + assert.Error(t, err) |
| 124 | + assert.Nil(t, dag) |
| 125 | + assert.Contains(t, err.Error(), "cycle detected") |
| 126 | + } else { |
| 127 | + assert.NoError(t, err) |
| 128 | + |
| 129 | + // Normalize the slices in the maps for consistent comparison |
| 130 | + normalizedDAG := make(map[string][]string) |
| 131 | + for k, v := range dag { |
| 132 | + normalizedDAG[k] = v |
| 133 | + } |
| 134 | + normalizedExpectedDAG := make(map[string][]string) |
| 135 | + for k, v := range tc.expectedDAG { |
| 136 | + normalizedExpectedDAG[k] = v |
| 137 | + } |
| 138 | + |
| 139 | + if diff := cmp.Diff(normalizedExpectedDAG, normalizedDAG); diff != "" { |
| 140 | + t.Errorf("prepareDataGraph() mismatch (-want +got):\n%s", diff) |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments