2020-11-18 00:55:31 +01:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-06-17 02:09:25 +02:00
|
|
|
"time"
|
2020-12-29 23:31:43 +01:00
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/pattern"
|
2020-11-18 00:55:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntegrationQuery(t *testing.T) {
|
2020-11-18 18:51:22 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
inputDNS DNS
|
|
|
|
inputURL string
|
|
|
|
expectedDNSCode string
|
|
|
|
expectedBody string
|
2020-11-18 23:07:38 +01:00
|
|
|
isErrExpected bool
|
2020-11-18 18:51:22 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test DNS with type A",
|
|
|
|
inputDNS: DNS{
|
|
|
|
QueryType: "A",
|
2020-11-18 23:07:38 +01:00
|
|
|
QueryName: "example.com.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
2022-11-16 03:39:31 +01:00
|
|
|
inputURL: "8.8.8.8",
|
2020-11-18 18:51:22 +01:00
|
|
|
expectedDNSCode: "NOERROR",
|
2024-04-19 03:04:34 +02:00
|
|
|
expectedBody: "93.184.215.14",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test DNS with type AAAA",
|
|
|
|
inputDNS: DNS{
|
|
|
|
QueryType: "AAAA",
|
2020-11-18 23:07:38 +01:00
|
|
|
QueryName: "example.com.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
2022-11-16 03:39:31 +01:00
|
|
|
inputURL: "8.8.8.8",
|
2020-11-18 18:51:22 +01:00
|
|
|
expectedDNSCode: "NOERROR",
|
2024-04-19 03:04:34 +02:00
|
|
|
expectedBody: "2606:2800:21f:cb07:6820:80da:af6b:8b2c",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test DNS with type CNAME",
|
|
|
|
inputDNS: DNS{
|
|
|
|
QueryType: "CNAME",
|
2022-06-17 01:12:32 +02:00
|
|
|
QueryName: "en.wikipedia.org.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
2022-11-16 03:39:31 +01:00
|
|
|
inputURL: "8.8.8.8",
|
2020-11-18 18:51:22 +01:00
|
|
|
expectedDNSCode: "NOERROR",
|
2022-06-17 01:12:32 +02:00
|
|
|
expectedBody: "dyna.wikimedia.org.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test DNS with type MX",
|
|
|
|
inputDNS: DNS{
|
|
|
|
QueryType: "MX",
|
2020-11-18 23:07:38 +01:00
|
|
|
QueryName: "example.com.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
2022-11-16 03:39:31 +01:00
|
|
|
inputURL: "8.8.8.8",
|
2020-11-18 18:51:22 +01:00
|
|
|
expectedDNSCode: "NOERROR",
|
|
|
|
expectedBody: ".",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test DNS with type NS",
|
|
|
|
inputDNS: DNS{
|
|
|
|
QueryType: "NS",
|
2020-11-18 23:07:38 +01:00
|
|
|
QueryName: "example.com.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
2022-11-16 03:39:31 +01:00
|
|
|
inputURL: "8.8.8.8",
|
2020-11-18 18:51:22 +01:00
|
|
|
expectedDNSCode: "NOERROR",
|
2020-12-29 23:31:43 +01:00
|
|
|
expectedBody: "*.iana-servers.net.",
|
2020-11-18 18:51:22 +01:00
|
|
|
},
|
2020-11-18 23:07:38 +01:00
|
|
|
{
|
|
|
|
name: "test DNS with fake type and retrieve error",
|
|
|
|
inputDNS: DNS{
|
|
|
|
QueryType: "B",
|
2022-06-17 01:12:32 +02:00
|
|
|
QueryName: "example",
|
2020-11-18 23:07:38 +01:00
|
|
|
},
|
2022-11-16 03:39:31 +01:00
|
|
|
inputURL: "8.8.8.8",
|
2020-11-18 23:07:38 +01:00
|
|
|
isErrExpected: true,
|
|
|
|
},
|
2020-11-18 00:55:31 +01:00
|
|
|
}
|
2020-11-18 18:51:22 +01:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
dns := test.inputDNS
|
|
|
|
result := &Result{}
|
|
|
|
dns.query(test.inputURL, result)
|
2020-11-18 23:07:38 +01:00
|
|
|
if test.isErrExpected && len(result.Errors) == 0 {
|
|
|
|
t.Errorf("there should be errors")
|
2020-11-18 18:51:22 +01:00
|
|
|
}
|
|
|
|
if result.DNSRCode != test.expectedDNSCode {
|
2022-06-17 01:12:32 +02:00
|
|
|
t.Errorf("expected DNSRCode to be %s, got %s", test.expectedDNSCode, result.DNSRCode)
|
2020-11-18 18:51:22 +01:00
|
|
|
}
|
2020-12-29 23:31:43 +01:00
|
|
|
if test.inputDNS.QueryType == "NS" {
|
|
|
|
// Because there are often multiple nameservers backing a single domain, we'll only look at the suffix
|
2023-03-15 01:02:31 +01:00
|
|
|
if !pattern.Match(test.expectedBody, string(result.Body)) {
|
|
|
|
t.Errorf("got %s, expected result %s,", string(result.Body), test.expectedBody)
|
2020-12-29 23:31:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-15 01:02:31 +01:00
|
|
|
if string(result.Body) != test.expectedBody {
|
|
|
|
t.Errorf("got %s, expected result %s,", string(result.Body), test.expectedBody)
|
2020-12-29 23:31:43 +01:00
|
|
|
}
|
2020-11-18 18:51:22 +01:00
|
|
|
}
|
|
|
|
})
|
2022-06-17 02:09:25 +02:00
|
|
|
time.Sleep(5 * time.Millisecond)
|
2020-11-18 00:55:31 +01:00
|
|
|
}
|
2020-11-18 18:51:22 +01:00
|
|
|
}
|
2020-11-18 00:55:31 +01:00
|
|
|
|
2024-04-09 03:00:40 +02:00
|
|
|
func TestDNS_validateAndSetDefault(t *testing.T) {
|
2020-11-18 18:51:22 +01:00
|
|
|
dns := &DNS{
|
|
|
|
QueryType: "A",
|
|
|
|
QueryName: "",
|
2020-11-18 00:55:31 +01:00
|
|
|
}
|
2021-05-19 05:27:43 +02:00
|
|
|
err := dns.validateAndSetDefault()
|
|
|
|
if err == nil {
|
2024-04-09 03:00:40 +02:00
|
|
|
t.Error("Should've returned an error because endpoint's dns didn't have a query name, which is a mandatory field for dns")
|
2021-05-19 05:27:43 +02:00
|
|
|
}
|
2020-11-18 18:51:22 +01:00
|
|
|
}
|
2020-11-18 00:55:31 +01:00
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
func TestEndpoint_ValidateAndSetDefaultsWithInvalidDNSQueryType(t *testing.T) {
|
2020-11-18 18:51:22 +01:00
|
|
|
dns := &DNS{
|
|
|
|
QueryType: "B",
|
|
|
|
QueryName: "example.com",
|
2020-11-18 00:55:31 +01:00
|
|
|
}
|
2021-05-19 05:27:43 +02:00
|
|
|
err := dns.validateAndSetDefault()
|
|
|
|
if err == nil {
|
2024-04-09 03:00:40 +02:00
|
|
|
t.Error("Should've returned an error because endpoint's dns query type is invalid, it needs to be a valid query name like A, AAAA, CNAME...")
|
2021-05-19 05:27:43 +02:00
|
|
|
}
|
2020-11-18 00:55:31 +01:00
|
|
|
}
|