2
0

nodemetrics_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. v1 "k8s.io/api/core/v1"
  8. "k8s.io/apimachinery/pkg/api/resource"
  9. "k8s.io/apimachinery/pkg/types"
  10. )
  11. func TestKubeNodeCollector_Describe(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. disabledMetrics []string
  15. expectedCount int
  16. }{
  17. {
  18. name: "all metrics enabled",
  19. disabledMetrics: []string{},
  20. expectedCount: 8,
  21. },
  22. {
  23. name: "capacity metric disabled",
  24. disabledMetrics: []string{"kube_node_status_capacity"},
  25. expectedCount: 7,
  26. },
  27. {
  28. name: "all metrics disabled",
  29. disabledMetrics: []string{"kube_node_status_capacity", "kube_node_status_capacity_memory_bytes", "kube_node_status_capacity_cpu_cores", "kube_node_status_allocatable", "kube_node_status_allocatable_cpu_cores", "kube_node_status_allocatable_memory_bytes", "kube_node_labels", "kube_node_status_condition"},
  30. expectedCount: 0,
  31. },
  32. }
  33. for _, tt := range tests {
  34. t.Run(tt.name, func(t *testing.T) {
  35. mc := MetricsConfig{
  36. DisabledMetrics: tt.disabledMetrics,
  37. }
  38. nc := KubeNodeCollector{
  39. KubeClusterCache: NewFakeNodeCache([]*clustercache.Node{}),
  40. metricsConfig: mc,
  41. }
  42. ch := make(chan *prometheus.Desc, 10)
  43. nc.Describe(ch)
  44. close(ch)
  45. count := 0
  46. for range ch {
  47. count++
  48. }
  49. if count != tt.expectedCount {
  50. t.Errorf("Expected %d metrics, got %d", tt.expectedCount, count)
  51. }
  52. })
  53. }
  54. }
  55. func TestKubeNodeCollector_Collect(t *testing.T) {
  56. tests := []struct {
  57. name string
  58. nodes []*clustercache.Node
  59. disabledMetrics []string
  60. expectedCount int
  61. }{
  62. {
  63. name: "single node with resources",
  64. nodes: []*clustercache.Node{
  65. {
  66. UID: types.UID("node-uid-1"),
  67. Name: "node-1",
  68. Labels: map[string]string{
  69. "app": "test",
  70. },
  71. Status: v1.NodeStatus{
  72. Capacity: v1.ResourceList{
  73. v1.ResourceCPU: resource.MustParse("4"),
  74. v1.ResourceMemory: resource.MustParse("8Gi"),
  75. },
  76. Allocatable: v1.ResourceList{
  77. v1.ResourceCPU: resource.MustParse("3.8"),
  78. v1.ResourceMemory: resource.MustParse("7.5Gi"),
  79. },
  80. Conditions: []v1.NodeCondition{
  81. {
  82. Type: v1.NodeReady,
  83. Status: v1.ConditionTrue,
  84. },
  85. },
  86. },
  87. },
  88. },
  89. disabledMetrics: []string{},
  90. expectedCount: 12, // 2 capacity + 2 capacity specific + 2 allocatable + 2 allocatable specific + 1 labels + 3 conditions
  91. },
  92. {
  93. name: "multiple_nodes",
  94. nodes: []*clustercache.Node{
  95. {
  96. Name: "node-1",
  97. Labels: map[string]string{}, // Empty labels to avoid label metrics
  98. Status: v1.NodeStatus{
  99. Capacity: v1.ResourceList{
  100. v1.ResourceCPU: resource.MustParse("4"),
  101. v1.ResourceMemory: resource.MustParse("8Gi"),
  102. },
  103. Allocatable: v1.ResourceList{
  104. v1.ResourceCPU: resource.MustParse("3"),
  105. v1.ResourceMemory: resource.MustParse("7Gi"),
  106. },
  107. Conditions: []v1.NodeCondition{}, // Empty conditions to avoid condition metrics
  108. },
  109. UID: types.UID("test-node-1-uid"),
  110. },
  111. {
  112. Name: "node-2",
  113. Labels: map[string]string{}, // Empty labels to avoid label metrics
  114. Status: v1.NodeStatus{
  115. Capacity: v1.ResourceList{
  116. v1.ResourceCPU: resource.MustParse("4"),
  117. v1.ResourceMemory: resource.MustParse("8Gi"),
  118. },
  119. Allocatable: v1.ResourceList{
  120. v1.ResourceCPU: resource.MustParse("3"),
  121. v1.ResourceMemory: resource.MustParse("7Gi"),
  122. },
  123. Conditions: []v1.NodeCondition{}, // Empty conditions to avoid condition metrics
  124. },
  125. UID: types.UID("test-node-2-uid"),
  126. },
  127. },
  128. expectedCount: 18, // 9 metrics per node × 2 nodes
  129. },
  130. {
  131. name: "no nodes",
  132. nodes: []*clustercache.Node{},
  133. disabledMetrics: []string{},
  134. expectedCount: 0,
  135. },
  136. {
  137. name: "metrics disabled",
  138. nodes: []*clustercache.Node{
  139. {
  140. UID: types.UID("node-uid-1"),
  141. Name: "node-1",
  142. Status: v1.NodeStatus{
  143. Capacity: v1.ResourceList{
  144. v1.ResourceCPU: resource.MustParse("2"),
  145. },
  146. },
  147. },
  148. },
  149. disabledMetrics: []string{"kube_node_status_capacity", "kube_node_status_capacity_cpu_cores", "kube_node_labels"},
  150. expectedCount: 0,
  151. },
  152. }
  153. for _, tt := range tests {
  154. t.Run(tt.name, func(t *testing.T) {
  155. mc := MetricsConfig{
  156. DisabledMetrics: tt.disabledMetrics,
  157. }
  158. nc := KubeNodeCollector{
  159. KubeClusterCache: NewFakeNodeCache(tt.nodes),
  160. metricsConfig: mc,
  161. }
  162. ch := make(chan prometheus.Metric, 20)
  163. nc.Collect(ch)
  164. close(ch)
  165. count := 0
  166. for range ch {
  167. count++
  168. }
  169. if count != tt.expectedCount {
  170. t.Errorf("Expected %d metrics, got %d", tt.expectedCount, count)
  171. }
  172. })
  173. }
  174. }
  175. func TestKubeNodeStatusCapacityMetric(t *testing.T) {
  176. metric := newKubeNodeStatusCapacityMetric("kube_node_status_capacity", "test-node", "cpu", "core", "test-uid", 4.0)
  177. // Test Desc method
  178. desc := metric.Desc()
  179. if desc == nil {
  180. t.Error("Expected non-nil descriptor")
  181. }
  182. // Test Write method
  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. if dtoMetric.Gauge == nil {
  189. t.Error("Expected gauge metric")
  190. }
  191. if *dtoMetric.Gauge.Value != 4.0 {
  192. t.Errorf("Expected gauge value 4.0, got %f", *dtoMetric.Gauge.Value)
  193. }
  194. // Verify labels
  195. expectedLabels := map[string]string{
  196. "node": "test-node",
  197. "resource": "cpu",
  198. "unit": "core",
  199. "uid": "test-uid",
  200. }
  201. actualLabels := make(map[string]string)
  202. for _, label := range dtoMetric.Label {
  203. actualLabels[*label.Name] = *label.Value
  204. }
  205. for key, expectedValue := range expectedLabels {
  206. if actualValue, ok := actualLabels[key]; !ok {
  207. t.Errorf("Missing label %s", key)
  208. } else if actualValue != expectedValue {
  209. t.Errorf("Expected label %s=%s, got %s=%s", key, expectedValue, key, actualValue)
  210. }
  211. }
  212. }
  213. func TestKubeNodeLabelsMetric(t *testing.T) {
  214. labelNames := []string{"app", "version"}
  215. labelValues := []string{"test-app", "v1.0"}
  216. uid := "test-uid"
  217. metric := newKubeNodeLabelsMetric("test-node", "kube_node_labels", labelNames, labelValues, uid)
  218. // Test Desc method
  219. desc := metric.Desc()
  220. if desc == nil {
  221. t.Error("Expected non-nil descriptor")
  222. }
  223. // Test Write method
  224. var dtoMetric dto.Metric
  225. err := metric.Write(&dtoMetric)
  226. if err != nil {
  227. t.Errorf("Expected no error, got %v", err)
  228. }
  229. if dtoMetric.Gauge == nil {
  230. t.Error("Expected gauge metric")
  231. }
  232. if *dtoMetric.Gauge.Value != 1.0 {
  233. t.Errorf("Expected gauge value 1.0, got %f", *dtoMetric.Gauge.Value)
  234. }
  235. // Verify labels
  236. expectedLabels := map[string]string{
  237. "app": "test-app",
  238. "version": "v1.0",
  239. "node": "test-node",
  240. "uid": uid,
  241. }
  242. actualLabels := make(map[string]string)
  243. for _, label := range dtoMetric.Label {
  244. actualLabels[*label.Name] = *label.Value
  245. }
  246. for key, expectedValue := range expectedLabels {
  247. if actualValue, ok := actualLabels[key]; !ok {
  248. t.Errorf("Missing label %s", key)
  249. } else if actualValue != expectedValue {
  250. t.Errorf("Expected label %s=%s, got %s=%s", key, expectedValue, key, actualValue)
  251. }
  252. }
  253. }
  254. func TestKubeNodeStatusConditionMetric(t *testing.T) {
  255. metric := newKubeNodeStatusConditionMetric("test-node", "kube_node_status_condition", "Ready", "true", 1.0, "test-uid")
  256. // Test Desc method
  257. desc := metric.Desc()
  258. if desc == nil {
  259. t.Error("Expected non-nil descriptor")
  260. }
  261. // Test Write method
  262. var dtoMetric dto.Metric
  263. err := metric.Write(&dtoMetric)
  264. if err != nil {
  265. t.Errorf("Expected no error, got %v", err)
  266. }
  267. if dtoMetric.Gauge == nil {
  268. t.Error("Expected gauge metric")
  269. }
  270. if *dtoMetric.Gauge.Value != 1.0 {
  271. t.Errorf("Expected gauge value 1.0, got %f", *dtoMetric.Gauge.Value)
  272. }
  273. // Verify labels
  274. expectedLabels := map[string]string{
  275. "node": "test-node",
  276. "condition": "Ready",
  277. "status": "true",
  278. "uid": "test-uid",
  279. }
  280. actualLabels := make(map[string]string)
  281. for _, label := range dtoMetric.Label {
  282. actualLabels[*label.Name] = *label.Value
  283. }
  284. for key, expectedValue := range expectedLabels {
  285. if actualValue, ok := actualLabels[key]; !ok {
  286. t.Errorf("Missing label %s", key)
  287. } else if actualValue != expectedValue {
  288. t.Errorf("Expected label %s=%s, got %s=%s", key, expectedValue, key, actualValue)
  289. }
  290. }
  291. }
  292. func TestKubeNodeStatusCapacityMemoryBytesMetric(t *testing.T) {
  293. metric := newKubeNodeStatusCapacityMemoryBytesMetric("kube_node_status_capacity_memory_bytes", "test-node", "test-uid", 8589934592.0)
  294. // Test Desc method
  295. desc := metric.Desc()
  296. if desc == nil {
  297. t.Error("Expected non-nil descriptor")
  298. }
  299. // Test Write method
  300. var dtoMetric dto.Metric
  301. err := metric.Write(&dtoMetric)
  302. if err != nil {
  303. t.Errorf("Expected no error, got %v", err)
  304. }
  305. if dtoMetric.Gauge == nil {
  306. t.Error("Expected gauge metric")
  307. }
  308. if *dtoMetric.Gauge.Value != 8589934592.0 {
  309. t.Errorf("Expected gauge value 8589934592.0, got %f", *dtoMetric.Gauge.Value)
  310. }
  311. }
  312. func TestKubeNodeStatusCapacityCPUCoresMetric(t *testing.T) {
  313. metric := newKubeNodeStatusCapacityCPUCoresMetric("kube_node_status_capacity_cpu_cores", "test-node", "test-uid", 4.0)
  314. // Test Desc method
  315. desc := metric.Desc()
  316. if desc == nil {
  317. t.Error("Expected non-nil descriptor")
  318. }
  319. // Test Write method
  320. var dtoMetric dto.Metric
  321. err := metric.Write(&dtoMetric)
  322. if err != nil {
  323. t.Errorf("Expected no error, got %v", err)
  324. }
  325. if dtoMetric.Gauge == nil {
  326. t.Error("Expected gauge metric")
  327. }
  328. if *dtoMetric.Gauge.Value != 4.0 {
  329. t.Errorf("Expected gauge value 4.0, got %f", *dtoMetric.Gauge.Value)
  330. }
  331. }
  332. func TestGetConditions(t *testing.T) {
  333. tests := []struct {
  334. name string
  335. status v1.ConditionStatus
  336. expectedValues map[string]float64
  337. }{
  338. {
  339. name: "condition true",
  340. status: v1.ConditionTrue,
  341. expectedValues: map[string]float64{
  342. "true": 1.0,
  343. "false": 0.0,
  344. "unknown": 0.0,
  345. },
  346. },
  347. {
  348. name: "condition false",
  349. status: v1.ConditionFalse,
  350. expectedValues: map[string]float64{
  351. "true": 0.0,
  352. "false": 1.0,
  353. "unknown": 0.0,
  354. },
  355. },
  356. {
  357. name: "condition unknown",
  358. status: v1.ConditionUnknown,
  359. expectedValues: map[string]float64{
  360. "true": 0.0,
  361. "false": 0.0,
  362. "unknown": 1.0,
  363. },
  364. },
  365. }
  366. for _, tt := range tests {
  367. t.Run(tt.name, func(t *testing.T) {
  368. conditions := getConditions(tt.status)
  369. if len(conditions) != 3 {
  370. t.Errorf("Expected 3 conditions, got %d", len(conditions))
  371. }
  372. actualValues := make(map[string]float64)
  373. for _, cond := range conditions {
  374. actualValues[cond.status] = cond.value
  375. }
  376. for status, expectedValue := range tt.expectedValues {
  377. if actualValue, ok := actualValues[status]; !ok {
  378. t.Errorf("Missing status %s", status)
  379. } else if actualValue != expectedValue {
  380. t.Errorf("Expected status %s=%f, got %f", status, expectedValue, actualValue)
  381. }
  382. }
  383. })
  384. }
  385. }
  386. // FakeNodeCache implements ClusterCache interface for testing
  387. type FakeNodeCache struct {
  388. clustercache.ClusterCache
  389. nodes []*clustercache.Node
  390. }
  391. func (f FakeNodeCache) GetAllNodes() []*clustercache.Node {
  392. return f.nodes
  393. }
  394. func NewFakeNodeCache(nodes []*clustercache.Node) FakeNodeCache {
  395. return FakeNodeCache{
  396. nodes: nodes,
  397. }
  398. }