Skip to content

Commit

Permalink
Add unit tests for the guestfs command
Browse files Browse the repository at this point in the history
Signed-off-by: Alice Frosi <[email protected]>
  • Loading branch information
alicefr committed Jun 1, 2021
1 parent 23ef4c3 commit baf6d2a
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pkg/virtctl/guestfs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand All @@ -20,3 +20,27 @@ go_library(
"//vendor/k8s.io/client-go/tools/remotecommand:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = [
"guestfs_suite_test.go",
"guestfs_test.go",
],
deps = [
":go_default_library",
"//staging/src/kubevirt.io/client-go/api/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//tests:go_default_library",
"//vendor/github.com/golang/mock/gomock:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//vendor/k8s.io/client-go/testing:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
],
)
13 changes: 13 additions & 0 deletions pkg/virtctl/guestfs/guestfs_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package guestfs_test

import (
"testing"

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

func TestGuestfs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Guestfs Suite")
}
146 changes: 146 additions & 0 deletions pkg/virtctl/guestfs/guestfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package guestfs_test

import (
"fmt"

"github.com/golang/mock/gomock"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/testing"

kubevirtv1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/pkg/virtctl/guestfs"
"kubevirt.io/kubevirt/tests"
)

const (
commandName = "guestfs"
pvcName = "test-pvc"
testNamespace = "default"
)

func fakeAttacherCreator(client *guestfs.K8sClient, p *corev1.Pod, command string) error {
return nil
}

var _ = Describe("Guestfs shell", func() {
var (
kubeClient *fake.Clientset
kubevirtClient *kubecli.MockKubevirtClient
)
mode := v1.PersistentVolumeFilesystem
pvc := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName,
Namespace: testNamespace,
},
Spec: v1.PersistentVolumeClaimSpec{
VolumeMode: &mode,
},
}
kv := &kubevirtv1.KubeVirt{
ObjectMeta: metav1.ObjectMeta{
Name: "kubevirt",
Namespace: "kubevirt",
},
Status: kubevirtv1.KubeVirtStatus{
TargetKubeVirtRegistry: "registry:5000",
TargetKubeVirtVersion: "test",
},
}
fakeCreateClientPVC := func(config *rest.Config, virtClientConfig clientcmd.ClientConfig) (*guestfs.K8sClient, error) {
kubeClient = fake.NewSimpleClientset(pvc)
kubeClient.Fake.PrependReactor("get", "pods", func(action testing.Action) (bool, runtime.Object, error) {
podRunning := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "libguestfs-tools",
Namespace: testNamespace,
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "virt",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{
StartedAt: metav1.Time{},
},
},
},
},
},
}
return true, podRunning, nil
})
return &guestfs.K8sClient{Client: kubeClient, VirtClient: kubevirtClient}, nil
}
fakeCreateClientPVCinUse := func(config *rest.Config, virtClientConfig clientcmd.ClientConfig) (*guestfs.K8sClient, error) {
otherPod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: testNamespace,
},
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: "volume-test",
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
},
},
},
}

kubeClient = fake.NewSimpleClientset(pvc, otherPod)
return &guestfs.K8sClient{Client: kubeClient, VirtClient: kubevirtClient}, nil
}
fakeCreateClient := func(config *rest.Config, virtClientConfig clientcmd.ClientConfig) (*guestfs.K8sClient, error) {
kubeClient = fake.NewSimpleClientset()
return &guestfs.K8sClient{Client: kubeClient, VirtClient: kubevirtClient}, nil
}
BeforeEach(func() {
ctrl := gomock.NewController(GinkgoT())
kubevirtClient = kubecli.NewMockKubevirtClient(ctrl)
kvInterface := kubecli.NewMockKubeVirtInterface(ctrl)
kvInterface.EXPECT().Get("kubevirt", gomock.Any()).Return(kv, nil).AnyTimes()
kubevirtClient.EXPECT().KubeVirt("kubevirt").Return(kvInterface)

guestfs.SetAttacher(fakeAttacherCreator)
})
Context("attach to PVC", func() {

It("Succesfully attach to PVC", func() {
guestfs.SetClient(fakeCreateClientPVC)
cmd := tests.NewRepeatableVirtctlCommand(commandName, "--pvc", pvcName)
Expect(cmd()).To(BeNil())
})
It("PVC in use", func() {
guestfs.SetClient(fakeCreateClientPVCinUse)
cmd := tests.NewRepeatableVirtctlCommand(commandName, "--pvc", pvcName)
err := cmd()
Expect(err).NotTo(BeNil())
Expect(err.Error()).Should(Equal(fmt.Sprintf("PVC %s is used by another pod", pvcName)))
})
It("PVC doesn't exist", func() {
guestfs.SetClient(fakeCreateClient)
cmd := tests.NewRepeatableVirtctlCommand(commandName, "--pvc", pvcName)
err := cmd()
Expect(err).NotTo(BeNil())
Expect(err.Error()).Should(Equal(fmt.Sprintf("The PVC %s doesn't exist", pvcName)))
})
})

})

0 comments on commit baf6d2a

Please sign in to comment.