From a392efcc916e5cac948eb06b03820dd333fda64f Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Wed, 7 Apr 2021 19:11:45 +0200 Subject: [PATCH] config: forbid duplicate host names --- cmd/safcm/config/hosts.go | 4 ++++ cmd/safcm/config/hosts_test.go | 5 +++++ cmd/safcm/testdata/host-invalid-duplicate/hosts.yaml | 2 ++ 3 files changed, 11 insertions(+) create mode 100644 cmd/safcm/testdata/host-invalid-duplicate/hosts.yaml diff --git a/cmd/safcm/config/hosts.go b/cmd/safcm/config/hosts.go index b3c4a24..823afa1 100644 --- a/cmd/safcm/config/hosts.go +++ b/cmd/safcm/config/hosts.go @@ -69,6 +69,10 @@ func LoadHosts() (*Hosts, error) { errPrefix, GroupSpecialSeparator) } + if hostMap[x.Name] != nil { + return nil, fmt.Errorf("%s host name already exists", + errPrefix) + } hostMap[x.Name] = x } diff --git a/cmd/safcm/config/hosts_test.go b/cmd/safcm/config/hosts_test.go index ecbc37e..5d3c334 100644 --- a/cmd/safcm/config/hosts_test.go +++ b/cmd/safcm/config/hosts_test.go @@ -64,6 +64,11 @@ func TestLoadHosts(t *testing.T) { nil, }, + { + "../testdata/host-invalid-duplicate", + nil, + fmt.Errorf("hosts.yaml: host \"host1.example.org\": host name already exists"), + }, { "../testdata/host-invalid-all", nil, diff --git a/cmd/safcm/testdata/host-invalid-duplicate/hosts.yaml b/cmd/safcm/testdata/host-invalid-duplicate/hosts.yaml new file mode 100644 index 0000000..d6670b0 --- /dev/null +++ b/cmd/safcm/testdata/host-invalid-duplicate/hosts.yaml @@ -0,0 +1,2 @@ +- name: host1.example.org +- name: host1.example.org -- 2.45.2