|
| 1 | +package checkdns |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/mackerelio/checkers" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNameServer(t *testing.T) { |
| 14 | + nameserver, err := adapterAddress() |
| 15 | + if err != nil { |
| 16 | + t.Errorf(err.Error()) |
| 17 | + } |
| 18 | + t.Logf(nameserver) |
| 19 | + address := net.ParseIP(nameserver) |
| 20 | + if address == nil { |
| 21 | + t.Errorf("nameserver is invalid IP") |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestCheckDns(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + args []string |
| 28 | + want_status checkers.Status |
| 29 | + want_msg []string |
| 30 | + }{ |
| 31 | + { |
| 32 | + []string{"-H", "example.com"}, |
| 33 | + checkers.OK, |
| 34 | + []string{"status: NOERROR"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + []string{"-H", "example.com", "--norec"}, |
| 38 | + checkers.OK, |
| 39 | + []string{"status: NOERROR"}, |
| 40 | + }, |
| 41 | + { |
| 42 | + []string{"-H", "exampleeeee.com"}, |
| 43 | + checkers.CRITICAL, |
| 44 | + []string{"status: NXDOMAIN"}, |
| 45 | + }, |
| 46 | + { |
| 47 | + []string{"-H", "example.com", "-s", "8.8.8.8"}, |
| 48 | + checkers.OK, |
| 49 | + []string{"status: NOERROR"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + []string{"-H", "exampleeeee.com", "-s", "8.8.8.8"}, |
| 53 | + checkers.CRITICAL, |
| 54 | + []string{"status: NXDOMAIN"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + []string{"-H", "example.com", "-s", "8.8.8"}, |
| 58 | + checkers.CRITICAL, |
| 59 | + []string{""}, |
| 60 | + }, |
| 61 | + { |
| 62 | + []string{"-H", "example.com", "-s", "8.8.8.8", "-q", "AAAA"}, |
| 63 | + checkers.OK, |
| 64 | + []string{"status: NOERROR", "AAAA"}, |
| 65 | + }, |
| 66 | + { |
| 67 | + []string{"-H", "example.com", "-s", "8.8.8.8", "-q", "AAA"}, |
| 68 | + checkers.CRITICAL, |
| 69 | + []string{"AAA is invalid queryType"}, |
| 70 | + }, |
| 71 | + { |
| 72 | + []string{"-H", "example.com", "-s", "8.8.8.8", "-c", "IN"}, |
| 73 | + checkers.OK, |
| 74 | + []string{"status: NOERROR"}, |
| 75 | + }, |
| 76 | + { |
| 77 | + []string{"-H", "example.com", "-s", "8.8.8.8", "-c", "INN"}, |
| 78 | + checkers.CRITICAL, |
| 79 | + []string{"INN is invalid queryClass"}, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for i, tt := range tests { |
| 84 | + t.Logf("=== Start #%d", i) |
| 85 | + opts, err := parseArgs(tt.args) |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + // when runs without setting server in CI, status will be REFUSED |
| 90 | + if opts.Server == "" && os.Getenv("CI") == "true" { |
| 91 | + continue |
| 92 | + } |
| 93 | + ckr := opts.run() |
| 94 | + |
| 95 | + assert.Equal(t, tt.want_status, ckr.Status) |
| 96 | + |
| 97 | + for _, want := range tt.want_msg { |
| 98 | + if !strings.Contains(ckr.Message, want) { |
| 99 | + t.Errorf("%s is not incleded in message", want) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments