Skip to content

Commit ca9063f

Browse files
authored
ensure aliases are unique (#116)
1 parent 21d7973 commit ca9063f

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

proxy/config.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proxy
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"sort"
78
"strings"
@@ -83,7 +84,16 @@ func (c *Config) FindConfig(modelName string) (ModelConfig, string, bool) {
8384
}
8485

8586
func LoadConfig(path string) (Config, error) {
86-
data, err := os.ReadFile(path)
87+
file, err := os.Open(path)
88+
if err != nil {
89+
return Config{}, err
90+
}
91+
defer file.Close()
92+
return LoadConfigFromReader(file)
93+
}
94+
95+
func LoadConfigFromReader(r io.Reader) (Config, error) {
96+
data, err := io.ReadAll(r)
8797
if err != nil {
8898
return Config{}, err
8999
}
@@ -102,6 +112,9 @@ func LoadConfig(path string) (Config, error) {
102112
config.aliases = make(map[string]string)
103113
for modelName, modelConfig := range config.Models {
104114
for _, alias := range modelConfig.Aliases {
115+
if _, found := config.aliases[alias]; found {
116+
return Config{}, fmt.Errorf("duplicate alias %s found in model: %s", alias, modelName)
117+
}
105118
config.aliases[alias] = modelName
106119
}
107120
}

proxy/config_test.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package proxy
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -138,14 +139,6 @@ groups:
138139
}
139140

140141
func 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")
149142
content := `
150143
models:
151144
model1:
@@ -171,15 +164,32 @@ groups:
171164
exclusive: false
172165
members: ["model2"]
173166
`
167+
// Load the config and verify
168+
_, err := LoadConfigFromReader(strings.NewReader(content))
169+
assert.Equal(t, "model member model2 is used in multiple groups: group1 and group2", err.Error())
174170

175-
if err := os.WriteFile(tempFile, []byte(content), 0644); err != nil {
176-
t.Fatalf("Failed to write temporary file: %v", err)
177-
}
171+
}
178172

179-
// Load the config and verify
180-
_, err = LoadConfig(tempFile)
181-
assert.NotNil(t, err)
173+
func TestConfig_ModelAliasesAreUnique(t *testing.T) {
174+
content := `
175+
models:
176+
model1:
177+
cmd: path/to/cmd --arg1 one
178+
proxy: "http://localhost:8080"
179+
aliases:
180+
- m1
181+
model2:
182+
cmd: path/to/cmd --arg1 one
183+
proxy: "http://localhost:8081"
184+
checkEndpoint: "/"
185+
aliases:
186+
- m1
187+
- m2
188+
`
182189

190+
// Load the config and verify
191+
_, err := LoadConfigFromReader(strings.NewReader(content))
192+
assert.Equal(t, "duplicate alias m1 found in model: model2", err.Error())
183193
}
184194

185195
func TestConfig_ModelConfigSanitizedCommand(t *testing.T) {

0 commit comments

Comments
 (0)