promutil_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package promutil
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. )
  7. func checkSlice(s1, s2 []string) error {
  8. if len(s1) != len(s2) {
  9. return fmt.Errorf("len(s1) [%d] != len(s2) [%d]", len(s1), len(s2))
  10. }
  11. for i := 0; i < len(s1); i++ {
  12. if s1[i] != s2[i] {
  13. return fmt.Errorf("At Index: %d. Different Values %s (s1) != %s (s2)", i, s1[i], s2[i])
  14. }
  15. }
  16. return nil
  17. }
  18. func TestEmptyKubeLabelsToPromLabels(t *testing.T) {
  19. labels, values := KubeLabelsToLabels(nil)
  20. if len(labels) != 0 {
  21. t.Errorf("Labels length is non-zero\n")
  22. }
  23. if len(values) != 0 {
  24. t.Errorf("Values length is non-zero\n")
  25. }
  26. labels, values = KubeLabelsToLabels(map[string]string{})
  27. if len(labels) != 0 {
  28. t.Errorf("Labels length is non-zero\n")
  29. }
  30. if len(values) != 0 {
  31. t.Errorf("Values length is non-zero\n")
  32. }
  33. }
  34. func TestKubeLabelsToPromLabels(t *testing.T) {
  35. var expectedLabels []string = []string{
  36. "label_app",
  37. "label_chart",
  38. "label_control_plane",
  39. "label_gatekeeper_sh_operation",
  40. "label_heritage",
  41. "label_pod_template_hash",
  42. "label_release",
  43. }
  44. var expectedValues []string = []string{
  45. "gatekeeper",
  46. "gatekeeper",
  47. "audit-controller",
  48. "audit",
  49. "Helm",
  50. "5599859cd4",
  51. "gatekeeper",
  52. }
  53. kubeLabels := map[string]string{
  54. "app": "gatekeeper",
  55. "chart": "gatekeeper",
  56. "control-plane": "audit-controller",
  57. "gatekeeper.sh/operation": "audit",
  58. "heritage": "Helm",
  59. "pod-template-hash": "5599859cd4",
  60. "release": "gatekeeper",
  61. }
  62. labels, values := KubePrependQualifierToLabels(kubeLabels, "label_")
  63. l2, v2 := KubeLabelsToLabels(kubeLabels)
  64. // Check to make sure we get expected labels and values returned
  65. err := checkSlice(labels, expectedLabels)
  66. if err != nil {
  67. t.Errorf("%s", err)
  68. }
  69. err = checkSlice(values, expectedValues)
  70. if err != nil {
  71. t.Errorf("%s", err)
  72. }
  73. // Check to make sure the helper function returns what the prependqualifier func
  74. // returns
  75. err = checkSlice(l2, labels)
  76. if err != nil {
  77. t.Errorf("%s", err)
  78. }
  79. err = checkSlice(v2, values)
  80. if err != nil {
  81. t.Errorf("%s", err)
  82. }
  83. }
  84. func TestSanitizeLabels(t *testing.T) {
  85. type testCase struct {
  86. in map[string]string
  87. exp map[string]string
  88. }
  89. tcs := map[string]testCase{
  90. "empty labels": {
  91. in: map[string]string{},
  92. exp: map[string]string{},
  93. },
  94. "no op": {
  95. in: map[string]string{
  96. "foo": "bar",
  97. "baz": "loo",
  98. },
  99. exp: map[string]string{
  100. "foo": "bar",
  101. "baz": "loo",
  102. },
  103. },
  104. "modification, no collisions": {
  105. in: map[string]string{
  106. "foo-foo": "bar",
  107. "baz---baz": "loo",
  108. },
  109. exp: map[string]string{
  110. "foo_foo": "bar",
  111. "baz___baz": "loo",
  112. },
  113. },
  114. "modification, one collision": {
  115. in: map[string]string{
  116. "foo-foo": "bar",
  117. "foo+foo": "bar",
  118. "baz---baz": "loo",
  119. },
  120. exp: map[string]string{
  121. "foo_foo": "bar",
  122. "baz___baz": "loo",
  123. },
  124. },
  125. "modification, all collisions": {
  126. in: map[string]string{
  127. "foo-foo": "bar",
  128. "foo+foo": "bar",
  129. "foo_foo": "bar",
  130. },
  131. exp: map[string]string{
  132. "foo_foo": "bar",
  133. },
  134. },
  135. }
  136. for name, tc := range tcs {
  137. t.Run(name, func(t *testing.T) {
  138. act := SanitizeLabels(tc.in)
  139. if !reflect.DeepEqual(tc.exp, act) {
  140. t.Errorf("sanitizing labels failed for case %s: %+v != %+v", name, tc.exp, act)
  141. }
  142. })
  143. }
  144. }