endpointslice_tracker_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 endpointslice
  14. import (
  15. "testing"
  16. discovery "k8s.io/api/discovery/v1beta1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func TestEndpointSliceTrackerUpdate(t *testing.T) {
  20. epSlice1 := &discovery.EndpointSlice{
  21. ObjectMeta: metav1.ObjectMeta{
  22. Name: "example-1",
  23. Namespace: "ns1",
  24. ResourceVersion: "rv1",
  25. Labels: map[string]string{discovery.LabelServiceName: "svc1"},
  26. },
  27. }
  28. epSlice1DifferentNS := epSlice1.DeepCopy()
  29. epSlice1DifferentNS.Namespace = "ns2"
  30. epSlice1DifferentService := epSlice1.DeepCopy()
  31. epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2"
  32. epSlice1DifferentRV := epSlice1.DeepCopy()
  33. epSlice1DifferentRV.ResourceVersion = "rv2"
  34. testCases := map[string]struct {
  35. updateParam *discovery.EndpointSlice
  36. checksParam *discovery.EndpointSlice
  37. expectHas bool
  38. expectStale bool
  39. }{
  40. "same slice": {
  41. updateParam: epSlice1,
  42. checksParam: epSlice1,
  43. expectHas: true,
  44. expectStale: false,
  45. },
  46. "different namespace": {
  47. updateParam: epSlice1,
  48. checksParam: epSlice1DifferentNS,
  49. expectHas: false,
  50. expectStale: true,
  51. },
  52. "different service": {
  53. updateParam: epSlice1,
  54. checksParam: epSlice1DifferentService,
  55. expectHas: false,
  56. expectStale: true,
  57. },
  58. "different resource version": {
  59. updateParam: epSlice1,
  60. checksParam: epSlice1DifferentRV,
  61. expectHas: true,
  62. expectStale: true,
  63. },
  64. }
  65. for name, tc := range testCases {
  66. t.Run(name, func(t *testing.T) {
  67. esTracker := newEndpointSliceTracker()
  68. esTracker.Update(tc.updateParam)
  69. if esTracker.Has(tc.checksParam) != tc.expectHas {
  70. t.Errorf("tc.tracker.Has(%+v) == %t, expected %t", tc.checksParam, esTracker.Has(tc.checksParam), tc.expectHas)
  71. }
  72. if esTracker.Stale(tc.checksParam) != tc.expectStale {
  73. t.Errorf("tc.tracker.Stale(%+v) == %t, expected %t", tc.checksParam, esTracker.Stale(tc.checksParam), tc.expectStale)
  74. }
  75. })
  76. }
  77. }
  78. func TestEndpointSliceTrackerDelete(t *testing.T) {
  79. epSlice1 := &discovery.EndpointSlice{
  80. ObjectMeta: metav1.ObjectMeta{
  81. Name: "example-1",
  82. Namespace: "ns1",
  83. ResourceVersion: "rv1",
  84. Labels: map[string]string{discovery.LabelServiceName: "svc1"},
  85. },
  86. }
  87. epSlice1DifferentNS := epSlice1.DeepCopy()
  88. epSlice1DifferentNS.Namespace = "ns2"
  89. epSlice1DifferentService := epSlice1.DeepCopy()
  90. epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2"
  91. epSlice1DifferentRV := epSlice1.DeepCopy()
  92. epSlice1DifferentRV.ResourceVersion = "rv2"
  93. testCases := map[string]struct {
  94. deleteParam *discovery.EndpointSlice
  95. checksParam *discovery.EndpointSlice
  96. expectHas bool
  97. expectStale bool
  98. }{
  99. "same slice": {
  100. deleteParam: epSlice1,
  101. checksParam: epSlice1,
  102. expectHas: false,
  103. expectStale: true,
  104. },
  105. "different namespace": {
  106. deleteParam: epSlice1DifferentNS,
  107. checksParam: epSlice1DifferentNS,
  108. expectHas: false,
  109. expectStale: true,
  110. },
  111. "different namespace, check original ep slice": {
  112. deleteParam: epSlice1DifferentNS,
  113. checksParam: epSlice1,
  114. expectHas: true,
  115. expectStale: false,
  116. },
  117. "different service": {
  118. deleteParam: epSlice1DifferentService,
  119. checksParam: epSlice1DifferentService,
  120. expectHas: false,
  121. expectStale: true,
  122. },
  123. "different service, check original ep slice": {
  124. deleteParam: epSlice1DifferentService,
  125. checksParam: epSlice1,
  126. expectHas: true,
  127. expectStale: false,
  128. },
  129. "different resource version": {
  130. deleteParam: epSlice1DifferentRV,
  131. checksParam: epSlice1DifferentRV,
  132. expectHas: false,
  133. expectStale: true,
  134. },
  135. "different resource version, check original ep slice": {
  136. deleteParam: epSlice1DifferentRV,
  137. checksParam: epSlice1,
  138. expectHas: false,
  139. expectStale: true,
  140. },
  141. }
  142. for name, tc := range testCases {
  143. t.Run(name, func(t *testing.T) {
  144. esTracker := newEndpointSliceTracker()
  145. esTracker.Update(epSlice1)
  146. esTracker.Delete(tc.deleteParam)
  147. if esTracker.Has(tc.checksParam) != tc.expectHas {
  148. t.Errorf("esTracker.Has(%+v) == %t, expected %t", tc.checksParam, esTracker.Has(tc.checksParam), tc.expectHas)
  149. }
  150. if esTracker.Stale(tc.checksParam) != tc.expectStale {
  151. t.Errorf("esTracker.Stale(%+v) == %t, expected %t", tc.checksParam, esTracker.Stale(tc.checksParam), tc.expectStale)
  152. }
  153. })
  154. }
  155. }