diff --git a/pkg/virtctl/guestfs/BUILD.bazel b/pkg/virtctl/guestfs/BUILD.bazel index c136fefc5f7a..4ae662b4818d 100644 --- a/pkg/virtctl/guestfs/BUILD.bazel +++ b/pkg/virtctl/guestfs/BUILD.bazel @@ -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", @@ -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", + ], +) diff --git a/pkg/virtctl/guestfs/guestfs_suite_test.go b/pkg/virtctl/guestfs/guestfs_suite_test.go new file mode 100644 index 000000000000..0118650fdd15 --- /dev/null +++ b/pkg/virtctl/guestfs/guestfs_suite_test.go @@ -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") +} diff --git a/pkg/virtctl/guestfs/guestfs_test.go b/pkg/virtctl/guestfs/guestfs_test.go new file mode 100644 index 000000000000..85116d040850 --- /dev/null +++ b/pkg/virtctl/guestfs/guestfs_test.go @@ -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))) + }) + }) + +})