...
1
16
17 package validation
18
19 import (
20 "testing"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/apimachinery/pkg/util/validation/field"
24 "k8s.io/kubernetes/pkg/apis/coordination"
25 )
26
27 func TestValidateLease(t *testing.T) {
28 lease := &coordination.Lease{
29 ObjectMeta: metav1.ObjectMeta{
30 Name: "invalidName++",
31 Namespace: "==invalid_Namespace==",
32 },
33 }
34 errs := ValidateLease(lease)
35 if len(errs) != 2 {
36 t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
37 }
38 }
39
40 func TestValidateLeaseSpec(t *testing.T) {
41 holder := "holder"
42 leaseDuration := int32(0)
43 leaseTransitions := int32(-1)
44 spec := &coordination.LeaseSpec{
45 HolderIdentity: &holder,
46 LeaseDurationSeconds: &leaseDuration,
47 LeaseTransitions: &leaseTransitions,
48 }
49 errs := ValidateLeaseSpec(spec, field.NewPath("foo"))
50 if len(errs) != 2 {
51 t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
52 }
53 }
54
55 func TestValidateLeaseSpecUpdate(t *testing.T) {
56 holder := "holder"
57 leaseDuration := int32(0)
58 leaseTransitions := int32(-1)
59 lease := &coordination.Lease{
60 ObjectMeta: metav1.ObjectMeta{
61 Name: "holder",
62 Namespace: "holder-namespace",
63 },
64 Spec: coordination.LeaseSpec{
65 HolderIdentity: &holder,
66 LeaseDurationSeconds: &leaseDuration,
67 LeaseTransitions: &leaseTransitions,
68 },
69 }
70 oldHolder := "oldHolder"
71 oldLeaseDuration := int32(3)
72 oldLeaseTransitions := int32(3)
73 oldLease := &coordination.Lease{
74 ObjectMeta: metav1.ObjectMeta{
75 Name: "holder",
76 Namespace: "holder-namespace",
77 },
78 Spec: coordination.LeaseSpec{
79 HolderIdentity: &oldHolder,
80 LeaseDurationSeconds: &oldLeaseDuration,
81 LeaseTransitions: &oldLeaseTransitions,
82 },
83 }
84 errs := ValidateLeaseUpdate(lease, oldLease)
85 if len(errs) != 3 {
86 t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
87 }
88
89 validLeaseDuration := int32(10)
90 validLeaseTransitions := int32(20)
91 validLease := &coordination.Lease{
92 ObjectMeta: oldLease.ObjectMeta,
93 Spec: coordination.LeaseSpec{
94 HolderIdentity: &holder,
95 LeaseDurationSeconds: &validLeaseDuration,
96 LeaseTransitions: &validLeaseTransitions,
97 },
98 }
99 validLease.ObjectMeta.ResourceVersion = "2"
100 errs = ValidateLeaseUpdate(validLease, oldLease)
101 if len(errs) != 0 {
102 t.Errorf("unexpected list of errors for valid update: %#v", errs.ToAggregate().Error())
103 }
104 }
105
View as plain text