1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

TRANSIP: Fixed integration tests: Edge cases and TXT records fixed (#2673)

Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
Vincent Hagen
2023-12-04 17:54:03 +01:00
committed by GitHub
parent a366e4bc4d
commit ef081da1a6
4 changed files with 90 additions and 17 deletions

View File

@@ -0,0 +1,11 @@
package transip
import (
"regexp"
)
var removeSlashesRegexp = regexp.MustCompile(`(?:\\(\\)+)|(?:\\)`)
func removeSlashes(s string) string {
return removeSlashesRegexp.ReplaceAllString(s, "$1")
}

View File

@@ -0,0 +1,25 @@
package transip
import (
"testing"
)
func TestRemoveSlashes(t *testing.T) {
data := [][]string{
{
`quote"d`, `quote"d`,
`quote\"d`, `quote"d`,
`quote\\"d`, `quote\"d`,
`m\o\\r\\\\e`, `mo\r\\e`,
},
}
for _, testCase := range data {
result := removeSlashes(testCase[0])
if result != testCase[1] {
t.Fatalf(`Failed on "%s". Expected "%s"; got "%s".`, testCase[0], testCase[1], result)
}
}
}

View File

@@ -156,22 +156,9 @@ func (n *transipProvider) getCorrectionsUsingDiff2(dc *models.DomainConfig, reco
)
corrections = append(corrections, correction)
} else {
oldEntries, err := recordsToNative(change.Old, true)
if err != nil {
return corrections, err
}
newEntries, err := recordsToNative(change.New, false)
if err != nil {
return corrections, err
}
deleteCorrection := wrapChangeFunction(oldEntries, func(rec domain.DNSEntry) error { return n.domains.RemoveDNSEntry(dc.Name, rec) })
createCorrection := wrapChangeFunction(newEntries, func(rec domain.DNSEntry) error { return n.domains.AddDNSEntry(dc.Name, rec) })
corrections = append(
corrections,
change.CreateCorrectionWithMessage("[1/2] delete", deleteCorrection),
change.CreateCorrectionWithMessage("[2/2] create", createCorrection),
n.recreateRecordSet(dc, change)...,
)
}
case diff2.REPORT:
@@ -183,6 +170,42 @@ func (n *transipProvider) getCorrectionsUsingDiff2(dc *models.DomainConfig, reco
return corrections, nil
}
func (n *transipProvider) recreateRecordSet(dc *models.DomainConfig, change diff2.Change) []*models.Correction {
var corrections []*models.Correction
for _, rec := range change.Old {
if existsInRecords(rec, change.New) {
continue
}
nativeRec, _ := recordToNative(rec, true)
createCorrection := change.CreateCorrectionWithMessage("[2/2] delete", func() error { return n.domains.RemoveDNSEntry(dc.Name, nativeRec) })
corrections = append(corrections, createCorrection)
}
for _, rec := range change.New {
if existsInRecords(rec, change.Old) {
continue
}
nativeRec, _ := recordToNative(rec, false)
createCorrection := change.CreateCorrectionWithMessage("[1/2] create", func() error { return n.domains.AddDNSEntry(dc.Name, nativeRec) })
corrections = append(corrections, createCorrection)
}
return corrections
}
func existsInRecords(rec *models.RecordConfig, set models.Records) bool {
for _, existing := range set {
if rec.ToComparableNoTTL() == existing.ToComparableNoTTL() && rec.TTL == existing.TTL {
return true
}
}
return false
}
func recordsToNative(records models.Records, useOriginal bool) ([]domain.DNSEntry, error) {
entries := make([]domain.DNSEntry, len(records))
@@ -324,6 +347,8 @@ func getTargetRecordContent(rc *models.RecordConfig) string {
return fmt.Sprintf("%d %d %d %s", rc.DsKeyTag, rc.DsAlgorithm, rc.DsDigestType, rc.DsDigest)
case "SRV":
return fmt.Sprintf("%d %d %d %s", rc.SrvPriority, rc.SrvWeight, rc.SrvPort, rc.GetTargetField())
case "TXT":
return removeSlashes(models.StripQuotes(rc.GetTargetCombined()))
default:
return models.StripQuotes(rc.GetTargetCombined())
}