From 87cd92719742339b59760e13f9c9f09ad598e6ad Mon Sep 17 00:00:00 2001 From: Mircea-Pavel ANTON Date: Sat, 26 Oct 2024 20:09:21 +0000 Subject: [PATCH] improve structure part 2 --- internal/mikrotik/record_test.go | 369 +++++++++++++++++-------------- 1 file changed, 199 insertions(+), 170 deletions(-) diff --git a/internal/mikrotik/record_test.go b/internal/mikrotik/record_test.go index 2d7d921..a5b2393 100644 --- a/internal/mikrotik/record_test.go +++ b/internal/mikrotik/record_test.go @@ -436,17 +436,6 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { expected: nil, expectError: true, }, - { - name: "Invalid TTL in DNSRecord", - record: &DNSRecord{ - Name: "example.com", - Type: "A", - Address: "192.0.2.1", - TTL: "invalid", - }, - expected: nil, - expectError: true, - }, { name: "Unsupported record type", record: &DNSRecord{ @@ -463,7 +452,7 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) { Name: "empty.example.com", Type: "A", TTL: "1h", - // Address is empty + //! Address is empty }, expected: nil, expectError: true, @@ -508,7 +497,9 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) { expected *DNSRecord expectError bool }{ - // Valid A record + // =============================================================== + // A RECORD TEST CASES + // =============================================================== { name: "Valid A record", endpoint: &endpoint.Endpoint{ @@ -525,58 +516,6 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) { }, expectError: false, }, - // Valid AAAA record - { - name: "Valid AAAA record", - endpoint: &endpoint.Endpoint{ - DNSName: "ipv6.example.com", - RecordType: "AAAA", - Targets: endpoint.NewTargets("2001:db8::1"), - RecordTTL: endpoint.TTL(7200), - }, - expected: &DNSRecord{ - Name: "ipv6.example.com", - Type: "AAAA", - Address: "2001:db8::1", - TTL: "2h", - }, - expectError: false, - }, - // Valid CNAME record - { - name: "Valid CNAME record", - endpoint: &endpoint.Endpoint{ - DNSName: "www.example.com", - RecordType: "CNAME", - Targets: endpoint.NewTargets("example.com"), - RecordTTL: endpoint.TTL(1800), - }, - expected: &DNSRecord{ - Name: "www.example.com", - Type: "CNAME", - CName: "example.com", - TTL: "30m", - }, - expectError: false, - }, - // Valid TXT record - { - name: "Valid TXT record", - endpoint: &endpoint.Endpoint{ - DNSName: "example.com", - RecordType: "TXT", - Targets: endpoint.NewTargets("v=spf1 include:example.com ~all"), - RecordTTL: endpoint.TTL(600), - }, - expected: &DNSRecord{ - Name: "example.com", - Type: "TXT", - Text: "v=spf1 include:example.com ~all", - TTL: "10m", - }, - expectError: false, - }, - // Valid A record with multiple targets (should use first target) { name: "Valid A record with multiple targets", endpoint: &endpoint.Endpoint{ @@ -593,36 +532,19 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) { }, expectError: false, }, - // Valid record with provider-specific properties { - name: "Valid record with provider-specific properties", + name: "Invalid A record (emopty address)", endpoint: &endpoint.Endpoint{ - DNSName: "provider.example.com", + DNSName: "invalid.example.com", RecordType: "A", - Targets: endpoint.NewTargets("192.0.2.3"), + Targets: endpoint.NewTargets(), RecordTTL: endpoint.TTL(3600), - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "comment", Value: "Test comment"}, - {Name: "regexp", Value: "^www\\."}, - {Name: "match-subdomain", Value: "yes"}, - {Name: "address-list", Value: "list1"}, - }, - }, - expected: &DNSRecord{ - Name: "provider.example.com", - Type: "A", - Address: "192.0.2.3", - TTL: "1h", - Comment: "Test comment", - Regexp: "^www\\.", - MatchSubdomain: "yes", - AddressList: "list1", }, - expectError: false, + expected: nil, + expectError: true, }, - // Invalid A record (invalid IP address) { - name: "Invalid A record (invalid IP address)", + name: "Invalid A record (malformed address)", endpoint: &endpoint.Endpoint{ DNSName: "invalid.example.com", RecordType: "A", @@ -632,134 +554,176 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) { expected: nil, expectError: true, }, - // Invalid AAAA record (invalid IPv6 address) { - name: "Invalid AAAA record (invalid IPv6 address)", + name: "Invalid A record (IPv6 address)", endpoint: &endpoint.Endpoint{ DNSName: "invalid.example.com", - RecordType: "AAAA", - Targets: endpoint.NewTargets("gggg::1"), + RecordType: "A", + Targets: endpoint.NewTargets("2001:db8::1"), RecordTTL: endpoint.TTL(3600), }, expected: nil, expectError: true, }, - // Invalid CNAME record (empty target) + + // =============================================================== + // AAAA RECORD TEST CASES + // =============================================================== { - name: "Invalid CNAME record (empty target)", + name: "Valid AAAA record", endpoint: &endpoint.Endpoint{ - DNSName: "invalid.example.com", - RecordType: "CNAME", - Targets: endpoint.NewTargets(""), - RecordTTL: endpoint.TTL(1800), + DNSName: "ipv6.example.com", + RecordType: "AAAA", + Targets: endpoint.NewTargets("2001:db8::1"), + RecordTTL: endpoint.TTL(7200), }, - expected: nil, - expectError: true, + expected: &DNSRecord{ + Name: "ipv6.example.com", + Type: "AAAA", + Address: "2001:db8::1", + TTL: "2h", + }, + expectError: false, }, - // Invalid TXT record (empty text) { - name: "Invalid TXT record (empty text)", + name: "Valid AAAA record with multiple targets", endpoint: &endpoint.Endpoint{ - DNSName: "invalid.example.com", - RecordType: "TXT", + DNSName: "multi.example.com", + RecordType: "AAAA", + Targets: endpoint.NewTargets("2001:db8::1", "2001:db8::2"), + RecordTTL: endpoint.TTL(3600), + }, + expected: &DNSRecord{ + Name: "multi.example.com", + Type: "AAAA", + Address: "2001:db8::1", // Should use the first target + TTL: "1h", + }, + expectError: false, + }, + { + name: "Invalid AAAA record (empty address)", + endpoint: &endpoint.Endpoint{ + DNSName: "multi.example.com", + RecordType: "AAAA", Targets: endpoint.NewTargets(""), - RecordTTL: endpoint.TTL(600), + RecordTTL: endpoint.TTL(3600), }, expected: nil, expectError: true, }, - // Record with empty targets { - name: "Record with empty targets", + name: "Invalid AAAA record (malformed address)", endpoint: &endpoint.Endpoint{ - DNSName: "empty.example.com", - RecordType: "A", - Targets: endpoint.Targets{}, + DNSName: "invalid.example.com", + RecordType: "AAAA", + Targets: endpoint.NewTargets("gggg::1"), RecordTTL: endpoint.TTL(3600), }, expected: nil, expectError: true, }, - // Unsupported record type { - name: "Unsupported record type", + name: "Invalid AAAA record (IPv4 address)", endpoint: &endpoint.Endpoint{ - DNSName: "unsupported.example.com", - RecordType: "MX", - Targets: endpoint.NewTargets("mail.example.com"), + DNSName: "invalid.example.com", + RecordType: "AAAA", + Targets: endpoint.NewTargets("1.2.3.4"), RecordTTL: endpoint.TTL(3600), }, expected: nil, expectError: true, }, - // Invalid provider-specific configuration + + // =============================================================== + // CNAME RECORD TEST CASES + // =============================================================== { - name: "Invalid provider-specific configuration", + name: "Valid CNAME record", endpoint: &endpoint.Endpoint{ - DNSName: "example.com", - RecordType: "TXT", - Targets: endpoint.NewTargets("some text"), - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "unsupported", Value: "value"}, - }, + DNSName: "www.example.com", + RecordType: "CNAME", + Targets: endpoint.NewTargets("example.com"), + RecordTTL: endpoint.TTL(1800), }, - expected: nil, - expectError: true, + expected: &DNSRecord{ + Name: "www.example.com", + Type: "CNAME", + CName: "example.com", + TTL: "30m", + }, + expectError: false, }, - // Empty DNSName { - name: "Empty DNSName", + name: "Invalid CNAME record (empty target)", endpoint: &endpoint.Endpoint{ - DNSName: "", - RecordType: "A", - Targets: endpoint.NewTargets("192.0.2.1"), - RecordTTL: endpoint.TTL(3600), + DNSName: "invalid.example.com", + RecordType: "CNAME", + Targets: endpoint.NewTargets(""), + RecordTTL: endpoint.TTL(1800), }, expected: nil, expectError: true, }, - // Empty RecordType { - name: "Empty RecordType", + name: "Invalid CNAME record (malformed target)", endpoint: &endpoint.Endpoint{ - DNSName: "example.com", - RecordType: "", - Targets: endpoint.NewTargets("192.0.2.1"), - RecordTTL: endpoint.TTL(3600), + DNSName: "invalid.example.com", + RecordType: "CNAME", + Targets: endpoint.NewTargets("sub...............domain"), + RecordTTL: endpoint.TTL(1800), }, expected: nil, expectError: true, }, - // Empty TTL (should default to "0s") + + // =============================================================== + // TXT RECORD TEST CASES + // =============================================================== { - name: "Empty TTL (should default to '0s')", + name: "Valid TXT record", endpoint: &endpoint.Endpoint{ DNSName: "example.com", - RecordType: "A", - Targets: endpoint.NewTargets("192.0.2.1"), - // RecordTTL is zero value + RecordType: "TXT", + Targets: endpoint.NewTargets("v=spf1 include:example.com ~all"), + RecordTTL: endpoint.TTL(600), }, expected: &DNSRecord{ - Name: "example.com", - Type: "A", - Address: "192.0.2.1", - TTL: "0s", + Name: "example.com", + Type: "TXT", + Text: "v=spf1 include:example.com ~all", + TTL: "10m", }, expectError: false, }, - // Invalid TTL value { - name: "Invalid TTL value (negative)", + name: "Invalid TXT record (empty text)", + endpoint: &endpoint.Endpoint{ + DNSName: "invalid.example.com", + RecordType: "TXT", + Targets: endpoint.NewTargets(""), + RecordTTL: endpoint.TTL(600), + }, + expected: nil, + expectError: true, + }, + + // =============================================================== + // PROVIDER-SPECIFIC DATA TEST CASES + // =============================================================== + { + name: "Invalid provider-specific configuration (unknown field)", endpoint: &endpoint.Endpoint{ DNSName: "example.com", - RecordType: "A", - Targets: endpoint.NewTargets("192.0.2.1"), - RecordTTL: endpoint.TTL(-1), + RecordType: "TXT", + Targets: endpoint.NewTargets("some text"), + ProviderSpecific: endpoint.ProviderSpecific{ + {Name: "unsupported", Value: "value"}, + }, }, expected: nil, expectError: true, }, - // Setting match-subdomain via provider-specific { name: "Setting match-subdomain via provider-specific", endpoint: &endpoint.Endpoint{ @@ -780,7 +744,6 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) { }, expectError: false, }, - // Setting address-list via provider-specific { name: "Setting address-list via provider-specific", endpoint: &endpoint.Endpoint{ @@ -801,41 +764,107 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) { }, expectError: false, }, - // Multiple provider-specific properties { name: "Multiple provider-specific properties", endpoint: &endpoint.Endpoint{ - DNSName: "multi.example.com", - RecordType: "TXT", - Targets: endpoint.NewTargets("some text"), - RecordTTL: endpoint.TTL(600), + DNSName: "provider.example.com", + RecordType: "A", + Targets: endpoint.NewTargets("192.0.2.3"), + RecordTTL: endpoint.TTL(3600), ProviderSpecific: endpoint.ProviderSpecific{ {Name: "comment", Value: "Test comment"}, - {Name: "address-list", Value: "list1"}, + {Name: "regexp", Value: "^www\\."}, {Name: "match-subdomain", Value: "yes"}, + {Name: "address-list", Value: "list1"}, }, }, expected: &DNSRecord{ - Name: "multi.example.com", - Type: "TXT", - Text: "some text", - TTL: "10m", + Name: "provider.example.com", + Type: "A", + Address: "192.0.2.3", + TTL: "1h", Comment: "Test comment", - AddressList: "list1", + Regexp: "^www\\.", MatchSubdomain: "yes", + AddressList: "list1", }, expectError: false, }, - // Invalid provider-specific name + + // =============================================================== + // DEFAULT VALUES FOR UNSET FIELDS TEST CASES + // =============================================================== { - name: "Invalid provider-specific name", + name: "Empty TTL (should default to '0s')", endpoint: &endpoint.Endpoint{ - DNSName: "invalid.example.com", + DNSName: "example.com", RecordType: "A", Targets: endpoint.NewTargets("192.0.2.1"), - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "invalid-name", Value: "value"}, - }, + // RecordTTL is zero value + }, + expected: &DNSRecord{ + Name: "example.com", + Type: "A", + Address: "192.0.2.1", + TTL: "0s", + }, + expectError: false, + }, + + // =============================================================== + // GENERIC ERROR CASES + // =============================================================== + { + name: "Record with empty targets", + endpoint: &endpoint.Endpoint{ + DNSName: "empty.example.com", + RecordType: "A", + Targets: endpoint.Targets{}, + RecordTTL: endpoint.TTL(3600), + }, + expected: nil, + expectError: true, + }, + { + name: "Unsupported record type", + endpoint: &endpoint.Endpoint{ + DNSName: "unsupported.example.com", + RecordType: "FWD", + Targets: endpoint.NewTargets("example.com"), + RecordTTL: endpoint.TTL(3600), + }, + expected: nil, + expectError: true, + }, + { + name: "Empty DNSName", + endpoint: &endpoint.Endpoint{ + DNSName: "", + RecordType: "A", + Targets: endpoint.NewTargets("192.0.2.1"), + RecordTTL: endpoint.TTL(3600), + }, + expected: nil, + expectError: true, + }, + { + name: "Empty RecordType", + endpoint: &endpoint.Endpoint{ + DNSName: "example.com", + RecordType: "", + Targets: endpoint.NewTargets("192.0.2.1"), + RecordTTL: endpoint.TTL(3600), + }, + expected: nil, + expectError: true, + }, + { + name: "Invalid TTL value (negative)", + endpoint: &endpoint.Endpoint{ + DNSName: "example.com", + RecordType: "A", + Targets: endpoint.NewTargets("192.0.2.1"), + RecordTTL: endpoint.TTL(-1), }, expected: nil, expectError: true,