@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "errors"
2222 "fmt"
23+ "maps"
2324 "testing"
2425 "time"
2526
@@ -105,38 +106,23 @@ func (ds *mockDatastore) PodList(predicate func(backendmetrics.PodMetrics) bool)
105106 return res
106107}
107108
108- type mockDataProducerPlugin struct {
109- tn plugins.TypedName
110- }
111-
112- func newMockDataProducerPlugin (name string ) * mockDataProducerPlugin {
113- return & mockDataProducerPlugin {
114- tn : plugins.TypedName {Type : "mock-prepare-request-data" , Name : name },
109+ func newMockPrepareDataPlugin (name string ) * mockPrepareDataPlugin {
110+ return & mockPrepareDataPlugin {
111+ name : name ,
112+ produces : map [string ]any {mockProducedDataKey : 0 },
113+ consumes : map [string ]any {},
115114 }
116115}
117116
118- func (m * mockDataProducerPlugin ) TypedName () plugins.TypedName {
119- return m .tn
120- }
121-
122- func (m * mockDataProducerPlugin ) Produces () map [string ]any {
123- // Produces data of type int, 0 denotes it is int.
124- return map [string ]any {mockProducedDataKey : 0 }
125- }
126-
127- func (m * mockDataProducerPlugin ) PrepareRequestData (ctx context.Context , request * schedulingtypes.LLMRequest , pods []schedulingtypes.Pod ) {
128- pods [0 ].Put (mockProducedDataKey , mockProducedDataType {value : 42 })
129- }
130-
131117type mockAdmissionPlugin struct {
132- tn plugins.TypedName
133- // TODO: Replace this will admission control.
134- admitRequestCalled bool
118+ tn plugins.TypedName
119+ denialError error
135120}
136121
137- func newMockAdmissionPlugin (name string ) * mockAdmissionPlugin {
122+ func newMockAdmissionPlugin (name string , denialError error ) * mockAdmissionPlugin {
138123 return & mockAdmissionPlugin {
139- tn : plugins.TypedName {Type : "mock-admit-data" , Name : name },
124+ tn : plugins.TypedName {Type : "mock-admit-data" , Name : name },
125+ denialError : denialError ,
140126 }
141127}
142128
@@ -145,8 +131,7 @@ func (m *mockAdmissionPlugin) TypedName() plugins.TypedName {
145131}
146132
147133func (m * mockAdmissionPlugin ) AdmitRequest (ctx context.Context , request * schedulingtypes.LLMRequest , pods []schedulingtypes.Pod ) error {
148- m .admitRequestCalled = true
149- return nil
134+ return m .denialError
150135}
151136
152137type mockProducedDataType struct {
@@ -279,9 +264,8 @@ func TestDirector_HandleRequest(t *testing.T) {
279264 wantReqCtx * handlers.RequestContext // Fields to check in the returned RequestContext
280265 wantMutatedBodyModel string // Expected model in reqCtx.Request.Body after PostDispatch
281266 targetModelName string // Expected model name after target model resolution
282- admitRequestCalled bool
283- dataProducerPlugin * mockDataProducerPlugin
284- admissionPlugin * mockAdmissionPlugin
267+ admitRequestDenialError error // Expected denial error from admission plugin
268+ prepareDataPlugin * mockPrepareDataPlugin
285269 }{
286270 {
287271 name : "successful completions request" ,
@@ -364,7 +348,7 @@ func TestDirector_HandleRequest(t *testing.T) {
364348 },
365349 wantMutatedBodyModel : model ,
366350 targetModelName : model ,
367- dataProducerPlugin : newMockDataProducerPlugin ("test-plugin" ),
351+ prepareDataPlugin : newMockPrepareDataPlugin ("test-plugin" ),
368352 },
369353 {
370354 name : "successful chat completions request with admit request plugins" ,
@@ -391,10 +375,29 @@ func TestDirector_HandleRequest(t *testing.T) {
391375 },
392376 TargetEndpoint : "192.168.1.100:8000,192.168.2.100:8000,192.168.4.100:8000" ,
393377 },
394- wantMutatedBodyModel : model ,
395- targetModelName : model ,
396- admitRequestCalled : true ,
397- admissionPlugin : newMockAdmissionPlugin ("test-plugin" ),
378+ wantMutatedBodyModel : model ,
379+ targetModelName : model ,
380+ admitRequestDenialError : nil ,
381+ },
382+ {
383+ name : "denied request by admit request plugin" ,
384+ reqBodyMap : map [string ]any {
385+ "model" : model ,
386+ "messages" : []any {
387+ map [string ]any {
388+ "role" : "user" ,
389+ "content" : "critical prompt" ,
390+ },
391+ },
392+ },
393+ mockAdmissionController : & mockAdmissionController {admitErr : nil },
394+ schedulerMockSetup : func (m * mockScheduler ) {
395+ m .scheduleResults = defaultSuccessfulScheduleResults
396+ },
397+ wantMutatedBodyModel : model ,
398+ targetModelName : model ,
399+ admitRequestDenialError : errors .New ("denied by admit plugin" ),
400+ wantErrCode : errutil .Internal ,
398401 },
399402 {
400403 name : "successful chat completions request with multiple messages" ,
@@ -546,12 +549,10 @@ func TestDirector_HandleRequest(t *testing.T) {
546549 test .schedulerMockSetup (mockSched )
547550 }
548551 config := NewConfig ()
549- if test .dataProducerPlugin != nil {
550- config = config .WithDataProducers (test .dataProducerPlugin )
551- }
552- if test .admissionPlugin != nil {
553- config = config .WithAdmissionPlugins (test .admissionPlugin )
552+ if test .prepareDataPlugin != nil {
553+ config = config .WithPrepareDataPlugins (test .prepareDataPlugin )
554554 }
555+ config = config .WithAdmissionPlugins (newMockAdmissionPlugin ("test-admit-plugin" , test .admitRequestDenialError ))
555556 director := NewDirectorWithConfig (ds , mockSched , test .mockAdmissionController , config )
556557
557558 reqCtx := & handlers.RequestContext {
@@ -566,9 +567,7 @@ func TestDirector_HandleRequest(t *testing.T) {
566567 TargetModelName : test .targetModelName ,
567568 }
568569 // Deep copy the body map.
569- for k , v := range test .reqBodyMap {
570- reqCtx .Request .Body [k ] = v
571- }
570+ maps .Copy (reqCtx .Request .Body , test .reqBodyMap )
572571
573572 returnedReqCtx , err := director .HandleRequest (ctx , reqCtx )
574573
@@ -596,9 +595,6 @@ func TestDirector_HandleRequest(t *testing.T) {
596595 assert .Equal (t , test .wantMutatedBodyModel , returnedReqCtx .Request .Body ["model" ],
597596 "Mutated reqCtx.Request.Body model mismatch" )
598597 }
599- if test .admissionPlugin != nil {
600- assert .True (t , test .admissionPlugin .admitRequestCalled , "AdmitRequest not called" )
601- }
602598 })
603599 }
604600}
0 commit comments