controller_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2018 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 nodelease
  14. import (
  15. "testing"
  16. "time"
  17. coordv1beta1 "k8s.io/api/coordination/v1beta1"
  18. corev1 "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/apimachinery/pkg/util/clock"
  23. "k8s.io/apimachinery/pkg/util/diff"
  24. "k8s.io/client-go/kubernetes/fake"
  25. "k8s.io/utils/pointer"
  26. )
  27. func TestNewLease(t *testing.T) {
  28. fakeClock := clock.NewFakeClock(time.Now())
  29. node := &corev1.Node{
  30. ObjectMeta: metav1.ObjectMeta{
  31. Name: "foo",
  32. UID: types.UID("foo-uid"),
  33. },
  34. }
  35. cases := []struct {
  36. desc string
  37. controller *controller
  38. base *coordv1beta1.Lease
  39. expect *coordv1beta1.Lease
  40. }{
  41. {
  42. desc: "nil base without node",
  43. controller: &controller{
  44. client: fake.NewSimpleClientset(),
  45. holderIdentity: node.Name,
  46. leaseDurationSeconds: 10,
  47. clock: fakeClock,
  48. },
  49. base: nil,
  50. expect: &coordv1beta1.Lease{
  51. ObjectMeta: metav1.ObjectMeta{
  52. Name: node.Name,
  53. Namespace: corev1.NamespaceNodeLease,
  54. },
  55. Spec: coordv1beta1.LeaseSpec{
  56. HolderIdentity: pointer.StringPtr(node.Name),
  57. LeaseDurationSeconds: pointer.Int32Ptr(10),
  58. RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
  59. },
  60. },
  61. },
  62. {
  63. desc: "nil base with node",
  64. controller: &controller{
  65. client: fake.NewSimpleClientset(node),
  66. holderIdentity: node.Name,
  67. leaseDurationSeconds: 10,
  68. clock: fakeClock,
  69. },
  70. base: nil,
  71. expect: &coordv1beta1.Lease{
  72. ObjectMeta: metav1.ObjectMeta{
  73. Name: node.Name,
  74. Namespace: corev1.NamespaceNodeLease,
  75. OwnerReferences: []metav1.OwnerReference{
  76. {
  77. APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version,
  78. Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind,
  79. Name: node.Name,
  80. UID: node.UID,
  81. },
  82. },
  83. },
  84. Spec: coordv1beta1.LeaseSpec{
  85. HolderIdentity: pointer.StringPtr(node.Name),
  86. LeaseDurationSeconds: pointer.Int32Ptr(10),
  87. RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
  88. },
  89. },
  90. },
  91. {
  92. desc: "non-nil base without owner ref, renew time is updated",
  93. controller: &controller{
  94. client: fake.NewSimpleClientset(node),
  95. holderIdentity: node.Name,
  96. leaseDurationSeconds: 10,
  97. clock: fakeClock,
  98. },
  99. base: &coordv1beta1.Lease{
  100. ObjectMeta: metav1.ObjectMeta{
  101. Name: node.Name,
  102. Namespace: corev1.NamespaceNodeLease,
  103. },
  104. Spec: coordv1beta1.LeaseSpec{
  105. HolderIdentity: pointer.StringPtr(node.Name),
  106. LeaseDurationSeconds: pointer.Int32Ptr(10),
  107. RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-10 * time.Second)},
  108. },
  109. },
  110. expect: &coordv1beta1.Lease{
  111. ObjectMeta: metav1.ObjectMeta{
  112. Name: node.Name,
  113. Namespace: corev1.NamespaceNodeLease,
  114. OwnerReferences: []metav1.OwnerReference{
  115. {
  116. APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version,
  117. Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind,
  118. Name: node.Name,
  119. UID: node.UID,
  120. },
  121. },
  122. },
  123. Spec: coordv1beta1.LeaseSpec{
  124. HolderIdentity: pointer.StringPtr(node.Name),
  125. LeaseDurationSeconds: pointer.Int32Ptr(10),
  126. RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
  127. },
  128. },
  129. },
  130. {
  131. desc: "non-nil base with owner ref, renew time is updated",
  132. controller: &controller{
  133. client: fake.NewSimpleClientset(node),
  134. holderIdentity: node.Name,
  135. leaseDurationSeconds: 10,
  136. clock: fakeClock,
  137. },
  138. base: &coordv1beta1.Lease{
  139. ObjectMeta: metav1.ObjectMeta{
  140. Name: node.Name,
  141. Namespace: corev1.NamespaceNodeLease,
  142. OwnerReferences: []metav1.OwnerReference{
  143. {
  144. APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version,
  145. Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind,
  146. Name: node.Name,
  147. UID: node.UID,
  148. },
  149. },
  150. },
  151. Spec: coordv1beta1.LeaseSpec{
  152. HolderIdentity: pointer.StringPtr(node.Name),
  153. LeaseDurationSeconds: pointer.Int32Ptr(10),
  154. RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-10 * time.Second)},
  155. },
  156. },
  157. expect: &coordv1beta1.Lease{
  158. ObjectMeta: metav1.ObjectMeta{
  159. Name: node.Name,
  160. Namespace: corev1.NamespaceNodeLease,
  161. OwnerReferences: []metav1.OwnerReference{
  162. {
  163. APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version,
  164. Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind,
  165. Name: node.Name,
  166. UID: node.UID,
  167. },
  168. },
  169. },
  170. Spec: coordv1beta1.LeaseSpec{
  171. HolderIdentity: pointer.StringPtr(node.Name),
  172. LeaseDurationSeconds: pointer.Int32Ptr(10),
  173. RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
  174. },
  175. },
  176. },
  177. }
  178. for _, tc := range cases {
  179. t.Run(tc.desc, func(t *testing.T) {
  180. newLease := tc.controller.newLease(tc.base)
  181. if newLease == tc.base {
  182. t.Fatalf("the new lease must be newly allocated, but got same address as base")
  183. }
  184. if !apiequality.Semantic.DeepEqual(tc.expect, newLease) {
  185. t.Errorf("unexpected result from newLease: %s", diff.ObjectDiff(tc.expect, newLease))
  186. }
  187. })
  188. }
  189. }