From 1e910099789a9edb2a6f428aad04501c64e97719 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Fri, 15 May 2015 16:04:09 -0700 Subject: [PATCH] Network and Endpoint query methods to return error on not found - As requested by Docker committers Signed-off-by: Alessandro Boch --- libnetwork/api/api.go | 12 ++++++------ libnetwork/controller.go | 10 +++++++--- libnetwork/error.go | 4 ++++ libnetwork/libnetwork_test.go | 18 ++++++++++++------ libnetwork/network.go | 10 +++++++--- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/libnetwork/api/api.go b/libnetwork/api/api.go index 8785901b38..6e2b3fbd70 100644 --- a/libnetwork/api/api.go +++ b/libnetwork/api/api.go @@ -499,11 +499,11 @@ func findNetwork(c libnetwork.NetworkController, s string, by int) (libnetwork.N panic(fmt.Sprintf("unexpected selector for network search: %d", by)) } if err != nil { + if err == libnetwork.ErrNoSuchNetwork { + return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound} + } return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest} } - if nw == nil { - return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound} - } return nw, &successResponse } @@ -525,11 +525,11 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int) panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy)) } if err != nil { + if err == libnetwork.ErrNoSuchEndpoint { + return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound} + } return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest} } - if ep == nil { - return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound} - } return ep, &successResponse } diff --git a/libnetwork/controller.go b/libnetwork/controller.go index 0082b3405a..7f9ad28412 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -70,10 +70,10 @@ type NetworkController interface { // WalkNetworks uses the provided function to walk the Network(s) managed by this controller. WalkNetworks(walker NetworkWalker) - // NetworkByName returns the Network which has the passed name, if it exists otherwise nil is returned + // NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned. NetworkByName(name string) (Network, error) - // NetworkByID returns the Network which has the passed id, if it exists otherwise nil is returned + // NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned. NetworkByID(id string) (Network, error) } @@ -212,6 +212,10 @@ func (c *controller) NetworkByName(name string) (Network, error) { c.WalkNetworks(s) + if n == nil { + return nil, ErrNoSuchNetwork + } + return n, nil } @@ -224,7 +228,7 @@ func (c *controller) NetworkByID(id string) (Network, error) { if n, ok := c.networks[types.UUID(id)]; ok { return n, nil } - return nil, nil + return nil, ErrNoSuchNetwork } func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) { diff --git a/libnetwork/error.go b/libnetwork/error.go index b3b99fafa3..088bfbc826 100644 --- a/libnetwork/error.go +++ b/libnetwork/error.go @@ -6,6 +6,10 @@ import ( ) var ( + // ErrNoSuchNetwork is returned when a network query finds no result + ErrNoSuchNetwork = errors.New("network not found") + // ErrNoSuchEndpoint is returned when a endpoint query finds no result + ErrNoSuchEndpoint = errors.New("endpoint not found") // ErrNilNetworkDriver is returned if a nil network driver // is passed to NewNetwork api. ErrNilNetworkDriver = errors.New("nil NetworkDriver instance") diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 9cc1def240..2a020ada37 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -591,11 +591,11 @@ func TestControllerQuery(t *testing.T) { } g, err := controller.NetworkByID("network1") - if err != nil { - t.Fatalf("Unexpected failure for NetworkByID(): %v", err) + if err == nil { + t.Fatalf("Unexpected success for NetworkByID(): %g", g) } - if g != nil { - t.Fatalf("NetworkByID() succeeded with unknown target id") + if err != libnetwork.ErrNoSuchNetwork { + t.Fatalf("NetworkByID() failed with unexpected error: %v", err) } g, err = controller.NetworkByName("network1") @@ -665,7 +665,10 @@ func TestNetworkQuery(t *testing.T) { } e, err = net1.EndpointByName("IamNotAnEndpoint") - if err != nil { + if err == nil { + t.Fatalf("EndpointByName() succeeded with unknown target name") + } + if err != libnetwork.ErrNoSuchEndpoint { t.Fatal(err) } if e != nil { @@ -673,6 +676,9 @@ func TestNetworkQuery(t *testing.T) { } e, err = net1.EndpointByID(ep12.ID()) + if err != nil { + t.Fatal(err) + } if ep12 != e { t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12) } @@ -1273,7 +1279,7 @@ func runParallelTests(t *testing.T, thrNumber int) { t.Fatal(err) } if ep == nil { - t.Fatal("Could not find ep1") + t.Fatal("Got nil ep with no error") } for i := 0; i < iterCnt; i++ { diff --git a/libnetwork/network.go b/libnetwork/network.go index 0a4425e3ee..8040f7941e 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -36,10 +36,10 @@ type Network interface { // WalkEndpoints uses the provided function to walk the Endpoints WalkEndpoints(walker EndpointWalker) - // EndpointByName returns the Endpoint which has the passed name, if it exists otherwise nil is returned + // EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned. EndpointByName(name string) (Endpoint, error) - // EndpointByID returns the Endpoint which has the passed id, if it exists otherwise nil is returned + // EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned. EndpointByID(id string) (Endpoint, error) } @@ -188,6 +188,10 @@ func (n *network) EndpointByName(name string) (Endpoint, error) { n.WalkEndpoints(s) + if e == nil { + return nil, ErrNoSuchEndpoint + } + return e, nil } @@ -200,5 +204,5 @@ func (n *network) EndpointByID(id string) (Endpoint, error) { if e, ok := n.endpoints[types.UUID(id)]; ok { return e, nil } - return nil, nil + return nil, ErrNoSuchEndpoint }