promutil_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 TestKubePrependQualifierToLabelsDuplicates(t *testing.T) {
  85. // 7 expected labels/values
  86. expectedLabels := []string{
  87. "label_app_",
  88. "label_chart",
  89. "label_control_plane",
  90. "label_gatekeeper_sh_operation",
  91. "label_heritage",
  92. "label_pod_template_hash",
  93. "label_release",
  94. }
  95. expectedValues := []string{
  96. "gatekeeper",
  97. "gatekeeper",
  98. "audit-controller",
  99. "audit",
  100. "Helm",
  101. "5599859cd4",
  102. "gatekeeper",
  103. }
  104. // 8 input labels/values, with one duplicate label
  105. kubeLabels := map[string]string{
  106. // app- will be sanitized to app_
  107. "app-": "gatekeeper",
  108. "app_": "gatekeeper",
  109. "chart": "gatekeeper",
  110. "control-plane": "audit-controller",
  111. "gatekeeper.sh/operation": "audit",
  112. "heritage": "Helm",
  113. "pod-template-hash": "5599859cd4",
  114. "release": "gatekeeper",
  115. }
  116. labels, values := KubePrependQualifierToLabels(kubeLabels, "label_")
  117. // Check to make sure we get expected labels and values returned
  118. err := checkSlice(labels, expectedLabels)
  119. if err != nil {
  120. t.Errorf("%s", err)
  121. }
  122. err = checkSlice(values, expectedValues)
  123. if err != nil {
  124. t.Errorf("%s", err)
  125. }
  126. }
  127. func TestSanitizeLabels(t *testing.T) {
  128. type testCase struct {
  129. in map[string]string
  130. exp map[string]string
  131. }
  132. tcs := map[string]testCase{
  133. "empty labels": {
  134. in: map[string]string{},
  135. exp: map[string]string{},
  136. },
  137. "no op": {
  138. in: map[string]string{
  139. "foo": "bar",
  140. "baz": "loo",
  141. },
  142. exp: map[string]string{
  143. "foo": "bar",
  144. "baz": "loo",
  145. },
  146. },
  147. "modification, no collisions": {
  148. in: map[string]string{
  149. "foo-foo": "bar",
  150. "baz---baz": "loo",
  151. },
  152. exp: map[string]string{
  153. "foo_foo": "bar",
  154. "baz___baz": "loo",
  155. },
  156. },
  157. "modification, one collision": {
  158. in: map[string]string{
  159. "foo-foo": "bar",
  160. "foo+foo": "bar",
  161. "baz---baz": "loo",
  162. },
  163. exp: map[string]string{
  164. "foo_foo": "bar",
  165. "baz___baz": "loo",
  166. },
  167. },
  168. "modification, all collisions": {
  169. in: map[string]string{
  170. "foo-foo": "bar",
  171. "foo+foo": "bar",
  172. "foo_foo": "bar",
  173. },
  174. exp: map[string]string{
  175. "foo_foo": "bar",
  176. },
  177. },
  178. }
  179. for name, tc := range tcs {
  180. t.Run(name, func(t *testing.T) {
  181. act := SanitizeLabels(tc.in)
  182. if !reflect.DeepEqual(tc.exp, act) {
  183. t.Errorf("sanitizing labels failed for case %s: %+v != %+v", name, tc.exp, act)
  184. }
  185. })
  186. }
  187. }