2019-04-04 08:05:35 -07:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2019-04-04 08:05:35 -07:00
|
|
|
"github.com/miekg/dns"
|
2020-10-22 02:00:08 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-27 01:38:08 -07:00
|
|
|
"go.uber.org/goleak"
|
2019-09-23 04:26:12 -07:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-04-04 08:05:35 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
)
|
|
|
|
|
2020-07-27 01:38:08 -07:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2019-04-04 08:05:35 -07:00
|
|
|
func TestDNS(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config SDConfig
|
|
|
|
lookup func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error)
|
|
|
|
|
|
|
|
expected []*targetgroup.Group
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "A record query with error",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"web.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
Port: 80,
|
|
|
|
Type: "A",
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return nil, fmt.Errorf("some error")
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "A record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"web.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
Port: 80,
|
|
|
|
Type: "A",
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.A{A: net.IPv4(192, 0, 2, 2)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 01:34:14 -07:00
|
|
|
{
|
2019-04-04 08:05:35 -07:00
|
|
|
Source: "web.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 13:09:01 -07:00
|
|
|
{
|
|
|
|
"__address__": "192.0.2.2:80",
|
|
|
|
"__meta_dns_name": "web.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
2022-08-03 02:19:26 -07:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2020-07-28 13:09:01 -07:00
|
|
|
},
|
2019-04-04 08:05:35 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AAAA record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"web.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
Port: 80,
|
|
|
|
Type: "AAAA",
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.AAAA{AAAA: net.IPv6loopback},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 01:34:14 -07:00
|
|
|
{
|
2019-04-04 08:05:35 -07:00
|
|
|
Source: "web.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 13:09:01 -07:00
|
|
|
{
|
|
|
|
"__address__": "[::1]:80",
|
|
|
|
"__meta_dns_name": "web.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
2022-08-03 02:19:26 -07:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2020-07-28 13:09:01 -07:00
|
|
|
},
|
2019-04-04 08:05:35 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SRV record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"_mysql._tcp.db.example.com."},
|
2019-09-23 04:26:12 -07:00
|
|
|
Type: "SRV",
|
2019-04-04 08:05:35 -07:00
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.SRV{Port: 3306, Target: "db1.example.com."},
|
|
|
|
&dns.SRV{Port: 3306, Target: "db2.example.com."},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 01:34:14 -07:00
|
|
|
{
|
2019-04-04 08:05:35 -07:00
|
|
|
Source: "_mysql._tcp.db.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 13:09:01 -07:00
|
|
|
{
|
|
|
|
"__address__": "db1.example.com:3306",
|
|
|
|
"__meta_dns_name": "_mysql._tcp.db.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "db1.example.com.",
|
|
|
|
"__meta_dns_srv_record_port": "3306",
|
2022-08-03 02:19:26 -07:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2020-07-28 13:09:01 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": "db2.example.com:3306",
|
|
|
|
"__meta_dns_name": "_mysql._tcp.db.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "db2.example.com.",
|
|
|
|
"__meta_dns_srv_record_port": "3306",
|
2022-08-03 02:19:26 -07:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2020-07-28 13:09:01 -07:00
|
|
|
},
|
2019-04-04 08:05:35 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SRV record query with unsupported resource records",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"_mysql._tcp.db.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.SRV{Port: 3306, Target: "db1.example.com."},
|
|
|
|
&dns.TXT{Txt: []string{"this should be discarded"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 01:34:14 -07:00
|
|
|
{
|
2019-04-04 08:05:35 -07:00
|
|
|
Source: "_mysql._tcp.db.example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
2020-07-28 13:09:01 -07:00
|
|
|
{
|
|
|
|
"__address__": "db1.example.com:3306",
|
|
|
|
"__meta_dns_name": "_mysql._tcp.db.example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "db1.example.com.",
|
|
|
|
"__meta_dns_srv_record_port": "3306",
|
2022-08-03 02:19:26 -07:00
|
|
|
"__meta_dns_mx_record_target": "",
|
2020-07-28 13:09:01 -07:00
|
|
|
},
|
2019-04-04 08:05:35 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SRV record query with empty answer (NXDOMAIN)",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"_mysql._tcp.db.example.com."},
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return &dns.Msg{}, nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
2019-08-13 01:34:14 -07:00
|
|
|
{
|
2019-04-04 08:05:35 -07:00
|
|
|
Source: "_mysql._tcp.db.example.com.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-03 02:19:26 -07:00
|
|
|
{
|
|
|
|
name: "MX record query",
|
|
|
|
config: SDConfig{
|
|
|
|
Names: []string{"example.com."},
|
|
|
|
Type: "MX",
|
|
|
|
Port: 25,
|
|
|
|
RefreshInterval: model.Duration(time.Minute),
|
|
|
|
},
|
|
|
|
lookup: func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error) {
|
|
|
|
return &dns.Msg{
|
|
|
|
Answer: []dns.RR{
|
|
|
|
&dns.MX{Preference: 0, Mx: "smtp1.example.com."},
|
|
|
|
&dns.MX{Preference: 10, Mx: "smtp2.example.com."},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil
|
|
|
|
},
|
|
|
|
expected: []*targetgroup.Group{
|
|
|
|
{
|
|
|
|
Source: "example.com.",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": "smtp1.example.com:25",
|
|
|
|
"__meta_dns_name": "example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
|
|
|
"__meta_dns_mx_record_target": "smtp1.example.com.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": "smtp2.example.com:25",
|
|
|
|
"__meta_dns_name": "example.com.",
|
|
|
|
"__meta_dns_srv_record_target": "",
|
|
|
|
"__meta_dns_srv_record_port": "",
|
|
|
|
"__meta_dns_mx_record_target": "smtp2.example.com.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-04 08:05:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
sd := NewDiscovery(tc.config, nil)
|
|
|
|
sd.lookupFn = tc.lookup
|
|
|
|
|
|
|
|
tgs, err := sd.refresh(context.Background())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, tgs)
|
2019-04-04 08:05:35 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 04:26:12 -07:00
|
|
|
|
|
|
|
func TestSDConfigUnmarshalYAML(t *testing.T) {
|
|
|
|
marshal := func(c SDConfig) []byte {
|
|
|
|
d, err := yaml.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshal := func(d []byte) func(interface{}) error {
|
|
|
|
return func(o interface{}) error {
|
|
|
|
return yaml.Unmarshal(d, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
input SDConfig
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid srv",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "SRV",
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid a",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "A",
|
|
|
|
Port: 5300,
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid aaaa",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "AAAA",
|
|
|
|
Port: 5300,
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid a without port",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "A",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid aaaa without port",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "AAAA",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid empty names",
|
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{},
|
|
|
|
Type: "AAAA",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2019-09-25 00:51:43 -07:00
|
|
|
name: "invalid unknown dns type",
|
2019-09-23 04:26:12 -07:00
|
|
|
input: SDConfig{
|
|
|
|
Names: []string{"a.example.com", "b.example.com"},
|
|
|
|
Type: "PTR",
|
|
|
|
},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
var config SDConfig
|
|
|
|
d := marshal(c.input)
|
|
|
|
err := config.UnmarshalYAML(unmarshal(d))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, c.expectErr, err != nil)
|
2019-09-23 04:26:12 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|