statsummary_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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: NodeCPUSecondsTotal,
  191. Labels: map[string]string{
  192. source.KubernetesNodeLabel: "node1",
  193. source.ModeLabel: "",
  194. },
  195. Value: 2,
  196. },
  197. {
  198. Name: 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: 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: 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: KubeletVolumeStatsUsedBytes,
  225. Labels: map[string]string{
  226. source.PVCLabel: "pvc1",
  227. source.NamespaceLabel: "namespace1",
  228. },
  229. Value: float64(1 * util.GB),
  230. },
  231. {
  232. Name: ContainerCPUUsageSecondsTotal,
  233. Labels: map[string]string{
  234. source.ContainerLabel: "container1",
  235. source.PodLabel: "pod1",
  236. source.NamespaceLabel: "namespace1",
  237. source.NodeLabel: "node1",
  238. source.InstanceLabel: "node1",
  239. },
  240. Value: 1,
  241. },
  242. {
  243. Name: ContainerMemoryWorkingSetBytes,
  244. Labels: map[string]string{
  245. source.ContainerLabel: "container1",
  246. source.PodLabel: "pod1",
  247. source.NamespaceLabel: "namespace1",
  248. source.NodeLabel: "node1",
  249. source.InstanceLabel: "node1",
  250. },
  251. Value: float64(5 * util.MB),
  252. },
  253. {
  254. Name: ContainerFSUsageBytes,
  255. Labels: map[string]string{
  256. source.InstanceLabel: "node1",
  257. source.DeviceLabel: "local",
  258. },
  259. Value: float64(1 * util.GB),
  260. },
  261. },
  262. },
  263. "repeat pvc": {
  264. summaries: []*stats.Summary{
  265. {
  266. Node: stats.NodeStats{
  267. NodeName: "node1",
  268. },
  269. Pods: []stats.PodStats{
  270. {
  271. PodRef: stats.PodReference{
  272. Name: "pod1",
  273. Namespace: "namespace1",
  274. UID: "uid1",
  275. },
  276. VolumeStats: []stats.VolumeStats{
  277. {
  278. Name: "vol1",
  279. PVCRef: &stats.PVCReference{
  280. Namespace: "namespace1",
  281. Name: "pvc1",
  282. },
  283. FsStats: stats.FsStats{
  284. Time: metav1.Time{Time: start1},
  285. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  286. },
  287. },
  288. },
  289. },
  290. {
  291. PodRef: stats.PodReference{
  292. Name: "pod2",
  293. Namespace: "namespace1",
  294. UID: "uid1",
  295. },
  296. VolumeStats: []stats.VolumeStats{
  297. {
  298. Name: "vol1",
  299. PVCRef: &stats.PVCReference{
  300. Namespace: "namespace1",
  301. Name: "pvc1",
  302. },
  303. FsStats: stats.FsStats{
  304. Time: metav1.Time{Time: start1},
  305. UsedBytes: util.Ptr(uint64(1 * util.GB)),
  306. },
  307. },
  308. },
  309. },
  310. },
  311. },
  312. },
  313. expected: []metric.Update{
  314. {
  315. Name: KubeletVolumeStatsUsedBytes,
  316. Labels: map[string]string{
  317. source.PVCLabel: "pvc1",
  318. source.NamespaceLabel: "namespace1",
  319. },
  320. Value: float64(1 * util.GB),
  321. },
  322. },
  323. },
  324. }
  325. for name, tt := range tests {
  326. t.Run(name, func(t *testing.T) {
  327. s := &StatSummaryScraper{
  328. client: &mockStatSummaryClient{results: tt.summaries},
  329. }
  330. scrapeResults := s.Scrape()
  331. if len(scrapeResults) != len(tt.expected) {
  332. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  333. }
  334. for i, expected := range tt.expected {
  335. got := scrapeResults[i]
  336. if !reflect.DeepEqual(expected, got) {
  337. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  338. }
  339. }
  340. })
  341. }
  342. }