Skip to content

Commit 44ed807

Browse files
author
wafuwafu13
committed
to be testable
1 parent e1c8559 commit 44ed807

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
pull_request:
1111
env:
1212
DEBIAN_FRONTEND: noninteractive
13+
RUN_TEST_ON_GITHUB_ACTIONS: 1
1314
jobs:
1415
lint:
1516
strategy:

check-dns/lib/check_dns.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ type dnsOpts struct {
1515
Host string `short:"H" long:"host" required:"true" description:"The name or address you want to query"`
1616
Server string `short:"s" long:"server" description:"DNS server you want to use for the lookup"`
1717
Port int `short:"p" long:"port" default:"53" description:"Port number you want to use"`
18+
Norec bool `long:"norec" description:"Set not recursive mode"`
1819
}
1920

2021
// Do the plugin
2122
func Do() {
22-
Run(os.Args[1:])
23+
opts, err := parseArgs(os.Args[1:])
24+
if err != nil {
25+
os.Exit(1)
26+
}
27+
ckr := opts.run()
28+
ckr.Name = "DNS"
29+
ckr.Exit()
2330
}
2431

2532
func parseArgs(args []string) (*dnsOpts, error) {
@@ -28,12 +35,9 @@ func parseArgs(args []string) (*dnsOpts, error) {
2835
return opts, err
2936
}
3037

31-
func Run(args []string) *checkers.Checker {
32-
opts, err := parseArgs(args)
33-
if err != nil {
34-
os.Exit(1)
35-
}
38+
func (opts *dnsOpts) run() *checkers.Checker {
3639
var nameserver string
40+
var err error
3741
if opts.Server != "" {
3842
nameserver = opts.Server
3943
} else {
@@ -47,10 +51,12 @@ func Run(args []string) *checkers.Checker {
4751
c := new(dns.Client)
4852
m := &dns.Msg{
4953
MsgHdr: dns.MsgHdr{
50-
Opcode: dns.OpcodeQuery,
54+
RecursionDesired: !opts.Norec,
55+
Opcode: dns.OpcodeQuery,
5156
},
5257
Question: []dns.Question{{Name: dns.Fqdn(opts.Host), Qtype: dns.TypeA, Qclass: uint16(dns.ClassINET)}},
5358
}
59+
m.Id = dns.Id()
5460

5561
r, _, err := c.Exchange(m, nameserver)
5662
if err != nil {

check-dns/lib/check_dns_test.go

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package checkdns
22

33
import (
44
"net"
5+
"os"
56
"strings"
67
"testing"
78

@@ -21,11 +22,63 @@ func TestNameServer(t *testing.T) {
2122
}
2223
}
2324

24-
func TestDnsStatus(t *testing.T) {
25-
ckr := Run([]string{"-H", "example.com", "-s", "8.8.8.8"})
26-
assert.Equal(t, checkers.OK, ckr.Status, "should be OK")
27-
t.Logf(ckr.Message)
28-
if !strings.Contains(ckr.Message, "status: NOERROR") {
29-
t.Errorf("status is not NOERROR")
25+
func TestCheckDns(t *testing.T) {
26+
tests := []struct {
27+
args []string
28+
want_status checkers.Status
29+
want_msg string
30+
local_only bool
31+
}{
32+
{
33+
[]string{"-H", "example.com"},
34+
checkers.OK,
35+
"status: NOERROR",
36+
true,
37+
},
38+
{
39+
[]string{"-H", "example.com", "--norec"},
40+
checkers.OK,
41+
"status: NOERROR",
42+
true,
43+
},
44+
{
45+
[]string{"-H", "exampleeeee.com"},
46+
checkers.CRITICAL,
47+
"status: NXDOMAIN",
48+
true,
49+
},
50+
{
51+
[]string{"-H", "example.com", "-s", "8.8.8.8"},
52+
checkers.OK,
53+
"status: NOERROR",
54+
false,
55+
},
56+
{
57+
[]string{"-H", "exampleeeee.com", "-s", "8.8.8.8"},
58+
checkers.CRITICAL,
59+
"status: NXDOMAIN",
60+
false,
61+
},
62+
}
63+
64+
for i, tt := range tests {
65+
t.Logf("=== Start #%d", i)
66+
// when runs without setting server in CI, status will be REFUSED
67+
if tt.local_only && os.Getenv("RUN_TEST_ON_GITHUB_ACTIONS") == "1" {
68+
continue
69+
}
70+
opts, err := parseArgs(tt.args)
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
ckr := opts.run()
75+
76+
assert.Equal(t, tt.want_status, ckr.Status)
77+
78+
if tt.want_msg != "" {
79+
if !strings.Contains(ckr.Message, tt.want_msg) {
80+
t.Errorf("%s is not incleded in message", tt.want_msg)
81+
}
82+
}
3083
}
3184
}

0 commit comments

Comments
 (0)