forked from github/multus-cni
Unit tests and update to quickstart guide (#354)
* Added a test for GetLoggingLevel * Added up to 96% coverage for checkpoint.go * Improved coverage of checkpoint.go to 96.4% * Improved coverage of checkpoint.go to 96.4% * Adding changes so i will have them saved for my remote fork thingy * Fixed unit tests in checkpoint_test.go and conf_test.go * Removed unnecessary comments * improved conf code coverage by an amount that is greater than 0! * improved coverage, but line 144 of conf.go needs a look * Added unit tests to multus and types, also fixed a bug in conf.go * added label to import types/020 in types.go * hopefully resolved merge conflicts * increased code coverage in multus.go and conf.go, also added bug fixes and formatting * addressed all comments in review * Updated testing.go with better comments * changed 'thejohn' to '_not_type' for readability * added additional unit tests * added tests to kubeletclient.go * added more unit tests to k8sclient.go and kubeletclient.go * Added network status annotations section to quickstart and added more unit tests * made changes to tests based on code review
This commit is contained in:
@@ -22,6 +22,8 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
types020 "github.com/containernetworking/cni/pkg/types/020"
|
||||
testhelpers "github.com/intel/multus-cni/testing"
|
||||
testutils "github.com/intel/multus-cni/testing"
|
||||
|
||||
"github.com/containernetworking/cni/pkg/skel"
|
||||
@@ -611,4 +613,187 @@ var _ = Describe("k8sclient operations", func() {
|
||||
Expect(err).To(MatchError("GetPodNetwork: namespace isolation violation: podnamespace: test / target namespace: kube-system"))
|
||||
|
||||
})
|
||||
|
||||
It("Returns proper error message", func() {
|
||||
// getPodNetwork will give us the error, then this test will use that error on the top func boi
|
||||
err := &NoK8sNetworkError{"no kubernetes network found"}
|
||||
Expect(err.Error()).To(Equal("no kubernetes network found"))
|
||||
})
|
||||
|
||||
It("Sets pod network annotations without error", func() {
|
||||
fakePod := testutils.NewFakePod("testpod", "kube-system/net1", "")
|
||||
|
||||
net1 := `{
|
||||
"name": "net1",
|
||||
"type": "mynet",
|
||||
"cniVersion": "0.2.0"
|
||||
}`
|
||||
|
||||
args := &skel.CmdArgs{
|
||||
Args: fmt.Sprintf("K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s", fakePod.ObjectMeta.Name, fakePod.ObjectMeta.Namespace),
|
||||
}
|
||||
|
||||
fKubeClient := testutils.NewFakeKubeClient()
|
||||
fKubeClient.AddPod(fakePod)
|
||||
fKubeClient.AddNetConfig("kube-system", "net1", net1)
|
||||
|
||||
kubeClient, err := GetK8sClient("", fKubeClient)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
k8sArgs, err := GetK8sArgs(args)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
pod, err := kubeClient.GetPod(string(k8sArgs.K8S_POD_NAMESPACE), string(k8sArgs.K8S_POD_NAME))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
networkstatus := "test status"
|
||||
_, err = setPodNetworkAnnotation(kubeClient, "test", pod, networkstatus)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
// Still figuring this one out. need to make "setPodNetworkAnnotation throw an error
|
||||
// It("Fails to set pod network annotations without error", func() {
|
||||
// fakePod := testutils.NewFakePod("testpod", "kube-system/net1", "")
|
||||
|
||||
// net1 := `{
|
||||
// "name": "net1",
|
||||
// "type": "mynet",
|
||||
// "cniVersion": "0.2.0"
|
||||
// }`
|
||||
|
||||
// args := &skel.CmdArgs{
|
||||
// Args: fmt.Sprintf("K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s", fakePod.ObjectMeta.Name, fakePod.ObjectMeta.Namespace),
|
||||
// }
|
||||
|
||||
// fKubeClient := testutils.NewFakeKubeClient()
|
||||
// fKubeClient.AddPod(fakePod)
|
||||
// fKubeClient.AddNetConfig("kube-system", "net1", net1)
|
||||
|
||||
// kubeClient, err := GetK8sClient("", fKubeClient)
|
||||
// Expect(err).NotTo(HaveOccurred())
|
||||
// k8sArgs, err := GetK8sArgs(args)
|
||||
// Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// pod, err := kubeClient.GetPod(string(k8sArgs.K8S_POD_NAMESPACE), string(k8sArgs.K8S_POD_NAME))
|
||||
// Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// networkstatus := "test status"
|
||||
// _, err = setPodNetworkAnnotation(kubeClient, "test", pod, networkstatus)
|
||||
// Expect(err).NotTo(HaveOccurred())
|
||||
// })
|
||||
|
||||
It("Sets network status without error", func() {
|
||||
result := &types020.Result{
|
||||
CNIVersion: "0.2.0",
|
||||
IP4: &types020.IPConfig{
|
||||
IP: *testhelpers.EnsureCIDR("1.1.1.2/24"),
|
||||
},
|
||||
}
|
||||
|
||||
conf := `{
|
||||
"name": "node-cni-network",
|
||||
"type": "multus",
|
||||
"kubeconfig": "/etc/kubernetes/node-kubeconfig.yaml",
|
||||
"delegates": [{
|
||||
"type": "weave-net"
|
||||
}],
|
||||
"runtimeConfig": {
|
||||
"portMappings": [
|
||||
{"hostPort": 8080, "containerPort": 80, "protocol": "tcp"}
|
||||
]
|
||||
}
|
||||
}`
|
||||
|
||||
delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
delegateNetStatus, err := types.LoadNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin)
|
||||
GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
netstatus := []*types.NetworkStatus{delegateNetStatus}
|
||||
|
||||
fakePod := testutils.NewFakePod("testpod", "kube-system/net1", "")
|
||||
|
||||
netConf, err := types.LoadNetConf([]byte(conf))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
net1 := `{
|
||||
"name": "net1",
|
||||
"type": "mynet",
|
||||
"cniVersion": "0.2.0"
|
||||
}`
|
||||
|
||||
args := &skel.CmdArgs{
|
||||
Args: fmt.Sprintf("K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s", fakePod.ObjectMeta.Name, fakePod.ObjectMeta.Namespace),
|
||||
}
|
||||
|
||||
fKubeClient := testutils.NewFakeKubeClient()
|
||||
fKubeClient.AddPod(fakePod)
|
||||
fKubeClient.AddNetConfig("kube-system", "net1", net1)
|
||||
|
||||
kubeClient, err := GetK8sClient("", fKubeClient)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
k8sArgs, err := GetK8sArgs(args)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = SetNetworkStatus(kubeClient, k8sArgs, netstatus, netConf)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Fails to set network status without error", func() {
|
||||
result := &types020.Result{
|
||||
CNIVersion: "0.2.0",
|
||||
IP4: &types020.IPConfig{
|
||||
IP: *testhelpers.EnsureCIDR("1.1.1.2/24"),
|
||||
},
|
||||
}
|
||||
|
||||
conf := `{
|
||||
"name": "node-cni-network",
|
||||
"type": "multus",
|
||||
"kubeconfig": "/etc/kubernetes/node-kubeconfig.yaml",
|
||||
"delegates": [{
|
||||
"type": "weave-net"
|
||||
}],
|
||||
"runtimeConfig": {
|
||||
"portMappings": [
|
||||
{"hostPort": 8080, "containerPort": 80, "protocol": "tcp"}
|
||||
]
|
||||
}
|
||||
}`
|
||||
|
||||
delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
delegateNetStatus, err := types.LoadNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin)
|
||||
GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
netstatus := []*types.NetworkStatus{delegateNetStatus}
|
||||
|
||||
fakePod := testutils.NewFakePod("testpod", "kube-system/net1", "")
|
||||
|
||||
netConf, err := types.LoadNetConf([]byte(conf))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
net1 := `{
|
||||
"name": "net1",
|
||||
"type": "mynet",
|
||||
"cniVersion": "0.2.0"
|
||||
}`
|
||||
|
||||
args := &skel.CmdArgs{
|
||||
Args: fmt.Sprintf("K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s", fakePod.ObjectMeta.Name, fakePod.ObjectMeta.Namespace),
|
||||
}
|
||||
|
||||
fKubeClient := testutils.NewFakeKubeClient()
|
||||
fKubeClient.AddPod(fakePod)
|
||||
fKubeClient.AddNetConfig("kube-system", "net1", net1)
|
||||
|
||||
k8sArgs, err := GetK8sArgs(args)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = SetNetworkStatus(nil, k8sArgs, netstatus, netConf)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user