Skip to content

Commit

Permalink
fix: ensure config does not have duplicate hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
claby2 committed Aug 11, 2023
1 parent 8f3410b commit f229a5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ linters:
disable-all: true
enable:
- bodyclose
- errname
- errorlint
- errcheck
- exhaustive
- gofumpt
- gosec
- gosimple
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
errInvalidConfig = fmt.Errorf("invalid config")
errMustHaveOneService = fmt.Errorf("must have exactly one service")
errNoServiceWithName = fmt.Errorf("no service with name")
errDuplicateHost = fmt.Errorf("duplicate host")
)

type serviceMap map[string]struct {
Expand All @@ -32,11 +33,19 @@ type Config struct {
ReadTimeout time.Duration `yaml:"readTimeout"`
}

// validate returns true if every service has exactly one service and
// there are no duplicate hosts.
func (s *serviceMap) validate() error {
hosts := make(map[string]bool)
for name, service := range *s {
if !service.Services.Validate() {
return fmt.Errorf("%s: %w", name, errMustHaveOneService)
}

if _, ok := hosts[service.Host]; ok {
return fmt.Errorf("%w %s", errDuplicateHost, service.Host)
}
hosts[service.Host] = true
}
return nil
}
Expand Down
36 changes: 14 additions & 22 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ func TestServiceMapValidate(t *testing.T) {
},
errMustHaveOneService,
},
"services with duplicate host": {
serviceMap{
"a": {
Config: services.Config{Host: "b"},
Services: services.Services{Redirect: "c"},
},
"d": {
Config: services.Config{Host: "b"},
Services: services.Services{Redirect: "c"},
},
},
errDuplicateHost,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -410,7 +423,7 @@ func TestConfigServiceListWithContainers(t *testing.T) {
}
}

func TestServiceListLoadBalancerError(t *testing.T) {
func TestConfigServiceListLoadBalancerError(t *testing.T) {
tests := map[string]struct {
lbInfo services.LoadBalancerInfo
}{
Expand Down Expand Up @@ -453,27 +466,6 @@ func TestServiceListLoadBalancerError(t *testing.T) {
}
}

func TestServiceList(t *testing.T) {
conf := Config{
Services: serviceMap{
"foo": {
Config: services.Config{Host: "a"},
Services: services.Services{
LoadBalancer: &services.LoadBalancerInfo{
Strategy: "not a valid strategy",
},
},
},
},
}

_, err := conf.ServiceList(dockerapi.NewMock(nil))

if !errors.Is(err, errInvalidConfig) {
t.Errorf("expected error %v got error %v", errInvalidConfig, err)
}
}

func TestConfigTLSHosts(t *testing.T) {
tests := map[string]struct {
conf Config
Expand Down

0 comments on commit f229a5e

Please sign in to comment.