Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fenio committed Oct 26, 2024
1 parent 534539d commit 0fd8ac5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/plugin/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,59 @@ func TestGetPodIP(t *testing.T) {
}
})
}

func TestContains(t *testing.T) {
modes := []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteOnce,
corev1.ReadWriteMany,
}
if !contains(modes, corev1.ReadWriteOnce) {
t.Error("Expected mode to be found")
}
if contains(modes, corev1.ReadOnlyMany) {
t.Error("Did not expect mode to be found")
}
}

func TestGeneratePodNameAndPort(t *testing.T) {
name1, port1 := generatePodNameAndPort("standalone")
name2, port2 := generatePodNameAndPort("standalone")
if name1 == name2 {
t.Error("Expected different pod names")
}
if port1 == port2 {
t.Error("Expected different ports")
}
}

func TestCreatePodSpec(t *testing.T) {
podSpec := createPodSpec("test-pod", 12345, "test-pvc", "publicKey", "standalone", 22, "", false)
if podSpec.Name != "test-pod" {
t.Errorf("Expected pod name 'test-pod', got '%s'", podSpec.Name)
}
// Additional checks for volumes, containers, etc.
}

func TestGetPVCVolumeName(t *testing.T) {
pod := &corev1.Pod{
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "test-volume",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "test-pvc",
},
},
},
},
},
}
volumeName, err := getPVCVolumeName(pod)
if err != nil {
t.Errorf("getPVCVolumeName returned an error: %v", err)
}
if volumeName != "test-volume" {
t.Errorf("Expected volume name 'test-volume', got '%s'", volumeName)
}
}
33 changes: 33 additions & 0 deletions pkg/plugin/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package plugin

import (
// Necessary imports
"crypto/elliptic"
"strings"
"testing"
)

func TestRandSeq(t *testing.T) {
length := 10
seq := randSeq(length)
if len(seq) != length {
t.Errorf("Expected length %d, got %d", length, len(seq))
}
allowedChars := "abcdefghijklmnopqrstuvwxyz0123456789"
for _, char := range seq {
if !strings.ContainsRune(allowedChars, char) {
t.Errorf("Invalid character %c in sequence", char)
}
}
}

func TestGenerateKeyPair(t *testing.T) {
privateKey, publicKey, err := GenerateKeyPair(elliptic.P256())
if err != nil {
t.Fatalf("GenerateKeyPair returned an error: %v", err)
}
if privateKey == "" || publicKey == "" {
t.Error("GenerateKeyPair returned empty keys")
}
// Additional checks can be added to validate the key formats
}

0 comments on commit 0fd8ac5

Please sign in to comment.