servicemetrics_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package metrics
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/clustercache"
  5. "github.com/prometheus/client_golang/prometheus"
  6. dto "github.com/prometheus/client_model/go"
  7. "k8s.io/apimachinery/pkg/types"
  8. )
  9. func TestKubecostServiceCollector_Describe(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. disabledMetrics []string
  13. expectMetric bool
  14. }{
  15. {
  16. name: "service_selector_labels enabled",
  17. disabledMetrics: []string{},
  18. expectMetric: true,
  19. },
  20. {
  21. name: "service_selector_labels disabled",
  22. disabledMetrics: []string{"service_selector_labels"},
  23. expectMetric: false,
  24. },
  25. }
  26. for _, tt := range tests {
  27. t.Run(tt.name, func(t *testing.T) {
  28. mc := MetricsConfig{
  29. DisabledMetrics: tt.disabledMetrics,
  30. }
  31. sc := KubecostServiceCollector{
  32. KubeClusterCache: NewFakeServiceCache([]*clustercache.Service{}),
  33. metricsConfig: mc,
  34. }
  35. ch := make(chan *prometheus.Desc, 10)
  36. sc.Describe(ch)
  37. close(ch)
  38. count := 0
  39. for range ch {
  40. count++
  41. }
  42. if tt.expectMetric && count == 0 {
  43. t.Error("Expected metric description but got none")
  44. }
  45. if !tt.expectMetric && count > 0 {
  46. t.Error("Expected no metric description but got some")
  47. }
  48. })
  49. }
  50. }
  51. func TestKubecostServiceCollector_Collect(t *testing.T) {
  52. tests := []struct {
  53. name string
  54. services []*clustercache.Service
  55. disabledMetrics []string
  56. expectedCount int
  57. }{
  58. {
  59. name: "single service with selector",
  60. services: []*clustercache.Service{
  61. {
  62. UID: types.UID("test-uid-1"),
  63. Name: "test-service",
  64. Namespace: "default",
  65. SpecSelector: map[string]string{"app": "test", "version": "v1"},
  66. },
  67. },
  68. disabledMetrics: []string{},
  69. expectedCount: 1,
  70. },
  71. {
  72. name: "service without selector",
  73. services: []*clustercache.Service{
  74. {
  75. UID: types.UID("test-uid-2"),
  76. Name: "headless-service",
  77. Namespace: "default",
  78. SpecSelector: map[string]string{},
  79. },
  80. },
  81. disabledMetrics: []string{},
  82. expectedCount: 0,
  83. },
  84. {
  85. name: "multiple services with selectors",
  86. services: []*clustercache.Service{
  87. {
  88. UID: types.UID("test-uid-3"),
  89. Name: "service1",
  90. Namespace: "ns1",
  91. SpecSelector: map[string]string{"app": "app1"},
  92. },
  93. {
  94. UID: types.UID("test-uid-4"),
  95. Name: "service2",
  96. Namespace: "ns2",
  97. SpecSelector: map[string]string{"component": "frontend"},
  98. },
  99. },
  100. disabledMetrics: []string{},
  101. expectedCount: 2,
  102. },
  103. {
  104. name: "metric disabled",
  105. services: []*clustercache.Service{
  106. {
  107. UID: types.UID("test-uid-5"),
  108. Name: "test-service",
  109. Namespace: "default",
  110. SpecSelector: map[string]string{"app": "test"},
  111. },
  112. },
  113. disabledMetrics: []string{"service_selector_labels"},
  114. expectedCount: 0,
  115. },
  116. }
  117. for _, tt := range tests {
  118. t.Run(tt.name, func(t *testing.T) {
  119. mc := MetricsConfig{
  120. DisabledMetrics: tt.disabledMetrics,
  121. }
  122. sc := KubecostServiceCollector{
  123. KubeClusterCache: NewFakeServiceCache(tt.services),
  124. metricsConfig: mc,
  125. }
  126. ch := make(chan prometheus.Metric, 10)
  127. sc.Collect(ch)
  128. close(ch)
  129. count := 0
  130. for range ch {
  131. count++
  132. }
  133. if count != tt.expectedCount {
  134. t.Errorf("Expected %d metrics, got %d", tt.expectedCount, count)
  135. }
  136. })
  137. }
  138. }
  139. func TestServiceSelectorLabelsMetric(t *testing.T) {
  140. labelNames := []string{"app", "version"}
  141. labelValues := []string{"test-app", "v1.0"}
  142. uid := "test-uid"
  143. metric := newServiceSelectorLabelsMetric("test-service", "default", "service_selector_labels", labelNames, labelValues, uid)
  144. // Test Desc method
  145. desc := metric.Desc()
  146. if desc == nil {
  147. t.Error("Expected non-nil descriptor")
  148. }
  149. // Test Write method
  150. var dtoMetric dto.Metric
  151. err := metric.Write(&dtoMetric)
  152. if err != nil {
  153. t.Errorf("Expected no error, got %v", err)
  154. }
  155. if dtoMetric.Gauge == nil {
  156. t.Error("Expected gauge metric")
  157. }
  158. if *dtoMetric.Gauge.Value != 1.0 {
  159. t.Errorf("Expected gauge value 1.0, got %f", *dtoMetric.Gauge.Value)
  160. }
  161. // Verify labels
  162. expectedLabels := map[string]string{
  163. "app": "test-app",
  164. "version": "v1.0",
  165. "service": "test-service",
  166. "namespace": "default",
  167. "uid": uid,
  168. }
  169. actualLabels := make(map[string]string)
  170. for _, label := range dtoMetric.Label {
  171. actualLabels[*label.Name] = *label.Value
  172. }
  173. for key, expectedValue := range expectedLabels {
  174. if actualValue, ok := actualLabels[key]; !ok {
  175. t.Errorf("Missing label %s", key)
  176. } else if actualValue != expectedValue {
  177. t.Errorf("Label %s: expected %s, got %s", key, expectedValue, actualValue)
  178. }
  179. }
  180. }
  181. func TestServiceSelectorLabelsMetric_EmptyLabels(t *testing.T) {
  182. metric := newServiceSelectorLabelsMetric("empty-service", "test-ns", "service_selector_labels", []string{}, []string{}, "empty-uid")
  183. var dtoMetric dto.Metric
  184. err := metric.Write(&dtoMetric)
  185. if err != nil {
  186. t.Errorf("Expected no error, got %v", err)
  187. }
  188. // Should still have the service metadata labels
  189. expectedCount := 3 // service, namespace, uid
  190. if len(dtoMetric.Label) != expectedCount {
  191. t.Errorf("Expected %d labels, got %d", expectedCount, len(dtoMetric.Label))
  192. }
  193. }
  194. // FakeServiceCache implements ClusterCache interface for testing
  195. type FakeServiceCache struct {
  196. clustercache.ClusterCache
  197. services []*clustercache.Service
  198. }
  199. func (f FakeServiceCache) GetAllServices() []*clustercache.Service {
  200. return f.services
  201. }
  202. func NewFakeServiceCache(services []*clustercache.Service) FakeServiceCache {
  203. return FakeServiceCache{
  204. services: services,
  205. }
  206. }