| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package ephemeral |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"testing" |
| 22 |
|
| 23 |
v1 "k8s.io/api/core/v1" |
| 24 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 |
"k8s.io/apimachinery/pkg/types" |
| 26 |
) |
| 27 |
|
| 28 |
func TestVolumeIsForPod(t *testing.T) { |
| 29 |
uid := 0 |
| 30 |
newUID := func() types.UID { |
| 31 |
uid++ |
| 32 |
return types.UID(fmt.Sprintf("%d", uid)) |
| 33 |
} |
| 34 |
isController := true |
| 35 |
|
| 36 |
podNotOwner := &v1.Pod{ |
| 37 |
ObjectMeta: metav1.ObjectMeta{ |
| 38 |
Namespace: "kube-system", |
| 39 |
Name: "podNotOwner", |
| 40 |
UID: newUID(), |
| 41 |
}, |
| 42 |
} |
| 43 |
podOwner := &v1.Pod{ |
| 44 |
ObjectMeta: metav1.ObjectMeta{ |
| 45 |
Namespace: "kube-system", |
| 46 |
Name: "podOwner", |
| 47 |
UID: newUID(), |
| 48 |
}, |
| 49 |
} |
| 50 |
pvcNoOwner := &v1.PersistentVolumeClaim{ |
| 51 |
ObjectMeta: metav1.ObjectMeta{ |
| 52 |
Namespace: "kube-system", |
| 53 |
Name: "pvcNoOwner", |
| 54 |
UID: newUID(), |
| 55 |
}, |
| 56 |
} |
| 57 |
pvcWithOwner := &v1.PersistentVolumeClaim{ |
| 58 |
ObjectMeta: metav1.ObjectMeta{ |
| 59 |
Namespace: "kube-system", |
| 60 |
Name: "pvcNoOwner", |
| 61 |
UID: newUID(), |
| 62 |
OwnerReferences: []metav1.OwnerReference{ |
| 63 |
{ |
| 64 |
UID: podOwner.UID, |
| 65 |
Controller: &isController, |
| 66 |
}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
} |
| 70 |
userPVCWithOwner := &v1.PersistentVolumeClaim{ |
| 71 |
ObjectMeta: metav1.ObjectMeta{ |
| 72 |
Namespace: "user-namespace", |
| 73 |
Name: "userPVCWithOwner", |
| 74 |
UID: newUID(), |
| 75 |
OwnerReferences: []metav1.OwnerReference{ |
| 76 |
{ |
| 77 |
UID: podOwner.UID, |
| 78 |
Controller: &isController, |
| 79 |
}, |
| 80 |
}, |
| 81 |
}, |
| 82 |
} |
| 83 |
|
| 84 |
testcases := map[string]struct { |
| 85 |
pod *v1.Pod |
| 86 |
pvc *v1.PersistentVolumeClaim |
| 87 |
expectedError string |
| 88 |
}{ |
| 89 |
"owned": { |
| 90 |
pod: podOwner, |
| 91 |
pvc: pvcWithOwner, |
| 92 |
}, |
| 93 |
"other-pod": { |
| 94 |
pod: podNotOwner, |
| 95 |
pvc: pvcWithOwner, |
| 96 |
expectedError: `PVC kube-system/pvcNoOwner was not created for pod kube-system/podNotOwner (pod is not owner)`, |
| 97 |
}, |
| 98 |
"no-owner": { |
| 99 |
pod: podOwner, |
| 100 |
pvc: pvcNoOwner, |
| 101 |
expectedError: `PVC kube-system/pvcNoOwner was not created for pod kube-system/podOwner (pod is not owner)`, |
| 102 |
}, |
| 103 |
"different-namespace": { |
| 104 |
pod: podOwner, |
| 105 |
pvc: userPVCWithOwner, |
| 106 |
expectedError: `PVC user-namespace/userPVCWithOwner was not created for pod kube-system/podOwner (pod is not owner)`, |
| 107 |
}, |
| 108 |
} |
| 109 |
|
| 110 |
for name, tc := range testcases { |
| 111 |
t.Run(name, func(t *testing.T) { |
| 112 |
err := VolumeIsForPod(tc.pod, tc.pvc) |
| 113 |
if tc.expectedError == "" { |
| 114 |
if err != nil { |
| 115 |
t.Errorf("expected no error, got %v", err) |
| 116 |
} |
| 117 |
} else { |
| 118 |
if err == nil { |
| 119 |
t.Errorf("expected error %q, got nil", tc.expectedError) |
| 120 |
} else if tc.expectedError != err.Error() { |
| 121 |
t.Errorf("expected error %q, got %v", tc.expectedError, err) |
| 122 |
} |
| 123 |
} |
| 124 |
}) |
| 125 |
} |
| 126 |
} |