statsummary_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package scrape
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  8. "github.com/opencost/opencost/modules/collector-source/pkg/util"
  9. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  10. stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
  11. )
  12. type mockStatSummaryClient struct {
  13. results []*stats.Summary
  14. }
  15. func (m *mockStatSummaryClient) GetNodeData() ([]*stats.Summary, error) {
  16. return m.results, nil
  17. }
  18. func TestStatScraper_Scrape(t *testing.T) {
  19. start1, _ := time.Parse(time.RFC3339, Start1Str)
  20. tests := map[string]struct {
  21. summaries []*stats.Summary
  22. expected []metric.Update
  23. }{
  24. "nil values": {
  25. summaries: []*stats.Summary{
  26. {
  27. Node: stats.NodeStats{
  28. NodeName: "node1",
  29. CPU: &stats.CPUStats{
  30. Time: metav1.Time{Time: start1},
  31. UsageCoreNanoSeconds: nil,
  32. },
  33. Fs: &stats.FsStats{
  34. Time: metav1.Time{Time: start1},
  35. CapacityBytes: nil,
  36. },
  37. },
  38. Pods: []stats.PodStats{
  39. {
  40. PodRef: stats.PodReference{
  41. Name: "pod1",
  42. Namespace: "namespace1",
  43. UID: "uid1",
  44. },
  45. Network: &stats.NetworkStats{
  46. Time: metav1.Time{Time: start1},
  47. InterfaceStats: stats.InterfaceStats{
  48. RxBytes: nil,
  49. TxBytes: nil,
  50. },
  51. },
  52. VolumeStats: []stats.VolumeStats{
  53. {
  54. Name: "vol1",
  55. PVCRef: &stats.PVCReference{
  56. Namespace: "namespace1",
  57. Name: "pvc1",
  58. },
  59. FsStats: stats.FsStats{
  60. Time: metav1.Time{Time: start1},
  61. UsedBytes: nil,
  62. },
  63. },
  64. },
  65. Containers: []stats.ContainerStats{
  66. {
  67. Name: "container1",
  68. CPU: &stats.CPUStats{
  69. Time: metav1.Time{Time: start1},
  70. UsageCoreNanoSeconds: nil,
  71. },
  72. Memory: &stats.MemoryStats{
  73. Time: metav1.Time{Time: start1},
  74. WorkingSetBytes: nil,
  75. },
  76. Rootfs: &stats.FsStats{
  77. Time: metav1.Time{Time: start1},
  78. UsedBytes: nil,
  79. },
  80. },
  81. },
  82. },
  83. },
  84. },
  85. },
  86. expected: []metric.Update{},
  87. },
  88. "nil structs": {
  89. summaries: []*stats.Summary{
  90. {
  91. Node: stats.NodeStats{
  92. NodeName: "node1",
  93. CPU: nil,
  94. Fs: nil,
  95. },
  96. Pods: []stats.PodStats{
  97. {
  98. PodRef: stats.PodReference{
  99. Name: "pod1",
  100. Namespace: "namespace1",
  101. UID: "uid1",
  102. },
  103. Network: nil,
  104. VolumeStats: nil,
  105. Containers: []stats.ContainerStats{
  106. {
  107. Name: "container1",
  108. CPU: nil,
  109. Memory: nil,
  110. Rootfs: nil,
  111. },
  112. },
  113. },
  114. },
  115. },
  116. },
  117. expected: []metric.Update{},
  118. },
  119. "single node": {
  120. summaries: []*stats.Summary{
  121. {
  122. Node: stats.NodeStats{
  123. NodeName: "node1",
  124. CPU: &stats.CPUStats{
  125. Time: metav1.Time{Time: start1},
  126. UsageCoreNanoSeconds: util.Ptr(uint64(2000000000)),
  127. },
  128. Fs: &stats.FsStats{
  129. Time: metav1.Time{Time: start1},
  130. CapacityBytes: util.Ptr(uint64(2 * util.GB)),
  131. },
  132. },
  133. Pods: []stats.PodStats{
  134. {
  135. PodRef: stats.PodReference{
  136. Name: "pod1",
  137. Namespace: "namespace1",
  138. UID: "uid1",
  139. },
  140. Network: &stats.NetworkStats{
  141. Time: metav1.Time{Time: start1},
  142. InterfaceStats: stats.InterfaceStats{
  143. RxBytes: util.Ptr(uint64(1 * util.MB)),
  144. TxBytes: util.Ptr(uint64(2 * util.MB)),
  145. },
  146. },
  147. VolumeStats: []stats.VolumeStats{
  148. {
  149. Name: "ignoreVol1",
  150. FsStats: stats.FsStats{
  151. Time: metav1.Time{Time: start1},
  152. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  153. },
  154. },
  155. {
  156. Name: "vol1",
  157. PVCRef: &stats.PVCReference{
  158. Namespace: "namespace1",
  159. Name: "pvc1",
  160. },
  161. FsStats: stats.FsStats{
  162. Time: metav1.Time{Time: start1},
  163. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  164. },
  165. },
  166. },
  167. Containers: []stats.ContainerStats{
  168. {
  169. Name: "container1",
  170. CPU: &stats.CPUStats{
  171. Time: metav1.Time{Time: start1},
  172. UsageCoreNanoSeconds: util.Ptr(uint64(1000000000)),
  173. },
  174. Memory: &stats.MemoryStats{
  175. Time: metav1.Time{Time: start1},
  176. WorkingSetBytes: util.Ptr(uint64(5 * util.MB)),
  177. },
  178. Rootfs: &stats.FsStats{
  179. Time: metav1.Time{Time: start1},
  180. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  181. },
  182. },
  183. },
  184. },
  185. },
  186. },
  187. },
  188. expected: []metric.Update{
  189. {
  190. Name: metric.NodeCPUSecondsTotal,
  191. Labels: map[string]string{
  192. source.KubernetesNodeLabel: "node1",
  193. source.ModeLabel: "",
  194. },
  195. Value: 2,
  196. },
  197. {
  198. Name: metric.NodeFSCapacityBytes,
  199. Labels: map[string]string{
  200. source.InstanceLabel: "node1",
  201. source.DeviceLabel: "local",
  202. },
  203. Value: float64(2 * util.GB),
  204. },
  205. {
  206. Name: metric.ContainerNetworkReceiveBytesTotal,
  207. Labels: map[string]string{
  208. source.UIDLabel: "uid1",
  209. source.PodLabel: "pod1",
  210. source.NamespaceLabel: "namespace1",
  211. },
  212. Value: float64(1 * util.MB),
  213. },
  214. {
  215. Name: metric.ContainerNetworkTransmitBytesTotal,
  216. Labels: map[string]string{
  217. source.UIDLabel: "uid1",
  218. source.PodLabel: "pod1",
  219. source.NamespaceLabel: "namespace1",
  220. },
  221. Value: float64(2 * util.MB),
  222. },
  223. {
  224. Name: metric.KubeletVolumeStatsUsedBytes,
  225. Labels: map[string]string{
  226. source.PVCLabel: "pvc1",
  227. source.NamespaceLabel: "namespace1",
  228. source.UIDLabel: "uid1",
  229. },
  230. Value: float64(1 * util.GB),
  231. },
  232. {
  233. Name: metric.ContainerCPUUsageSecondsTotal,
  234. Labels: map[string]string{
  235. source.ContainerLabel: "container1",
  236. source.PodLabel: "pod1",
  237. source.NamespaceLabel: "namespace1",
  238. source.NodeLabel: "node1",
  239. source.InstanceLabel: "node1",
  240. source.UIDLabel: "uid1",
  241. },
  242. Value: 1,
  243. },
  244. {
  245. Name: metric.ContainerMemoryWorkingSetBytes,
  246. Labels: map[string]string{
  247. source.ContainerLabel: "container1",
  248. source.PodLabel: "pod1",
  249. source.NamespaceLabel: "namespace1",
  250. source.NodeLabel: "node1",
  251. source.InstanceLabel: "node1",
  252. source.UIDLabel: "uid1",
  253. },
  254. Value: float64(5 * util.MB),
  255. },
  256. {
  257. Name: metric.ContainerFSUsageBytes,
  258. Labels: map[string]string{
  259. source.InstanceLabel: "node1",
  260. source.DeviceLabel: "local",
  261. source.UIDLabel: "uid1",
  262. },
  263. Value: float64(1 * util.GB),
  264. },
  265. },
  266. },
  267. "repeat pvc": {
  268. summaries: []*stats.Summary{
  269. {
  270. Node: stats.NodeStats{
  271. NodeName: "node1",
  272. },
  273. Pods: []stats.PodStats{
  274. {
  275. PodRef: stats.PodReference{
  276. Name: "pod1",
  277. Namespace: "namespace1",
  278. UID: "uid1",
  279. },
  280. VolumeStats: []stats.VolumeStats{
  281. {
  282. Name: "vol1",
  283. PVCRef: &stats.PVCReference{
  284. Namespace: "namespace1",
  285. Name: "pvc1",
  286. },
  287. FsStats: stats.FsStats{
  288. Time: metav1.Time{Time: start1},
  289. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  290. },
  291. },
  292. },
  293. },
  294. {
  295. PodRef: stats.PodReference{
  296. Name: "pod2",
  297. Namespace: "namespace1",
  298. UID: "uid1",
  299. },
  300. VolumeStats: []stats.VolumeStats{
  301. {
  302. Name: "vol1",
  303. PVCRef: &stats.PVCReference{
  304. Namespace: "namespace1",
  305. Name: "pvc1",
  306. },
  307. FsStats: stats.FsStats{
  308. Time: metav1.Time{Time: start1},
  309. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  310. },
  311. },
  312. },
  313. },
  314. },
  315. },
  316. },
  317. expected: []metric.Update{
  318. {
  319. Name: metric.KubeletVolumeStatsUsedBytes,
  320. Labels: map[string]string{
  321. source.PVCLabel: "pvc1",
  322. source.NamespaceLabel: "namespace1",
  323. source.UIDLabel: "uid1",
  324. },
  325. Value: float64(1 * util.GB),
  326. },
  327. },
  328. },
  329. }
  330. for name, tt := range tests {
  331. t.Run(name, func(t *testing.T) {
  332. s := &StatSummaryScraper{
  333. client: &mockStatSummaryClient{results: tt.summaries},
  334. }
  335. scrapeResults := s.Scrape()
  336. if len(scrapeResults) != len(tt.expected) {
  337. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  338. }
  339. for i, expected := range tt.expected {
  340. got := scrapeResults[i]
  341. if !reflect.DeepEqual(expected, got) {
  342. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  343. }
  344. }
  345. })
  346. }
  347. }