Skip to content

Commit

Permalink
Added e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasgun committed Jun 27, 2022
1 parent 0347941 commit cf8b5b6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
16 changes: 15 additions & 1 deletion pkg/kn/flags/podspec_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
ConfigMapVolumeSourceType VolumeSourceType = iota
SecretVolumeSourceType
EmptyDirVolumeSourceType
PVCVolumeSourceType
PortFormatErr = "the port specification '%s' is not valid. Please provide in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'."
)

Expand Down Expand Up @@ -470,6 +471,8 @@ func updateVolume(volume *corev1.Volume, info *volumeSourceInfo) error {
volume.Secret = &corev1.SecretVolumeSource{SecretName: info.volumeSourceName}
case EmptyDirVolumeSourceType:
volume.EmptyDir = &corev1.EmptyDirVolumeSource{Medium: corev1.StorageMedium(info.emptyDirMemoryType), SizeLimit: info.emptyDirSize}
case PVCVolumeSourceType:
volume.PersistentVolumeClaim = &corev1.PersistentVolumeClaimVolumeSource{ClaimName: info.volumeSourceName, ReadOnly: true}
default:
return fmt.Errorf("Invalid VolumeSourceType")
}
Expand Down Expand Up @@ -607,6 +610,8 @@ func newVolumeSourceInfoWithSpecString(spec string) (*volumeSourceInfo, error) {
volumeSourceType = SecretVolumeSourceType
case "emptyDir", "ed":
volumeSourceType = EmptyDirVolumeSourceType
case "persistentVolumeClaim", "pvc":
volumeSourceType = PVCVolumeSourceType
default:
return nil, fmt.Errorf("unsupported volume source type \"%q\"; supported volume source types are \"config-map\" and \"secret\"", slices[0])
}
Expand All @@ -622,7 +627,7 @@ func newVolumeSourceInfoWithSpecString(spec string) (*volumeSourceInfo, error) {
} else {
typeString := strings.TrimSpace(slices[0])
switch typeString {
case "config-map", "cm", "secret", "sc":
case "config-map", "cm", "secret", "sc", "persistentVolumeClaim", "pvc":
return nil, fmt.Errorf("incorrect mount details for type %q", typeString)
case "emptyDir", "ed":
volName := slices[1]
Expand Down Expand Up @@ -763,6 +768,15 @@ func reviseVolumeInfoAndMountsToUpdate(mountsToUpdate *util.OrderedMap, volumesT
})
mountInfo.VolumeName = generatedName
mountsToUpdateRevised.Set(path, mountInfo)
case "persistentVolumeClaim", "pvc":
generatedName := util.GenerateVolumeName(path)
mountInfo := getMountInfo(slices[1])
volumeSourceInfoByName.Set(generatedName, &volumeSourceInfo{
volumeSourceType: PVCVolumeSourceType,
volumeSourceName: mountInfo.VolumeName,
})
mountInfo.VolumeName = generatedName
mountsToUpdateRevised.Set(path, mountInfo)
default:
return nil, nil, fmt.Errorf("unsupported volume type \"%q\"; supported volume types are \"config-map or cm\", \"secret or sc\", \"volume or vo\", and \"emptyDir or ed\"", slices[0])
}
Expand Down
48 changes: 46 additions & 2 deletions test/e2e/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package e2e
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -33,6 +35,20 @@ import (
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)

const (
TestPVCSpec = `kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
`
)

func TestService(t *testing.T) {
t.Parallel()
it, err := test.NewKnTest()
Expand Down Expand Up @@ -178,10 +194,16 @@ func serviceDeleteAll(r *test.KnRunResultCollector) {

func serviceCreateWithMount(r *test.KnRunResultCollector) {
it := r.KnTest()
kubectl := test.NewKubectl(it.Namespace())
kubectl := test.NewKubectl("knative-serving")

_, err := kubectl.Run("patch", "cm", "config-features", "--patch={\"data\":{\"kubernetes.podspec-persistent-volume-claim\": \"enabled\", \"kubernetes.podspec-volumes-emptydir\": \"enabled\"}}")
assert.NilError(r.T(), err)
defer kubectl.Run("patch", "cm", "config-features", "--patch={\"data\":{\"kubernetes.podspec-persistent-volume-claim\": \"disabled\", \"kubernetes.podspec-volumes-emptydir\": \"disabled\"}}")

kubectl = test.NewKubectl(it.Namespace())

r.T().Log("create cm test-cm")
_, err := kubectl.Run("create", "configmap", "test-cm", "--from-literal=key=value")
_, err = kubectl.Run("create", "configmap", "test-cm", "--from-literal=key=value")
assert.NilError(r.T(), err)

r.T().Log("create service with configmap mounted")
Expand All @@ -200,6 +222,28 @@ func serviceCreateWithMount(r *test.KnRunResultCollector) {
out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir2=sc:test-sec/key1")
r.AssertNoError(out)

r.T().Log("update service with a new emptyDir mount")
out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir3=ed:myvol")
r.AssertNoError(out)

r.T().Log("update service with a new emptyDir mount with Memory and dir size")
out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir4=ed:myvol:type=Memory,size=100Mi")
r.AssertNoError(out)

r.T().Log("create PVC test-pvc")
fp, err := ioutil.TempFile("", "my-pvc")
assert.NilError(r.T(), err)
fmt.Fprintf(fp, "%s", TestPVCSpec)
defer os.Remove(fp.Name())

_, err = kubectl.Run("create", "-f", fp.Name())
assert.NilError(r.T(), err)
r.AssertNoError(out)

r.T().Log("update service with a new pvc mount")
out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir5=pvc:test-pvc")
r.AssertNoError(out)

serviceDescribeMount(r, "test-svc", "/mydir", "key")
}

Expand Down

0 comments on commit cf8b5b6

Please sign in to comment.