add UT
This commit is contained in:
parent
751007c17d
commit
eb8dd093fe
@ -151,9 +151,7 @@ func getPersistentVolumeClaims(set *apps.StatefulSet, pod *v1.Pod) map[string]v1
|
|||||||
claim.Namespace = set.Namespace
|
claim.Namespace = set.Namespace
|
||||||
if claim.Labels != nil {
|
if claim.Labels != nil {
|
||||||
for key, value := range set.Spec.Selector.MatchLabels {
|
for key, value := range set.Spec.Selector.MatchLabels {
|
||||||
if claim.Labels != nil {
|
claim.Labels[key] = value
|
||||||
claim.Labels[key] = value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
claim.Labels = set.Spec.Selector.MatchLabels
|
claim.Labels = set.Spec.Selector.MatchLabels
|
||||||
|
@ -19,6 +19,7 @@ package statefulset
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@ -288,6 +289,68 @@ func TestCreateApplyRevision(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPersistentVolumeClaims(t *testing.T) {
|
||||||
|
|
||||||
|
// nil inherits statefulset labels
|
||||||
|
pod := newPod()
|
||||||
|
statefulSet := newStatefulSet(1)
|
||||||
|
statefulSet.Spec.Selector.MatchLabels = nil
|
||||||
|
claims := getPersistentVolumeClaims(statefulSet, pod)
|
||||||
|
pvc := newPVC("datadir-foo-0")
|
||||||
|
pvc.SetNamespace(v1.NamespaceDefault)
|
||||||
|
resultClaims := map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(claims, resultClaims) {
|
||||||
|
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||||
|
}
|
||||||
|
|
||||||
|
// nil inherits statefulset labels
|
||||||
|
statefulSet.Spec.Selector.MatchLabels = map[string]string{"test": "test"}
|
||||||
|
claims = getPersistentVolumeClaims(statefulSet, pod)
|
||||||
|
pvc.SetLabels(map[string]string{"test": "test"})
|
||||||
|
resultClaims = map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||||
|
if !reflect.DeepEqual(claims, resultClaims) {
|
||||||
|
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-nil with non-overlapping labels merge pvc and statefulset labels
|
||||||
|
statefulSet.Spec.Selector.MatchLabels = map[string]string{"name": "foo"}
|
||||||
|
statefulSet.Spec.VolumeClaimTemplates[0].ObjectMeta.Labels = map[string]string{"test": "test"}
|
||||||
|
claims = getPersistentVolumeClaims(statefulSet, pod)
|
||||||
|
pvc.SetLabels(map[string]string{"test": "test", "name": "foo"})
|
||||||
|
resultClaims = map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||||
|
if !reflect.DeepEqual(claims, resultClaims) {
|
||||||
|
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-nil with overlapping labels merge pvc and statefulset labels and prefer statefulset labels
|
||||||
|
statefulSet.Spec.Selector.MatchLabels = map[string]string{"test": "foo"}
|
||||||
|
statefulSet.Spec.VolumeClaimTemplates[0].ObjectMeta.Labels = map[string]string{"test": "test"}
|
||||||
|
claims = getPersistentVolumeClaims(statefulSet, pod)
|
||||||
|
pvc.SetLabels(map[string]string{"test": "foo"})
|
||||||
|
resultClaims = map[string]v1.PersistentVolumeClaim{"datadir": pvc}
|
||||||
|
if !reflect.DeepEqual(claims, resultClaims) {
|
||||||
|
t.Fatalf("Unexpected pvc:\n %+v\n, desired pvc:\n %+v", claims, resultClaims)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPod() *v1.Pod {
|
||||||
|
return &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "foo-0",
|
||||||
|
Namespace: v1.NamespaceDefault,
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{
|
||||||
|
Name: "nginx",
|
||||||
|
Image: "nginx",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newPVC(name string) v1.PersistentVolumeClaim {
|
func newPVC(name string) v1.PersistentVolumeClaim {
|
||||||
return v1.PersistentVolumeClaim{
|
return v1.PersistentVolumeClaim{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Loading…
Reference in New Issue
Block a user