2024-01-29 16:40:10 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
//go:build !consulent
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/discovery"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getAdditionalTestCases(t *testing.T) []HandleTestCase {
|
|
|
|
// PTR Lookups
|
|
|
|
return []HandleTestCase{
|
|
|
|
// PTR Lookups
|
|
|
|
{
|
|
|
|
name: "PTR Lookup for node w/ peer name, query type is ANY",
|
|
|
|
request: &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Opcode: dns.OpcodeQuery,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{
|
|
|
|
{
|
|
|
|
Name: "4.3.2.1.in-addr.arpa",
|
|
|
|
Qtype: dns.TypeANY,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
configureDataFetcher: func(fetcher discovery.CatalogDataFetcher) {
|
|
|
|
results := []*discovery.Result{
|
|
|
|
{
|
2024-02-03 03:23:52 +00:00
|
|
|
Node: &discovery.Location{Name: "foonode", Address: "1.2.3.4"},
|
2024-01-29 16:40:10 +00:00
|
|
|
Type: discovery.ResultTypeNode,
|
2024-02-03 03:23:52 +00:00
|
|
|
Service: &discovery.Location{Name: "foo", Address: "foo"},
|
2024-01-29 16:40:10 +00:00
|
|
|
Tenancy: discovery.ResultTenancy{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
PeerName: "peer1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.(*discovery.MockCatalogDataFetcher).
|
|
|
|
On("FetchRecordsByIp", mock.Anything, mock.Anything).
|
|
|
|
Return(results, nil).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
req := args.Get(1).(net.IP)
|
|
|
|
|
|
|
|
require.NotNil(t, req)
|
|
|
|
require.Equal(t, "1.2.3.4", req.String())
|
|
|
|
})
|
|
|
|
},
|
|
|
|
response: &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Opcode: dns.OpcodeQuery,
|
|
|
|
Response: true,
|
|
|
|
Authoritative: true,
|
|
|
|
},
|
|
|
|
Compress: true,
|
|
|
|
Question: []dns.Question{
|
|
|
|
{
|
|
|
|
Name: "4.3.2.1.in-addr.arpa.",
|
|
|
|
Qtype: dns.TypeANY,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.PTR{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "4.3.2.1.in-addr.arpa.",
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Ptr: "foo.node.peer1.peer.consul.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PTR Lookup for service, query type is PTR",
|
|
|
|
request: &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Opcode: dns.OpcodeQuery,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{
|
|
|
|
{
|
|
|
|
Name: "4.3.2.1.in-addr.arpa",
|
|
|
|
Qtype: dns.TypePTR,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
configureDataFetcher: func(fetcher discovery.CatalogDataFetcher) {
|
|
|
|
results := []*discovery.Result{
|
|
|
|
{
|
2024-02-03 03:23:52 +00:00
|
|
|
Node: &discovery.Location{Name: "foonode", Address: "1.2.3.4"},
|
|
|
|
Service: &discovery.Location{Name: "foo", Address: "foo"},
|
2024-01-29 16:40:10 +00:00
|
|
|
Type: discovery.ResultTypeService,
|
|
|
|
Tenancy: discovery.ResultTenancy{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.(*discovery.MockCatalogDataFetcher).
|
|
|
|
On("FetchRecordsByIp", mock.Anything, mock.Anything).
|
|
|
|
Return(results, nil).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
req := args.Get(1).(net.IP)
|
|
|
|
|
|
|
|
require.NotNil(t, req)
|
|
|
|
require.Equal(t, "1.2.3.4", req.String())
|
|
|
|
})
|
|
|
|
},
|
|
|
|
response: &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Opcode: dns.OpcodeQuery,
|
|
|
|
Response: true,
|
|
|
|
Authoritative: true,
|
|
|
|
},
|
|
|
|
Compress: true,
|
|
|
|
Question: []dns.Question{
|
|
|
|
{
|
|
|
|
Name: "4.3.2.1.in-addr.arpa.",
|
|
|
|
Qtype: dns.TypePTR,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.PTR{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: "4.3.2.1.in-addr.arpa.",
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
},
|
|
|
|
Ptr: "foo.service.dc2.consul.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|