promutil_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package promutil
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "github.com/opencost/opencost/core/pkg/util/json"
  7. )
  8. func checkSlice(s1, s2 []string) error {
  9. if len(s1) != len(s2) {
  10. return fmt.Errorf("len(s1) [%d] != len(s2) [%d]", len(s1), len(s2))
  11. }
  12. for i := 0; i < len(s1); i++ {
  13. if s1[i] != s2[i] {
  14. return fmt.Errorf("At Index: %d. Different Values %s (s1) != %s (s2)", i, s1[i], s2[i])
  15. }
  16. }
  17. return nil
  18. }
  19. func TestEmptyKubeLabelsToPromLabels(t *testing.T) {
  20. labels, values := KubeLabelsToLabels(nil)
  21. if len(labels) != 0 {
  22. t.Errorf("Labels length is non-zero\n")
  23. }
  24. if len(values) != 0 {
  25. t.Errorf("Values length is non-zero\n")
  26. }
  27. labels, values = KubeLabelsToLabels(map[string]string{})
  28. if len(labels) != 0 {
  29. t.Errorf("Labels length is non-zero\n")
  30. }
  31. if len(values) != 0 {
  32. t.Errorf("Values length is non-zero\n")
  33. }
  34. }
  35. func TestKubeLabelsToPromLabels(t *testing.T) {
  36. var expectedLabels []string = []string{
  37. "label_app",
  38. "label_chart",
  39. "label_control_plane",
  40. "label_gatekeeper_sh_operation",
  41. "label_heritage",
  42. "label_pod_template_hash",
  43. "label_release",
  44. }
  45. var expectedValues []string = []string{
  46. "gatekeeper",
  47. "gatekeeper",
  48. "audit-controller",
  49. "audit",
  50. "Helm",
  51. "5599859cd4",
  52. "gatekeeper",
  53. }
  54. kubeLabels := map[string]string{
  55. "app": "gatekeeper",
  56. "chart": "gatekeeper",
  57. "control-plane": "audit-controller",
  58. "gatekeeper.sh/operation": "audit",
  59. "heritage": "Helm",
  60. "pod-template-hash": "5599859cd4",
  61. "release": "gatekeeper",
  62. }
  63. labels, values := KubePrependQualifierToLabels(kubeLabels, "label_")
  64. l2, v2 := KubeLabelsToLabels(kubeLabels)
  65. // Check to make sure we get expected labels and values returned
  66. err := checkSlice(labels, expectedLabels)
  67. if err != nil {
  68. t.Errorf("%s", err)
  69. }
  70. err = checkSlice(values, expectedValues)
  71. if err != nil {
  72. t.Errorf("%s", err)
  73. }
  74. // Check to make sure the helper function returns what the prependqualifier func
  75. // returns
  76. err = checkSlice(l2, labels)
  77. if err != nil {
  78. t.Errorf("%s", err)
  79. }
  80. err = checkSlice(v2, values)
  81. if err != nil {
  82. t.Errorf("%s", err)
  83. }
  84. }
  85. func TestKubePrependQualifierToLabelsDuplicates(t *testing.T) {
  86. // 7 expected labels/values
  87. expectedLabels := []string{
  88. "label_app_",
  89. "label_chart",
  90. "label_control_plane",
  91. "label_gatekeeper_sh_operation",
  92. "label_heritage",
  93. "label_pod_template_hash",
  94. "label_release",
  95. }
  96. expectedValues := []string{
  97. "gatekeeper",
  98. "gatekeeper",
  99. "audit-controller",
  100. "audit",
  101. "Helm",
  102. "5599859cd4",
  103. "gatekeeper",
  104. }
  105. // 8 input labels/values, with one duplicate label
  106. kubeLabels := map[string]string{
  107. // app- will be sanitized to app_
  108. "app-": "gatekeeper",
  109. "app_": "gatekeeper",
  110. "chart": "gatekeeper",
  111. "control-plane": "audit-controller",
  112. "gatekeeper.sh/operation": "audit",
  113. "heritage": "Helm",
  114. "pod-template-hash": "5599859cd4",
  115. "release": "gatekeeper",
  116. }
  117. labels, values := KubePrependQualifierToLabels(kubeLabels, "label_")
  118. // Check to make sure we get expected labels and values returned
  119. err := checkSlice(labels, expectedLabels)
  120. if err != nil {
  121. t.Errorf("%s", err)
  122. }
  123. err = checkSlice(values, expectedValues)
  124. if err != nil {
  125. t.Errorf("%s", err)
  126. }
  127. }
  128. func TestSanitizeLabels(t *testing.T) {
  129. type testCase struct {
  130. in map[string]string
  131. exp map[string]string
  132. }
  133. tcs := map[string]testCase{
  134. "empty labels": {
  135. in: map[string]string{},
  136. exp: map[string]string{},
  137. },
  138. "no op": {
  139. in: map[string]string{
  140. "foo": "bar",
  141. "baz": "loo",
  142. },
  143. exp: map[string]string{
  144. "foo": "bar",
  145. "baz": "loo",
  146. },
  147. },
  148. "modification, no collisions": {
  149. in: map[string]string{
  150. "foo-foo": "bar",
  151. "baz---baz": "loo",
  152. },
  153. exp: map[string]string{
  154. "foo_foo": "bar",
  155. "baz___baz": "loo",
  156. },
  157. },
  158. "modification, one collision": {
  159. in: map[string]string{
  160. "foo-foo": "bar",
  161. "foo+foo": "bar",
  162. "baz---baz": "loo",
  163. },
  164. exp: map[string]string{
  165. "foo_foo": "bar",
  166. "baz___baz": "loo",
  167. },
  168. },
  169. "modification, all collisions": {
  170. in: map[string]string{
  171. "foo-foo": "bar",
  172. "foo+foo": "bar",
  173. "foo_foo": "bar",
  174. },
  175. exp: map[string]string{
  176. "foo_foo": "bar",
  177. },
  178. },
  179. }
  180. for name, tc := range tcs {
  181. t.Run(name, func(t *testing.T) {
  182. act := SanitizeLabels(tc.in)
  183. if !reflect.DeepEqual(tc.exp, act) {
  184. t.Errorf("sanitizing labels failed for case %s: %+v != %+v", name, tc.exp, act)
  185. }
  186. })
  187. }
  188. }
  189. func TestClusterInfoLabels(t *testing.T) {
  190. expected := map[string]bool{"clusterprofile": true, "errorreporting": true, "id": true, "logcollection": true, "name": true, "productanalytics": true, "provider": true, "provisioner": true, "remotereadenabled": true, "thanosenabled": true, "valuesreporting": true, "version": true}
  191. clusterInfo := `{"clusterProfile":"production","errorReporting":"true","id":"cluster-one","logCollection":"true","name":"bolt-3","productAnalytics":"true","provider":"GCP","provisioner":"GKE","remoteReadEnabled":"false","thanosEnabled":"false","valuesReporting":"true","version":"1.14+"}`
  192. var m map[string]any
  193. err := json.Unmarshal([]byte(clusterInfo), &m)
  194. if err != nil {
  195. t.Errorf("Error: %s", err)
  196. return
  197. }
  198. labels := MapToLabels(m)
  199. for k := range expected {
  200. if _, ok := labels[k]; !ok {
  201. t.Errorf("Failed to locate key: \"%s\" in labels.", k)
  202. return
  203. }
  204. }
  205. }