labels_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://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 metrics
  14. import (
  15. "testing"
  16. "github.com/google/go-cmp/cmp"
  17. "github.com/prometheus/client_golang/prometheus"
  18. )
  19. func TestSetUpLabelNames(t *testing.T) {
  20. testCases := []struct {
  21. description string
  22. addKubeStandardLabels bool
  23. expectedNonConditionMetricLabelNames []string
  24. expectedConditionMetricLabelNames []string
  25. expectedNonConditionMetricLabels map[string]string
  26. expectedConditionMetricLabels map[string]string
  27. }{
  28. {
  29. description: "Add standard labels disabled",
  30. addKubeStandardLabels: false,
  31. expectedNonConditionMetricLabelNames: []string{
  32. "name",
  33. "namespace",
  34. },
  35. expectedConditionMetricLabelNames: []string{
  36. "name",
  37. "namespace",
  38. "condition",
  39. "status",
  40. },
  41. expectedNonConditionMetricLabels: map[string]string{
  42. "name": "",
  43. "namespace": "",
  44. },
  45. expectedConditionMetricLabels: map[string]string{
  46. "name": "",
  47. "namespace": "",
  48. "condition": "",
  49. "status": "",
  50. },
  51. },
  52. {
  53. description: "Add standard labels enabled",
  54. addKubeStandardLabels: true,
  55. expectedNonConditionMetricLabelNames: []string{
  56. "name",
  57. "namespace",
  58. "app_kubernetes_io_name",
  59. "app_kubernetes_io_instance",
  60. "app_kubernetes_io_version",
  61. "app_kubernetes_io_component",
  62. "app_kubernetes_io_part_of",
  63. "app_kubernetes_io_managed_by",
  64. },
  65. expectedConditionMetricLabelNames: []string{
  66. "name",
  67. "namespace",
  68. "condition",
  69. "status",
  70. "app_kubernetes_io_name",
  71. "app_kubernetes_io_instance",
  72. "app_kubernetes_io_version",
  73. "app_kubernetes_io_component",
  74. "app_kubernetes_io_part_of",
  75. "app_kubernetes_io_managed_by",
  76. },
  77. expectedNonConditionMetricLabels: map[string]string{
  78. "name": "",
  79. "namespace": "",
  80. "app_kubernetes_io_name": "",
  81. "app_kubernetes_io_instance": "",
  82. "app_kubernetes_io_version": "",
  83. "app_kubernetes_io_component": "",
  84. "app_kubernetes_io_part_of": "",
  85. "app_kubernetes_io_managed_by": "",
  86. },
  87. expectedConditionMetricLabels: map[string]string{
  88. "name": "",
  89. "namespace": "",
  90. "condition": "",
  91. "status": "",
  92. "app_kubernetes_io_name": "",
  93. "app_kubernetes_io_instance": "",
  94. "app_kubernetes_io_version": "",
  95. "app_kubernetes_io_component": "",
  96. "app_kubernetes_io_part_of": "",
  97. "app_kubernetes_io_managed_by": "",
  98. },
  99. },
  100. }
  101. for _, tc := range testCases {
  102. t.Run(tc.description, func(t *testing.T) {
  103. SetUpLabelNames(tc.addKubeStandardLabels)
  104. if diff := cmp.Diff(NonConditionMetricLabelNames, tc.expectedNonConditionMetricLabelNames); diff != "" {
  105. t.Errorf("NonConditionMetricLabelNames does not match the expected value. (-got +want)\n%s", diff)
  106. }
  107. if diff := cmp.Diff(ConditionMetricLabelNames, tc.expectedConditionMetricLabelNames); diff != "" {
  108. t.Errorf("ConditionMetricLabelNames does not match the expected value. (-got +want)\n%s", diff)
  109. }
  110. if diff := cmp.Diff(NonConditionMetricLabels, tc.expectedNonConditionMetricLabels); diff != "" {
  111. t.Errorf("NonConditionMetricLabels are not initialized with empty strings. (-got +want)\n%s", diff)
  112. }
  113. if diff := cmp.Diff(ConditionMetricLabels, tc.expectedConditionMetricLabels); diff != "" {
  114. t.Errorf("ConditionMetricLabels are not initialized with empty strings. (-got +want)\n%s", diff)
  115. }
  116. })
  117. }
  118. }
  119. func TestRefineLabels(t *testing.T) {
  120. testCases := []struct {
  121. description string
  122. promLabels prometheus.Labels
  123. newLabels map[string]string
  124. expectedRefinement prometheus.Labels
  125. }{
  126. {
  127. description: "No new labels",
  128. promLabels: prometheus.Labels{
  129. "label1": "value1",
  130. "label2": "value2",
  131. },
  132. newLabels: map[string]string{},
  133. expectedRefinement: prometheus.Labels{"label1": "value1", "label2": "value2"},
  134. },
  135. {
  136. description: "Add unregistered labels",
  137. promLabels: prometheus.Labels{
  138. "label1": "value1",
  139. "label2": "value2",
  140. },
  141. newLabels: map[string]string{
  142. "new_label1": "new_value1",
  143. "new_label2": "new_value2",
  144. },
  145. expectedRefinement: prometheus.Labels{
  146. "label1": "value1",
  147. "label2": "value2",
  148. },
  149. },
  150. {
  151. description: "Overwrite existing labels",
  152. promLabels: prometheus.Labels{
  153. "label1": "value1",
  154. "label2": "value2",
  155. },
  156. newLabels: map[string]string{
  157. "label1": "new_value1",
  158. "label2": "new_value2",
  159. },
  160. expectedRefinement: prometheus.Labels{
  161. "label1": "new_value1",
  162. "label2": "new_value2",
  163. },
  164. },
  165. {
  166. description: "Clean non-alphanumeric characters in new labels",
  167. promLabels: prometheus.Labels{
  168. "label_1": "value1",
  169. },
  170. newLabels: map[string]string{
  171. "label@1": "new_value",
  172. },
  173. expectedRefinement: prometheus.Labels{
  174. "label_1": "new_value",
  175. },
  176. },
  177. }
  178. for _, tc := range testCases {
  179. t.Run(tc.description, func(t *testing.T) {
  180. refinement := RefineLabels(tc.promLabels, tc.newLabels)
  181. if diff := cmp.Diff(refinement, tc.expectedRefinement); diff != "" {
  182. t.Errorf("Refinement does not match the expected value. (-got +want)\n%s", diff)
  183. }
  184. })
  185. }
  186. }