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 8 commits
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
4 changes: 4 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inpackage: False
testonly: False
with-expecter: True
keeptree: True
ykulazhenkov marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ GO_BUILD_OPTS ?= CGO_ENABLED=0 GOOS=$(TARGET_OS) GOARCH=$(TARGET_ARCH)
GO_LDFLAGS = $(VERSION_LDFLAGS)


PKGS = $(or $(PKG),$(shell cd $(PROJECT_DIR) && go list ./... | grep -v "^nvidia-k8s-ipam/vendor/" | grep -v ".*/mocks"))

.PHONY: all
all: build

Expand Down Expand Up @@ -69,7 +71,7 @@ LCOV_PATH = $(PROJECT_DIR)/lcov.info

.PHONY: unit-test
unit-test: envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverpkg=./... -covermode=$(COVERAGE_MODE) -coverprofile=$(COVER_PROFILE)
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -covermode=$(COVERAGE_MODE) -coverprofile=$(COVER_PROFILE) $(PKGS)

.PHONY: test
test: lint unit-test
Expand Down Expand Up @@ -111,6 +113,10 @@ KIND_CLUSTER ?= kind
kind-load-image: ## Load ipam image to kind cluster
kind load docker-image --name $(KIND_CLUSTER) $(IMG)

.PHONY: generate-mocks
generate-mocks: ## generate mock objects
PATH=$(PATH):$(LOCALBIN) go generate ./...

## Location to install dependencies to
LOCALBIN ?= $(PROJECT_DIR)/bin
$(LOCALBIN):
Expand All @@ -120,10 +126,12 @@ $(LOCALBIN):
ENVTEST ?= $(LOCALBIN)/setup-envtest
GOLANGCILINT ?= $(LOCALBIN)/golangci-lint
GCOV2LCOV ?= $(LOCALBIN)/gcov2lcov
MOCKERY ?= $(LOCALBIN)/mockery

## Tool Versions
GOLANGCILINT_VERSION ?= v1.52.2
GCOV2LCOV_VERSION ?= v1.0.5
MOCKERY_VERSION ?= v2.27.1

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
Expand All @@ -140,6 +148,10 @@ gcov2lcov: $(GCOV2LCOV) ## Download gcov2lcov locally if necessary.
$(GCOV2LCOV): | $(LOCALBIN)
GOBIN=$(LOCALBIN) go install github.com/jandelgado/gcov2lcov@$(GCOV2LCOV_VERSION)

.PHONY: mockery
mockery: $(MOCKERY) ## Download gcov2lcov locally if necessary.
ykulazhenkov marked this conversation as resolved.
Show resolved Hide resolved
$(MOCKERY): | $(LOCALBIN)
GOBIN=$(LOCALBIN) go install github.com/vektra/mockery/v2@$(MOCKERY_VERSION)

.PHONY: clean
clean: ## Remove downloaded tools and compiled binaries
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/onsi/gomega v1.24.2
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
ykulazhenkov marked this conversation as resolved.
Show resolved Hide resolved
k8s.io/api v0.26.4
k8s.io/apimachinery v0.26.4
k8s.io/client-go v0.26.4
Expand All @@ -27,6 +28,7 @@ require (
github.com/coreos/go-iptables v0.6.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
Expand All @@ -51,11 +53,13 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/safchain/ethtool v0.2.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/vishvananda/netlink v1.2.1-beta.2 // indirect
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
go.uber.org/atomic v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -294,6 +295,7 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmdutils/cmdutils_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
limitations under the License.
*/

// Package cmdutils is the package that contains utilities for nv-ipam command
package cmdutils
package cmdutils_test

import (
. "github.com/onsi/ginkgo/v2"
Expand All @@ -21,7 +20,7 @@ import (
"testing"
)

func TestServer(t *testing.T) {
func TestCmdUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "cmdutils")
RunSpecs(t, "cmdutils Suite")
}
1 change: 0 additions & 1 deletion pkg/cmdutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
limitations under the License.
*/

// Package cmdutils is the package that contains utilities for multus command
package cmdutils

import (
Expand Down
94 changes: 52 additions & 42 deletions pkg/cmdutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,69 @@
limitations under the License.
*/

// Package cmdutils is the package that contains utilities for multus command
package cmdutils
package cmdutils_test

import (
"fmt"
"os"
"fmt"
"os"

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

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

var _ = Describe("thin entrypoint testing", func() {
It("Run CopyFileAtomic()", func() {
// create directory and files
tmpDir, err := os.MkdirTemp("", "multus_thin_entrypoint_tmp")
Expect(err).NotTo(HaveOccurred())
var _ = Describe("cmdutils testing", func() {
var (
tmpDir string
)

BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "nv_ipam_thin_entrypoint_tmp")
ykulazhenkov marked this conversation as resolved.
Show resolved Hide resolved
Expect(err).NotTo(HaveOccurred())
})

// create source directory
srcDir := fmt.Sprintf("%s/src", tmpDir)
err = os.Mkdir(srcDir, 0755)
Expect(err).NotTo(HaveOccurred())
AfterEach(func() {
err := os.RemoveAll(tmpDir)
Expect(err).NotTo(HaveOccurred())
})

// create destination directory
destDir := fmt.Sprintf("%s/dest", tmpDir)
err = os.Mkdir(destDir, 0755)
Expect(err).NotTo(HaveOccurred())
It("Run CopyFileAtomic()", func() {
var err error
// create source directory
srcDir := fmt.Sprintf("%s/src", tmpDir)
err = os.Mkdir(srcDir, 0755)
Expect(err).NotTo(HaveOccurred())

// sample source file
srcFilePath := fmt.Sprintf("%s/sampleInput", srcDir)
err = os.WriteFile(srcFilePath, []byte("sampleInputABC"), 0744)
Expect(err).NotTo(HaveOccurred())
// create destination directory
destDir := fmt.Sprintf("%s/dest", tmpDir)
err = os.Mkdir(destDir, 0755)
Expect(err).NotTo(HaveOccurred())

// old files in dest
destFileName := "sampleInputDest"
destFilePath := fmt.Sprintf("%s/%s", destDir, destFileName)
err = os.WriteFile(destFilePath, []byte("inputOldXYZ"), 0611)
Expect(err).NotTo(HaveOccurred())
// sample source file
srcFilePath := fmt.Sprintf("%s/sampleInput", srcDir)
err = os.WriteFile(srcFilePath, []byte("sampleInputABC"), 0744)
Expect(err).NotTo(HaveOccurred())

tempFileName := "temp_file"
err = CopyFileAtomic(srcFilePath, destDir, tempFileName, destFileName)
Expect(err).NotTo(HaveOccurred())
// old files in dest
destFileName := "sampleInputDest"
destFilePath := fmt.Sprintf("%s/%s", destDir, destFileName)
err = os.WriteFile(destFilePath, []byte("inputOldXYZ"), 0611)
Expect(err).NotTo(HaveOccurred())

// check file mode
stat, err := os.Stat(destFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(stat.Mode()).To(Equal(os.FileMode(0744)))
tempFileName := "temp_file"
err = cmdutils.CopyFileAtomic(srcFilePath, destDir, tempFileName, destFileName)
Expect(err).NotTo(HaveOccurred())

// check file contents
destFileByte, err := os.ReadFile(destFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(destFileByte).To(Equal([]byte("sampleInputABC")))
// check file mode
stat, err := os.Stat(destFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(stat.Mode()).To(Equal(os.FileMode(0744)))

err = os.RemoveAll(tmpDir)
Expect(err).NotTo(HaveOccurred())
})
// check file contents
destFileByte, err := os.ReadFile(destFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(destFileByte).To(Equal([]byte("sampleInputABC")))
})
})
26 changes: 26 additions & 0 deletions pkg/cni/k8sclient/k8sclient_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 k8sclient_test

import (
"testing"

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

func TestK8sClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "K8sClient Suite")
}
66 changes: 66 additions & 0 deletions pkg/cni/k8sclient/k8sclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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 k8sclient_test

import (
"os"
"path"
"path/filepath"

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

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

var _ = Describe("k8sclient tests", func() {
var (
tmpDir string
validKubeconfigPath string
invalidKubeconfigPath string
)

BeforeEach(func() {
var err error
tmpDir := GinkgoT().TempDir()

validKubeconfigPath = filepath.Join(tmpDir, "kube.conf")
invalidKubeconfigPath = filepath.Join(tmpDir, "kube-invalid.conf")

data, err := os.ReadFile(path.Join("..", "..", "..", "testdata", "test.kubeconfig"))
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(validKubeconfigPath, data, 0o644)
Expect(err).NotTo(HaveOccurred())

err = os.WriteFile(invalidKubeconfigPath, []byte("This is an invalid kubeconfig content"), 0o644)
Expect(err).NotTo(HaveOccurred())
moshe010 marked this conversation as resolved.
Show resolved Hide resolved
})

Context("FromKubeconfig", func() {
It("creates k8s deffered client if kubeconfig valid", func() {
_, err := k8sclient.FromKubeconfig(validKubeconfigPath)
Expect(err).ToNot(HaveOccurred())
})

It("fails if kubeconfig path not found", func() {
_, err := k8sclient.FromKubeconfig(filepath.Join(tmpDir, "does-not-exist.conf"))
Expect(err).To(HaveOccurred())
})

It("fails if kubeconfig file contains garbage", func() {
_, err := k8sclient.FromKubeconfig(invalidKubeconfigPath)
Expect(err).To(HaveOccurred())
})
})
})
Loading