watch_cache_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. Copyright 2014 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 storage
  14. import (
  15. "strconv"
  16. "testing"
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/errors"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/client/cache"
  22. "k8s.io/kubernetes/pkg/runtime"
  23. "k8s.io/kubernetes/pkg/util/clock"
  24. "k8s.io/kubernetes/pkg/util/sets"
  25. "k8s.io/kubernetes/pkg/util/wait"
  26. "k8s.io/kubernetes/pkg/watch"
  27. )
  28. func makeTestPod(name string, resourceVersion uint64) *api.Pod {
  29. return &api.Pod{
  30. ObjectMeta: api.ObjectMeta{
  31. Namespace: "ns",
  32. Name: name,
  33. ResourceVersion: strconv.FormatUint(resourceVersion, 10),
  34. },
  35. }
  36. }
  37. // newTestWatchCache just adds a fake clock.
  38. func newTestWatchCache(capacity int) *watchCache {
  39. wc := newWatchCache(capacity)
  40. wc.clock = clock.NewFakeClock(time.Now())
  41. return wc
  42. }
  43. func TestWatchCacheBasic(t *testing.T) {
  44. store := newTestWatchCache(2)
  45. // Test Add/Update/Delete.
  46. pod1 := makeTestPod("pod", 1)
  47. if err := store.Add(pod1); err != nil {
  48. t.Errorf("unexpected error: %v", err)
  49. }
  50. if item, ok, _ := store.Get(pod1); !ok {
  51. t.Errorf("didn't find pod")
  52. } else {
  53. if !api.Semantic.DeepEqual(pod1, item) {
  54. t.Errorf("expected %v, got %v", pod1, item)
  55. }
  56. }
  57. pod2 := makeTestPod("pod", 2)
  58. if err := store.Update(pod2); err != nil {
  59. t.Errorf("unexpected error: %v", err)
  60. }
  61. if item, ok, _ := store.Get(pod2); !ok {
  62. t.Errorf("didn't find pod")
  63. } else {
  64. if !api.Semantic.DeepEqual(pod2, item) {
  65. t.Errorf("expected %v, got %v", pod1, item)
  66. }
  67. }
  68. pod3 := makeTestPod("pod", 3)
  69. if err := store.Delete(pod3); err != nil {
  70. t.Errorf("unexpected error: %v", err)
  71. }
  72. if _, ok, _ := store.Get(pod3); ok {
  73. t.Errorf("found pod")
  74. }
  75. // Test List.
  76. store.Add(makeTestPod("pod1", 4))
  77. store.Add(makeTestPod("pod2", 5))
  78. store.Add(makeTestPod("pod3", 6))
  79. {
  80. podNames := sets.String{}
  81. for _, item := range store.List() {
  82. podNames.Insert(item.(*api.Pod).ObjectMeta.Name)
  83. }
  84. if !podNames.HasAll("pod1", "pod2", "pod3") {
  85. t.Errorf("missing pods, found %v", podNames)
  86. }
  87. if len(podNames) != 3 {
  88. t.Errorf("found missing/extra items")
  89. }
  90. }
  91. // Test Replace.
  92. store.Replace([]interface{}{
  93. makeTestPod("pod4", 7),
  94. makeTestPod("pod5", 8),
  95. }, "8")
  96. {
  97. podNames := sets.String{}
  98. for _, item := range store.List() {
  99. podNames.Insert(item.(*api.Pod).ObjectMeta.Name)
  100. }
  101. if !podNames.HasAll("pod4", "pod5") {
  102. t.Errorf("missing pods, found %v", podNames)
  103. }
  104. if len(podNames) != 2 {
  105. t.Errorf("found missing/extra items")
  106. }
  107. }
  108. }
  109. func TestEvents(t *testing.T) {
  110. store := newTestWatchCache(5)
  111. store.Add(makeTestPod("pod", 3))
  112. // Test for Added event.
  113. {
  114. _, err := store.GetAllEventsSince(1)
  115. if err == nil {
  116. t.Errorf("expected error too old")
  117. }
  118. if _, ok := err.(*errors.StatusError); !ok {
  119. t.Errorf("expected error to be of type StatusError")
  120. }
  121. }
  122. {
  123. result, err := store.GetAllEventsSince(2)
  124. if err != nil {
  125. t.Errorf("unexpected error: %v", err)
  126. }
  127. if len(result) != 1 {
  128. t.Fatalf("unexpected events: %v", result)
  129. }
  130. if result[0].Type != watch.Added {
  131. t.Errorf("unexpected event type: %v", result[0].Type)
  132. }
  133. pod := makeTestPod("pod", uint64(3))
  134. if !api.Semantic.DeepEqual(pod, result[0].Object) {
  135. t.Errorf("unexpected item: %v, expected: %v", result[0].Object, pod)
  136. }
  137. if result[0].PrevObject != nil {
  138. t.Errorf("unexpected item: %v", result[0].PrevObject)
  139. }
  140. }
  141. store.Update(makeTestPod("pod", 4))
  142. store.Update(makeTestPod("pod", 5))
  143. // Test with not full cache.
  144. {
  145. _, err := store.GetAllEventsSince(1)
  146. if err == nil {
  147. t.Errorf("expected error too old")
  148. }
  149. }
  150. {
  151. result, err := store.GetAllEventsSince(3)
  152. if err != nil {
  153. t.Errorf("unexpected error: %v", err)
  154. }
  155. if len(result) != 2 {
  156. t.Fatalf("unexpected events: %v", result)
  157. }
  158. for i := 0; i < 2; i++ {
  159. if result[i].Type != watch.Modified {
  160. t.Errorf("unexpected event type: %v", result[i].Type)
  161. }
  162. pod := makeTestPod("pod", uint64(i+4))
  163. if !api.Semantic.DeepEqual(pod, result[i].Object) {
  164. t.Errorf("unexpected item: %v, expected: %v", result[i].Object, pod)
  165. }
  166. prevPod := makeTestPod("pod", uint64(i+3))
  167. if !api.Semantic.DeepEqual(prevPod, result[i].PrevObject) {
  168. t.Errorf("unexpected item: %v, expected: %v", result[i].PrevObject, prevPod)
  169. }
  170. }
  171. }
  172. for i := 6; i < 10; i++ {
  173. store.Update(makeTestPod("pod", uint64(i)))
  174. }
  175. // Test with full cache - there should be elements from 5 to 9.
  176. {
  177. _, err := store.GetAllEventsSince(3)
  178. if err == nil {
  179. t.Errorf("expected error too old")
  180. }
  181. }
  182. {
  183. result, err := store.GetAllEventsSince(4)
  184. if err != nil {
  185. t.Errorf("unexpected error: %v", err)
  186. }
  187. if len(result) != 5 {
  188. t.Fatalf("unexpected events: %v", result)
  189. }
  190. for i := 0; i < 5; i++ {
  191. pod := makeTestPod("pod", uint64(i+5))
  192. if !api.Semantic.DeepEqual(pod, result[i].Object) {
  193. t.Errorf("unexpected item: %v, expected: %v", result[i].Object, pod)
  194. }
  195. }
  196. }
  197. // Test for delete event.
  198. store.Delete(makeTestPod("pod", uint64(10)))
  199. {
  200. result, err := store.GetAllEventsSince(9)
  201. if err != nil {
  202. t.Errorf("unexpected error: %v", err)
  203. }
  204. if len(result) != 1 {
  205. t.Fatalf("unexpected events: %v", result)
  206. }
  207. if result[0].Type != watch.Deleted {
  208. t.Errorf("unexpected event type: %v", result[0].Type)
  209. }
  210. pod := makeTestPod("pod", uint64(10))
  211. if !api.Semantic.DeepEqual(pod, result[0].Object) {
  212. t.Errorf("unexpected item: %v, expected: %v", result[0].Object, pod)
  213. }
  214. prevPod := makeTestPod("pod", uint64(9))
  215. if !api.Semantic.DeepEqual(prevPod, result[0].PrevObject) {
  216. t.Errorf("unexpected item: %v, expected: %v", result[0].PrevObject, prevPod)
  217. }
  218. }
  219. }
  220. func TestWaitUntilFreshAndList(t *testing.T) {
  221. store := newTestWatchCache(3)
  222. // In background, update the store.
  223. go func() {
  224. store.Add(makeTestPod("foo", 2))
  225. store.Add(makeTestPod("bar", 5))
  226. }()
  227. list, resourceVersion, err := store.WaitUntilFreshAndList(5)
  228. if err != nil {
  229. t.Fatalf("unexpected error: %v", err)
  230. }
  231. if resourceVersion != 5 {
  232. t.Errorf("unexpected resourceVersion: %v, expected: 5", resourceVersion)
  233. }
  234. if len(list) != 2 {
  235. t.Errorf("unexpected list returned: %#v", list)
  236. }
  237. }
  238. func TestWaitUntilFreshAndListTimeout(t *testing.T) {
  239. store := newTestWatchCache(3)
  240. fc := store.clock.(*clock.FakeClock)
  241. // In background, step clock after the below call starts the timer.
  242. go func() {
  243. for !fc.HasWaiters() {
  244. time.Sleep(time.Millisecond)
  245. }
  246. fc.Step(MaximumListWait)
  247. // Add an object to make sure the test would
  248. // eventually fail instead of just waiting
  249. // forever.
  250. time.Sleep(30 * time.Second)
  251. store.Add(makeTestPod("bar", 5))
  252. }()
  253. _, _, err := store.WaitUntilFreshAndList(5)
  254. if err == nil {
  255. t.Fatalf("unexpected lack of timeout error")
  256. }
  257. }
  258. type testLW struct {
  259. ListFunc func(options api.ListOptions) (runtime.Object, error)
  260. WatchFunc func(options api.ListOptions) (watch.Interface, error)
  261. }
  262. func (t *testLW) List(options api.ListOptions) (runtime.Object, error) {
  263. return t.ListFunc(options)
  264. }
  265. func (t *testLW) Watch(options api.ListOptions) (watch.Interface, error) {
  266. return t.WatchFunc(options)
  267. }
  268. func TestReflectorForWatchCache(t *testing.T) {
  269. store := newTestWatchCache(5)
  270. {
  271. _, version, err := store.WaitUntilFreshAndList(0)
  272. if err != nil {
  273. t.Fatalf("unexpected error: %v", err)
  274. }
  275. if version != 0 {
  276. t.Errorf("unexpected resource version: %d", version)
  277. }
  278. }
  279. lw := &testLW{
  280. WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
  281. fw := watch.NewFake()
  282. go fw.Stop()
  283. return fw, nil
  284. },
  285. ListFunc: func(options api.ListOptions) (runtime.Object, error) {
  286. return &api.PodList{ListMeta: unversioned.ListMeta{ResourceVersion: "10"}}, nil
  287. },
  288. }
  289. r := cache.NewReflector(lw, &api.Pod{}, store, 0)
  290. r.ListAndWatch(wait.NeverStop)
  291. {
  292. _, version, err := store.WaitUntilFreshAndList(10)
  293. if err != nil {
  294. t.Fatalf("unexpected error: %v", err)
  295. }
  296. if version != 10 {
  297. t.Errorf("unexpected resource version: %d", version)
  298. }
  299. }
  300. }