labels_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright 2016 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 labels
  14. import (
  15. "reflect"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func TestCloneAndAddLabel(t *testing.T) {
  20. labels := map[string]string{
  21. "foo1": "bar1",
  22. "foo2": "bar2",
  23. "foo3": "bar3",
  24. }
  25. cases := []struct {
  26. labels map[string]string
  27. labelKey string
  28. labelValue string
  29. want map[string]string
  30. }{
  31. {
  32. labels: labels,
  33. want: labels,
  34. },
  35. {
  36. labels: labels,
  37. labelKey: "foo4",
  38. labelValue: "42",
  39. want: map[string]string{
  40. "foo1": "bar1",
  41. "foo2": "bar2",
  42. "foo3": "bar3",
  43. "foo4": "42",
  44. },
  45. },
  46. }
  47. for _, tc := range cases {
  48. got := CloneAndAddLabel(tc.labels, tc.labelKey, tc.labelValue)
  49. if !reflect.DeepEqual(got, tc.want) {
  50. t.Errorf("[Add] got %v, want %v", got, tc.want)
  51. }
  52. // now test the inverse.
  53. got_rm := CloneAndRemoveLabel(got, tc.labelKey)
  54. if !reflect.DeepEqual(got_rm, tc.labels) {
  55. t.Errorf("[RM] got %v, want %v", got_rm, tc.labels)
  56. }
  57. }
  58. }
  59. func TestAddLabel(t *testing.T) {
  60. labels := map[string]string{
  61. "foo1": "bar1",
  62. "foo2": "bar2",
  63. "foo3": "bar3",
  64. }
  65. cases := []struct {
  66. labels map[string]string
  67. labelKey string
  68. labelValue string
  69. want map[string]string
  70. }{
  71. {
  72. labels: labels,
  73. want: labels,
  74. },
  75. {
  76. labels: labels,
  77. labelKey: "foo4",
  78. labelValue: "food",
  79. want: map[string]string{
  80. "foo1": "bar1",
  81. "foo2": "bar2",
  82. "foo3": "bar3",
  83. "foo4": "food",
  84. },
  85. },
  86. {
  87. labels: nil,
  88. labelKey: "foo4",
  89. labelValue: "food",
  90. want: map[string]string{
  91. "foo4": "food",
  92. },
  93. },
  94. }
  95. for _, tc := range cases {
  96. got := AddLabel(tc.labels, tc.labelKey, tc.labelValue)
  97. if !reflect.DeepEqual(got, tc.want) {
  98. t.Errorf("got %v, want %v", got, tc.want)
  99. }
  100. }
  101. }
  102. func TestCloneSelectorAndAddLabel(t *testing.T) {
  103. labels := map[string]string{
  104. "foo1": "bar1",
  105. "foo2": "bar2",
  106. "foo3": "bar3",
  107. }
  108. cases := []struct {
  109. labels map[string]string
  110. labelKey string
  111. labelValue string
  112. want map[string]string
  113. }{
  114. {
  115. labels: labels,
  116. want: labels,
  117. },
  118. {
  119. labels: labels,
  120. labelKey: "foo4",
  121. labelValue: "89",
  122. want: map[string]string{
  123. "foo1": "bar1",
  124. "foo2": "bar2",
  125. "foo3": "bar3",
  126. "foo4": "89",
  127. },
  128. },
  129. {
  130. labels: nil,
  131. labelKey: "foo4",
  132. labelValue: "12",
  133. want: map[string]string{
  134. "foo4": "12",
  135. },
  136. },
  137. }
  138. for _, tc := range cases {
  139. ls_in := metav1.LabelSelector{MatchLabels: tc.labels}
  140. ls_out := metav1.LabelSelector{MatchLabels: tc.want}
  141. got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue)
  142. if !reflect.DeepEqual(got, &ls_out) {
  143. t.Errorf("got %v, want %v", got, tc.want)
  144. }
  145. }
  146. }
  147. func TestAddLabelToSelector(t *testing.T) {
  148. labels := map[string]string{
  149. "foo1": "bar1",
  150. "foo2": "bar2",
  151. "foo3": "bar3",
  152. }
  153. cases := []struct {
  154. labels map[string]string
  155. labelKey string
  156. labelValue string
  157. want map[string]string
  158. }{
  159. {
  160. labels: labels,
  161. want: labels,
  162. },
  163. {
  164. labels: labels,
  165. labelKey: "foo4",
  166. labelValue: "89",
  167. want: map[string]string{
  168. "foo1": "bar1",
  169. "foo2": "bar2",
  170. "foo3": "bar3",
  171. "foo4": "89",
  172. },
  173. },
  174. {
  175. labels: nil,
  176. labelKey: "foo4",
  177. labelValue: "12",
  178. want: map[string]string{
  179. "foo4": "12",
  180. },
  181. },
  182. }
  183. for _, tc := range cases {
  184. ls_in := metav1.LabelSelector{MatchLabels: tc.labels}
  185. ls_out := metav1.LabelSelector{MatchLabels: tc.want}
  186. got := AddLabelToSelector(&ls_in, tc.labelKey, tc.labelValue)
  187. if !reflect.DeepEqual(got, &ls_out) {
  188. t.Errorf("got %v, want %v", got, tc.want)
  189. }
  190. }
  191. }