Factor out error type checks

This commit is contained in:
Bryan Boreham committed 2014-10-22 18:24:02 +01:00
1 parent 0a5990bd6e
commit 94a812d98f
3 files changed
+20 -15

No files matched your search

+1 -3
View File
@@ -69,9 +69,7 @@ func TestHttp(t *testing.T) {
// Check that the address is not there now. // Check that the address is not there now.
_, err = zone.MatchLocal(success_test_name) _, err = zone.MatchLocal(success_test_name)
if _, ok := err.(LookupError); !ok { assertErrorType(t, err, (*LookupError)(nil), "nonexistent lookup")
t.Fatal(err)
}
// Delete the address again, it should accept this // Delete the address again, it should accept this
resp, err = genForm("DELETE", addr_url, nil) resp, err = genForm("DELETE", addr_url, nil)
+15
View File
@@ -1,6 +1,7 @@
package weavedns package weavedns
import ( import (
"reflect"
"testing" "testing"
) )
@@ -15,3 +16,17 @@ func assertStatus(t *testing.T, got int, wanted int, desc string) {
t.Fatalf("Expected %s %d but got %d", desc, wanted, got) t.Fatalf("Expected %s %d but got %d", desc, wanted, got)
} }
} }
func assertErrorInterface(t *testing.T, got interface{}, wanted interface{}, desc string) {
got_t, wanted_t := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
if !got_t.Implements(wanted_t) {
t.Fatalf("Expected %s but got %s (%s)", wanted_t.String(), got_t.String(), desc)
}
}
func assertErrorType(t *testing.T, got interface{}, wanted interface{}, desc string) {
got_t, wanted_t := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
if got_t != wanted_t {
t.Fatalf("Expected %s but got %s (%s)", wanted_t.String(), got_t.String(), desc)
}
}
+4 -12
View File
@@ -30,9 +30,7 @@ func TestZone(t *testing.T) {
// Now try to add the same address again // Now try to add the same address again
err = zone.AddRecord(container_id, success_test_name, ip, weave_ip, subnet) err = zone.AddRecord(container_id, success_test_name, ip, weave_ip, subnet)
if _, ok := err.(DuplicateError); !ok { assertErrorType(t, err, (*DuplicateError)(nil), "duplicate add")
t.Fatal(err)
}
// Now delete the record // Now delete the record
err = zone.DeleteRecord(container_id, weave_ip) err = zone.DeleteRecord(container_id, weave_ip)
@@ -40,15 +38,11 @@ func TestZone(t *testing.T) {
// Check that the address is not there now. // Check that the address is not there now.
_, err = zone.MatchLocal(success_test_name) _, err = zone.MatchLocal(success_test_name)
if _, ok := err.(LookupError); !ok { assertErrorType(t, err, (*LookupError)(nil), "after deleting record")
t.Fatal("Unexpected result when deleting record", success_test_name, err)
}
// Delete a record that isn't there // Delete a record that isn't there
err = zone.DeleteRecord(container_id, net.ParseIP("0.0.0.0")) err = zone.DeleteRecord(container_id, net.ParseIP("0.0.0.0"))
if _, ok := err.(LookupError); !ok { assertErrorType(t, err, (*LookupError)(nil), "when deleting record that doesn't exist")
t.Fatal("Expected a LookupError when deleting a record that doesn't exist")
}
} }
func TestDeleteFor(t *testing.T) { func TestDeleteFor(t *testing.T) {
@@ -72,7 +66,5 @@ func TestDeleteFor(t *testing.T) {
err = zone.DeleteRecordsFor(id) err = zone.DeleteRecordsFor(id)
_, err = zone.MatchLocal(name) _, err = zone.MatchLocal(name)
if _, ok := err.(LookupError); !ok { assertErrorType(t, err, (*LookupError)(nil), "after deleting records for ident")
t.Fatal("Expected no results after deleting records for ident")
}
} }