From 0f14da0f157ba51137bda7c82bbcec9a2811170b Mon Sep 17 00:00:00 2001 From: Seweryn Chlewicki Date: Mon, 26 Jun 2023 15:19:16 +0100 Subject: [PATCH] Handle old format --- registry/txt.go | 8 ++++++-- registry/txt_test.go | 43 +++++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/registry/txt.go b/registry/txt.go index 4ea92d316a..8afdc8b100 100644 --- a/registry/txt.go +++ b/registry/txt.go @@ -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)) } diff --git a/registry/txt_test.go b/registry/txt_test.go index ce6d21fb96..0b21995f3d 100644 --- a/registry/txt_test.go +++ b/registry/txt_test.go @@ -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) {