2017-02-08 03:42:11 +08:00
|
|
|
package normalize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-02-16 01:02:50 +08:00
|
|
|
|
2020-04-15 04:47:30 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v3/models"
|
2017-02-08 03:42:11 +08:00
|
|
|
)
|
|
|
|
|
2018-03-20 05:18:58 +08:00
|
|
|
func makeRC(label, domain, target string, rc models.RecordConfig) *models.RecordConfig {
|
|
|
|
rc.SetLabel(label, domain)
|
|
|
|
rc.SetTarget(target)
|
|
|
|
return &rc
|
|
|
|
}
|
|
|
|
|
2017-02-08 03:42:11 +08:00
|
|
|
func TestImportTransform(t *testing.T) {
|
|
|
|
|
|
|
|
const transformDouble = "0.0.0.0~1.1.1.1~~9.0.0.0,10.0.0.0"
|
|
|
|
const transformSingle = "0.0.0.0~1.1.1.1~~8.0.0.0"
|
|
|
|
src := &models.DomainConfig{
|
|
|
|
Name: "stackexchange.com",
|
|
|
|
Records: []*models.RecordConfig{
|
2018-03-20 05:18:58 +08:00
|
|
|
makeRC("*", "stackexchange.com", "0.0.2.2", models.RecordConfig{Type: "A"}),
|
|
|
|
makeRC("www", "stackexchange.com", "0.0.1.1", models.RecordConfig{Type: "A"}),
|
2017-02-08 03:42:11 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
dst := &models.DomainConfig{
|
|
|
|
Name: "internal",
|
|
|
|
Records: []*models.RecordConfig{
|
2018-03-20 05:18:58 +08:00
|
|
|
makeRC("*.stackexchange.com", "*.stackexchange.com.internal", "0.0.3.3", models.RecordConfig{Type: "A", Metadata: map[string]string{"transform_table": transformSingle}}),
|
|
|
|
makeRC("@", "internal", "stackexchange.com", models.RecordConfig{Type: "IMPORT_TRANSFORM", Metadata: map[string]string{"transform_table": transformDouble}}),
|
2017-02-08 03:42:11 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cfg := &models.DNSConfig{
|
|
|
|
Domains: []*models.DomainConfig{src, dst},
|
|
|
|
}
|
2020-03-11 04:53:17 +08:00
|
|
|
if errs := ValidateAndNormalizeConfig(cfg); len(errs) != 0 {
|
2017-02-08 03:42:11 +08:00
|
|
|
for _, err := range errs {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
d := cfg.FindDomain("internal")
|
|
|
|
if len(d.Records) != 3 {
|
|
|
|
for _, r := range d.Records {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
t.Fatalf("Expected 3 records in internal, but got %d", len(d.Records))
|
|
|
|
}
|
|
|
|
}
|