topologies_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright 2017 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 util
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apimachinery/pkg/selection"
  21. "k8s.io/apimachinery/pkg/util/sets"
  22. )
  23. func fakePod() *v1.Pod {
  24. return &v1.Pod{
  25. ObjectMeta: metav1.ObjectMeta{
  26. Name: "topologies_pod",
  27. Namespace: metav1.NamespaceDefault,
  28. UID: "551f5a43-9f2f-11e7-a589-fa163e148d75",
  29. },
  30. }
  31. }
  32. func TestGetNamespacesFromPodAffinityTerm(t *testing.T) {
  33. tests := []struct {
  34. name string
  35. podAffinityTerm *v1.PodAffinityTerm
  36. expectedValue sets.String
  37. }{
  38. {
  39. "podAffinityTerm_namespace_empty",
  40. &v1.PodAffinityTerm{},
  41. sets.String{metav1.NamespaceDefault: sets.Empty{}},
  42. },
  43. {
  44. "podAffinityTerm_namespace_not_empty",
  45. &v1.PodAffinityTerm{
  46. Namespaces: []string{metav1.NamespacePublic, metav1.NamespaceSystem},
  47. },
  48. sets.String{metav1.NamespacePublic: sets.Empty{}, metav1.NamespaceSystem: sets.Empty{}},
  49. },
  50. }
  51. for _, test := range tests {
  52. t.Run(test.name, func(t *testing.T) {
  53. realValue := GetNamespacesFromPodAffinityTerm(fakePod(), test.podAffinityTerm)
  54. assert.EqualValuesf(t, test.expectedValue, realValue, "Failed to test: %s", test.name)
  55. })
  56. }
  57. }
  58. func TestPodMatchesTermsNamespaceAndSelector(t *testing.T) {
  59. fakeNamespaces := sets.String{metav1.NamespacePublic: sets.Empty{}, metav1.NamespaceSystem: sets.Empty{}}
  60. fakeRequirement, _ := labels.NewRequirement("service", selection.In, []string{"topologies_service1", "topologies_service2"})
  61. fakeSelector := labels.NewSelector().Add(*fakeRequirement)
  62. tests := []struct {
  63. name string
  64. podNamespaces string
  65. podLabels map[string]string
  66. expectedResult bool
  67. }{
  68. {
  69. "namespace_not_in",
  70. metav1.NamespaceDefault,
  71. map[string]string{"service": "topologies_service1"},
  72. false,
  73. },
  74. {
  75. "label_not_match",
  76. metav1.NamespacePublic,
  77. map[string]string{"service": "topologies_service3"},
  78. false,
  79. },
  80. {
  81. "normal_case",
  82. metav1.NamespacePublic,
  83. map[string]string{"service": "topologies_service1"},
  84. true,
  85. },
  86. }
  87. for _, test := range tests {
  88. t.Run(test.name, func(t *testing.T) {
  89. fakeTestPod := fakePod()
  90. fakeTestPod.Namespace = test.podNamespaces
  91. fakeTestPod.Labels = test.podLabels
  92. realValue := PodMatchesTermsNamespaceAndSelector(fakeTestPod, fakeNamespaces, fakeSelector)
  93. assert.EqualValuesf(t, test.expectedResult, realValue, "Failed to test: %s", test.name)
  94. })
  95. }
  96. }
  97. func TestNodesHaveSameTopologyKey(t *testing.T) {
  98. tests := []struct {
  99. name string
  100. nodeA, nodeB *v1.Node
  101. topologyKey string
  102. expected bool
  103. }{
  104. {
  105. name: "nodeA{'a':'a'} vs. empty label in nodeB",
  106. nodeA: &v1.Node{
  107. ObjectMeta: metav1.ObjectMeta{
  108. Labels: map[string]string{
  109. "a": "a",
  110. },
  111. },
  112. },
  113. nodeB: &v1.Node{},
  114. expected: false,
  115. topologyKey: "a",
  116. },
  117. {
  118. name: "nodeA{'a':'a'} vs. nodeB{'a':'a'}",
  119. nodeA: &v1.Node{
  120. ObjectMeta: metav1.ObjectMeta{
  121. Labels: map[string]string{
  122. "a": "a",
  123. },
  124. },
  125. },
  126. nodeB: &v1.Node{
  127. ObjectMeta: metav1.ObjectMeta{
  128. Labels: map[string]string{
  129. "a": "a",
  130. },
  131. },
  132. },
  133. expected: true,
  134. topologyKey: "a",
  135. },
  136. {
  137. name: "nodeA{'a':''} vs. empty label in nodeB",
  138. nodeA: &v1.Node{
  139. ObjectMeta: metav1.ObjectMeta{
  140. Labels: map[string]string{
  141. "a": "",
  142. },
  143. },
  144. },
  145. nodeB: &v1.Node{},
  146. expected: false,
  147. topologyKey: "a",
  148. },
  149. {
  150. name: "nodeA{'a':''} vs. nodeB{'a':''}",
  151. nodeA: &v1.Node{
  152. ObjectMeta: metav1.ObjectMeta{
  153. Labels: map[string]string{
  154. "a": "",
  155. },
  156. },
  157. },
  158. nodeB: &v1.Node{
  159. ObjectMeta: metav1.ObjectMeta{
  160. Labels: map[string]string{
  161. "a": "",
  162. },
  163. },
  164. },
  165. expected: true,
  166. topologyKey: "a",
  167. },
  168. {
  169. name: "nodeA{'a':'a'} vs. nodeB{'a':'a'} by key{'b'}",
  170. nodeA: &v1.Node{
  171. ObjectMeta: metav1.ObjectMeta{
  172. Labels: map[string]string{
  173. "a": "a",
  174. },
  175. },
  176. },
  177. nodeB: &v1.Node{
  178. ObjectMeta: metav1.ObjectMeta{
  179. Labels: map[string]string{
  180. "a": "a",
  181. },
  182. },
  183. },
  184. expected: false,
  185. topologyKey: "b",
  186. },
  187. {
  188. name: "topologyKey empty",
  189. nodeA: &v1.Node{
  190. ObjectMeta: metav1.ObjectMeta{
  191. Labels: map[string]string{
  192. "a": "",
  193. },
  194. },
  195. },
  196. nodeB: &v1.Node{
  197. ObjectMeta: metav1.ObjectMeta{
  198. Labels: map[string]string{
  199. "a": "",
  200. },
  201. },
  202. },
  203. expected: false,
  204. topologyKey: "",
  205. },
  206. {
  207. name: "nodeA label nil vs. nodeB{'a':''} by key('a')",
  208. nodeA: &v1.Node{
  209. ObjectMeta: metav1.ObjectMeta{},
  210. },
  211. nodeB: &v1.Node{
  212. ObjectMeta: metav1.ObjectMeta{
  213. Labels: map[string]string{
  214. "a": "",
  215. },
  216. },
  217. },
  218. expected: false,
  219. topologyKey: "a",
  220. },
  221. {
  222. name: "nodeA{'a':''} vs. nodeB label is nil by key('a')",
  223. nodeA: &v1.Node{
  224. ObjectMeta: metav1.ObjectMeta{
  225. Labels: map[string]string{
  226. "a": "",
  227. },
  228. },
  229. },
  230. nodeB: &v1.Node{
  231. ObjectMeta: metav1.ObjectMeta{},
  232. },
  233. expected: false,
  234. topologyKey: "a",
  235. },
  236. }
  237. for _, test := range tests {
  238. t.Run(test.name, func(t *testing.T) {
  239. got := NodesHaveSameTopologyKey(test.nodeA, test.nodeB, test.topologyKey)
  240. assert.Equalf(t, test.expected, got, "Failed to test: %s", test.name)
  241. })
  242. }
  243. }