fix unit test for new subPathExpr feature (#2638)

* fix unit test for new subPathExpr feature
* add subPathExpr flag to CRD and re-sort
This commit is contained in:
Felix Kunde
2024-05-24 15:07:17 +02:00
committed by GitHub
parent 7bcb73a402
commit b550f8ae39
9 changed files with 78 additions and 52 deletions
+9 -6
View File
@@ -146,12 +146,18 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
Type: "object",
Required: []string{"name", "mountPath", "volumeSource"},
Properties: map[string]apiextv1.JSONSchemaProps{
"isSubPathExpr": {
Type: "boolean",
},
"name": {
Type: "string",
},
"mountPath": {
Type: "string",
},
"subPath": {
Type: "string",
},
"targetContainers": {
Type: "array",
Nullable: true,
@@ -165,12 +171,6 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
Type: "object",
XPreserveUnknownFields: util.True(),
},
"subPath": {
Type: "string",
},
"isSubPathExpr": {
Type: "boolean",
},
},
},
},
@@ -1033,6 +1033,9 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
Type: "object",
Required: []string{"size"},
Properties: map[string]apiextv1.JSONSchemaProps{
"isSubPathExpr": {
Type: "boolean",
},
"iops": {
Type: "integer",
},
+9 -8
View File
@@ -129,13 +129,14 @@ type MaintenanceWindow struct {
// Volume describes a single volume in the manifest.
type Volume struct {
Selector *metav1.LabelSelector `json:"selector,omitempty"`
Size string `json:"size"`
StorageClass string `json:"storageClass,omitempty"`
SubPath string `json:"subPath,omitempty"`
Iops *int64 `json:"iops,omitempty"`
Throughput *int64 `json:"throughput,omitempty"`
VolumeType string `json:"type,omitempty"`
Selector *metav1.LabelSelector `json:"selector,omitempty"`
Size string `json:"size"`
StorageClass string `json:"storageClass,omitempty"`
SubPath string `json:"subPath,omitempty"`
IsSubPathExpr *bool `json:"isSubPathExpr,omitemtpy"`
Iops *int64 `json:"iops,omitempty"`
Throughput *int64 `json:"throughput,omitempty"`
VolumeType string `json:"type,omitempty"`
}
// AdditionalVolume specs additional optional volumes for statefulset
@@ -143,7 +144,7 @@ type AdditionalVolume struct {
Name string `json:"name"`
MountPath string `json:"mountPath"`
SubPath string `json:"subPath,omitempty"`
IsSubPathExpr bool `json:"isSubPathExpr,omitemtpy"`
IsSubPathExpr *bool `json:"isSubPathExpr,omitemtpy"`
TargetContainers []string `json:"targetContainers"`
VolumeSource v1.VolumeSource `json:"volumeSource"`
}
@@ -53,6 +53,11 @@ func (in *AWSGCPConfiguration) DeepCopy() *AWSGCPConfiguration {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *AdditionalVolume) DeepCopyInto(out *AdditionalVolume) {
*out = *in
if in.IsSubPathExpr != nil {
in, out := &in.IsSubPathExpr, &out.IsSubPathExpr
*out = new(bool)
**out = **in
}
if in.TargetContainers != nil {
in, out := &in.TargetContainers, &out.TargetContainers
*out = make([]string, len(*in))
@@ -1447,6 +1452,11 @@ func (in *Volume) DeepCopyInto(out *Volume) {
*out = new(metav1.LabelSelector)
(*in).DeepCopyInto(*out)
}
if in.IsSubPathExpr != nil {
in, out := &in.IsSubPathExpr, &out.IsSubPathExpr
*out = new(bool)
**out = **in
}
if in.Iops != nil {
in, out := &in.Iops, &out.Iops
*out = new(int64)
+9 -3
View File
@@ -668,13 +668,19 @@ func isBootstrapOnlyParameter(param string) bool {
}
func generateVolumeMounts(volume acidv1.Volume) []v1.VolumeMount {
return []v1.VolumeMount{
volumeMount := []v1.VolumeMount{
{
Name: constants.DataVolumeName,
MountPath: constants.PostgresDataMount, //TODO: fetch from manifest
SubPath: volume.SubPath,
},
}
if volume.IsSubPathExpr != nil && *volume.IsSubPathExpr {
volumeMount[0].SubPathExpr = volume.SubPath
} else {
volumeMount[0].SubPath = volume.SubPath
}
return volumeMount
}
func generateContainer(
@@ -1825,7 +1831,7 @@ func (c *Cluster) addAdditionalVolumes(podSpec *v1.PodSpec,
MountPath: additionalVolume.MountPath,
}
if additionalVolume.IsSubPathExpr {
if additionalVolume.IsSubPathExpr != nil && *additionalVolume.IsSubPathExpr {
v.SubPathExpr = additionalVolume.SubPath
} else {
v.SubPath = additionalVolume.SubPath
+24 -27
View File
@@ -1902,7 +1902,7 @@ func TestAdditionalVolume(t *testing.T) {
Name: "test6",
MountPath: "/test6",
SubPath: "$(POD_NAME)",
IsSubPathExpr: true,
IsSubPathExpr: util.True(),
TargetContainers: nil, // should mount only to postgres
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
@@ -1922,7 +1922,9 @@ func TestAdditionalVolume(t *testing.T) {
ResourceLimits: acidv1.ResourceDescription{CPU: k8sutil.StringToPointer("1"), Memory: k8sutil.StringToPointer("10")},
},
Volume: acidv1.Volume{
Size: "1G",
Size: "1G",
SubPath: "$(POD_NAME)",
IsSubPathExpr: util.True(),
},
AdditionalVolumes: additionalVolumes,
Sidecars: []acidv1.Sidecar{
@@ -1954,31 +1956,25 @@ func TestAdditionalVolume(t *testing.T) {
assert.NoError(t, err)
tests := []struct {
subTest string
container string
expectedMounts []string
expectedSubPath []string
subTest string
container string
expectedMounts []string
expectedSubPaths []string
expectedSubPathExprs []string
}{
{
subTest: "checking volume mounts of postgres container",
container: constants.PostgresContainerName,
expectedMounts: []string{"pgdata", "test1", "test3", "test4"},
subTest: "checking volume mounts of postgres container",
container: constants.PostgresContainerName,
expectedMounts: []string{"pgdata", "test1", "test3", "test4", "test5", "test6"},
expectedSubPaths: []string{"", "", "", "", "subpath", ""},
expectedSubPathExprs: []string{"$(POD_NAME)", "", "", "", "", "$(POD_NAME)"},
},
{
subTest: "checking volume mounts of sidecar container",
container: "sidecar",
expectedMounts: []string{"pgdata", "test1", "test2"},
},
{
subTest: "checking volume mounts with subPath",
container: constants.PostgresContainerName,
expectedMounts: []string{"test5"},
expectedSubPath: []string{"subpath"},
},
{
subTest: "checking volume mounts with subPathExpr",
container: constants.PostgresContainerName,
expectedMounts: []string{"test6"},
subTest: "checking volume mounts of sidecar container",
container: "sidecar",
expectedMounts: []string{"pgdata", "test1", "test2"},
expectedSubPaths: []string{"", "", ""},
expectedSubPathExprs: []string{"$(POD_NAME)", "", ""},
},
}
@@ -1990,6 +1986,7 @@ func TestAdditionalVolume(t *testing.T) {
mounts := []string{}
subPaths := []string{}
subPathExprs := []string{}
for _, volumeMounts := range container.VolumeMounts {
mounts = append(mounts, volumeMounts.Name)
subPaths = append(subPaths, volumeMounts.SubPath)
@@ -2001,14 +1998,14 @@ func TestAdditionalVolume(t *testing.T) {
t.Name(), tt.subTest, mounts, tt.expectedMounts)
}
if !util.IsEqualIgnoreOrder(subPaths, tt.expectedSubPath) {
if !util.IsEqualIgnoreOrder(subPaths, tt.expectedSubPaths) {
t.Errorf("%s %s: different volume subPaths: got %v, expected %v",
t.Name(), tt.subTest, mounts, tt.expectedSubPath)
t.Name(), tt.subTest, subPaths, tt.expectedSubPaths)
}
if !util.IsEqualIgnoreOrder(subPathExprs, []string{container.Name}) {
if !util.IsEqualIgnoreOrder(subPathExprs, tt.expectedSubPathExprs) {
t.Errorf("%s %s: different volume subPathExprs: got %v, expected %v",
t.Name(), tt.subTest, mounts, tt.expectedMounts)
t.Name(), tt.subTest, subPathExprs, tt.expectedSubPathExprs)
}
}
}