@@ -3,6 +3,7 @@ package proxy
33import (
44 "os"
55 "path/filepath"
6+ "strings"
67 "testing"
78
89 "github.com/stretchr/testify/assert"
@@ -43,6 +44,7 @@ models:
4344 checkEndpoint: "/"
4445 model4:
4546 cmd: path/to/cmd --arg1 one
47+ proxy: "http://localhost:8082"
4648 checkEndpoint: "/"
4749
4850healthCheckTimeout: 15
@@ -73,6 +75,7 @@ groups:
7375 }
7476
7577 expected := Config {
78+ StartPort : 5800 ,
7679 Models : map [string ]ModelConfig {
7780 "model1" : {
7881 Cmd : "path/to/cmd --arg1 one" ,
@@ -97,6 +100,7 @@ groups:
97100 },
98101 "model4" : {
99102 Cmd : "path/to/cmd --arg1 one" ,
103+ Proxy : "http://localhost:8082" ,
100104 CheckEndpoint : "/" ,
101105 },
102106 },
@@ -138,14 +142,6 @@ groups:
138142}
139143
140144func TestConfig_GroupMemberIsUnique (t * testing.T ) {
141- // Create a temporary YAML file for testing
142- tempDir , err := os .MkdirTemp ("" , "test-config" )
143- if err != nil {
144- t .Fatalf ("Failed to create temporary directory: %v" , err )
145- }
146- defer os .RemoveAll (tempDir )
147-
148- tempFile := filepath .Join (tempDir , "config.yaml" )
149145 content := `
150146models:
151147 model1:
@@ -171,15 +167,35 @@ groups:
171167 exclusive: false
172168 members: ["model2"]
173169`
170+ // Load the config and verify
171+ _ , err := LoadConfigFromReader (strings .NewReader (content ))
174172
175- if err := os . WriteFile ( tempFile , [] byte ( content ), 0644 ); err != nil {
176- t . Fatalf ( "Failed to write temporary file: %v" , err )
177- }
173+ // a Contains as order of the map is not guaranteed
174+ assert . Contains ( t , err . Error (), "model member model2 is used in multiple groups:" )
175+ }
178176
177+ func TestConfig_ModelAliasesAreUnique (t * testing.T ) {
178+ content := `
179+ models:
180+ model1:
181+ cmd: path/to/cmd --arg1 one
182+ proxy: "http://localhost:8080"
183+ aliases:
184+ - m1
185+ model2:
186+ cmd: path/to/cmd --arg1 one
187+ proxy: "http://localhost:8081"
188+ checkEndpoint: "/"
189+ aliases:
190+ - m1
191+ - m2
192+ `
179193 // Load the config and verify
180- _ , err = LoadConfig (tempFile )
181- assert .NotNil (t , err )
194+ _ , err := LoadConfigFromReader (strings .NewReader (content ))
182195
196+ // this is a contains because it could be `model1` or `model2` depending on the order
197+ // go decided on the order of the map
198+ assert .Contains (t , err .Error (), "duplicate alias m1 found in model: model" )
183199}
184200
185201func TestConfig_ModelConfigSanitizedCommand (t * testing.T ) {
@@ -269,3 +285,77 @@ func TestConfig_SanitizeCommand(t *testing.T) {
269285 assert .Error (t , err )
270286 assert .Nil (t , args )
271287}
288+
289+ func TestConfig_AutomaticPortAssignments (t * testing.T ) {
290+
291+ t .Run ("Default Port Ranges" , func (t * testing.T ) {
292+ content := ``
293+ config , err := LoadConfigFromReader (strings .NewReader (content ))
294+ if ! assert .NoError (t , err ) {
295+ t .Fatalf ("Failed to load config: %v" , err )
296+ }
297+
298+ assert .Equal (t , 5800 , config .StartPort )
299+ })
300+ t .Run ("User specific port ranges" , func (t * testing.T ) {
301+ content := `startPort: 1000`
302+ config , err := LoadConfigFromReader (strings .NewReader (content ))
303+ if ! assert .NoError (t , err ) {
304+ t .Fatalf ("Failed to load config: %v" , err )
305+ }
306+
307+ assert .Equal (t , 1000 , config .StartPort )
308+ })
309+
310+ t .Run ("Invalid start port" , func (t * testing.T ) {
311+ content := `startPort: abcd`
312+ _ , err := LoadConfigFromReader (strings .NewReader (content ))
313+ assert .NotNil (t , err )
314+ })
315+
316+ t .Run ("start port must be greater than 1" , func (t * testing.T ) {
317+ content := `startPort: -99`
318+ _ , err := LoadConfigFromReader (strings .NewReader (content ))
319+ assert .NotNil (t , err )
320+ })
321+
322+ t .Run ("Automatic port assignments" , func (t * testing.T ) {
323+ content := `
324+ startPort: 5800
325+ models:
326+ model1:
327+ cmd: svr --port ${PORT}
328+ model2:
329+ cmd: svr --port ${PORT}
330+ proxy: "http://172.11.22.33:${PORT}"
331+ model3:
332+ cmd: svr --port 1999
333+ proxy: "http://1.2.3.4:1999"
334+ `
335+ config , err := LoadConfigFromReader (strings .NewReader (content ))
336+ if ! assert .NoError (t , err ) {
337+ t .Fatalf ("Failed to load config: %v" , err )
338+ }
339+
340+ assert .Equal (t , 5800 , config .StartPort )
341+ assert .Equal (t , "svr --port 5800" , config .Models ["model1" ].Cmd )
342+ assert .Equal (t , "http://localhost:5800" , config .Models ["model1" ].Proxy )
343+
344+ assert .Equal (t , "svr --port 5801" , config .Models ["model2" ].Cmd )
345+ assert .Equal (t , "http://172.11.22.33:5801" , config .Models ["model2" ].Proxy )
346+
347+ assert .Equal (t , "svr --port 1999" , config .Models ["model3" ].Cmd )
348+ assert .Equal (t , "http://1.2.3.4:1999" , config .Models ["model3" ].Proxy )
349+
350+ })
351+
352+ t .Run ("Proxy value required if no ${PORT} in cmd" , func (t * testing.T ) {
353+ content := `
354+ models:
355+ model1:
356+ cmd: svr --port 111
357+ `
358+ _ , err := LoadConfigFromReader (strings .NewReader (content ))
359+ assert .Equal (t , "model model1 requires a proxy value when not using automatic ${PORT}" , err .Error ())
360+ })
361+ }
0 commit comments