Skip to content

Commit

Permalink
Handle old format
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewci0 committed Jun 26, 2023
1 parent 013f8cf commit 0f14da0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
8 changes: 6 additions & 2 deletions registry/txt.go
Expand Up @@ -350,13 +350,17 @@ func (pr affixNameMapper) dropAffixExtractType(name string) (string, string) {
return strings.TrimSuffix(name, iSuffix), t
}
}

// handle old TXT records
pr.prefix = pr.dropAffixTemplate(pr.prefix)
pr.suffix = pr.dropAffixTemplate(pr.suffix)
}

if strings.HasPrefix(name, pr.prefix) && pr.isPrefix() {
if pr.isPrefix() && strings.HasPrefix(name, pr.prefix) {
return extractRecordTypeDefaultPosition(strings.TrimPrefix(name, pr.prefix))
}

if strings.HasSuffix(name, pr.suffix) && pr.isSuffix() {
if pr.isSuffix() && strings.HasSuffix(name, pr.suffix) {
return extractRecordTypeDefaultPosition(strings.TrimSuffix(name, pr.suffix))
}

Expand Down
43 changes: 29 additions & 14 deletions registry/txt_test.go
Expand Up @@ -1112,24 +1112,39 @@ func TestCacheMethods(t *testing.T) {

func TestDropPrefix(t *testing.T) {
mapper := newaffixNameMapper("foo-%{record_type}-", "", "")
cnameRecord := "foo-cname-test.example.com"
aRecord := "foo-a-test.example.com"
expectedCnameRecord := "test.example.com"
expectedARecord := "test.example.com"
actualCnameRecord, _ := mapper.dropAffixExtractType(cnameRecord)
actualARecord, _ := mapper.dropAffixExtractType(aRecord)
assert.Equal(t, expectedCnameRecord, actualCnameRecord)
assert.Equal(t, expectedARecord, actualARecord)
expectedOutput := "test.example.com"

tests := []string{
"foo-cname-test.example.com",
"foo-a-test.example.com",
"foo--test.example.com",
}

for _, tc := range tests {
t.Run(tc, func(t *testing.T) {
actualOutput, _ := mapper.dropAffixExtractType(tc)
assert.Equal(t, expectedOutput, actualOutput)
})
}
}

func TestDropSuffix(t *testing.T) {
mapper := newaffixNameMapper("", "-%{record_type}-foo", "")
aRecord := "test-a-foo.example.com"
expectedARecord := "test.example.com"
r := strings.SplitN(aRecord, ".", 2)
rClean, _ := mapper.dropAffixExtractType(r[0])
actualARecord := rClean + "." + r[1]
assert.Equal(t, expectedARecord, actualARecord)
expectedOutput := "test.example.com"

tests := []string{
"test-a-foo.example.com",
"test--foo.example.com",
}

for _, tc := range tests {
t.Run(tc, func(t *testing.T) {
r := strings.SplitN(tc, ".", 2)
rClean, _ := mapper.dropAffixExtractType(r[0])
actualOutput := rClean + "." + r[1]
assert.Equal(t, expectedOutput, actualOutput)
})
}
}

func TestExtractRecordTypeDefaultPosition(t *testing.T) {
Expand Down

0 comments on commit 0f14da0

Please sign in to comment.