2016-02-21 17:43:24 +00:00
|
|
|
package dns_query
|
2016-02-13 18:00:42 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-03 15:44:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-02-13 18:00:42 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-03-03 15:44:16 +00:00
|
|
|
|
2016-02-13 18:00:42 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-03-03 15:44:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-13 18:00:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var servers = []string{"8.8.8.8"}
|
2016-02-23 18:10:28 +00:00
|
|
|
var domains = []string{"google.com"}
|
2016-02-13 18:00:42 +00:00
|
|
|
|
|
|
|
func TestGathering(t *testing.T) {
|
2016-04-28 20:44:08 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping network-dependent test in short mode.")
|
|
|
|
}
|
2016-02-21 17:43:24 +00:00
|
|
|
var dnsConfig = DnsQuery{
|
2016-02-13 18:00:42 +00:00
|
|
|
Servers: servers,
|
|
|
|
Domains: domains,
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2016-02-23 18:10:28 +00:00
|
|
|
err := dnsConfig.Gather(&acc)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
metric, ok := acc.Get("dns_query")
|
2016-03-03 15:44:16 +00:00
|
|
|
require.True(t, ok)
|
2016-02-15 15:10:04 +00:00
|
|
|
queryTime, _ := metric.Fields["query_time_ms"].(float64)
|
2016-02-13 18:00:42 +00:00
|
|
|
|
|
|
|
assert.NotEqual(t, 0, queryTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatheringMxRecord(t *testing.T) {
|
2016-04-28 20:44:08 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping network-dependent test in short mode.")
|
|
|
|
}
|
2016-02-21 17:43:24 +00:00
|
|
|
var dnsConfig = DnsQuery{
|
2016-02-13 18:00:42 +00:00
|
|
|
Servers: servers,
|
|
|
|
Domains: domains,
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
dnsConfig.RecordType = "MX"
|
|
|
|
|
2016-02-23 18:10:28 +00:00
|
|
|
err := dnsConfig.Gather(&acc)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
metric, ok := acc.Get("dns_query")
|
2016-03-03 15:44:16 +00:00
|
|
|
require.True(t, ok)
|
2016-02-15 15:10:04 +00:00
|
|
|
queryTime, _ := metric.Fields["query_time_ms"].(float64)
|
2016-02-13 18:00:42 +00:00
|
|
|
|
|
|
|
assert.NotEqual(t, 0, queryTime)
|
|
|
|
}
|
|
|
|
|
2016-02-21 17:43:24 +00:00
|
|
|
func TestGatheringRootDomain(t *testing.T) {
|
2016-04-28 20:44:08 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping network-dependent test in short mode.")
|
|
|
|
}
|
2016-02-21 17:43:24 +00:00
|
|
|
var dnsConfig = DnsQuery{
|
|
|
|
Servers: servers,
|
|
|
|
Domains: []string{"."},
|
|
|
|
RecordType: "MX",
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
tags := map[string]string{
|
|
|
|
"server": "8.8.8.8",
|
|
|
|
"domain": ".",
|
|
|
|
"record_type": "MX",
|
|
|
|
}
|
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
2016-02-23 18:10:28 +00:00
|
|
|
err := dnsConfig.Gather(&acc)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
metric, ok := acc.Get("dns_query")
|
2016-03-03 15:44:16 +00:00
|
|
|
require.True(t, ok)
|
2016-02-21 17:43:24 +00:00
|
|
|
queryTime, _ := metric.Fields["query_time_ms"].(float64)
|
|
|
|
|
|
|
|
fields["query_time_ms"] = queryTime
|
|
|
|
acc.AssertContainsTaggedFields(t, "dns_query", fields, tags)
|
|
|
|
}
|
|
|
|
|
2016-02-13 18:00:42 +00:00
|
|
|
func TestMetricContainsServerAndDomainAndRecordTypeTags(t *testing.T) {
|
2016-04-28 20:44:08 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping network-dependent test in short mode.")
|
|
|
|
}
|
2016-02-21 17:43:24 +00:00
|
|
|
var dnsConfig = DnsQuery{
|
2016-02-13 18:00:42 +00:00
|
|
|
Servers: servers,
|
|
|
|
Domains: domains,
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
tags := map[string]string{
|
2016-02-15 15:10:04 +00:00
|
|
|
"server": "8.8.8.8",
|
2016-02-23 18:10:28 +00:00
|
|
|
"domain": "google.com",
|
2016-02-21 17:43:24 +00:00
|
|
|
"record_type": "NS",
|
2016-02-13 18:00:42 +00:00
|
|
|
}
|
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
2016-02-23 18:10:28 +00:00
|
|
|
err := dnsConfig.Gather(&acc)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
metric, ok := acc.Get("dns_query")
|
2016-03-03 15:44:16 +00:00
|
|
|
require.True(t, ok)
|
2016-02-15 15:10:04 +00:00
|
|
|
queryTime, _ := metric.Fields["query_time_ms"].(float64)
|
2016-02-13 18:00:42 +00:00
|
|
|
|
2016-02-15 15:10:04 +00:00
|
|
|
fields["query_time_ms"] = queryTime
|
2016-02-21 17:43:24 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "dns_query", fields, tags)
|
2016-02-13 18:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatheringTimeout(t *testing.T) {
|
2016-04-28 20:44:08 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping network-dependent test in short mode.")
|
|
|
|
}
|
2016-02-21 17:43:24 +00:00
|
|
|
var dnsConfig = DnsQuery{
|
2016-02-13 18:00:42 +00:00
|
|
|
Servers: servers,
|
|
|
|
Domains: domains,
|
|
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
dnsConfig.Port = 60054
|
|
|
|
dnsConfig.Timeout = 1
|
|
|
|
var err error
|
|
|
|
|
|
|
|
channel := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
channel <- dnsConfig.Gather(&acc)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case res := <-channel:
|
|
|
|
err = res
|
|
|
|
case <-time.After(time.Second * 2):
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "i/o timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSettingDefaultValues(t *testing.T) {
|
2016-02-21 17:43:24 +00:00
|
|
|
dnsConfig := DnsQuery{}
|
2016-02-13 18:00:42 +00:00
|
|
|
|
|
|
|
dnsConfig.setDefaultValues()
|
|
|
|
|
2016-02-21 17:43:24 +00:00
|
|
|
assert.Equal(t, []string{"."}, dnsConfig.Domains, "Default domain not equal \".\"")
|
|
|
|
assert.Equal(t, "NS", dnsConfig.RecordType, "Default record type not equal 'NS'")
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Equal(t, 53, dnsConfig.Port, "Default port number not equal 53")
|
|
|
|
assert.Equal(t, 2, dnsConfig.Timeout, "Default timeout not equal 2")
|
2016-02-21 17:43:24 +00:00
|
|
|
|
|
|
|
dnsConfig = DnsQuery{Domains: []string{"."}}
|
|
|
|
|
|
|
|
dnsConfig.setDefaultValues()
|
|
|
|
|
|
|
|
assert.Equal(t, "NS", dnsConfig.RecordType, "Default record type not equal 'NS'")
|
2016-02-13 18:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordTypeParser(t *testing.T) {
|
2016-02-21 17:43:24 +00:00
|
|
|
var dnsConfig = DnsQuery{}
|
2016-02-13 18:00:42 +00:00
|
|
|
var recordType uint16
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "A"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Equal(t, dns.TypeA, recordType)
|
|
|
|
|
2016-02-15 15:10:04 +00:00
|
|
|
dnsConfig.RecordType = "AAAA"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-15 15:10:04 +00:00
|
|
|
assert.Equal(t, dns.TypeAAAA, recordType)
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "ANY"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-15 15:10:04 +00:00
|
|
|
assert.Equal(t, dns.TypeANY, recordType)
|
|
|
|
|
2016-02-13 18:00:42 +00:00
|
|
|
dnsConfig.RecordType = "CNAME"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Equal(t, dns.TypeCNAME, recordType)
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "MX"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Equal(t, dns.TypeMX, recordType)
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "NS"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Equal(t, dns.TypeNS, recordType)
|
|
|
|
|
2016-02-21 17:43:24 +00:00
|
|
|
dnsConfig.RecordType = "PTR"
|
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
|
|
|
assert.Equal(t, dns.TypePTR, recordType)
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "SOA"
|
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
|
|
|
assert.Equal(t, dns.TypeSOA, recordType)
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "SPF"
|
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
|
|
|
assert.Equal(t, dns.TypeSPF, recordType)
|
|
|
|
|
|
|
|
dnsConfig.RecordType = "SRV"
|
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
|
|
|
assert.Equal(t, dns.TypeSRV, recordType)
|
|
|
|
|
2016-02-13 18:00:42 +00:00
|
|
|
dnsConfig.RecordType = "TXT"
|
2016-02-21 17:43:24 +00:00
|
|
|
recordType, _ = dnsConfig.parseRecordType()
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Equal(t, dns.TypeTXT, recordType)
|
2016-02-21 17:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordTypeParserError(t *testing.T) {
|
|
|
|
var dnsConfig = DnsQuery{}
|
|
|
|
var err error
|
2016-02-13 18:00:42 +00:00
|
|
|
|
|
|
|
dnsConfig.RecordType = "nil"
|
2016-02-21 17:43:24 +00:00
|
|
|
_, err = dnsConfig.parseRecordType()
|
2016-02-13 18:00:42 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|