From 5cebb62184e992600690ad841853685dd27ae15e Mon Sep 17 00:00:00 2001 From: taku-k Date: Thu, 12 Oct 2017 16:41:30 +0900 Subject: [PATCH] Fix to return empty slice --- pkg/ipam/ip.go | 8 +++++++- pkg/ipam/ipam.go | 8 +++++--- pkg/ipam/pool_test.go | 8 ++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/ipam/ip.go b/pkg/ipam/ip.go index 2d2052a..111dc4f 100644 --- a/pkg/ipam/ip.go +++ b/pkg/ipam/ip.go @@ -47,6 +47,12 @@ func getIPAddr(r *storage.Redis, ip net.IP) (*model.IPAddr, error) { } func getIPAddrs(r *storage.Redis, ips []net.IP) ([]*model.IPAddr, error) { + addrs := make([]*model.IPAddr, len(ips)) + + if len(ips) == 0 { + return addrs, nil + } + dkeys := make([]string, len(ips)) for i, ip := range ips { dkeys[i] = makeIPDetailsKey(ip) @@ -55,7 +61,7 @@ func getIPAddrs(r *storage.Redis, ips []net.IP) ([]*model.IPAddr, error) { if err != nil { return nil, err } - addrs := make([]*model.IPAddr, len(ips)) + for i, d := range data { if s, ok := d.(string); ok { addrs[i] = &model.IPAddr{} diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index ab003e5..ceb0cd1 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -292,9 +292,11 @@ func (m *IPManager) CreatePool(ctx context.Context, n *model.Network, pool *mode } members = append(members, z) } - _, err = m.redis.Client.ZAdd(usedkey, members...).Result() - if err != nil { - return err + if len(members) != 0 { + _, err = m.redis.Client.ZAdd(usedkey, members...).Result() + if err != nil { + return err + } } return setPool(m.redis, n, pool) diff --git a/pkg/ipam/pool_test.go b/pkg/ipam/pool_test.go index abd5727..ba3412a 100644 --- a/pkg/ipam/pool_test.go +++ b/pkg/ipam/pool_test.go @@ -28,8 +28,12 @@ func Test_getPools(t *testing.T) { Status: model.Network_AVAILABLE, } - _ = m.CreateNetwork(ctx, n) - _ = m.CreatePool(ctx, n, pool) + if err := m.CreateNetwork(ctx, n); err != nil { + t.Fatalf("CreateNetwork returns error %v; want success", err) + } + if err := m.CreatePool(ctx, n, pool); err != nil { + t.Fatalf("CreatePool returns error %v; want success", err) + } pools, err := getPoolsInNetwork(r, n) if err != nil {