@@ -2,6 +2,8 @@ package commands
22
33import (
44 "testing"
5+
6+ "github.com/docker/model-runner/pkg/inference"
57)
68
79func TestConfigureCmdHfOverridesFlag (t * testing.T ) {
@@ -326,6 +328,115 @@ func TestThinkFlagBehavior(t *testing.T) {
326328 }
327329}
328330
331+ func TestConfigureCmdKeepAliveFlag (t * testing.T ) {
332+ // Create the configure command
333+ cmd := newConfigureCmd ()
334+
335+ // Verify the --keep-alive flag exists
336+ keepAliveFlag := cmd .Flags ().Lookup ("keep-alive" )
337+ if keepAliveFlag == nil {
338+ t .Fatal ("--keep-alive flag not found" )
339+ return
340+ }
341+
342+ // Verify the default value is empty
343+ if keepAliveFlag .DefValue != "" {
344+ t .Errorf ("Expected default keep-alive value to be empty, got '%s'" , keepAliveFlag .DefValue )
345+ }
346+
347+ // Verify the flag type
348+ if keepAliveFlag .Value .Type () != "string" {
349+ t .Errorf ("Expected keep-alive flag type to be 'string', got '%s'" , keepAliveFlag .Value .Type ())
350+ }
351+
352+ // Test setting the flag value
353+ if err := cmd .Flags ().Set ("keep-alive" , "10m" ); err != nil {
354+ t .Errorf ("Failed to set keep-alive flag: %v" , err )
355+ }
356+
357+ // Verify the value was set
358+ if keepAliveFlag .Value .String () != "10m" {
359+ t .Errorf ("Expected keep-alive flag value to be '10m', got '%s'" , keepAliveFlag .Value .String ())
360+ }
361+ }
362+
363+ func TestKeepAliveFlagBehavior (t * testing.T ) {
364+ tests := []struct {
365+ name string
366+ keepAlive string
367+ expectSet bool
368+ expectError bool
369+ expectedValue inference.KeepAlive
370+ }{
371+ {
372+ name : "default - not set" ,
373+ keepAlive : "" ,
374+ expectSet : false ,
375+ },
376+ {
377+ name : "5 minutes" ,
378+ keepAlive : "5m" ,
379+ expectSet : true ,
380+ expectedValue : inference .KeepAliveDefault ,
381+ },
382+ {
383+ name : "unload immediately" ,
384+ keepAlive : "0" ,
385+ expectSet : true ,
386+ expectedValue : inference .KeepAliveImmediate ,
387+ },
388+ {
389+ name : "never unload" ,
390+ keepAlive : "-1" ,
391+ expectSet : true ,
392+ expectedValue : inference .KeepAliveForever ,
393+ },
394+ {
395+ name : "negative duration means forever" ,
396+ keepAlive : "-1m" ,
397+ expectSet : true ,
398+ expectedValue : inference .KeepAliveForever ,
399+ },
400+ {
401+ name : "invalid value" ,
402+ keepAlive : "abc" ,
403+ expectError : true ,
404+ },
405+ }
406+
407+ for _ , tt := range tests {
408+ t .Run (tt .name , func (t * testing.T ) {
409+ flags := ConfigureFlags {
410+ KeepAlive : tt .keepAlive ,
411+ }
412+
413+ req , err := flags .BuildConfigureRequest ("test-model" )
414+ if tt .expectError {
415+ if err == nil {
416+ t .Fatal ("Expected error but got none" )
417+ }
418+ return
419+ }
420+ if err != nil {
421+ t .Fatalf ("Unexpected error: %v" , err )
422+ }
423+
424+ if tt .expectSet {
425+ if req .KeepAlive == nil {
426+ t .Fatal ("Expected KeepAlive to be set" )
427+ }
428+ if * req .KeepAlive != tt .expectedValue {
429+ t .Errorf ("Expected KeepAlive to be %v, got %v" , tt .expectedValue , * req .KeepAlive )
430+ }
431+ } else {
432+ if req .KeepAlive != nil {
433+ t .Errorf ("Expected KeepAlive to be nil, got %v" , * req .KeepAlive )
434+ }
435+ }
436+ })
437+ }
438+ }
439+
329440func TestRuntimeFlagsValidation (t * testing.T ) {
330441 tests := []struct {
331442 name string
0 commit comments