diff --git a/pkg/plugin/clean_test.go b/pkg/plugin/clean_test.go new file mode 100644 index 0000000..85dd9e4 --- /dev/null +++ b/pkg/plugin/clean_test.go @@ -0,0 +1,86 @@ +package plugin + +import ( + "context" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/fake" +) + +func TestClean(t *testing.T) { + // Create a fake clientset + clientset := fake.NewSimpleClientset() + + // Create a test pod + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + Namespace: "default", + Labels: map[string]string{ + "pvcName": "test-pvc", + "portNumber": "12345", + }, + }, + } + _, err := clientset.CoreV1().Pods("default").Create(context.TODO(), pod, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating test pod: %v", err) + } + + // Mock BuildKubeClient to return our fake clientset + oldBuildKubeClient := BuildKubeClient + BuildKubeClient = func() (*kubernetes.Clientset, error) { + return clientset, nil + } + defer func() { BuildKubeClient = oldBuildKubeClient }() + + // Call the Clean function + err = Clean(context.TODO(), "default", "test-pvc", "/tmp/test-mount") + if err != nil { + t.Errorf("Clean function returned an error: %v", err) + } + + // Verify that the pod was deleted + _, err = clientset.CoreV1().Pods("default").Get(context.TODO(), "test-pod", metav1.GetOptions{}) + if err == nil { + t.Error("Pod was not deleted") + } +} + +func TestKillProcessInEphemeralContainer(t *testing.T) { + // Create a fake clientset + clientset := fake.NewSimpleClientset() + + // Create a test pod with an ephemeral container + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + Namespace: "default", + }, + Spec: corev1.PodSpec{ + EphemeralContainers: []corev1.EphemeralContainer{ + { + EphemeralContainerCommon: corev1.EphemeralContainerCommon{ + Name: "test-ephemeral", + }, + }, + }, + }, + } + _, err := clientset.CoreV1().Pods("default").Create(context.TODO(), pod, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating test pod: %v", err) + } + + // Call the killProcessInEphemeralContainer function + err = killProcessInEphemeralContainer(context.TODO(), clientset, "default", "test-pod") + if err != nil { + t.Errorf("killProcessInEphemeralContainer function returned an error: %v", err) + } + + // Note: We can't easily verify if the process was killed in a unit test, + // but we can at least check that the function runs without error +} diff --git a/pkg/plugin/mount_test.go b/pkg/plugin/mount_test.go index 9fccb80..8248c82 100644 --- a/pkg/plugin/mount_test.go +++ b/pkg/plugin/mount_test.go @@ -125,3 +125,47 @@ func TestGetPodIP(t *testing.T) { } }) } + +func TestCheckPVAccessMode(t *testing.T) { + // Create a fake clientset + clientset := fake.NewSimpleClientset() + + // Create a test PV and PVC + pv := &corev1.PersistentVolume{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pv", + }, + Spec: corev1.PersistentVolumeSpec{ + AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}, + }, + } + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pvc", + Namespace: "default", + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "test-pv", + }, + } + _, err := clientset.CoreV1().PersistentVolumes().Create(context.TODO(), pv, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating test PV: %v", err) + } + _, err = clientset.CoreV1().PersistentVolumeClaims("default").Create(context.TODO(), pvc, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating test PVC: %v", err) + } + + // Call the checkPVAccessMode function + canBeMounted, podUsingPVC, err := checkPVAccessMode(context.TODO(), clientset, pvc, "default") + if err != nil { + t.Errorf("checkPVAccessMode function returned an error: %v", err) + } + if !canBeMounted { + t.Error("checkPVAccessMode function returned false for canBeMounted, expected true") + } + if podUsingPVC != "" { + t.Errorf("checkPVAccessMode function returned non-empty podUsingPVC: %s", podUsingPVC) + } +}