lease.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package common
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "time"
  19. coordinationv1 "k8s.io/api/coordination/v1"
  20. apiequality "k8s.io/apimachinery/pkg/api/equality"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/labels"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/apimachinery/pkg/util/strategicpatch"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. "k8s.io/utils/pointer"
  28. )
  29. func getPatchBytes(oldLease, newLease *coordinationv1.Lease) ([]byte, error) {
  30. oldData, err := json.Marshal(oldLease)
  31. if err != nil {
  32. return nil, fmt.Errorf("failed to Marshal oldData: %v", err)
  33. }
  34. newData, err := json.Marshal(newLease)
  35. if err != nil {
  36. return nil, fmt.Errorf("failed to Marshal newData: %v", err)
  37. }
  38. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, coordinationv1.Lease{})
  39. if err != nil {
  40. return nil, fmt.Errorf("failed to CreateTwoWayMergePatch: %v", err)
  41. }
  42. return patchBytes, nil
  43. }
  44. var _ = framework.KubeDescribe("Lease", func() {
  45. f := framework.NewDefaultFramework("lease-test")
  46. /*
  47. Release : v1.17
  48. Testname: lease API should be available
  49. Description: Create Lease object, and get it; create and get MUST be successful and Spec of the
  50. read Lease MUST match Spec of original Lease. Update the Lease and get it; update and get MUST
  51. be successful and Spec of the read Lease MUST match Spec of updated Lease. Patch the Lease and
  52. get it; patch and get MUST be successful and Spec of the read Lease MUST match Spec of patched
  53. Lease. Create a second Lease with labels and list Leases; create and list MUST be successful and
  54. list MUST return both leases. Delete the labels lease via delete collection; the delete MUST be
  55. successful and MUST delete only the labels lease. List leases; list MUST be successful and MUST
  56. return just the remaining lease. Delete the lease; delete MUST be successful. Get the lease; get
  57. MUST return not found error.
  58. */
  59. framework.ConformanceIt("lease API should be available", func() {
  60. leaseClient := f.ClientSet.CoordinationV1().Leases(f.Namespace.Name)
  61. name := "lease"
  62. lease := &coordinationv1.Lease{
  63. ObjectMeta: metav1.ObjectMeta{
  64. Name: name,
  65. },
  66. Spec: coordinationv1.LeaseSpec{
  67. HolderIdentity: pointer.StringPtr("holder"),
  68. LeaseDurationSeconds: pointer.Int32Ptr(30),
  69. AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(2 * time.Second)},
  70. RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(5 * time.Second)},
  71. LeaseTransitions: pointer.Int32Ptr(0),
  72. },
  73. }
  74. createdLease, err := leaseClient.Create(context.TODO(), lease, metav1.CreateOptions{})
  75. framework.ExpectNoError(err, "creating Lease failed")
  76. readLease, err := leaseClient.Get(context.TODO(), name, metav1.GetOptions{})
  77. framework.ExpectNoError(err, "couldn't read Lease")
  78. framework.ExpectEqual(apiequality.Semantic.DeepEqual(lease.Spec, readLease.Spec), true)
  79. createdLease.Spec = coordinationv1.LeaseSpec{
  80. HolderIdentity: pointer.StringPtr("holder2"),
  81. LeaseDurationSeconds: pointer.Int32Ptr(30),
  82. AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(20 * time.Second)},
  83. RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(50 * time.Second)},
  84. LeaseTransitions: pointer.Int32Ptr(1),
  85. }
  86. _, err = leaseClient.Update(context.TODO(), createdLease, metav1.UpdateOptions{})
  87. framework.ExpectNoError(err, "updating Lease failed")
  88. readLease, err = leaseClient.Get(context.TODO(), name, metav1.GetOptions{})
  89. framework.ExpectNoError(err, "couldn't read Lease")
  90. framework.ExpectEqual(apiequality.Semantic.DeepEqual(createdLease.Spec, readLease.Spec), true)
  91. patchedLease := readLease.DeepCopy()
  92. patchedLease.Spec = coordinationv1.LeaseSpec{
  93. HolderIdentity: pointer.StringPtr("holder3"),
  94. LeaseDurationSeconds: pointer.Int32Ptr(60),
  95. AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(50 * time.Second)},
  96. RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(70 * time.Second)},
  97. LeaseTransitions: pointer.Int32Ptr(2),
  98. }
  99. patchBytes, err := getPatchBytes(readLease, patchedLease)
  100. framework.ExpectNoError(err, "creating patch failed")
  101. _, err = leaseClient.Patch(context.TODO(), name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
  102. framework.ExpectNoError(err, "patching Lease failed")
  103. readLease, err = leaseClient.Get(context.TODO(), name, metav1.GetOptions{})
  104. framework.ExpectNoError(err, "couldn't read Lease")
  105. framework.ExpectEqual(apiequality.Semantic.DeepEqual(patchedLease.Spec, readLease.Spec), true)
  106. name2 := "lease2"
  107. lease2 := &coordinationv1.Lease{
  108. ObjectMeta: metav1.ObjectMeta{
  109. Name: name2,
  110. Labels: map[string]string{"deletecollection": "true"},
  111. },
  112. Spec: coordinationv1.LeaseSpec{
  113. HolderIdentity: pointer.StringPtr("holder"),
  114. LeaseDurationSeconds: pointer.Int32Ptr(30),
  115. AcquireTime: &metav1.MicroTime{Time: time.Time{}.Add(2 * time.Second)},
  116. RenewTime: &metav1.MicroTime{Time: time.Time{}.Add(5 * time.Second)},
  117. LeaseTransitions: pointer.Int32Ptr(0),
  118. },
  119. }
  120. _, err = leaseClient.Create(context.TODO(), lease2, metav1.CreateOptions{})
  121. framework.ExpectNoError(err, "creating Lease failed")
  122. leases, err := leaseClient.List(context.TODO(), metav1.ListOptions{})
  123. framework.ExpectNoError(err, "couldn't list Leases")
  124. framework.ExpectEqual(len(leases.Items), 2)
  125. selector := labels.Set(map[string]string{"deletecollection": "true"}).AsSelector()
  126. err = leaseClient.DeleteCollection(context.TODO(), &metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: selector.String()})
  127. framework.ExpectNoError(err, "couldn't delete collection")
  128. leases, err = leaseClient.List(context.TODO(), metav1.ListOptions{})
  129. framework.ExpectNoError(err, "couldn't list Leases")
  130. framework.ExpectEqual(len(leases.Items), 1)
  131. err = leaseClient.Delete(context.TODO(), name, &metav1.DeleteOptions{})
  132. framework.ExpectNoError(err, "deleting Lease failed")
  133. _, err = leaseClient.Get(context.TODO(), name, metav1.GetOptions{})
  134. framework.ExpectEqual(apierrors.IsNotFound(err), true)
  135. })
  136. })