|
| 1 | +package chart |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/api/resource" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 15 | + "testing" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAdjustToClusterSize(t *testing.T) { |
| 19 | + testCases := map[string]struct { |
| 20 | + rawPVCToInstall *corev1.PersistentVolumeClaim |
| 21 | + clusterPVC []client.Object |
| 22 | + expectedPVC *corev1.PersistentVolumeClaim |
| 23 | + }{ |
| 24 | + "pvc not exists in cluster": { |
| 25 | + rawPVCToInstall: fixPVC(dockerRegistryPVCName, 20), |
| 26 | + expectedPVC: fixPVC(dockerRegistryPVCName, 20), |
| 27 | + }, |
| 28 | + "pvc is not docker registry": { |
| 29 | + rawPVCToInstall: fixPVC("random-pvc", 20), |
| 30 | + expectedPVC: fixPVC("random-pvc", 20), |
| 31 | + }, |
| 32 | + "pvc exists with the same size": { |
| 33 | + rawPVCToInstall: fixPVC(dockerRegistryPVCName, 20), |
| 34 | + clusterPVC: []client.Object{fixPVC(dockerRegistryPVCName, 20)}, |
| 35 | + expectedPVC: fixPVC(dockerRegistryPVCName, 20), |
| 36 | + }, |
| 37 | + "pvc exists with bigger size": { |
| 38 | + rawPVCToInstall: fixPVC(dockerRegistryPVCName, 20), |
| 39 | + clusterPVC: []client.Object{fixPVC(dockerRegistryPVCName, 30)}, |
| 40 | + expectedPVC: fixPVC(dockerRegistryPVCName, 30), |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + for name, testCase := range testCases { |
| 45 | + t.Run(name, func(t *testing.T) { |
| 46 | + //GIVEN |
| 47 | + out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(testCase.rawPVCToInstall) |
| 48 | + require.NoError(t, err) |
| 49 | + obj := unstructured.Unstructured{Object: out} |
| 50 | + |
| 51 | + c := fake.NewClientBuilder().WithObjects(testCase.clusterPVC...).Build() |
| 52 | + |
| 53 | + //WHEN |
| 54 | + finalObj, err := AdjustDockerRegToClusterPVCSize(context.TODO(), c, obj) |
| 55 | + |
| 56 | + //THEN |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + expected, err := runtime.DefaultUnstructuredConverter.ToUnstructured(testCase.expectedPVC) |
| 60 | + |
| 61 | + require.NoError(t, err) |
| 62 | + require.EqualValues(t, expected, finalObj.Object) |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func fixPVC(name string, size int) *corev1.PersistentVolumeClaim { |
| 68 | + return &corev1.PersistentVolumeClaim{ |
| 69 | + ObjectMeta: metav1.ObjectMeta{ |
| 70 | + Name: name, |
| 71 | + Namespace: "kyma-system", |
| 72 | + }, |
| 73 | + Spec: corev1.PersistentVolumeClaimSpec{ |
| 74 | + Resources: corev1.ResourceRequirements{ |
| 75 | + Requests: corev1.ResourceList{ |
| 76 | + corev1.ResourceStorage: resource.MustParse(fmt.Sprintf("%dGi", size)), |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestIsPVC(t *testing.T) { |
| 84 | + testCases := map[string]struct { |
| 85 | + input schema.GroupVersionKind |
| 86 | + expected bool |
| 87 | + }{ |
| 88 | + "Equal": { |
| 89 | + input: schema.GroupVersionKind{ |
| 90 | + Group: "", |
| 91 | + Version: "v1", |
| 92 | + Kind: "PersistentVolumeClaim", |
| 93 | + }, |
| 94 | + expected: true, |
| 95 | + }, |
| 96 | + "Different kind": { |
| 97 | + input: schema.GroupVersionKind{ |
| 98 | + Group: "", |
| 99 | + Version: "v1", |
| 100 | + Kind: "Pod", |
| 101 | + }, |
| 102 | + expected: false, |
| 103 | + }, |
| 104 | + "Different version": { |
| 105 | + input: schema.GroupVersionKind{ |
| 106 | + Group: "", |
| 107 | + Version: "v2alpha1", |
| 108 | + Kind: "PersistentVolumeClaim", |
| 109 | + }, |
| 110 | + expected: false, |
| 111 | + }, |
| 112 | + "Different group": { |
| 113 | + input: schema.GroupVersionKind{ |
| 114 | + Group: "networking", |
| 115 | + Version: "v1", |
| 116 | + Kind: "NetworkPolicy", |
| 117 | + }, |
| 118 | + expected: false, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + for name, testCase := range testCases { |
| 123 | + t.Run(name, func(t *testing.T) { |
| 124 | + //GIVEN |
| 125 | + |
| 126 | + //WHEN |
| 127 | + equal := IsPVC(testCase.input) |
| 128 | + //THEN |
| 129 | + require.Equal(t, testCase.expected, equal) |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments