Made zone return errors and http idempotent

The Zone DB tells you if you have a duplicate or missing record, but
the HTTP server smoothes these over.
This commit is contained in:
Michael Bridgen committed 2014-10-21 17:05:03 +01:00
1 parent 5c6410a3c6
commit 0615d122e9
2 files changed
+34 -5

No files matched your search

+12 -3
View File
@@ -48,7 +48,13 @@ func ListenHttp(domain string, db Zone, port int) {
Info.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr)
err = db.AddRecord(identifier, name, ip, weave_ip, subnet)
if err != nil {
http.Error(w, err.Error(), http.StatusConflict)
dup, ok := err.(DuplicateError)
if !ok {
Error.Print("Unexpected error from DB", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
} else if dup.Ident != identifier {
http.Error(w, err.Error(), http.StatusConflict)
}
return
}
} else {
@@ -70,8 +76,11 @@ func ListenHttp(domain string, db Zone, port int) {
Info.Printf("Deleting %s (%s)", identifier, weave_ipstr)
err = db.DeleteRecord(identifier, weave_ip)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
if _, ok := err.(LookupError); !ok {
Error.Print("Unexpected error from DB", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
}
default:
Warning.Println("Unexpected http method", r.Method)
+22 -2
View File
@@ -41,7 +41,7 @@ func TestHttp(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
t.Fatal("Unexpected http response", resp.Status)
}
@@ -55,12 +55,23 @@ func TestHttp(t *testing.T) {
t.Fatal("Unexpected result for", success_test_name, ip)
}
// Now try adding the same address again - should fail
// Adding exactly the same address should be OK
resp, err = genForm("PUT", addr_url,
url.Values{"fqdn": {success_test_name}, "local_ip": {docker_ip}, "routing_prefix": {addr_parts[1]}})
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatal("Expected duplicate add to succeed with 200 OK")
}
// Now try adding the same address again with a different ident - should fail
other_url := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, "other", addr_parts[0])
resp, err = genForm("PUT", other_url,
url.Values{"fqdn": {success_test_name}, "local_ip": {docker_ip}, "routing_prefix": {addr_parts[1]}})
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusConflict {
t.Fatal("Unexpected http response", resp.Status)
}
@@ -80,6 +91,15 @@ func TestHttp(t *testing.T) {
t.Fatal(err)
}
// Delete the address again, it should accept this
resp, err = genForm("DELETE", addr_url, nil)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatal("Unexpected http response", resp.Status)
}
// Would like to shut down the http server at the end of this test
// but it's complicated.
// See https://groups.google.com/forum/#!topic/golang-nuts/vLHWa5sHnCE