index_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2015 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 cache
  14. import (
  15. "strings"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. )
  19. func testIndexFunc(obj interface{}) ([]string, error) {
  20. pod := obj.(*api.Pod)
  21. return []string{pod.Labels["foo"]}, nil
  22. }
  23. func TestGetIndexFuncValues(t *testing.T) {
  24. index := NewIndexer(MetaNamespaceKeyFunc, Indexers{"testmodes": testIndexFunc})
  25. pod1 := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "one", Labels: map[string]string{"foo": "bar"}}}
  26. pod2 := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "two", Labels: map[string]string{"foo": "bar"}}}
  27. pod3 := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "tre", Labels: map[string]string{"foo": "biz"}}}
  28. index.Add(pod1)
  29. index.Add(pod2)
  30. index.Add(pod3)
  31. keys := index.ListIndexFuncValues("testmodes")
  32. if len(keys) != 2 {
  33. t.Errorf("Expected 2 keys but got %v", len(keys))
  34. }
  35. for _, key := range keys {
  36. if key != "bar" && key != "biz" {
  37. t.Errorf("Expected only 'bar' or 'biz' but got %s", key)
  38. }
  39. }
  40. }
  41. func testUsersIndexFunc(obj interface{}) ([]string, error) {
  42. pod := obj.(*api.Pod)
  43. usersString := pod.Annotations["users"]
  44. return strings.Split(usersString, ","), nil
  45. }
  46. func TestMultiIndexKeys(t *testing.T) {
  47. index := NewIndexer(MetaNamespaceKeyFunc, Indexers{"byUser": testUsersIndexFunc})
  48. pod1 := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "one", Annotations: map[string]string{"users": "ernie,bert"}}}
  49. pod2 := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "two", Annotations: map[string]string{"users": "bert,oscar"}}}
  50. pod3 := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "tre", Annotations: map[string]string{"users": "ernie,elmo"}}}
  51. index.Add(pod1)
  52. index.Add(pod2)
  53. index.Add(pod3)
  54. erniePods, err := index.ByIndex("byUser", "ernie")
  55. if err != nil {
  56. t.Errorf("unexpected error: %v", err)
  57. }
  58. if len(erniePods) != 2 {
  59. t.Errorf("Expected 2 pods but got %v", len(erniePods))
  60. }
  61. bertPods, err := index.ByIndex("byUser", "bert")
  62. if err != nil {
  63. t.Errorf("unexpected error: %v", err)
  64. }
  65. if len(bertPods) != 2 {
  66. t.Errorf("Expected 2 pods but got %v", len(bertPods))
  67. }
  68. oscarPods, err := index.ByIndex("byUser", "oscar")
  69. if err != nil {
  70. t.Errorf("unexpected error: %v", err)
  71. }
  72. if len(oscarPods) != 1 {
  73. t.Errorf("Expected 1 pods but got %v", len(erniePods))
  74. }
  75. ernieAndBertKeys, err := index.Index("byUser", pod1)
  76. if err != nil {
  77. t.Errorf("unexpected error: %v", err)
  78. }
  79. if len(ernieAndBertKeys) != 3 {
  80. t.Errorf("Expected 3 pods but got %v", len(ernieAndBertKeys))
  81. }
  82. index.Delete(pod3)
  83. erniePods, err = index.ByIndex("byUser", "ernie")
  84. if err != nil {
  85. t.Errorf("unexpected error: %v", err)
  86. }
  87. if len(erniePods) != 1 {
  88. t.Errorf("Expected 1 pods but got %v", len(erniePods))
  89. }
  90. elmoPods, err := index.ByIndex("byUser", "elmo")
  91. if err != nil {
  92. t.Errorf("unexpected error: %v", err)
  93. }
  94. if len(elmoPods) != 0 {
  95. t.Errorf("Expected 0 pods but got %v", len(elmoPods))
  96. }
  97. obj, err := api.Scheme.DeepCopy(pod2)
  98. if err != nil {
  99. t.Errorf("unexpected error: %v", err)
  100. }
  101. copyOfPod2 := obj.(*api.Pod)
  102. copyOfPod2.Annotations["users"] = "oscar"
  103. index.Update(copyOfPod2)
  104. bertPods, err = index.ByIndex("byUser", "bert")
  105. if err != nil {
  106. t.Errorf("unexpected error: %v", err)
  107. }
  108. if len(bertPods) != 1 {
  109. t.Errorf("Expected 1 pods but got %v", len(bertPods))
  110. }
  111. }