namespacemetrics_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 TestKubecostNamespaceCollector_Collect(t *testing.T) {
  10. // Test with namespace that has annotations
  11. cache := &clustercache.MockClusterCache{
  12. Namespaces: []*clustercache.Namespace{
  13. {
  14. Name: "test-ns",
  15. UID: types.UID("test-uid"),
  16. Annotations: map[string]string{"team": "backend"},
  17. },
  18. },
  19. }
  20. collector := KubecostNamespaceCollector{
  21. KubeClusterCache: cache,
  22. metricsConfig: MetricsConfig{},
  23. }
  24. ch := make(chan prometheus.Metric, 10)
  25. go func() {
  26. collector.Collect(ch)
  27. close(ch)
  28. }()
  29. count := 0
  30. for range ch {
  31. count++
  32. }
  33. if count != 1 {
  34. t.Errorf("Expected 1 metric, got %d", count)
  35. }
  36. }
  37. func TestKubeNamespaceCollector_Collect(t *testing.T) {
  38. // Test with namespace that has labels
  39. cache := &clustercache.MockClusterCache{
  40. Namespaces: []*clustercache.Namespace{
  41. {
  42. Name: "test-ns",
  43. UID: types.UID("test-uid"),
  44. Labels: map[string]string{"env": "prod"},
  45. },
  46. },
  47. }
  48. collector := KubeNamespaceCollector{
  49. KubeClusterCache: cache,
  50. metricsConfig: MetricsConfig{},
  51. }
  52. ch := make(chan prometheus.Metric, 10)
  53. go func() {
  54. collector.Collect(ch)
  55. close(ch)
  56. }()
  57. count := 0
  58. for range ch {
  59. count++
  60. }
  61. if count != 1 {
  62. t.Errorf("Expected 1 metric, got %d", count)
  63. }
  64. }
  65. func TestNamespaceAnnotationsMetric_Write(t *testing.T) {
  66. metric := newNamespaceAnnotationsMetric(
  67. "test_metric",
  68. "test-ns",
  69. "test-uid",
  70. []string{"team"},
  71. []string{"backend"},
  72. )
  73. pbMetric := &dto.Metric{}
  74. err := metric.Write(pbMetric)
  75. if err != nil {
  76. t.Fatalf("Write failed: %v", err)
  77. }
  78. if pbMetric.Gauge == nil || *pbMetric.Gauge.Value != 1.0 {
  79. t.Error("Expected gauge value 1.0")
  80. }
  81. if len(pbMetric.Label) != 3 { // team + namespace + uid
  82. t.Errorf("Expected 3 labels, got %d", len(pbMetric.Label))
  83. }
  84. // Verify UID label exists and has correct value
  85. foundUID := false
  86. for _, label := range pbMetric.Label {
  87. if *label.Name == "uid" && *label.Value == "test-uid" {
  88. foundUID = true
  89. break
  90. }
  91. }
  92. if !foundUID {
  93. t.Error("Expected uid label with value 'test-uid' not found")
  94. }
  95. }
  96. func TestKubeNamespaceLabelsMetric_Write(t *testing.T) {
  97. metric := newKubeNamespaceLabelsMetric(
  98. "test_metric",
  99. "test-ns",
  100. "test-uid",
  101. []string{"env"},
  102. []string{"prod"},
  103. )
  104. pbMetric := &dto.Metric{}
  105. err := metric.Write(pbMetric)
  106. if err != nil {
  107. t.Fatalf("Write failed: %v", err)
  108. }
  109. if pbMetric.Gauge == nil || *pbMetric.Gauge.Value != 1.0 {
  110. t.Error("Expected gauge value 1.0")
  111. }
  112. if len(pbMetric.Label) != 3 { // env + namespace + uid
  113. t.Errorf("Expected 3 labels, got %d", len(pbMetric.Label))
  114. }
  115. // Verify UID label exists and has correct value
  116. foundUID := false
  117. for _, label := range pbMetric.Label {
  118. if *label.Name == "uid" && *label.Value == "test-uid" {
  119. foundUID = true
  120. break
  121. }
  122. }
  123. if !foundUID {
  124. t.Error("Expected uid label with value 'test-uid' not found")
  125. }
  126. }
  127. func TestKubecostNamespaceCollector_Describe(t *testing.T) {
  128. collector := KubecostNamespaceCollector{metricsConfig: MetricsConfig{}}
  129. ch := make(chan *prometheus.Desc, 1)
  130. go func() {
  131. collector.Describe(ch)
  132. close(ch)
  133. }()
  134. count := 0
  135. for range ch {
  136. count++
  137. }
  138. if count != 1 {
  139. t.Errorf("Expected 1 descriptor, got %d", count)
  140. }
  141. }
  142. func TestKubeNamespaceCollector_Describe(t *testing.T) {
  143. collector := KubeNamespaceCollector{metricsConfig: MetricsConfig{}}
  144. ch := make(chan *prometheus.Desc, 1)
  145. go func() {
  146. collector.Describe(ch)
  147. close(ch)
  148. }()
  149. count := 0
  150. for range ch {
  151. count++
  152. }
  153. if count != 1 {
  154. t.Errorf("Expected 1 descriptor, got %d", count)
  155. }
  156. }