deploymentmetrics_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 TestKubecostDeploymentCollector_Describe(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. disabledMetrics []string
  13. expectMetric bool
  14. }{
  15. {
  16. name: "deployment_match_labels enabled",
  17. disabledMetrics: []string{},
  18. expectMetric: true,
  19. },
  20. {
  21. name: "deployment_match_labels disabled",
  22. disabledMetrics: []string{"deployment_match_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. kdc := KubecostDeploymentCollector{
  32. KubeClusterCache: NewFakeDeploymentCache([]*clustercache.Deployment{}),
  33. metricsConfig: mc,
  34. }
  35. ch := make(chan *prometheus.Desc, 10)
  36. kdc.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 TestKubecostDeploymentCollector_Collect(t *testing.T) {
  52. tests := []struct {
  53. name string
  54. deployments []*clustercache.Deployment
  55. disabledMetrics []string
  56. expectedCount int
  57. }{
  58. {
  59. name: "single deployment with match labels",
  60. deployments: []*clustercache.Deployment{
  61. {
  62. UID: types.UID("test-uid-1"),
  63. Name: "test-deployment",
  64. Namespace: "default",
  65. MatchLabels: map[string]string{"app": "test", "version": "v1"},
  66. },
  67. },
  68. disabledMetrics: []string{},
  69. expectedCount: 1,
  70. },
  71. {
  72. name: "deployment without match labels",
  73. deployments: []*clustercache.Deployment{
  74. {
  75. UID: types.UID("test-uid-2"),
  76. Name: "empty-deployment",
  77. Namespace: "default",
  78. MatchLabels: map[string]string{},
  79. },
  80. },
  81. disabledMetrics: []string{},
  82. expectedCount: 0,
  83. },
  84. {
  85. name: "multiple deployments with match labels",
  86. deployments: []*clustercache.Deployment{
  87. {
  88. UID: types.UID("test-uid-3"),
  89. Name: "deployment1",
  90. Namespace: "ns1",
  91. MatchLabels: map[string]string{"app": "app1"},
  92. },
  93. {
  94. UID: types.UID("test-uid-4"),
  95. Name: "deployment2",
  96. Namespace: "ns2",
  97. MatchLabels: map[string]string{"component": "frontend", "tier": "web"},
  98. },
  99. },
  100. disabledMetrics: []string{},
  101. expectedCount: 2,
  102. },
  103. {
  104. name: "metric disabled",
  105. deployments: []*clustercache.Deployment{
  106. {
  107. UID: types.UID("test-uid-5"),
  108. Name: "test-deployment",
  109. Namespace: "default",
  110. MatchLabels: map[string]string{"app": "test"},
  111. },
  112. },
  113. disabledMetrics: []string{"deployment_match_labels"},
  114. expectedCount: 0,
  115. },
  116. {
  117. name: "mixed deployments with and without labels",
  118. deployments: []*clustercache.Deployment{
  119. {
  120. UID: types.UID("test-uid-6"),
  121. Name: "with-labels",
  122. Namespace: "default",
  123. MatchLabels: map[string]string{"app": "test"},
  124. },
  125. {
  126. UID: types.UID("test-uid-7"),
  127. Name: "without-labels",
  128. Namespace: "default",
  129. MatchLabels: map[string]string{},
  130. },
  131. },
  132. disabledMetrics: []string{},
  133. expectedCount: 1,
  134. },
  135. }
  136. for _, tt := range tests {
  137. t.Run(tt.name, func(t *testing.T) {
  138. mc := MetricsConfig{
  139. DisabledMetrics: tt.disabledMetrics,
  140. }
  141. kdc := KubecostDeploymentCollector{
  142. KubeClusterCache: NewFakeDeploymentCache(tt.deployments),
  143. metricsConfig: mc,
  144. }
  145. ch := make(chan prometheus.Metric, 10)
  146. kdc.Collect(ch)
  147. close(ch)
  148. count := 0
  149. for range ch {
  150. count++
  151. }
  152. if count != tt.expectedCount {
  153. t.Errorf("Expected %d metrics, got %d", tt.expectedCount, count)
  154. }
  155. })
  156. }
  157. }
  158. func TestDeploymentMatchLabelsMetric(t *testing.T) {
  159. labelNames := []string{"app", "version", "tier"}
  160. labelValues := []string{"myapp", "v2.0", "backend"}
  161. uid := "test-deployment-uid"
  162. metric := newDeploymentMatchLabelsMetric("test-deployment", "production", "deployment_match_labels", labelNames, labelValues, uid)
  163. // Test Desc method
  164. desc := metric.Desc()
  165. if desc == nil {
  166. t.Error("Expected non-nil descriptor")
  167. }
  168. // Test Write method
  169. var dtoMetric dto.Metric
  170. err := metric.Write(&dtoMetric)
  171. if err != nil {
  172. t.Errorf("Expected no error, got %v", err)
  173. }
  174. if dtoMetric.Gauge == nil {
  175. t.Error("Expected gauge metric")
  176. }
  177. if *dtoMetric.Gauge.Value != 1.0 {
  178. t.Errorf("Expected gauge value 1.0, got %f", *dtoMetric.Gauge.Value)
  179. }
  180. // Verify labels
  181. expectedLabels := map[string]string{
  182. "app": "myapp",
  183. "version": "v2.0",
  184. "tier": "backend",
  185. "deployment": "test-deployment",
  186. "namespace": "production",
  187. "uid": uid,
  188. }
  189. actualLabels := make(map[string]string)
  190. for _, label := range dtoMetric.Label {
  191. actualLabels[*label.Name] = *label.Value
  192. }
  193. for key, expectedValue := range expectedLabels {
  194. if actualValue, ok := actualLabels[key]; !ok {
  195. t.Errorf("Missing label %s", key)
  196. } else if actualValue != expectedValue {
  197. t.Errorf("Label %s: expected %s, got %s", key, expectedValue, actualValue)
  198. }
  199. }
  200. }
  201. func TestKubeDeploymentCollector_Describe(t *testing.T) {
  202. tests := []struct {
  203. name string
  204. disabledMetrics []string
  205. expectedCount int
  206. }{
  207. {
  208. name: "all metrics enabled",
  209. disabledMetrics: []string{},
  210. expectedCount: 2,
  211. },
  212. {
  213. name: "spec replicas disabled",
  214. disabledMetrics: []string{"kube_deployment_spec_replicas"},
  215. expectedCount: 1,
  216. },
  217. {
  218. name: "status replicas disabled",
  219. disabledMetrics: []string{"kube_deployment_status_replicas_available"},
  220. expectedCount: 1,
  221. },
  222. {
  223. name: "all metrics disabled",
  224. disabledMetrics: []string{"kube_deployment_spec_replicas", "kube_deployment_status_replicas_available"},
  225. expectedCount: 0,
  226. },
  227. }
  228. for _, tt := range tests {
  229. t.Run(tt.name, func(t *testing.T) {
  230. mc := MetricsConfig{
  231. DisabledMetrics: tt.disabledMetrics,
  232. }
  233. kdc := KubeDeploymentCollector{
  234. KubeClusterCache: NewFakeDeploymentCache([]*clustercache.Deployment{}),
  235. metricsConfig: mc,
  236. }
  237. ch := make(chan *prometheus.Desc, 10)
  238. kdc.Describe(ch)
  239. close(ch)
  240. count := 0
  241. for range ch {
  242. count++
  243. }
  244. if count != tt.expectedCount {
  245. t.Errorf("Expected %d metrics, got %d", tt.expectedCount, count)
  246. }
  247. })
  248. }
  249. }
  250. func TestKubeDeploymentCollector_Collect(t *testing.T) {
  251. replicas3 := int32(3)
  252. replicas0 := int32(0)
  253. tests := []struct {
  254. name string
  255. deployments []*clustercache.Deployment
  256. disabledMetrics []string
  257. expectedCount int
  258. }{
  259. {
  260. name: "deployment with explicit replicas",
  261. deployments: []*clustercache.Deployment{
  262. {
  263. UID: types.UID("test-uid-1"),
  264. Name: "test-deployment",
  265. Namespace: "default",
  266. SpecReplicas: &replicas3,
  267. StatusAvailableReplicas: 2,
  268. },
  269. },
  270. disabledMetrics: []string{},
  271. expectedCount: 2, // spec replicas + status available replicas
  272. },
  273. {
  274. name: "deployment with nil replicas defaults to 1",
  275. deployments: []*clustercache.Deployment{
  276. {
  277. UID: types.UID("test-uid-2"),
  278. Name: "default-replicas",
  279. Namespace: "default",
  280. SpecReplicas: nil,
  281. StatusAvailableReplicas: 1,
  282. },
  283. },
  284. disabledMetrics: []string{},
  285. expectedCount: 2,
  286. },
  287. {
  288. name: "deployment with zero replicas",
  289. deployments: []*clustercache.Deployment{
  290. {
  291. UID: types.UID("test-uid-3"),
  292. Name: "zero-replicas",
  293. Namespace: "default",
  294. SpecReplicas: &replicas0,
  295. StatusAvailableReplicas: 0,
  296. },
  297. },
  298. disabledMetrics: []string{},
  299. expectedCount: 2,
  300. },
  301. {
  302. name: "multiple deployments",
  303. deployments: []*clustercache.Deployment{
  304. {
  305. UID: types.UID("test-uid-4"),
  306. Name: "deployment1",
  307. Namespace: "ns1",
  308. SpecReplicas: &replicas3,
  309. StatusAvailableReplicas: 3,
  310. },
  311. {
  312. UID: types.UID("test-uid-5"),
  313. Name: "deployment2",
  314. Namespace: "ns2",
  315. SpecReplicas: nil,
  316. StatusAvailableReplicas: 0,
  317. },
  318. },
  319. disabledMetrics: []string{},
  320. expectedCount: 4, // 2 metrics per deployment
  321. },
  322. {
  323. name: "spec replicas disabled",
  324. deployments: []*clustercache.Deployment{
  325. {
  326. UID: types.UID("test-uid-6"),
  327. Name: "test-deployment",
  328. Namespace: "default",
  329. SpecReplicas: &replicas3,
  330. StatusAvailableReplicas: 2,
  331. },
  332. },
  333. disabledMetrics: []string{"kube_deployment_spec_replicas"},
  334. expectedCount: 1, // only status available replicas
  335. },
  336. {
  337. name: "status replicas disabled",
  338. deployments: []*clustercache.Deployment{
  339. {
  340. UID: types.UID("test-uid-7"),
  341. Name: "test-deployment",
  342. Namespace: "default",
  343. SpecReplicas: &replicas3,
  344. StatusAvailableReplicas: 2,
  345. },
  346. },
  347. disabledMetrics: []string{"kube_deployment_status_replicas_available"},
  348. expectedCount: 1, // only spec replicas
  349. },
  350. }
  351. for _, tt := range tests {
  352. t.Run(tt.name, func(t *testing.T) {
  353. mc := MetricsConfig{
  354. DisabledMetrics: tt.disabledMetrics,
  355. }
  356. kdc := KubeDeploymentCollector{
  357. KubeClusterCache: NewFakeDeploymentCache(tt.deployments),
  358. metricsConfig: mc,
  359. }
  360. ch := make(chan prometheus.Metric, 10)
  361. kdc.Collect(ch)
  362. close(ch)
  363. count := 0
  364. for range ch {
  365. count++
  366. }
  367. if count != tt.expectedCount {
  368. t.Errorf("Expected %d metrics, got %d", tt.expectedCount, count)
  369. }
  370. })
  371. }
  372. }
  373. func TestKubeDeploymentReplicasMetric(t *testing.T) {
  374. metric := newKubeDeploymentReplicasMetric("kube_deployment_spec_replicas", "web-app", "production", 5, "deployment-uid")
  375. // Test Desc method
  376. desc := metric.Desc()
  377. if desc == nil {
  378. t.Error("Expected non-nil descriptor")
  379. }
  380. // Test Write method
  381. var dtoMetric dto.Metric
  382. err := metric.Write(&dtoMetric)
  383. if err != nil {
  384. t.Errorf("Expected no error, got %v", err)
  385. }
  386. if dtoMetric.Gauge == nil {
  387. t.Error("Expected gauge metric")
  388. }
  389. if *dtoMetric.Gauge.Value != 5.0 {
  390. t.Errorf("Expected gauge value 5.0, got %f", *dtoMetric.Gauge.Value)
  391. }
  392. // Verify labels
  393. expectedLabels := map[string]string{
  394. "deployment": "web-app",
  395. "namespace": "production",
  396. "uid": "deployment-uid",
  397. }
  398. actualLabels := make(map[string]string)
  399. for _, label := range dtoMetric.Label {
  400. actualLabels[*label.Name] = *label.Value
  401. }
  402. for key, expectedValue := range expectedLabels {
  403. if actualValue, ok := actualLabels[key]; !ok {
  404. t.Errorf("Missing label %s", key)
  405. } else if actualValue != expectedValue {
  406. t.Errorf("Label %s: expected %s, got %s", key, expectedValue, actualValue)
  407. }
  408. }
  409. }
  410. func TestKubeDeploymentStatusAvailableReplicasMetric(t *testing.T) {
  411. metric := newKubeDeploymentStatusAvailableReplicasMetric("kube_deployment_status_replicas_available", "api-server", "backend", 3, "api-uid")
  412. // Test Desc method
  413. desc := metric.Desc()
  414. if desc == nil {
  415. t.Error("Expected non-nil descriptor")
  416. }
  417. // Test Write method
  418. var dtoMetric dto.Metric
  419. err := metric.Write(&dtoMetric)
  420. if err != nil {
  421. t.Errorf("Expected no error, got %v", err)
  422. }
  423. if dtoMetric.Gauge == nil {
  424. t.Error("Expected gauge metric")
  425. }
  426. if *dtoMetric.Gauge.Value != 3.0 {
  427. t.Errorf("Expected gauge value 3.0, got %f", *dtoMetric.Gauge.Value)
  428. }
  429. // Verify labels
  430. expectedLabels := map[string]string{
  431. "deployment": "api-server",
  432. "namespace": "backend",
  433. "uid": "api-uid",
  434. }
  435. actualLabels := make(map[string]string)
  436. for _, label := range dtoMetric.Label {
  437. actualLabels[*label.Name] = *label.Value
  438. }
  439. for key, expectedValue := range expectedLabels {
  440. if actualValue, ok := actualLabels[key]; !ok {
  441. t.Errorf("Missing label %s", key)
  442. } else if actualValue != expectedValue {
  443. t.Errorf("Label %s: expected %s, got %s", key, expectedValue, actualValue)
  444. }
  445. }
  446. }
  447. func TestKubeDeploymentCollector_DefaultReplicas(t *testing.T) {
  448. // Test that nil replicas defaults to 1
  449. deployment := &clustercache.Deployment{
  450. UID: types.UID("test-uid"),
  451. Name: "test-deployment",
  452. Namespace: "default",
  453. SpecReplicas: nil,
  454. StatusAvailableReplicas: 0,
  455. }
  456. mc := MetricsConfig{
  457. DisabledMetrics: []string{"kube_deployment_status_replicas_available"}, // Only test spec replicas
  458. }
  459. kdc := KubeDeploymentCollector{
  460. KubeClusterCache: NewFakeDeploymentCache([]*clustercache.Deployment{deployment}),
  461. metricsConfig: mc,
  462. }
  463. ch := make(chan prometheus.Metric, 10)
  464. kdc.Collect(ch)
  465. close(ch)
  466. for metric := range ch {
  467. var dtoMetric dto.Metric
  468. metric.Write(&dtoMetric)
  469. if *dtoMetric.Gauge.Value != 1.0 {
  470. t.Errorf("Expected default replicas value 1.0, got %f", *dtoMetric.Gauge.Value)
  471. }
  472. }
  473. }
  474. // FakeDeploymentCache implements ClusterCache interface for testing
  475. type FakeDeploymentCache struct {
  476. clustercache.ClusterCache
  477. deployments []*clustercache.Deployment
  478. }
  479. func (f FakeDeploymentCache) GetAllDeployments() []*clustercache.Deployment {
  480. return f.deployments
  481. }
  482. func NewFakeDeploymentCache(deployments []*clustercache.Deployment) FakeDeploymentCache {
  483. return FakeDeploymentCache{
  484. deployments: deployments,
  485. }
  486. }