@@ -44,6 +44,7 @@ models:
4444 checkEndpoint: "/"
4545 model4:
4646 cmd: path/to/cmd --arg1 one
47+ proxy: "http://localhost:8082"
4748 checkEndpoint: "/"
4849
4950healthCheckTimeout: 15
@@ -74,6 +75,7 @@ groups:
7475 }
7576
7677 expected := Config {
78+ StartPort : 5800 ,
7779 Models : map [string ]ModelConfig {
7880 "model1" : {
7981 Cmd : "path/to/cmd --arg1 one" ,
@@ -98,6 +100,7 @@ groups:
98100 },
99101 "model4" : {
100102 Cmd : "path/to/cmd --arg1 one" ,
103+ Proxy : "http://localhost:8082" ,
101104 CheckEndpoint : "/" ,
102105 },
103106 },
@@ -166,8 +169,9 @@ groups:
166169`
167170 // Load the config and verify
168171 _ , err := LoadConfigFromReader (strings .NewReader (content ))
169- assert .Equal (t , "model member model2 is used in multiple groups: group1 and group2" , err .Error ())
170172
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:" )
171175}
172176
173177func TestConfig_ModelAliasesAreUnique (t * testing.T ) {
@@ -186,10 +190,12 @@ models:
186190 - m1
187191 - m2
188192`
189-
190193 // Load the config and verify
191194 _ , err := LoadConfigFromReader (strings .NewReader (content ))
192- assert .Equal (t , "duplicate alias m1 found in model: model2" , err .Error ())
195+
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" )
193199}
194200
195201func TestConfig_ModelConfigSanitizedCommand (t * testing.T ) {
@@ -279,3 +285,77 @@ func TestConfig_SanitizeCommand(t *testing.T) {
279285 assert .Error (t , err )
280286 assert .Nil (t , args )
281287}
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