Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test #21

Merged
merged 9 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/pool/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ func SetIPBlockAnnotation(node *v1.Node, pools map[string]*IPPool) error {
if err != nil {
return fmt.Errorf("failed to serialize pools config: %v", err)
}
annotations[ipBlocksAnnotation] = string(data)
annotations[IPBlocksAnnotation] = string(data)
node.SetAnnotations(annotations)
return nil
}

// IPBlockAnnotationExists returns true if ip-block annotation exist
func IPBlockAnnotationExists(node *v1.Node) bool {
_, exist := node.GetAnnotations()[ipBlocksAnnotation]
_, exist := node.GetAnnotations()[IPBlocksAnnotation]
return exist
}

// RemoveIPBlockAnnotation removes annotation with ip-block from the node object
func RemoveIPBlockAnnotation(node *v1.Node) {
annotations := node.GetAnnotations()
delete(annotations, ipBlocksAnnotation)
delete(annotations, IPBlocksAnnotation)
node.SetAnnotations(annotations)
}
111 changes: 111 additions & 0 deletions pkg/pool/annotations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2023, NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pool_test

import (
"encoding/json"

v1 "k8s.io/api/core/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/Mellanox/nvidia-k8s-ipam/pkg/pool"
)

var _ = Describe("annotations tests", func() {
Context("SetIPBlockAnnotation", func() {
testPools := make(map[string]*pool.IPPool)
testPools["my-pool-1"] = &pool.IPPool{
Name: "my-pool-1",
Subnet: "192.168.0.0/16",
StartIP: "192.168.0.2",
EndIP: "192.168.0.254",
Gateway: "192.168.0.1",
}
testPools["my-pool-2"] = &pool.IPPool{
Name: "my-pool-2",
Subnet: "10.100.0.0/16",
StartIP: "10.100.0.2",
EndIP: "10.100.0.254",
Gateway: "10.100.0.1",
}

It("sets annotation successfully", func() {
n := v1.Node{}
pool.SetIPBlockAnnotation(&n, testPools)
Expect(n.GetAnnotations()).ToNot(BeNil())

data, err := json.Marshal(testPools)
Expect(err).ToNot(HaveOccurred())
Expect(n.GetAnnotations()[pool.IPBlocksAnnotation]).To(Equal(string(data)))
})

It("overwrites annotation successfully", func() {
n := v1.Node{}
annot := map[string]string{
pool.IPBlocksAnnotation: `{"my-pool": {"some": "content"}}`,
}
n.SetAnnotations(annot)

pool.SetIPBlockAnnotation(&n, testPools)
Expect(n.GetAnnotations()).ToNot(BeNil())

data, err := json.Marshal(testPools)
Expect(err).ToNot(HaveOccurred())
Expect(n.GetAnnotations()[pool.IPBlocksAnnotation]).To(Equal(string(data)))
})
})

Context("IPBlockAnnotationExists", func() {
It("returns true if annotation exists", func() {
n := v1.Node{}
ipBlockAnnot := make(map[string]string)
ipBlockAnnot[pool.IPBlocksAnnotation] = "foobar"
n.SetAnnotations(ipBlockAnnot)

Expect(pool.IPBlockAnnotationExists(&n)).To(BeTrue())
})

It("returns false if annotation does not exists", func() {
Expect(pool.IPBlockAnnotationExists(&v1.Node{})).To(BeFalse())
})
})

Context("RemoveIPBlockAnnotation", func() {
It("Succeeds if annotation does not exist", func() {
n := v1.Node{}
annot := map[string]string{
"foo": "bar",
}
n.SetAnnotations(annot)

pool.RemoveIPBlockAnnotation(&n)
Expect(n.GetAnnotations()).To(HaveKey("foo"))
})

It("removes annotation if exists", func() {
n := v1.Node{}
annot := map[string]string{
"foo": "bar",
pool.IPBlocksAnnotation: "baz",
}
n.SetAnnotations(annot)

pool.RemoveIPBlockAnnotation(&n)
Expect(n.GetAnnotations()).To(HaveKey("foo"))
Expect(n.GetAnnotations()).ToNot(HaveKey(pool.IPBlocksAnnotation))
})
})
})
10 changes: 6 additions & 4 deletions pkg/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

const (
ipBlocksAnnotation = "ipam.nvidia.com/ip-blocks"
IPBlocksAnnotation = "ipam.nvidia.com/ip-blocks"
moshe010 marked this conversation as resolved.
Show resolved Hide resolved
)

// IPPool represents a block of IPs from a given Subnet
type IPPool struct {
Name string `json:"-"`
Subnet string `json:"subnet"`
Expand All @@ -32,6 +33,7 @@ type IPPool struct {
Gateway string `json:"gateway"`
}

// Manager is an interface to manage IPPools
type Manager interface {
// GetPoolByName returns IPPool for the provided pool name or nil if pool doesnt exist
GetPoolByName(name string) *IPPool
Expand All @@ -48,15 +50,15 @@ func NewManagerImpl(node *v1.Node) (*ManagerImpl, error) {
return nil, fmt.Errorf("nil node provided")
}

blocks, ok := node.Annotations[ipBlocksAnnotation]
blocks, ok := node.Annotations[IPBlocksAnnotation]
if !ok {
return nil, fmt.Errorf("%s node annotation not found", ipBlocksAnnotation)
return nil, fmt.Errorf("%s node annotation not found", IPBlocksAnnotation)
}

poolByName := make(map[string]*IPPool)
err := json.Unmarshal([]byte(blocks), &poolByName)
if err != nil {
return nil, fmt.Errorf("failed to parse %s annotation content. %w", ipBlocksAnnotation, err)
return nil, fmt.Errorf("failed to parse %s annotation content. %w", IPBlocksAnnotation, err)
}

for poolName, pool := range poolByName {
Expand Down
26 changes: 26 additions & 0 deletions pkg/pool/pool_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2023, NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pool_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"testing"
)

func TestPool(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "pool Suite")
}
92 changes: 92 additions & 0 deletions pkg/pool/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2023, NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pool_test

import (
v1 "k8s.io/api/core/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/Mellanox/nvidia-k8s-ipam/pkg/pool"
)

var _ = Describe("pool tests", func() {
Context("NewManagerImpl()", func() {
It("Creates a Manager successfully if node has ip-pool annotation", func() {
n := v1.Node{}
emptyAnnot := map[string]string{
pool.IPBlocksAnnotation: "{}",
}
n.SetAnnotations(emptyAnnot)
m, err := pool.NewManagerImpl(&n)
Expect(err).ToNot(HaveOccurred())
Expect(m.GetPools()).To(HaveLen(0))

annot := map[string]string{
pool.IPBlocksAnnotation: `{"my-pool":
{"subnet": "192.168.0.0/16", "startIP": "192.168.0.2",
"endIP": "192.168.0.254", "gateway": "192.168.0.1"}}`,
}
n.SetAnnotations(annot)
m, err = pool.NewManagerImpl(&n)
Expect(err).ToNot(HaveOccurred())
Expect(m.GetPools()).To(HaveLen(1))
})

It("Fails to create Manager if node is missing ip-pool annotation", func() {
n := v1.Node{}
_, err := pool.NewManagerImpl(&n)
Expect(err).To(HaveOccurred())
})

It("Fails to create Manager if node has empty ip-pool annotation", func() {
moshe010 marked this conversation as resolved.
Show resolved Hide resolved
n := v1.Node{}
emptyAnnot := map[string]string{
pool.IPBlocksAnnotation: "",
}
n.SetAnnotations(emptyAnnot)
_, err := pool.NewManagerImpl(&n)
Expect(err).To(HaveOccurred())
})
})

Context("GetPoolByName()", func() {
var m pool.Manager

BeforeEach(func() {
var err error
n := v1.Node{}
annot := map[string]string{
pool.IPBlocksAnnotation: `{"my-pool":
{"subnet": "192.168.0.0/16", "startIP": "192.168.0.2",
"endIP": "192.168.0.254", "gateway": "192.168.0.1"}}`,
}
n.SetAnnotations(annot)
m, err = pool.NewManagerImpl(&n)
Expect(err).ToNot(HaveOccurred())
})

It("returns nil if pool does not exist", func() {
p := m.GetPoolByName("non-existent-pool")
Expect(p).To(BeNil())
})

It("returns pool if exists", func() {
p := m.GetPoolByName("my-pool")
Expect(p).ToNot(BeNil())
Expect(p.Subnet).To(Equal("192.168.0.0/16"))
})
})
})