-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
pvc_test.go
124 lines (106 loc) · 3.51 KB
/
pvc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright 2021 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package k8sutil
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apiresource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func TestExpandPVCIfRequired(t *testing.T) {
testcases := []struct {
label string
currentPVCSize string
desiredPVCSize string
expansionAllowed bool
}{
{
label: "case 1: size is equal",
currentPVCSize: "1Mi",
desiredPVCSize: "1Mi",
expansionAllowed: true,
},
{
label: "case 2: current size is less",
currentPVCSize: "1Mi",
desiredPVCSize: "2Mi",
expansionAllowed: true,
},
{
label: "case 3: current size is more",
currentPVCSize: "2Mi",
desiredPVCSize: "1Mi",
expansionAllowed: true,
},
{
label: "case 4: storage class allows expansion",
currentPVCSize: "1Mi",
desiredPVCSize: "2Mi",
expansionAllowed: true,
},
{
label: "case 5: storage class does not allow expansion",
currentPVCSize: "1Mi",
desiredPVCSize: "2Mi",
expansionAllowed: false,
},
}
storageClass := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
}
desiredPVC := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "rook-ceph",
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): apiresource.MustParse("1Mi"),
},
},
StorageClassName: &storageClass.ObjectMeta.Name,
},
}
for _, tc := range testcases {
desiredPVC.Spec.Resources.Requests[v1.ResourceStorage] = apiresource.MustParse(tc.currentPVCSize)
storageClass.AllowVolumeExpansion = &tc.expansionAllowed
// create fake client with PVC
cl := fake.NewClientBuilder().WithRuntimeObjects(desiredPVC, storageClass).Build()
// get existing PVC
existingPVC := &v1.PersistentVolumeClaim{}
err := cl.Get(context.TODO(), client.ObjectKey{Name: "test", Namespace: "rook-ceph"}, existingPVC)
assert.NoError(t, err)
desiredPVC.Spec.Resources.Requests[v1.ResourceStorage] = apiresource.MustParse(tc.desiredPVCSize)
ExpandPVCIfRequired(context.TODO(), cl, desiredPVC, existingPVC)
// get existing PVC
err = cl.Get(context.TODO(), client.ObjectKey{Name: "test", Namespace: "rook-ceph"}, existingPVC)
assert.NoError(t, err)
// verify size
if tc.currentPVCSize <= tc.desiredPVCSize && tc.expansionAllowed {
assert.Equal(t, desiredPVC.Spec.Resources.Requests[v1.ResourceStorage], existingPVC.Spec.Resources.Requests[v1.ResourceStorage])
} else {
assert.Equal(t, apiresource.MustParse(tc.currentPVCSize), existingPVC.Spec.Resources.Requests[v1.ResourceStorage])
}
}
}