labels_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright 2020 The Rook Authors. All rights reserved.
  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 v1
  14. import (
  15. "encoding/json"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/yaml"
  20. )
  21. func TestCephLabelsMerge(t *testing.T) {
  22. // No Labels defined
  23. testLabels := LabelsSpec{}
  24. a := GetOSDLabels(testLabels)
  25. assert.Nil(t, a)
  26. // Only a specific component labels without "all"
  27. testLabels = LabelsSpec{
  28. "mgr": {"mgrkey": "mgrval"},
  29. "mon": {"monkey": "monval"},
  30. "osd": {"osdkey": "osdval"},
  31. "rgw": {"rgwkey": "rgwval"},
  32. "rbdmirror": {"rbdmirrorkey": "rbdmirrorval"},
  33. }
  34. a = GetMgrLabels(testLabels)
  35. assert.Equal(t, "mgrval", a["mgrkey"])
  36. assert.Equal(t, 1, len(a))
  37. a = GetMonLabels(testLabels)
  38. assert.Equal(t, "monval", a["monkey"])
  39. assert.Equal(t, 1, len(a))
  40. a = GetOSDLabels(testLabels)
  41. assert.Equal(t, "osdval", a["osdkey"])
  42. assert.Equal(t, 1, len(a))
  43. // No Labels matching the component
  44. testLabels = LabelsSpec{
  45. "mgr": {"mgrkey": "mgrval"},
  46. }
  47. a = GetMonLabels(testLabels)
  48. assert.Nil(t, a)
  49. // Merge with "all"
  50. testLabels = LabelsSpec{
  51. "all": {"allkey1": "allval1", "allkey2": "allval2"},
  52. "mgr": {"mgrkey": "mgrval"},
  53. }
  54. a = GetMonLabels(testLabels)
  55. assert.Equal(t, "allval1", a["allkey1"])
  56. assert.Equal(t, "allval2", a["allkey2"])
  57. assert.Equal(t, 2, len(a))
  58. a = GetMgrLabels(testLabels)
  59. assert.Equal(t, "mgrval", a["mgrkey"])
  60. assert.Equal(t, "allval1", a["allkey1"])
  61. assert.Equal(t, "allval2", a["allkey2"])
  62. assert.Equal(t, 3, len(a))
  63. }
  64. func TestLabelsSpec(t *testing.T) {
  65. specYaml := []byte(`
  66. mgr:
  67. foo: bar
  68. hello: world
  69. mon:
  70. `)
  71. // convert the raw spec yaml into JSON
  72. rawJSON, err := yaml.ToJSON(specYaml)
  73. assert.Nil(t, err)
  74. // unmarshal the JSON into a strongly typed Labels spec object
  75. var Labels LabelsSpec
  76. err = json.Unmarshal(rawJSON, &Labels)
  77. assert.Nil(t, err)
  78. // the unmarshalled Labels spec should equal the expected spec below
  79. expected := LabelsSpec{
  80. "mgr": map[string]string{
  81. "foo": "bar",
  82. "hello": "world",
  83. },
  84. "mon": nil,
  85. }
  86. assert.Equal(t, expected, Labels)
  87. }
  88. func TestLabelsApply(t *testing.T) {
  89. tcs := []struct {
  90. name string
  91. target *metav1.ObjectMeta
  92. input Labels
  93. expected Labels
  94. }{
  95. {
  96. name: "it should be able to update meta with no label",
  97. target: &metav1.ObjectMeta{},
  98. input: Labels{
  99. "foo": "bar",
  100. },
  101. expected: Labels{
  102. "foo": "bar",
  103. },
  104. },
  105. {
  106. name: "it should keep the original labels when new labels are set",
  107. target: &metav1.ObjectMeta{
  108. Labels: Labels{
  109. "foo": "bar",
  110. },
  111. },
  112. input: Labels{
  113. "hello": "world",
  114. },
  115. expected: Labels{
  116. "foo": "bar",
  117. "hello": "world",
  118. },
  119. },
  120. {
  121. name: "it should NOT overwrite the existing keys",
  122. target: &metav1.ObjectMeta{
  123. Labels: Labels{
  124. "foo": "bar",
  125. },
  126. },
  127. input: Labels{
  128. "foo": "baz",
  129. },
  130. expected: Labels{
  131. "foo": "bar",
  132. },
  133. },
  134. }
  135. for _, tc := range tcs {
  136. tc.input.ApplyToObjectMeta(tc.target)
  137. assert.Equal(t, map[string]string(tc.expected), tc.target.Labels)
  138. }
  139. }
  140. func TestLabelsOverwriteApply(t *testing.T) {
  141. tcs := []struct {
  142. name string
  143. target *metav1.ObjectMeta
  144. input Labels
  145. expected Labels
  146. }{
  147. {
  148. name: "it should be able to update meta with no label",
  149. target: &metav1.ObjectMeta{},
  150. input: Labels{
  151. "foo": "bar",
  152. },
  153. expected: Labels{
  154. "foo": "bar",
  155. },
  156. },
  157. {
  158. name: "it should keep the original labels when new labels are set",
  159. target: &metav1.ObjectMeta{
  160. Labels: Labels{
  161. "foo": "bar",
  162. },
  163. },
  164. input: Labels{
  165. "hello": "world",
  166. },
  167. expected: Labels{
  168. "foo": "bar",
  169. "hello": "world",
  170. },
  171. },
  172. {
  173. name: "it should overwrite the existing keys",
  174. target: &metav1.ObjectMeta{
  175. Labels: Labels{
  176. "foo": "bar",
  177. },
  178. },
  179. input: Labels{
  180. "foo": "baz",
  181. },
  182. expected: Labels{
  183. "foo": "baz",
  184. },
  185. },
  186. }
  187. for _, tc := range tcs {
  188. tc.input.OverwriteApplyToObjectMeta(tc.target)
  189. assert.Equal(t, map[string]string(tc.expected), tc.target.Labels)
  190. }
  191. }
  192. func TestLabelsMerge(t *testing.T) {
  193. testLabelsPart1 := Labels{
  194. "foo": "bar",
  195. "hello": "world",
  196. }
  197. testLabelsPart2 := Labels{
  198. "bar": "foo",
  199. "hello": "earth",
  200. }
  201. expected := map[string]string{
  202. "foo": "bar",
  203. "bar": "foo",
  204. "hello": "world",
  205. }
  206. assert.Equal(t, expected, map[string]string(testLabelsPart1.Merge(testLabelsPart2)))
  207. // Test that nil Labels can still be appended to
  208. testLabelsPart3 := Labels{
  209. "hello": "world",
  210. }
  211. var empty Labels
  212. assert.Equal(t, map[string]string(testLabelsPart3), map[string]string(empty.Merge(testLabelsPart3)))
  213. }