diff --git a/pkg/provision/storage/ephemeralStorage_test.go b/pkg/provision/storage/ephemeralStorage_test.go new file mode 100644 index 000000000..5d0a5b828 --- /dev/null +++ b/pkg/provision/storage/ephemeralStorage_test.go @@ -0,0 +1,50 @@ +// +// Copyright (c) 2019-2021 Red Hat, Inc. +// This program and the accompanying materials are made +// available under the terms of the Eclipse Public License 2.0 +// which is available at https://www.eclipse.org/legal/epl-2.0/ +// +// SPDX-License-Identifier: EPL-2.0 +// +// Contributors: +// Red Hat, Inc. - initial API and implementation +// + +package storage + +import ( + "testing" + + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + "github.com/stretchr/testify/assert" + + "github.com/devfile/devworkspace-operator/controllers/workspace/provision" +) + +func TestRewriteContainerVolumeMountsForEphemeralStorageClass(t *testing.T) { + tests := loadAllTestCasesOrPanic(t, "testdata/ephemeral-storage") + setupControllerCfg() + commonStorage := EphemeralStorageProvisioner{} + + for _, tt := range tests { + t.Run(tt.Name, func(t *testing.T) { + // sanity check that file is read correctly. + assert.NotNil(t, tt.Input.Workspace, "Input does not define workspace") + workspace := &dw.DevWorkspace{} + workspace.Spec.Template = *tt.Input.Workspace + workspace.Status.DevWorkspaceId = tt.Input.DevWorkspaceID + workspace.Namespace = "test-namespace" + err := commonStorage.ProvisionStorage(&tt.Input.PodAdditions, workspace, provision.ClusterAPI{}) + if tt.Output.ErrRegexp != nil && assert.Error(t, err) { + assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match") + } else { + if !assert.NoError(t, err, "Should not return error") { + return + } + sortVolumesAndVolumeMounts(&tt.Output.PodAdditions) + sortVolumesAndVolumeMounts(&tt.Input.PodAdditions) + assert.Equal(t, tt.Output.PodAdditions, tt.Input.PodAdditions, "PodAdditions should match expected output") + } + }) + } +} diff --git a/pkg/provision/storage/testdata/ephemeral-storage/supports-ephemeral-storageclass.yaml b/pkg/provision/storage/testdata/ephemeral-storage/supports-ephemeral-storageclass.yaml new file mode 100644 index 000000000..aaffb7650 --- /dev/null +++ b/pkg/provision/storage/testdata/ephemeral-storage/supports-ephemeral-storageclass.yaml @@ -0,0 +1,68 @@ +name: "Supports ephemeral storageclass" + +input: + devworkspaceId: "test-workspaceid" + podAdditions: + containers: + - name: testing-container-1 + image: testing-image-1 + volumeMounts: + - name: testvol-1 + mountPath: testPath1 + - name: testvol-3 + mountPath: testPath3 + - name: testing-container-2 + image: testing-image-2 + volumeMounts: + - name: testvol-2 + mountPath: testPath2 + - name: "projects" + mountPath: "/projects" + + workspace: + components: + - name: testing-container-1 + container: + image: testing-image-1 + mountSources: false + - name: testing-container-2 + container: + image: testing-image-2 + mountSources: true + + - name: testvol-1 + volume: + ephemeral: true + - name: testvol-2 + volume: + ephemeral: false + - name: testvol-3 + volume: {} + +output: + podAdditions: + containers: + - name: testing-container-1 + image: testing-image-1 + volumeMounts: + - name: testvol-1 + mountPath: testPath1 + - name: testvol-3 + mountPath: testPath3 + - name: testing-container-2 + image: testing-image-2 + volumeMounts: + - name: testvol-2 + mountPath: testPath2 + - name: projects + mountPath: /projects + + volumes: + - name: projects + emptyDir: {} + - name: testvol-1 + emptyDir: {} + - name: testvol-2 + emptyDir: {} + - name: testvol-3 + emptyDir: {}