helpers_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 unversioned
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/labels"
  19. )
  20. func TestLabelSelectorAsSelector(t *testing.T) {
  21. matchLabels := map[string]string{"foo": "bar"}
  22. matchExpressions := []LabelSelectorRequirement{{
  23. Key: "baz",
  24. Operator: LabelSelectorOpIn,
  25. Values: []string{"qux", "norf"},
  26. }}
  27. mustParse := func(s string) labels.Selector {
  28. out, e := labels.Parse(s)
  29. if e != nil {
  30. panic(e)
  31. }
  32. return out
  33. }
  34. tc := []struct {
  35. in *LabelSelector
  36. out labels.Selector
  37. expectErr bool
  38. }{
  39. {in: nil, out: labels.Nothing()},
  40. {in: &LabelSelector{}, out: labels.Everything()},
  41. {
  42. in: &LabelSelector{MatchLabels: matchLabels},
  43. out: mustParse("foo=bar"),
  44. },
  45. {
  46. in: &LabelSelector{MatchExpressions: matchExpressions},
  47. out: mustParse("baz in (norf,qux)"),
  48. },
  49. {
  50. in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions},
  51. out: mustParse("baz in (norf,qux),foo=bar"),
  52. },
  53. {
  54. in: &LabelSelector{
  55. MatchExpressions: []LabelSelectorRequirement{{
  56. Key: "baz",
  57. Operator: LabelSelectorOpExists,
  58. Values: []string{"qux", "norf"},
  59. }},
  60. },
  61. expectErr: true,
  62. },
  63. }
  64. for i, tc := range tc {
  65. out, err := LabelSelectorAsSelector(tc.in)
  66. if err == nil && tc.expectErr {
  67. t.Errorf("[%v]expected error but got none.", i)
  68. }
  69. if err != nil && !tc.expectErr {
  70. t.Errorf("[%v]did not expect error but got: %v", i, err)
  71. }
  72. if !reflect.DeepEqual(out, tc.out) {
  73. t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)
  74. }
  75. }
  76. }
  77. func TestLabelSelectorAsMap(t *testing.T) {
  78. matchLabels := map[string]string{"foo": "bar"}
  79. matchExpressions := func(operator LabelSelectorOperator, values []string) []LabelSelectorRequirement {
  80. return []LabelSelectorRequirement{{
  81. Key: "baz",
  82. Operator: operator,
  83. Values: values,
  84. }}
  85. }
  86. tests := []struct {
  87. in *LabelSelector
  88. out map[string]string
  89. errString string
  90. }{
  91. {in: nil, out: nil},
  92. {
  93. in: &LabelSelector{MatchLabels: matchLabels},
  94. out: map[string]string{"foo": "bar"},
  95. },
  96. {
  97. in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions(LabelSelectorOpIn, []string{"norf"})},
  98. out: map[string]string{"foo": "bar", "baz": "norf"},
  99. },
  100. {
  101. in: &LabelSelector{MatchExpressions: matchExpressions(LabelSelectorOpIn, []string{"norf"})},
  102. out: map[string]string{"baz": "norf"},
  103. },
  104. {
  105. in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions(LabelSelectorOpIn, []string{"norf", "qux"})},
  106. out: map[string]string{"foo": "bar"},
  107. errString: "without a single value cannot be converted",
  108. },
  109. {
  110. in: &LabelSelector{MatchExpressions: matchExpressions(LabelSelectorOpNotIn, []string{"norf", "qux"})},
  111. out: map[string]string{},
  112. errString: "cannot be converted",
  113. },
  114. {
  115. in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions(LabelSelectorOpExists, []string{})},
  116. out: map[string]string{"foo": "bar"},
  117. errString: "cannot be converted",
  118. },
  119. {
  120. in: &LabelSelector{MatchExpressions: matchExpressions(LabelSelectorOpDoesNotExist, []string{})},
  121. out: map[string]string{},
  122. errString: "cannot be converted",
  123. },
  124. }
  125. for i, tc := range tests {
  126. out, err := LabelSelectorAsMap(tc.in)
  127. if err == nil && len(tc.errString) > 0 {
  128. t.Errorf("[%v]expected error but got none.", i)
  129. continue
  130. }
  131. if err != nil && len(tc.errString) == 0 {
  132. t.Errorf("[%v]did not expect error but got: %v", i, err)
  133. continue
  134. }
  135. if err != nil && len(tc.errString) > 0 && !strings.Contains(err.Error(), tc.errString) {
  136. t.Errorf("[%v]expected error with %q but got: %v", i, tc.errString, err)
  137. continue
  138. }
  139. if !reflect.DeepEqual(out, tc.out) {
  140. t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)
  141. }
  142. }
  143. }