Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from yuuki/fix-nil-err-wrap
Browse files Browse the repository at this point in the history
Fix panic when GetNetworkByIP returns 'not found error'
  • Loading branch information
taku-k authored Oct 23, 2017
2 parents 07292cc + 9205b67 commit 5752a9e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/ipam/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ func getNetwork(r *storage.Redis, ipnet *net.IPNet) (*model.Network, error) {
dkey := makeNetworkDetailsKey(ipnet)

check, err := r.Client.Exists(dkey).Result()
if err != nil || check == 0 {
if err != nil {
return nil, errors.Wrap(err, "not found Network")
}
if check == 0 {
return nil, errors.New("not found Network")
}

data, err := r.Client.Get(dkey).Result()
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions pkg/server/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"fmt"
"net"
"testing"

Expand Down Expand Up @@ -29,6 +30,12 @@ var (
"192.168.0.1",
},
Status: model.Network_AVAILABLE,
Tags: []*model.Tag{
{
Key: "Name",
Value: "test",
},
},
}

testPool = &model.Pool{
Expand Down Expand Up @@ -300,3 +307,70 @@ func TestGetIPInPool(t *testing.T) {
t.Errorf("Got wrong ips %v; want %v", resp.Ips[0], ips[0])
}
}

func TestGetNetwork(t *testing.T) {
te := newTest(t)
defer te.tearDown()

te.manager.CreateNetwork(te.ctx, testNetwork)
te.manager.CreatePool(te.ctx, testNetwork, testPool)

testCases := []struct {
req *serverpb.GetNetworkRequest
expected string
errmsg string
desc string
}{
{
req: &serverpb.GetNetworkRequest{
Ip: "192.168.0.0",
Mask: 24,
},
expected: "192.168.0.0/24",
errmsg: "",
desc: "GetNetworkByIP",
},
{
req: &serverpb.GetNetworkRequest{
Name: "test",
},
expected: "192.168.0.0/24",
errmsg: "",
desc: "GetNetworkByName",
},
{
req: &serverpb.GetNetworkRequest{
Ip: "192.168.0.255",
Mask: 24,
},
errmsg: "not found Network",
desc: "not exist ip",
},
{
req: &serverpb.GetNetworkRequest{
Name: "notfound",
},
errmsg: "Not found network",
desc: "not exist name",
},
}

for _, tc := range testCases {
resp, err := te.api.GetNetwork(te.ctx, tc.req)

if err == nil && tc.errmsg != "" {
t.Fatalf("desc: %s, Got nil; want error %q", tc.desc, err, tc.errmsg)
} else if err != nil && fmt.Sprintf("%s", err) != tc.errmsg {
t.Fatalf("desc: %s, Got error %q; want error %q", tc.desc, err, tc.errmsg)
}

if err != nil {
continue
}

if resp.Network != tc.expected {
t.Errorf("desc: %s, Got wrong ip %v; want %v", tc.desc, resp.Network, tc.expected)
}
}

}

0 comments on commit 5752a9e

Please sign in to comment.