eventhandler_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package kube
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. v1 "k8s.io/api/core/v1"
  6. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  7. )
  8. func TestAdd(t *testing.T) {
  9. var endpoints []string
  10. h := NewEventHandler(func(change []string) {
  11. endpoints = change
  12. })
  13. h.OnAdd("bad")
  14. h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
  15. {
  16. Addresses: []v1.EndpointAddress{
  17. {
  18. IP: "0.0.0.1",
  19. },
  20. {
  21. IP: "0.0.0.2",
  22. },
  23. {
  24. IP: "0.0.0.3",
  25. },
  26. },
  27. },
  28. }})
  29. assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2", "0.0.0.3"}, endpoints)
  30. }
  31. func TestDelete(t *testing.T) {
  32. var endpoints []string
  33. h := NewEventHandler(func(change []string) {
  34. endpoints = change
  35. })
  36. h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
  37. {
  38. Addresses: []v1.EndpointAddress{
  39. {
  40. IP: "0.0.0.1",
  41. },
  42. {
  43. IP: "0.0.0.2",
  44. },
  45. {
  46. IP: "0.0.0.3",
  47. },
  48. },
  49. },
  50. }})
  51. h.OnDelete("bad")
  52. h.OnDelete(&v1.Endpoints{Subsets: []v1.EndpointSubset{
  53. {
  54. Addresses: []v1.EndpointAddress{
  55. {
  56. IP: "0.0.0.1",
  57. },
  58. {
  59. IP: "0.0.0.2",
  60. },
  61. },
  62. },
  63. }})
  64. assert.ElementsMatch(t, []string{"0.0.0.3"}, endpoints)
  65. }
  66. func TestUpdate(t *testing.T) {
  67. var endpoints []string
  68. h := NewEventHandler(func(change []string) {
  69. endpoints = change
  70. })
  71. h.OnUpdate(&v1.Endpoints{
  72. Subsets: []v1.EndpointSubset{
  73. {
  74. Addresses: []v1.EndpointAddress{
  75. {
  76. IP: "0.0.0.1",
  77. },
  78. {
  79. IP: "0.0.0.2",
  80. },
  81. },
  82. },
  83. },
  84. ObjectMeta: metav1.ObjectMeta{
  85. ResourceVersion: "1",
  86. },
  87. }, &v1.Endpoints{
  88. Subsets: []v1.EndpointSubset{
  89. {
  90. Addresses: []v1.EndpointAddress{
  91. {
  92. IP: "0.0.0.1",
  93. },
  94. {
  95. IP: "0.0.0.2",
  96. },
  97. {
  98. IP: "0.0.0.3",
  99. },
  100. },
  101. },
  102. },
  103. ObjectMeta: metav1.ObjectMeta{
  104. ResourceVersion: "2",
  105. },
  106. })
  107. assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2", "0.0.0.3"}, endpoints)
  108. }
  109. func TestUpdateNoChange(t *testing.T) {
  110. h := NewEventHandler(func(change []string) {
  111. assert.Fail(t, "should not called")
  112. })
  113. h.OnUpdate(&v1.Endpoints{
  114. Subsets: []v1.EndpointSubset{
  115. {
  116. Addresses: []v1.EndpointAddress{
  117. {
  118. IP: "0.0.0.1",
  119. },
  120. {
  121. IP: "0.0.0.2",
  122. },
  123. },
  124. },
  125. },
  126. ObjectMeta: metav1.ObjectMeta{
  127. ResourceVersion: "1",
  128. },
  129. }, &v1.Endpoints{
  130. Subsets: []v1.EndpointSubset{
  131. {
  132. Addresses: []v1.EndpointAddress{
  133. {
  134. IP: "0.0.0.1",
  135. },
  136. {
  137. IP: "0.0.0.2",
  138. },
  139. },
  140. },
  141. },
  142. ObjectMeta: metav1.ObjectMeta{
  143. ResourceVersion: "1",
  144. },
  145. })
  146. }
  147. func TestUpdateChangeWithDifferentVersion(t *testing.T) {
  148. var endpoints []string
  149. h := NewEventHandler(func(change []string) {
  150. endpoints = change
  151. })
  152. h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
  153. {
  154. Addresses: []v1.EndpointAddress{
  155. {
  156. IP: "0.0.0.1",
  157. },
  158. {
  159. IP: "0.0.0.3",
  160. },
  161. },
  162. },
  163. }})
  164. h.OnUpdate(&v1.Endpoints{
  165. Subsets: []v1.EndpointSubset{
  166. {
  167. Addresses: []v1.EndpointAddress{
  168. {
  169. IP: "0.0.0.1",
  170. },
  171. {
  172. IP: "0.0.0.3",
  173. },
  174. },
  175. },
  176. },
  177. ObjectMeta: metav1.ObjectMeta{
  178. ResourceVersion: "1",
  179. },
  180. }, &v1.Endpoints{
  181. Subsets: []v1.EndpointSubset{
  182. {
  183. Addresses: []v1.EndpointAddress{
  184. {
  185. IP: "0.0.0.1",
  186. },
  187. {
  188. IP: "0.0.0.2",
  189. },
  190. },
  191. },
  192. },
  193. ObjectMeta: metav1.ObjectMeta{
  194. ResourceVersion: "2",
  195. },
  196. })
  197. assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2"}, endpoints)
  198. }
  199. func TestUpdateNoChangeWithDifferentVersion(t *testing.T) {
  200. var endpoints []string
  201. h := NewEventHandler(func(change []string) {
  202. endpoints = change
  203. })
  204. h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
  205. {
  206. Addresses: []v1.EndpointAddress{
  207. {
  208. IP: "0.0.0.1",
  209. },
  210. {
  211. IP: "0.0.0.2",
  212. },
  213. },
  214. },
  215. }})
  216. h.OnUpdate("bad", &v1.Endpoints{Subsets: []v1.EndpointSubset{
  217. {
  218. Addresses: []v1.EndpointAddress{
  219. {
  220. IP: "0.0.0.1",
  221. },
  222. },
  223. },
  224. }})
  225. h.OnUpdate(&v1.Endpoints{Subsets: []v1.EndpointSubset{
  226. {
  227. Addresses: []v1.EndpointAddress{
  228. {
  229. IP: "0.0.0.1",
  230. },
  231. },
  232. },
  233. }}, "bad")
  234. h.OnUpdate(&v1.Endpoints{
  235. Subsets: []v1.EndpointSubset{
  236. {
  237. Addresses: []v1.EndpointAddress{
  238. {
  239. IP: "0.0.0.1",
  240. },
  241. {
  242. IP: "0.0.0.2",
  243. },
  244. },
  245. },
  246. },
  247. ObjectMeta: metav1.ObjectMeta{
  248. ResourceVersion: "1",
  249. },
  250. }, &v1.Endpoints{
  251. Subsets: []v1.EndpointSubset{
  252. {
  253. Addresses: []v1.EndpointAddress{
  254. {
  255. IP: "0.0.0.1",
  256. },
  257. {
  258. IP: "0.0.0.2",
  259. },
  260. },
  261. },
  262. },
  263. ObjectMeta: metav1.ObjectMeta{
  264. ResourceVersion: "2",
  265. },
  266. })
  267. assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2"}, endpoints)
  268. }