diff --git a/server/http.go b/server/http.go index ae5be5ce..69755620 100644 --- a/server/http.go +++ b/server/http.go @@ -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) diff --git a/server/http_test.go b/server/http_test.go index e33bfe20..9f08bdc1 100644 --- a/server/http_test.go +++ b/server/http_test.go @@ -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