checking only one cname on a name (#81)

This commit is contained in:
Craig Peterson 2017-04-13 11:10:15 -06:00 committed by GitHub
parent 3c22807b91
commit 0203b3eebc
2 changed files with 53 additions and 0 deletions

View file

@ -260,9 +260,30 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
errs = append(errs, err)
}
}
for _, d := range config.Domains {
errs = append(errs, checkCNAMEs(d)...)
}
return errs
}
func checkCNAMEs(dc *models.DomainConfig) (errs []error) {
cnames := map[string]bool{}
for _, r := range dc.Records {
if r.Type == "CNAME" {
if cnames[r.Name] {
errs = append(errs, fmt.Errorf("Cannot have multiple CNAMEs with same name: %s", r.NameFQDN))
}
cnames[r.Name] = true
}
}
for _, r := range dc.Records {
if cnames[r.Name] && r.Type != "CNAME" {
errs = append(errs, fmt.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.NameFQDN))
}
}
return
}
func applyRecordTransforms(domain *models.DomainConfig) error {
for _, rec := range domain.Records {
if rec.Type != "A" {

View file

@ -3,6 +3,8 @@ package normalize
import (
"testing"
"fmt"
"github.com/StackExchange/dnscontrol/models"
)
@ -137,3 +139,33 @@ func TestTransforms(t *testing.T) {
}
}
}
func TestCNAMEMutex(t *testing.T) {
var recA = &models.RecordConfig{Type: "CNAME", Name: "foo", NameFQDN: "foo.example.com", Target: "example.com."}
tests := []struct {
rType string
name string
fail bool
}{
{"A", "foo", true},
{"A", "foo2", false},
{"CNAME", "foo", true},
{"CNAME", "foo2", false},
}
for _, tst := range tests {
t.Run(fmt.Sprintf("%s %s", tst.rType, tst.name), func(t *testing.T) {
var recB = &models.RecordConfig{Type: tst.rType, Name: tst.name, NameFQDN: tst.name + ".example.com", Target: "example2.com."}
dc := &models.DomainConfig{
Name: "example.com",
Records: []*models.RecordConfig{recA, recB},
}
errs := checkCNAMEs(dc)
if errs != nil && !tst.fail {
t.Error("Got error but expected none")
}
if errs == nil && tst.fail {
t.Error("Expected error but got none")
}
})
}
}