mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
5.4 KiB
225 lines
5.4 KiB
10 months ago
|
// Copyright (c) HashiCorp, Inc.
|
||
|
// SPDX-License-Identifier: BUSL-1.1
|
||
|
|
||
|
package dns
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"github.com/miekg/dns"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/stretchr/testify/require"
|
||
10 months ago
|
|
||
|
"github.com/hashicorp/consul/agent/discovery"
|
||
10 months ago
|
)
|
||
|
|
||
|
// testCaseBuildQueryFromDNSMessage is a test case for the buildQueryFromDNSMessage function.
|
||
|
type testCaseBuildQueryFromDNSMessage struct {
|
||
|
name string
|
||
|
request *dns.Msg
|
||
10 months ago
|
requestContext *Context
|
||
10 months ago
|
expectedQuery *discovery.Query
|
||
|
}
|
||
|
|
||
|
// Test_buildQueryFromDNSMessage tests the buildQueryFromDNSMessage function.
|
||
|
func Test_buildQueryFromDNSMessage(t *testing.T) {
|
||
|
|
||
10 months ago
|
testCases := []testCaseBuildQueryFromDNSMessage{
|
||
|
// virtual ip queries
|
||
|
{
|
||
|
name: "test A 'virtual.' query",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "db.virtual.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeVirtual,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "db",
|
||
|
Tenancy: discovery.QueryTenancy{},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "test A 'virtual.' with kitchen sink labels",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "db.virtual.banana.ns.orange.ap.foo.peer.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeVirtual,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "db",
|
||
|
Tenancy: discovery.QueryTenancy{
|
||
|
Peer: "foo",
|
||
|
Namespace: "banana",
|
||
|
Partition: "orange",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "test A 'virtual.' with implicit peer",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "db.virtual.foo.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeVirtual,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "db",
|
||
|
Tenancy: discovery.QueryTenancy{
|
||
|
Peer: "foo",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "test A 'virtual.' with implicit peer and namespace query",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "db.virtual.frontend.foo.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeVirtual,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "db",
|
||
|
Tenancy: discovery.QueryTenancy{
|
||
|
Namespace: "frontend",
|
||
|
Peer: "foo",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "test A 'workload.'",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "foo.workload.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeWorkload,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "foo",
|
||
|
Tenancy: discovery.QueryTenancy{},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "test A 'workload.' with all possible labels",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "api.port.foo.workload.banana.ns.orange.ap.apple.peer.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
10 months ago
|
requestContext: &Context{
|
||
|
DefaultDatacenter: "default-dc",
|
||
|
DefaultPartition: "default-partition",
|
||
|
},
|
||
10 months ago
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeWorkload,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "foo",
|
||
|
PortName: "api",
|
||
|
Tenancy: discovery.QueryTenancy{
|
||
10 months ago
|
Namespace: "banana",
|
||
|
Partition: "orange",
|
||
|
Peer: "apple",
|
||
|
Datacenter: "default-dc",
|
||
10 months ago
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "test sameness group with all possible labels",
|
||
|
request: &dns.Msg{
|
||
|
MsgHdr: dns.MsgHdr{
|
||
|
Opcode: dns.OpcodeQuery,
|
||
|
},
|
||
|
Question: []dns.Question{
|
||
|
{
|
||
|
Name: "foo.service.apple.sg.banana.ns.orange.ap.consul", // "intentionally missing the trailing dot"
|
||
|
Qtype: dns.TypeA,
|
||
|
Qclass: dns.ClassINET,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
10 months ago
|
requestContext: &Context{
|
||
|
DefaultDatacenter: "default-dc",
|
||
|
DefaultPartition: "default-partition",
|
||
|
},
|
||
10 months ago
|
expectedQuery: &discovery.Query{
|
||
|
QueryType: discovery.QueryTypeService,
|
||
|
QueryPayload: discovery.QueryPayload{
|
||
|
Name: "foo",
|
||
|
Tenancy: discovery.QueryTenancy{
|
||
|
Namespace: "banana",
|
||
|
Partition: "orange",
|
||
|
SamenessGroup: "apple",
|
||
10 months ago
|
Datacenter: "default-dc",
|
||
10 months ago
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
10 months ago
|
|
||
|
for _, tc := range testCases {
|
||
|
t.Run(tc.name, func(t *testing.T) {
|
||
10 months ago
|
context := tc.requestContext
|
||
|
if context == nil {
|
||
|
context = &Context{}
|
||
|
}
|
||
10 months ago
|
query, err := buildQueryFromDNSMessage(tc.request, *context, "consul.", ".", nil)
|
||
10 months ago
|
require.NoError(t, err)
|
||
|
assert.Equal(t, tc.expectedQuery, query)
|
||
|
})
|
||
|
}
|
||
|
}
|