statsummary_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. source.ContainerLabel: "container1",
  266. },
  267. Value: float64(1 * util.GB),
  268. },
  269. },
  270. },
  271. "single node with error": {
  272. summaries: []*stats.Summary{
  273. {
  274. Node: stats.NodeStats{
  275. NodeName: "node1",
  276. CPU: &stats.CPUStats{
  277. Time: metav1.Time{Time: start1},
  278. UsageCoreNanoSeconds: util.Ptr(uint64(2000000000)),
  279. },
  280. Fs: &stats.FsStats{
  281. Time: metav1.Time{Time: start1},
  282. CapacityBytes: util.Ptr(uint64(2 * util.GB)),
  283. },
  284. },
  285. Pods: []stats.PodStats{
  286. {
  287. PodRef: stats.PodReference{
  288. Name: "pod1",
  289. Namespace: "namespace1",
  290. UID: "uid1",
  291. },
  292. Network: &stats.NetworkStats{
  293. Time: metav1.Time{Time: start1},
  294. InterfaceStats: stats.InterfaceStats{
  295. RxBytes: util.Ptr(uint64(1 * util.MB)),
  296. TxBytes: util.Ptr(uint64(2 * util.MB)),
  297. },
  298. },
  299. VolumeStats: []stats.VolumeStats{
  300. {
  301. Name: "ignoreVol1",
  302. FsStats: stats.FsStats{
  303. Time: metav1.Time{Time: start1},
  304. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  305. },
  306. },
  307. {
  308. Name: "vol1",
  309. PVCRef: &stats.PVCReference{
  310. Namespace: "namespace1",
  311. Name: "pvc1",
  312. },
  313. FsStats: stats.FsStats{
  314. Time: metav1.Time{Time: start1},
  315. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  316. },
  317. },
  318. },
  319. Containers: []stats.ContainerStats{
  320. {
  321. Name: "container1",
  322. CPU: &stats.CPUStats{
  323. Time: metav1.Time{Time: start1},
  324. UsageCoreNanoSeconds: util.Ptr(uint64(1000000000)),
  325. },
  326. Memory: &stats.MemoryStats{
  327. Time: metav1.Time{Time: start1},
  328. WorkingSetBytes: util.Ptr(uint64(5 * util.MB)),
  329. },
  330. Rootfs: &stats.FsStats{
  331. Time: metav1.Time{Time: start1},
  332. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  333. },
  334. },
  335. },
  336. },
  337. },
  338. },
  339. },
  340. err: fmt.Errorf("failed to retrieve node-XYZ"),
  341. expected: []metric.Update{
  342. {
  343. Name: metric.NodeCPUSecondsTotal,
  344. Labels: map[string]string{
  345. source.KubernetesNodeLabel: "node1",
  346. source.ModeLabel: "",
  347. },
  348. Value: 2,
  349. },
  350. {
  351. Name: metric.NodeFSCapacityBytes,
  352. Labels: map[string]string{
  353. source.InstanceLabel: "node1",
  354. source.DeviceLabel: "local",
  355. },
  356. Value: float64(2 * util.GB),
  357. },
  358. {
  359. Name: metric.ContainerNetworkReceiveBytesTotal,
  360. Labels: map[string]string{
  361. source.UIDLabel: "uid1",
  362. source.PodLabel: "pod1",
  363. source.NamespaceLabel: "namespace1",
  364. },
  365. Value: float64(1 * util.MB),
  366. },
  367. {
  368. Name: metric.ContainerNetworkTransmitBytesTotal,
  369. Labels: map[string]string{
  370. source.UIDLabel: "uid1",
  371. source.PodLabel: "pod1",
  372. source.NamespaceLabel: "namespace1",
  373. },
  374. Value: float64(2 * util.MB),
  375. },
  376. {
  377. Name: metric.KubeletVolumeStatsUsedBytes,
  378. Labels: map[string]string{
  379. source.PVCLabel: "pvc1",
  380. source.NamespaceLabel: "namespace1",
  381. source.UIDLabel: "uid1",
  382. },
  383. Value: float64(1 * util.GB),
  384. },
  385. {
  386. Name: metric.ContainerCPUUsageSecondsTotal,
  387. Labels: map[string]string{
  388. source.ContainerLabel: "container1",
  389. source.PodLabel: "pod1",
  390. source.NamespaceLabel: "namespace1",
  391. source.NodeLabel: "node1",
  392. source.InstanceLabel: "node1",
  393. source.UIDLabel: "uid1",
  394. },
  395. Value: 1,
  396. },
  397. {
  398. Name: metric.ContainerMemoryWorkingSetBytes,
  399. Labels: map[string]string{
  400. source.ContainerLabel: "container1",
  401. source.PodLabel: "pod1",
  402. source.NamespaceLabel: "namespace1",
  403. source.NodeLabel: "node1",
  404. source.InstanceLabel: "node1",
  405. source.UIDLabel: "uid1",
  406. },
  407. Value: float64(5 * util.MB),
  408. },
  409. {
  410. Name: metric.ContainerFSUsageBytes,
  411. Labels: map[string]string{
  412. source.InstanceLabel: "node1",
  413. source.DeviceLabel: "local",
  414. source.UIDLabel: "uid1",
  415. source.ContainerLabel: "container1",
  416. },
  417. Value: float64(1 * util.GB),
  418. },
  419. },
  420. },
  421. "repeat pvc": {
  422. summaries: []*stats.Summary{
  423. {
  424. Node: stats.NodeStats{
  425. NodeName: "node1",
  426. },
  427. Pods: []stats.PodStats{
  428. {
  429. PodRef: stats.PodReference{
  430. Name: "pod1",
  431. Namespace: "namespace1",
  432. UID: "uid1",
  433. },
  434. VolumeStats: []stats.VolumeStats{
  435. {
  436. Name: "vol1",
  437. PVCRef: &stats.PVCReference{
  438. Namespace: "namespace1",
  439. Name: "pvc1",
  440. },
  441. FsStats: stats.FsStats{
  442. Time: metav1.Time{Time: start1},
  443. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  444. },
  445. },
  446. },
  447. },
  448. {
  449. PodRef: stats.PodReference{
  450. Name: "pod2",
  451. Namespace: "namespace1",
  452. UID: "uid1",
  453. },
  454. VolumeStats: []stats.VolumeStats{
  455. {
  456. Name: "vol1",
  457. PVCRef: &stats.PVCReference{
  458. Namespace: "namespace1",
  459. Name: "pvc1",
  460. },
  461. FsStats: stats.FsStats{
  462. Time: metav1.Time{Time: start1},
  463. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  464. },
  465. },
  466. },
  467. },
  468. },
  469. },
  470. },
  471. expected: []metric.Update{
  472. {
  473. Name: metric.KubeletVolumeStatsUsedBytes,
  474. Labels: map[string]string{
  475. source.PVCLabel: "pvc1",
  476. source.NamespaceLabel: "namespace1",
  477. source.UIDLabel: "uid1",
  478. },
  479. Value: float64(1 * util.GB),
  480. },
  481. },
  482. },
  483. }
  484. for name, tt := range tests {
  485. t.Run(name, func(t *testing.T) {
  486. s := &StatSummaryScraper{
  487. client: &mockStatSummaryClient{results: tt.summaries},
  488. }
  489. scrapeResults := s.Scrape()
  490. if len(scrapeResults) != len(tt.expected) {
  491. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  492. }
  493. for i, expected := range tt.expected {
  494. got := scrapeResults[i]
  495. if !reflect.DeepEqual(expected, got) {
  496. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  497. }
  498. }
  499. })
  500. }
  501. }