statsummary_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package scrape
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/source"
  8. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  9. "github.com/opencost/opencost/modules/collector-source/pkg/util"
  10. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  11. stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
  12. )
  13. type mockStatSummaryClient struct {
  14. results []*stats.Summary
  15. err error
  16. }
  17. func (m *mockStatSummaryClient) GetNodeData() ([]*stats.Summary, error) {
  18. return m.results, m.err
  19. }
  20. func TestStatScraper_Scrape(t *testing.T) {
  21. start1, _ := time.Parse(time.RFC3339, Start1Str)
  22. tests := map[string]struct {
  23. summaries []*stats.Summary
  24. err error
  25. expected []metric.Update
  26. }{
  27. "nil values": {
  28. summaries: []*stats.Summary{
  29. {
  30. Node: stats.NodeStats{
  31. NodeName: "node1",
  32. CPU: &stats.CPUStats{
  33. Time: metav1.Time{Time: start1},
  34. UsageCoreNanoSeconds: nil,
  35. },
  36. Fs: &stats.FsStats{
  37. Time: metav1.Time{Time: start1},
  38. CapacityBytes: nil,
  39. },
  40. },
  41. Pods: []stats.PodStats{
  42. {
  43. PodRef: stats.PodReference{
  44. Name: "pod1",
  45. Namespace: "namespace1",
  46. UID: "uid1",
  47. },
  48. Network: &stats.NetworkStats{
  49. Time: metav1.Time{Time: start1},
  50. InterfaceStats: stats.InterfaceStats{
  51. RxBytes: nil,
  52. TxBytes: nil,
  53. },
  54. },
  55. VolumeStats: []stats.VolumeStats{
  56. {
  57. Name: "vol1",
  58. PVCRef: &stats.PVCReference{
  59. Namespace: "namespace1",
  60. Name: "pvc1",
  61. },
  62. FsStats: stats.FsStats{
  63. Time: metav1.Time{Time: start1},
  64. UsedBytes: nil,
  65. },
  66. },
  67. },
  68. Containers: []stats.ContainerStats{
  69. {
  70. Name: "container1",
  71. CPU: &stats.CPUStats{
  72. Time: metav1.Time{Time: start1},
  73. UsageCoreNanoSeconds: nil,
  74. },
  75. Memory: &stats.MemoryStats{
  76. Time: metav1.Time{Time: start1},
  77. WorkingSetBytes: nil,
  78. },
  79. Rootfs: &stats.FsStats{
  80. Time: metav1.Time{Time: start1},
  81. UsedBytes: nil,
  82. },
  83. },
  84. },
  85. },
  86. },
  87. },
  88. },
  89. expected: []metric.Update{},
  90. },
  91. "nil structs": {
  92. summaries: []*stats.Summary{
  93. {
  94. Node: stats.NodeStats{
  95. NodeName: "node1",
  96. CPU: nil,
  97. Fs: nil,
  98. },
  99. Pods: []stats.PodStats{
  100. {
  101. PodRef: stats.PodReference{
  102. Name: "pod1",
  103. Namespace: "namespace1",
  104. UID: "uid1",
  105. },
  106. Network: nil,
  107. VolumeStats: nil,
  108. Containers: []stats.ContainerStats{
  109. {
  110. Name: "container1",
  111. CPU: nil,
  112. Memory: nil,
  113. Rootfs: nil,
  114. },
  115. },
  116. },
  117. },
  118. },
  119. },
  120. expected: []metric.Update{},
  121. },
  122. "single node": {
  123. summaries: []*stats.Summary{
  124. {
  125. Node: stats.NodeStats{
  126. NodeName: "node1",
  127. CPU: &stats.CPUStats{
  128. Time: metav1.Time{Time: start1},
  129. UsageCoreNanoSeconds: util.Ptr(uint64(2000000000)),
  130. },
  131. Fs: &stats.FsStats{
  132. Time: metav1.Time{Time: start1},
  133. CapacityBytes: util.Ptr(uint64(2 * util.GB)),
  134. },
  135. },
  136. Pods: []stats.PodStats{
  137. {
  138. PodRef: stats.PodReference{
  139. Name: "pod1",
  140. Namespace: "namespace1",
  141. UID: "uid1",
  142. },
  143. Network: &stats.NetworkStats{
  144. Time: metav1.Time{Time: start1},
  145. InterfaceStats: stats.InterfaceStats{
  146. RxBytes: util.Ptr(uint64(1 * util.MB)),
  147. TxBytes: util.Ptr(uint64(2 * util.MB)),
  148. },
  149. },
  150. VolumeStats: []stats.VolumeStats{
  151. {
  152. Name: "ignoreVol1",
  153. FsStats: stats.FsStats{
  154. Time: metav1.Time{Time: start1},
  155. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  156. },
  157. },
  158. {
  159. Name: "vol1",
  160. PVCRef: &stats.PVCReference{
  161. Namespace: "namespace1",
  162. Name: "pvc1",
  163. },
  164. FsStats: stats.FsStats{
  165. Time: metav1.Time{Time: start1},
  166. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  167. },
  168. },
  169. },
  170. Containers: []stats.ContainerStats{
  171. {
  172. Name: "container1",
  173. CPU: &stats.CPUStats{
  174. Time: metav1.Time{Time: start1},
  175. UsageCoreNanoSeconds: util.Ptr(uint64(1000000000)),
  176. },
  177. Memory: &stats.MemoryStats{
  178. Time: metav1.Time{Time: start1},
  179. WorkingSetBytes: util.Ptr(uint64(5 * util.MB)),
  180. },
  181. Rootfs: &stats.FsStats{
  182. Time: metav1.Time{Time: start1},
  183. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  184. },
  185. },
  186. },
  187. },
  188. },
  189. },
  190. },
  191. expected: []metric.Update{
  192. {
  193. Name: metric.NodeCPUSecondsTotal,
  194. Labels: map[string]string{
  195. source.KubernetesNodeLabel: "node1",
  196. source.ModeLabel: "",
  197. },
  198. Value: 2,
  199. },
  200. {
  201. Name: metric.NodeFSCapacityBytes,
  202. Labels: map[string]string{
  203. source.InstanceLabel: "node1",
  204. source.DeviceLabel: "local",
  205. },
  206. Value: float64(2 * util.GB),
  207. },
  208. {
  209. Name: metric.ContainerNetworkReceiveBytesTotal,
  210. Labels: map[string]string{
  211. source.UIDLabel: "uid1",
  212. source.PodLabel: "pod1",
  213. source.NamespaceLabel: "namespace1",
  214. },
  215. Value: float64(1 * util.MB),
  216. },
  217. {
  218. Name: metric.ContainerNetworkTransmitBytesTotal,
  219. Labels: map[string]string{
  220. source.UIDLabel: "uid1",
  221. source.PodLabel: "pod1",
  222. source.NamespaceLabel: "namespace1",
  223. },
  224. Value: float64(2 * util.MB),
  225. },
  226. {
  227. Name: metric.KubeletVolumeStatsUsedBytes,
  228. Labels: map[string]string{
  229. source.PVCLabel: "pvc1",
  230. source.NamespaceLabel: "namespace1",
  231. source.UIDLabel: "uid1",
  232. },
  233. Value: float64(1 * util.GB),
  234. },
  235. {
  236. Name: metric.ContainerCPUUsageSecondsTotal,
  237. Labels: map[string]string{
  238. source.ContainerLabel: "container1",
  239. source.PodLabel: "pod1",
  240. source.NamespaceLabel: "namespace1",
  241. source.NodeLabel: "node1",
  242. source.InstanceLabel: "node1",
  243. source.UIDLabel: "uid1",
  244. },
  245. Value: 1,
  246. },
  247. {
  248. Name: metric.ContainerMemoryWorkingSetBytes,
  249. Labels: map[string]string{
  250. source.ContainerLabel: "container1",
  251. source.PodLabel: "pod1",
  252. source.NamespaceLabel: "namespace1",
  253. source.NodeLabel: "node1",
  254. source.InstanceLabel: "node1",
  255. source.UIDLabel: "uid1",
  256. },
  257. Value: float64(5 * util.MB),
  258. },
  259. {
  260. Name: metric.ContainerFSUsageBytes,
  261. Labels: map[string]string{
  262. source.InstanceLabel: "node1",
  263. source.DeviceLabel: "local",
  264. source.UIDLabel: "uid1",
  265. },
  266. Value: float64(1 * util.GB),
  267. },
  268. },
  269. },
  270. "single node with error": {
  271. summaries: []*stats.Summary{
  272. {
  273. Node: stats.NodeStats{
  274. NodeName: "node1",
  275. CPU: &stats.CPUStats{
  276. Time: metav1.Time{Time: start1},
  277. UsageCoreNanoSeconds: util.Ptr(uint64(2000000000)),
  278. },
  279. Fs: &stats.FsStats{
  280. Time: metav1.Time{Time: start1},
  281. CapacityBytes: util.Ptr(uint64(2 * util.GB)),
  282. },
  283. },
  284. Pods: []stats.PodStats{
  285. {
  286. PodRef: stats.PodReference{
  287. Name: "pod1",
  288. Namespace: "namespace1",
  289. UID: "uid1",
  290. },
  291. Network: &stats.NetworkStats{
  292. Time: metav1.Time{Time: start1},
  293. InterfaceStats: stats.InterfaceStats{
  294. RxBytes: util.Ptr(uint64(1 * util.MB)),
  295. TxBytes: util.Ptr(uint64(2 * util.MB)),
  296. },
  297. },
  298. VolumeStats: []stats.VolumeStats{
  299. {
  300. Name: "ignoreVol1",
  301. FsStats: stats.FsStats{
  302. Time: metav1.Time{Time: start1},
  303. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  304. },
  305. },
  306. {
  307. Name: "vol1",
  308. PVCRef: &stats.PVCReference{
  309. Namespace: "namespace1",
  310. Name: "pvc1",
  311. },
  312. FsStats: stats.FsStats{
  313. Time: metav1.Time{Time: start1},
  314. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  315. },
  316. },
  317. },
  318. Containers: []stats.ContainerStats{
  319. {
  320. Name: "container1",
  321. CPU: &stats.CPUStats{
  322. Time: metav1.Time{Time: start1},
  323. UsageCoreNanoSeconds: util.Ptr(uint64(1000000000)),
  324. },
  325. Memory: &stats.MemoryStats{
  326. Time: metav1.Time{Time: start1},
  327. WorkingSetBytes: util.Ptr(uint64(5 * util.MB)),
  328. },
  329. Rootfs: &stats.FsStats{
  330. Time: metav1.Time{Time: start1},
  331. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  332. },
  333. },
  334. },
  335. },
  336. },
  337. },
  338. },
  339. err: fmt.Errorf("failed to retrieve node-XYZ"),
  340. expected: []metric.Update{
  341. {
  342. Name: metric.NodeCPUSecondsTotal,
  343. Labels: map[string]string{
  344. source.KubernetesNodeLabel: "node1",
  345. source.ModeLabel: "",
  346. },
  347. Value: 2,
  348. },
  349. {
  350. Name: metric.NodeFSCapacityBytes,
  351. Labels: map[string]string{
  352. source.InstanceLabel: "node1",
  353. source.DeviceLabel: "local",
  354. },
  355. Value: float64(2 * util.GB),
  356. },
  357. {
  358. Name: metric.ContainerNetworkReceiveBytesTotal,
  359. Labels: map[string]string{
  360. source.UIDLabel: "uid1",
  361. source.PodLabel: "pod1",
  362. source.NamespaceLabel: "namespace1",
  363. },
  364. Value: float64(1 * util.MB),
  365. },
  366. {
  367. Name: metric.ContainerNetworkTransmitBytesTotal,
  368. Labels: map[string]string{
  369. source.UIDLabel: "uid1",
  370. source.PodLabel: "pod1",
  371. source.NamespaceLabel: "namespace1",
  372. },
  373. Value: float64(2 * util.MB),
  374. },
  375. {
  376. Name: metric.KubeletVolumeStatsUsedBytes,
  377. Labels: map[string]string{
  378. source.PVCLabel: "pvc1",
  379. source.NamespaceLabel: "namespace1",
  380. source.UIDLabel: "uid1",
  381. },
  382. Value: float64(1 * util.GB),
  383. },
  384. {
  385. Name: metric.ContainerCPUUsageSecondsTotal,
  386. Labels: map[string]string{
  387. source.ContainerLabel: "container1",
  388. source.PodLabel: "pod1",
  389. source.NamespaceLabel: "namespace1",
  390. source.NodeLabel: "node1",
  391. source.InstanceLabel: "node1",
  392. source.UIDLabel: "uid1",
  393. },
  394. Value: 1,
  395. },
  396. {
  397. Name: metric.ContainerMemoryWorkingSetBytes,
  398. Labels: map[string]string{
  399. source.ContainerLabel: "container1",
  400. source.PodLabel: "pod1",
  401. source.NamespaceLabel: "namespace1",
  402. source.NodeLabel: "node1",
  403. source.InstanceLabel: "node1",
  404. source.UIDLabel: "uid1",
  405. },
  406. Value: float64(5 * util.MB),
  407. },
  408. {
  409. Name: metric.ContainerFSUsageBytes,
  410. Labels: map[string]string{
  411. source.InstanceLabel: "node1",
  412. source.DeviceLabel: "local",
  413. source.UIDLabel: "uid1",
  414. },
  415. Value: float64(1 * util.GB),
  416. },
  417. },
  418. },
  419. "repeat pvc": {
  420. summaries: []*stats.Summary{
  421. {
  422. Node: stats.NodeStats{
  423. NodeName: "node1",
  424. },
  425. Pods: []stats.PodStats{
  426. {
  427. PodRef: stats.PodReference{
  428. Name: "pod1",
  429. Namespace: "namespace1",
  430. UID: "uid1",
  431. },
  432. VolumeStats: []stats.VolumeStats{
  433. {
  434. Name: "vol1",
  435. PVCRef: &stats.PVCReference{
  436. Namespace: "namespace1",
  437. Name: "pvc1",
  438. },
  439. FsStats: stats.FsStats{
  440. Time: metav1.Time{Time: start1},
  441. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  442. },
  443. },
  444. },
  445. },
  446. {
  447. PodRef: stats.PodReference{
  448. Name: "pod2",
  449. Namespace: "namespace1",
  450. UID: "uid1",
  451. },
  452. VolumeStats: []stats.VolumeStats{
  453. {
  454. Name: "vol1",
  455. PVCRef: &stats.PVCReference{
  456. Namespace: "namespace1",
  457. Name: "pvc1",
  458. },
  459. FsStats: stats.FsStats{
  460. Time: metav1.Time{Time: start1},
  461. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  462. },
  463. },
  464. },
  465. },
  466. },
  467. },
  468. },
  469. expected: []metric.Update{
  470. {
  471. Name: metric.KubeletVolumeStatsUsedBytes,
  472. Labels: map[string]string{
  473. source.PVCLabel: "pvc1",
  474. source.NamespaceLabel: "namespace1",
  475. source.UIDLabel: "uid1",
  476. },
  477. Value: float64(1 * util.GB),
  478. },
  479. },
  480. },
  481. }
  482. for name, tt := range tests {
  483. t.Run(name, func(t *testing.T) {
  484. s := &StatSummaryScraper{
  485. client: &mockStatSummaryClient{results: tt.summaries},
  486. }
  487. scrapeResults := s.Scrape()
  488. if len(scrapeResults) != len(tt.expected) {
  489. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  490. }
  491. for i, expected := range tt.expected {
  492. got := scrapeResults[i]
  493. if !reflect.DeepEqual(expected, got) {
  494. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  495. }
  496. }
  497. })
  498. }
  499. }