forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for the guestfs command
Signed-off-by: Alice Frosi <[email protected]>
- Loading branch information
Showing
3 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
}) | ||
}) | ||
|
||
}) |