Merge pull request #1898 from hashicorp/b-dns-single-record

Fix DNS tests
This commit is contained in:
Sean Chittenden 2016-03-30 12:40:01 -07:00
commit e7fba3b31c
3 changed files with 52 additions and 24 deletions

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -2123,16 +2124,19 @@ func testDNS_ServiceLookup_responseLimits(t *testing.T, answerLimit int, qType u
switch idx { switch idx {
case 0: case 0:
if len(in.Answer) != expectedService { if (expectedService > 0 && len(in.Answer) != expectedService) ||
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, question) (expectedService < -1 && len(in.Answer) < lib.AbsInt(expectedService)) {
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, qType, question)
} }
case 1: case 1:
if len(in.Answer) != expectedQuery { if (expectedQuery > 0 && len(in.Answer) != expectedQuery) ||
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, question) (expectedQuery < -1 && len(in.Answer) < lib.AbsInt(expectedQuery)) {
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, qType, question)
} }
case 2: case 2:
if len(in.Answer) != expectedQueryID { if (expectedQueryID > 0 && len(in.Answer) != expectedQueryID) ||
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, question) (expectedQueryID < -1 && len(in.Answer) < lib.AbsInt(expectedQueryID)) {
return false, fmt.Errorf("%d/%d answers received for type %v for %s", len(in.Answer), answerLimit, qType, question)
} }
default: default:
panic("abort") panic("abort")
@ -2144,7 +2148,15 @@ func testDNS_ServiceLookup_responseLimits(t *testing.T, answerLimit int, qType u
func TestDNS_ServiceLookup_AnswerLimits(t *testing.T) { func TestDNS_ServiceLookup_AnswerLimits(t *testing.T) {
// Build a matrix of config parameters (udpAnswerLimit), and the // Build a matrix of config parameters (udpAnswerLimit), and the
// length of the response per query type and question. // length of the response per query type and question. Negative
// values imply the test must return at least the abs(value) number
// of records in the answer section. This is required because, for
// example, on OS-X and Linux, the number of answers returned in a
// 512B response is different even though both platforms are x86_64
// and using the same version of Go.
//
// TODO(sean@): Why is it not identical everywhere when using the
// same compiler?
tests := []struct { tests := []struct {
name string name string
udpAnswerLimit int udpAnswerLimit int
@ -2164,27 +2176,27 @@ func TestDNS_ServiceLookup_AnswerLimits(t *testing.T) {
{"3", 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {"3", 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{"4", 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, {"4", 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
{"5", 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, {"5", 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{"6", 6, 6, 6, 6, 6, 6, 5, 6, 6, 6}, {"6", 6, 6, 6, 6, 6, 6, 5, 6, 6, -5},
{"7", 7, 7, 7, 6, 7, 7, 5, 7, 7, 6}, {"7", 7, 7, 7, 6, 7, 7, 5, 7, 7, -5},
{"8", 8, 8, 8, 6, 8, 8, 5, 8, 8, 6}, {"8", 8, 8, 8, 6, 8, 8, 5, 8, 8, -5},
{"9", 9, 8, 8, 6, 8, 8, 5, 8, 8, 6}, {"9", 9, 8, 8, 6, 8, 8, 5, 8, 8, -5},
{"20", 20, 8, 8, 6, 8, 8, 5, 8, 8, 6}, {"20", 20, 8, 8, 6, 8, 8, 5, 8, -5, -5},
{"30", 30, 8, 8, 6, 8, 8, 5, 8, 8, 6}, {"30", 30, 8, 8, 6, 8, 8, 5, 8, -5, -5},
} }
for _, test := range tests { for _, test := range tests {
ok, err := testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeA, test.expectedAService, test.expectedAQuery, test.expectedAQueryID) ok, err := testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeA, test.expectedAService, test.expectedAQuery, test.expectedAQueryID)
if !ok { if !ok {
t.Errorf("Expected service A lookup %d to pass: %v", test.name, err) t.Errorf("Expected service A lookup %s to pass: %v", test.name, err)
} }
ok, err = testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeAAAA, test.expectedAAAAService, test.expectedAAAAQuery, test.expectedAAAAQueryID) ok, err = testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeAAAA, test.expectedAAAAService, test.expectedAAAAQuery, test.expectedAAAAQueryID)
if !ok { if !ok {
t.Errorf("Expected service AAAA lookup %d to pass: %v", test.name, err) t.Errorf("Expected service AAAA lookup %s to pass: %v", test.name, err)
} }
ok, err = testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeANY, test.expectedANYService, test.expectedANYQuery, test.expectedANYQueryID) ok, err = testDNS_ServiceLookup_responseLimits(t, test.udpAnswerLimit, dns.TypeANY, test.expectedANYService, test.expectedANYQuery, test.expectedANYQueryID)
if !ok { if !ok {
t.Errorf("Expected service ANY lookup %d to %v: %v", test.name, err) t.Errorf("Expected service ANY lookup %s to pass: %v", test.name, err)
} }
} }
} }

View File

@ -1,5 +1,12 @@
package lib package lib
func AbsInt(a int) int {
if a > 0 {
return a
}
return a * -1
}
func MaxInt(a, b int) int { func MaxInt(a, b int) int {
if a > b { if a > b {
return a return a

View File

@ -6,24 +6,33 @@ import (
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
) )
func TestMathAbsInt(t *testing.T) {
tests := [][3]int{{1, 1}, {-1, 1}, {0, 0}}
for _, test := range tests {
if test[1] != lib.AbsInt(test[0]) {
t.Fatalf("expected %d, got %d", test[1], test[0])
}
}
}
func TestMathMaxInt(t *testing.T) { func TestMathMaxInt(t *testing.T) {
tests := [][3]int{{1, 2, 2}, {-1, 1, 1}, {2, 0, 2}} tests := [][3]int{{1, 2, 2}, {-1, 1, 1}, {2, 0, 2}}
for i, _ := range tests { for _, test := range tests {
expected := tests[i][2] expected := test[2]
actual := lib.MaxInt(tests[i][0], tests[i][1]) actual := lib.MaxInt(test[0], test[1])
if expected != actual { if expected != actual {
t.Fatalf("in iteration %d expected %d, got %d", i, expected, actual) t.Fatalf("expected %d, got %d", expected, actual)
} }
} }
} }
func TestMathMinInt(t *testing.T) { func TestMathMinInt(t *testing.T) {
tests := [][3]int{{1, 2, 1}, {-1, 1, -1}, {2, 0, 0}} tests := [][3]int{{1, 2, 1}, {-1, 1, -1}, {2, 0, 0}}
for i, _ := range tests { for _, test := range tests {
expected := tests[i][2] expected := test[2]
actual := lib.MinInt(tests[i][0], tests[i][1]) actual := lib.MinInt(test[0], test[1])
if expected != actual { if expected != actual {
t.Fatalf("in iteration %d expected %d, got %d", i, expected, actual) t.Fatalf("expected %d, got %d", expected, actual)
} }
} }
} }