From 4adb74e9f9e221d06df71867672f257298b9f550 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 22 Nov 2022 11:22:25 +0100 Subject: [PATCH] Add unit test --- pkg/dev/podmandev/pod_test.go | 43 +++++++++++++++++++++++++++ pkg/libdevfile/generator/component.go | 14 +++++++++ 2 files changed, 57 insertions(+) diff --git a/pkg/dev/podmandev/pod_test.go b/pkg/dev/podmandev/pod_test.go index f78d0967204..1cf896770a0 100644 --- a/pkg/dev/podmandev/pod_test.go +++ b/pkg/dev/podmandev/pod_test.go @@ -37,6 +37,10 @@ var ( }, }) + volume = generator.GetVolumeComponent(generator.VolumeComponentParams{ + Name: "myvolume", + }) + basePod = &corev1.Pod{ TypeMeta: v1.TypeMeta{ APIVersion: "v1", @@ -273,6 +277,45 @@ func Test_createPodFromComponent(t *testing.T) { }, }, }, + { + name: "basic component with volume mount", + args: args{ + devfileObj: func() parser.DevfileObj { + data, _ := data.NewDevfileData(string(data.APISchemaVersion200)) + _ = data.AddCommands([]v1alpha2.Command{command}) + _ = data.AddComponents([]v1alpha2.Component{baseComponent, volume}) + _ = data.AddVolumeMounts(baseComponent.Name, []v1alpha2.VolumeMount{ + { + Name: volume.Name, + Path: "/path/to/mount", + }, + }) + + return parser.DevfileObj{ + Data: data, + } + }, + componentName: devfileName, + appName: appName, + }, + wantPod: func() *corev1.Pod { + pod := basePod.DeepCopy() + pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{ + Name: volume.Name, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: "odo-projects-" + devfileName + "-" + appName + "-" + volume.Name, + }, + }, + }) + pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{ + Name: volume.Name, + MountPath: "/path/to/mount", + }) + return pod + }, + }, + // TODO: Add test cases. } for _, tt := range tests { diff --git a/pkg/libdevfile/generator/component.go b/pkg/libdevfile/generator/component.go index 1644e4c5e13..cec387633ec 100644 --- a/pkg/libdevfile/generator/component.go +++ b/pkg/libdevfile/generator/component.go @@ -90,3 +90,17 @@ func GetOpenshiftComponent(params OpenshiftComponentParams) v1alpha2.Component { } return cmp } + +type VolumeComponentParams struct { + Name string +} + +func GetVolumeComponent(params VolumeComponentParams) v1alpha2.Component { + cmp := v1alpha2.Component{ + Name: params.Name, + ComponentUnion: v1alpha2.ComponentUnion{ + Volume: &v1alpha2.VolumeComponent{}, + }, + } + return cmp +}