labels_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package metrics
  13. import (
  14. "testing"
  15. "github.com/google/go-cmp/cmp"
  16. "github.com/prometheus/client_golang/prometheus"
  17. )
  18. func TestSetUpLabelNames(t *testing.T) {
  19. testCases := []struct {
  20. description string
  21. addKubeStandardLabels bool
  22. expectedNonConditionMetricLabelNames []string
  23. expectedConditionMetricLabelNames []string
  24. expectedNonConditionMetricLabels map[string]string
  25. expectedConditionMetricLabels map[string]string
  26. }{
  27. {
  28. description: "Add standard labels disabled",
  29. addKubeStandardLabels: false,
  30. expectedNonConditionMetricLabelNames: []string{
  31. "name",
  32. "namespace",
  33. },
  34. expectedConditionMetricLabelNames: []string{
  35. "name",
  36. "namespace",
  37. "condition",
  38. "status",
  39. },
  40. expectedNonConditionMetricLabels: map[string]string{
  41. "name": "",
  42. "namespace": "",
  43. },
  44. expectedConditionMetricLabels: map[string]string{
  45. "name": "",
  46. "namespace": "",
  47. "condition": "",
  48. "status": "",
  49. },
  50. },
  51. {
  52. description: "Add standard labels enabled",
  53. addKubeStandardLabels: true,
  54. expectedNonConditionMetricLabelNames: []string{
  55. "name",
  56. "namespace",
  57. "app_kubernetes_io_name",
  58. "app_kubernetes_io_instance",
  59. "app_kubernetes_io_version",
  60. "app_kubernetes_io_component",
  61. "app_kubernetes_io_part_of",
  62. "app_kubernetes_io_managed_by",
  63. },
  64. expectedConditionMetricLabelNames: []string{
  65. "name",
  66. "namespace",
  67. "condition",
  68. "status",
  69. "app_kubernetes_io_name",
  70. "app_kubernetes_io_instance",
  71. "app_kubernetes_io_version",
  72. "app_kubernetes_io_component",
  73. "app_kubernetes_io_part_of",
  74. "app_kubernetes_io_managed_by",
  75. },
  76. expectedNonConditionMetricLabels: map[string]string{
  77. "name": "",
  78. "namespace": "",
  79. "app_kubernetes_io_name": "",
  80. "app_kubernetes_io_instance": "",
  81. "app_kubernetes_io_version": "",
  82. "app_kubernetes_io_component": "",
  83. "app_kubernetes_io_part_of": "",
  84. "app_kubernetes_io_managed_by": "",
  85. },
  86. expectedConditionMetricLabels: map[string]string{
  87. "name": "",
  88. "namespace": "",
  89. "condition": "",
  90. "status": "",
  91. "app_kubernetes_io_name": "",
  92. "app_kubernetes_io_instance": "",
  93. "app_kubernetes_io_version": "",
  94. "app_kubernetes_io_component": "",
  95. "app_kubernetes_io_part_of": "",
  96. "app_kubernetes_io_managed_by": "",
  97. },
  98. },
  99. }
  100. for _, tc := range testCases {
  101. t.Run(tc.description, func(t *testing.T) {
  102. SetUpLabelNames(tc.addKubeStandardLabels)
  103. if diff := cmp.Diff(NonConditionMetricLabelNames, tc.expectedNonConditionMetricLabelNames); diff != "" {
  104. t.Errorf("NonConditionMetricLabelNames does not match the expected value. (-got +want)\n%s", diff)
  105. }
  106. if diff := cmp.Diff(ConditionMetricLabelNames, tc.expectedConditionMetricLabelNames); diff != "" {
  107. t.Errorf("ConditionMetricLabelNames does not match the expected value. (-got +want)\n%s", diff)
  108. }
  109. if diff := cmp.Diff(NonConditionMetricLabels, tc.expectedNonConditionMetricLabels); diff != "" {
  110. t.Errorf("NonConditionMetricLabels are not initialized with empty strings. (-got +want)\n%s", diff)
  111. }
  112. if diff := cmp.Diff(ConditionMetricLabels, tc.expectedConditionMetricLabels); diff != "" {
  113. t.Errorf("ConditionMetricLabels are not initialized with empty strings. (-got +want)\n%s", diff)
  114. }
  115. })
  116. }
  117. }
  118. func TestRefineLabels(t *testing.T) {
  119. testCases := []struct {
  120. description string
  121. promLabels prometheus.Labels
  122. newLabels map[string]string
  123. expectedRefinement prometheus.Labels
  124. }{
  125. {
  126. description: "No new labels",
  127. promLabels: prometheus.Labels{
  128. "label1": "value1",
  129. "label2": "value2",
  130. },
  131. newLabels: map[string]string{},
  132. expectedRefinement: prometheus.Labels{"label1": "value1", "label2": "value2"},
  133. },
  134. {
  135. description: "Add unregistered labels",
  136. promLabels: prometheus.Labels{
  137. "label1": "value1",
  138. "label2": "value2",
  139. },
  140. newLabels: map[string]string{
  141. "new_label1": "new_value1",
  142. "new_label2": "new_value2",
  143. },
  144. expectedRefinement: prometheus.Labels{
  145. "label1": "value1",
  146. "label2": "value2",
  147. },
  148. },
  149. {
  150. description: "Overwrite existing labels",
  151. promLabels: prometheus.Labels{
  152. "label1": "value1",
  153. "label2": "value2",
  154. },
  155. newLabels: map[string]string{
  156. "label1": "new_value1",
  157. "label2": "new_value2",
  158. },
  159. expectedRefinement: prometheus.Labels{
  160. "label1": "new_value1",
  161. "label2": "new_value2",
  162. },
  163. },
  164. {
  165. description: "Clean non-alphanumeric characters in new labels",
  166. promLabels: prometheus.Labels{
  167. "label_1": "value1",
  168. },
  169. newLabels: map[string]string{
  170. "label@1": "new_value",
  171. },
  172. expectedRefinement: prometheus.Labels{
  173. "label_1": "new_value",
  174. },
  175. },
  176. }
  177. for _, tc := range testCases {
  178. t.Run(tc.description, func(t *testing.T) {
  179. refinement := RefineLabels(tc.promLabels, tc.newLabels)
  180. if diff := cmp.Diff(refinement, tc.expectedRefinement); diff != "" {
  181. t.Errorf("Refinement does not match the expected value. (-got +want)\n%s", diff)
  182. }
  183. })
  184. }
  185. }