clustercache_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. package scrape
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/clustercache"
  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. v1 "k8s.io/api/core/v1"
  10. "k8s.io/apimachinery/pkg/api/resource"
  11. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  12. )
  13. var Start1Str = "2025-01-01T00:00:00Z00:00"
  14. func Test_kubernetesScraper_scrapeNodes(t *testing.T) {
  15. start1, _ := time.Parse(time.RFC3339, Start1Str)
  16. type scrape struct {
  17. Nodes []*clustercache.Node
  18. Timestamp time.Time
  19. }
  20. tests := []struct {
  21. name string
  22. scrapes []scrape
  23. expected []metric.UpdateArgs
  24. }{
  25. {
  26. name: "simple",
  27. scrapes: []scrape{
  28. {
  29. Nodes: []*clustercache.Node{
  30. {
  31. Name: "node1",
  32. SpecProviderID: "i-1",
  33. Status: v1.NodeStatus{
  34. Capacity: v1.ResourceList{
  35. v1.ResourceCPU: resource.MustParse("2"),
  36. v1.ResourceMemory: resource.MustParse("2048"),
  37. },
  38. Allocatable: v1.ResourceList{
  39. v1.ResourceCPU: resource.MustParse("1"),
  40. v1.ResourceMemory: resource.MustParse("1024"),
  41. },
  42. },
  43. Labels: map[string]string{
  44. "test1": "blah",
  45. "test2": "blah2",
  46. },
  47. },
  48. },
  49. Timestamp: start1,
  50. },
  51. },
  52. expected: []metric.UpdateArgs{
  53. {
  54. MetricName: KubeNodeStatusCapacityCPUCores,
  55. Labels: map[string]string{
  56. source.NodeLabel: "node1",
  57. source.ProviderIDLabel: "i-1",
  58. },
  59. Value: 2.0,
  60. Timestamp: &start1,
  61. AdditionalInformation: nil,
  62. },
  63. {
  64. MetricName: KubeNodeStatusCapacityMemoryBytes,
  65. Labels: map[string]string{
  66. source.NodeLabel: "node1",
  67. source.ProviderIDLabel: "i-1",
  68. },
  69. Value: 2048.0,
  70. Timestamp: &start1,
  71. AdditionalInformation: nil,
  72. },
  73. {
  74. MetricName: KubeNodeStatusAllocatableCPUCores,
  75. Labels: map[string]string{
  76. source.NodeLabel: "node1",
  77. source.ProviderIDLabel: "i-1",
  78. },
  79. Value: 1.0,
  80. Timestamp: &start1,
  81. AdditionalInformation: nil,
  82. },
  83. {
  84. MetricName: KubeNodeStatusAllocatableMemoryBytes,
  85. Labels: map[string]string{
  86. source.NodeLabel: "node1",
  87. source.ProviderIDLabel: "i-1",
  88. },
  89. Value: 1024.0,
  90. Timestamp: &start1,
  91. AdditionalInformation: nil,
  92. },
  93. {
  94. MetricName: KubeNodeLabels,
  95. Labels: map[string]string{
  96. source.NodeLabel: "node1",
  97. source.ProviderIDLabel: "i-1",
  98. },
  99. Value: 0,
  100. Timestamp: &start1,
  101. AdditionalInformation: map[string]string{
  102. "label_test1": "blah",
  103. "label_test2": "blah2",
  104. },
  105. },
  106. },
  107. },
  108. }
  109. for _, tt := range tests {
  110. t.Run(tt.name, func(t *testing.T) {
  111. updateRecorder := metric.ArgRecordUpdater{}
  112. ks := &ClusterCacheScraper{
  113. updater: &updateRecorder,
  114. }
  115. for _, s := range tt.scrapes {
  116. ks.scrapeNodes(s.Nodes, s.Timestamp)
  117. }
  118. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  119. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  120. }
  121. for i, expected := range tt.expected {
  122. updateArg := updateRecorder.UpdateArgs[i]
  123. err := expected.Equals(updateArg)
  124. if err != nil {
  125. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  126. }
  127. }
  128. })
  129. }
  130. }
  131. func Test_kubernetesScraper_scrapeDeployments(t *testing.T) {
  132. start1, _ := time.Parse(time.RFC3339, Start1Str)
  133. type scrape struct {
  134. Deployments []*clustercache.Deployment
  135. Timestamp time.Time
  136. }
  137. tests := []struct {
  138. name string
  139. scrapes []scrape
  140. expected []metric.UpdateArgs
  141. }{
  142. {
  143. name: "simple",
  144. scrapes: []scrape{
  145. {
  146. Deployments: []*clustercache.Deployment{
  147. {
  148. Name: "deployment1",
  149. Namespace: "namespace1",
  150. MatchLabels: map[string]string{
  151. "test1": "blah",
  152. "test2": "blah2",
  153. },
  154. },
  155. },
  156. Timestamp: start1,
  157. },
  158. },
  159. expected: []metric.UpdateArgs{
  160. {
  161. MetricName: DeploymentMatchLabels,
  162. Labels: map[string]string{
  163. source.DeploymentLabel: "deployment1",
  164. source.NamespaceLabel: "namespace1",
  165. },
  166. Value: 0,
  167. Timestamp: &start1,
  168. AdditionalInformation: map[string]string{
  169. "label_test1": "blah",
  170. "label_test2": "blah2",
  171. },
  172. },
  173. },
  174. },
  175. }
  176. for _, tt := range tests {
  177. t.Run(tt.name, func(t *testing.T) {
  178. updateRecorder := metric.ArgRecordUpdater{}
  179. ks := &ClusterCacheScraper{
  180. updater: &updateRecorder,
  181. }
  182. for _, s := range tt.scrapes {
  183. ks.scrapeDeployments(s.Deployments, s.Timestamp)
  184. }
  185. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  186. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  187. }
  188. for i, expected := range tt.expected {
  189. updateArg := updateRecorder.UpdateArgs[i]
  190. err := expected.Equals(updateArg)
  191. if err != nil {
  192. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  193. }
  194. }
  195. })
  196. }
  197. }
  198. func Test_kubernetesScraper_scrapeNamespaces(t *testing.T) {
  199. start1, _ := time.Parse(time.RFC3339, Start1Str)
  200. type scrape struct {
  201. Namespaces []*clustercache.Namespace
  202. Timestamp time.Time
  203. }
  204. tests := []struct {
  205. name string
  206. scrapes []scrape
  207. expected []metric.UpdateArgs
  208. }{
  209. {
  210. name: "simple",
  211. scrapes: []scrape{
  212. {
  213. Namespaces: []*clustercache.Namespace{
  214. {
  215. Name: "namespace1",
  216. Labels: map[string]string{
  217. "test1": "blah",
  218. "test2": "blah2",
  219. },
  220. Annotations: map[string]string{
  221. "test3": "blah3",
  222. "test4": "blah4",
  223. },
  224. },
  225. },
  226. Timestamp: start1,
  227. },
  228. },
  229. expected: []metric.UpdateArgs{
  230. {
  231. MetricName: KubeNamespaceLabels,
  232. Labels: map[string]string{
  233. source.NamespaceLabel: "namespace1",
  234. },
  235. Value: 0,
  236. Timestamp: &start1,
  237. AdditionalInformation: map[string]string{
  238. "label_test1": "blah",
  239. "label_test2": "blah2",
  240. },
  241. },
  242. {
  243. MetricName: KubeNamespaceAnnotations,
  244. Labels: map[string]string{
  245. source.NamespaceLabel: "namespace1",
  246. },
  247. Value: 0,
  248. Timestamp: &start1,
  249. AdditionalInformation: map[string]string{
  250. "annotation_test3": "blah3",
  251. "annotation_test4": "blah4",
  252. },
  253. },
  254. },
  255. },
  256. }
  257. for _, tt := range tests {
  258. t.Run(tt.name, func(t *testing.T) {
  259. updateRecorder := metric.ArgRecordUpdater{}
  260. ks := &ClusterCacheScraper{
  261. updater: &updateRecorder,
  262. }
  263. for _, s := range tt.scrapes {
  264. ks.scrapeNamespaces(s.Namespaces, s.Timestamp)
  265. }
  266. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  267. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  268. }
  269. for i, expected := range tt.expected {
  270. updateArg := updateRecorder.UpdateArgs[i]
  271. err := expected.Equals(updateArg)
  272. if err != nil {
  273. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  274. }
  275. }
  276. })
  277. }
  278. }
  279. func Test_kubernetesScraper_scrapePods(t *testing.T) {
  280. start1, _ := time.Parse(time.RFC3339, Start1Str)
  281. type scrape struct {
  282. Pods []*clustercache.Pod
  283. Timestamp time.Time
  284. }
  285. tests := []struct {
  286. name string
  287. scrapes []scrape
  288. expected []metric.UpdateArgs
  289. }{
  290. {
  291. name: "simple",
  292. scrapes: []scrape{
  293. {
  294. Pods: []*clustercache.Pod{
  295. {
  296. Name: "pod1",
  297. Namespace: "namespace1",
  298. UID: "uuid1",
  299. Spec: clustercache.PodSpec{
  300. NodeName: "node1",
  301. Containers: []clustercache.Container{
  302. {
  303. Name: "container1",
  304. Resources: v1.ResourceRequirements{
  305. Requests: map[v1.ResourceName]resource.Quantity{
  306. v1.ResourceCPU: resource.MustParse("500m"),
  307. v1.ResourceMemory: resource.MustParse("512"),
  308. },
  309. },
  310. },
  311. },
  312. },
  313. Labels: map[string]string{
  314. "test1": "blah",
  315. "test2": "blah2",
  316. },
  317. Annotations: map[string]string{
  318. "test3": "blah3",
  319. "test4": "blah4",
  320. },
  321. OwnerReferences: []metav1.OwnerReference{
  322. {
  323. Kind: source.DeploymentLabel,
  324. Name: "deployment1",
  325. Controller: nil,
  326. },
  327. },
  328. Status: clustercache.PodStatus{
  329. ContainerStatuses: []v1.ContainerStatus{
  330. {
  331. Name: "container1",
  332. State: v1.ContainerState{
  333. Running: &v1.ContainerStateRunning{},
  334. },
  335. },
  336. },
  337. },
  338. },
  339. },
  340. Timestamp: start1,
  341. },
  342. },
  343. expected: []metric.UpdateArgs{
  344. {
  345. MetricName: KubePodLabels,
  346. Labels: map[string]string{
  347. source.PodLabel: "pod1",
  348. source.NamespaceLabel: "namespace1",
  349. source.UIDLabel: "uuid1",
  350. source.NodeLabel: "node1",
  351. source.InstanceLabel: "node1",
  352. },
  353. Value: 0,
  354. Timestamp: &start1,
  355. AdditionalInformation: map[string]string{
  356. "label_test1": "blah",
  357. "label_test2": "blah2",
  358. },
  359. },
  360. {
  361. MetricName: KubePodAnnotations,
  362. Labels: map[string]string{
  363. source.PodLabel: "pod1",
  364. source.NamespaceLabel: "namespace1",
  365. source.UIDLabel: "uuid1",
  366. source.NodeLabel: "node1",
  367. source.InstanceLabel: "node1",
  368. },
  369. Value: 0,
  370. Timestamp: &start1,
  371. AdditionalInformation: map[string]string{
  372. "annotation_test3": "blah3",
  373. "annotation_test4": "blah4",
  374. },
  375. },
  376. {
  377. MetricName: KubePodOwner,
  378. Labels: map[string]string{
  379. source.PodLabel: "pod1",
  380. source.NamespaceLabel: "namespace1",
  381. source.UIDLabel: "uuid1",
  382. source.NodeLabel: "node1",
  383. source.InstanceLabel: "node1",
  384. source.OwnerKindLabel: "deployment",
  385. source.OwnerNameLabel: "deployment1",
  386. },
  387. Value: 0,
  388. Timestamp: &start1,
  389. AdditionalInformation: nil,
  390. },
  391. {
  392. MetricName: KubePodContainerStatusRunning,
  393. Labels: map[string]string{
  394. source.PodLabel: "pod1",
  395. source.NamespaceLabel: "namespace1",
  396. source.UIDLabel: "uuid1",
  397. source.NodeLabel: "node1",
  398. source.InstanceLabel: "node1",
  399. source.ContainerLabel: "container1",
  400. },
  401. Value: 0,
  402. Timestamp: &start1,
  403. AdditionalInformation: nil,
  404. },
  405. {
  406. MetricName: KubePodContainerResourceRequests,
  407. Labels: map[string]string{
  408. source.PodLabel: "pod1",
  409. source.NamespaceLabel: "namespace1",
  410. source.UIDLabel: "uuid1",
  411. source.NodeLabel: "node1",
  412. source.InstanceLabel: "node1",
  413. source.ContainerLabel: "container1",
  414. source.ResourceLabel: "cpu",
  415. source.UnitLabel: "core",
  416. },
  417. Value: 0.5,
  418. Timestamp: &start1,
  419. AdditionalInformation: nil,
  420. },
  421. {
  422. MetricName: KubePodContainerResourceRequests,
  423. Labels: map[string]string{
  424. source.PodLabel: "pod1",
  425. source.NamespaceLabel: "namespace1",
  426. source.UIDLabel: "uuid1",
  427. source.NodeLabel: "node1",
  428. source.InstanceLabel: "node1",
  429. source.ContainerLabel: "container1",
  430. source.ResourceLabel: "memory",
  431. source.UnitLabel: "byte",
  432. },
  433. Value: 512,
  434. Timestamp: &start1,
  435. AdditionalInformation: nil,
  436. },
  437. },
  438. },
  439. }
  440. for _, tt := range tests {
  441. t.Run(tt.name, func(t *testing.T) {
  442. updateRecorder := metric.ArgRecordUpdater{}
  443. ks := &ClusterCacheScraper{
  444. updater: &updateRecorder,
  445. }
  446. for _, s := range tt.scrapes {
  447. ks.scrapePods(s.Pods, s.Timestamp)
  448. }
  449. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  450. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  451. }
  452. for i, expected := range tt.expected {
  453. updateArg := updateRecorder.UpdateArgs[i]
  454. err := expected.Equals(updateArg)
  455. if err != nil {
  456. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  457. }
  458. }
  459. })
  460. }
  461. }
  462. func Test_kubernetesScraper_scrapePVCs(t *testing.T) {
  463. start1, _ := time.Parse(time.RFC3339, Start1Str)
  464. type scrape struct {
  465. PVCs []*clustercache.PersistentVolumeClaim
  466. Timestamp time.Time
  467. }
  468. tests := []struct {
  469. name string
  470. scrapes []scrape
  471. expected []metric.UpdateArgs
  472. }{
  473. {
  474. name: "simple",
  475. scrapes: []scrape{
  476. {
  477. PVCs: []*clustercache.PersistentVolumeClaim{
  478. {
  479. Name: "pvc1",
  480. Namespace: "namespace1",
  481. Spec: v1.PersistentVolumeClaimSpec{
  482. VolumeName: "vol1",
  483. StorageClassName: util.Ptr("storageClass1"),
  484. Resources: v1.VolumeResourceRequirements{
  485. Requests: v1.ResourceList{
  486. v1.ResourceStorage: resource.MustParse("4096"),
  487. },
  488. },
  489. },
  490. },
  491. },
  492. Timestamp: start1,
  493. },
  494. },
  495. expected: []metric.UpdateArgs{
  496. {
  497. MetricName: KubePersistentVolumeClaimInfo,
  498. Labels: map[string]string{
  499. source.PVCLabel: "pvc1",
  500. source.NamespaceLabel: "namespace1",
  501. source.VolumeNameLabel: "vol1",
  502. source.StorageClassLabel: "storageClass1",
  503. },
  504. Value: 0,
  505. Timestamp: &start1,
  506. AdditionalInformation: nil,
  507. },
  508. {
  509. MetricName: KubePersistentVolumeClaimResourceRequestsStorageBytes,
  510. Labels: map[string]string{
  511. source.PVCLabel: "pvc1",
  512. source.NamespaceLabel: "namespace1",
  513. source.VolumeNameLabel: "vol1",
  514. source.StorageClassLabel: "storageClass1",
  515. },
  516. Value: 4096,
  517. Timestamp: &start1,
  518. AdditionalInformation: nil,
  519. },
  520. },
  521. },
  522. }
  523. for _, tt := range tests {
  524. t.Run(tt.name, func(t *testing.T) {
  525. updateRecorder := metric.ArgRecordUpdater{}
  526. ks := &ClusterCacheScraper{
  527. updater: &updateRecorder,
  528. }
  529. for _, s := range tt.scrapes {
  530. ks.scrapePVCs(s.PVCs, s.Timestamp)
  531. }
  532. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  533. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  534. }
  535. for i, expected := range tt.expected {
  536. updateArg := updateRecorder.UpdateArgs[i]
  537. err := expected.Equals(updateArg)
  538. if err != nil {
  539. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  540. }
  541. }
  542. })
  543. }
  544. }
  545. func Test_kubernetesScraper_scrapePVs(t *testing.T) {
  546. start1, _ := time.Parse(time.RFC3339, Start1Str)
  547. type scrape struct {
  548. PVs []*clustercache.PersistentVolume
  549. Timestamp time.Time
  550. }
  551. tests := []struct {
  552. name string
  553. scrapes []scrape
  554. expected []metric.UpdateArgs
  555. }{
  556. {
  557. name: "simple",
  558. scrapes: []scrape{
  559. {
  560. PVs: []*clustercache.PersistentVolume{
  561. {
  562. Name: "pv1",
  563. Spec: v1.PersistentVolumeSpec{
  564. StorageClassName: "storageClass1",
  565. PersistentVolumeSource: v1.PersistentVolumeSource{
  566. CSI: &v1.CSIPersistentVolumeSource{
  567. VolumeHandle: "vol-1",
  568. },
  569. },
  570. Capacity: v1.ResourceList{
  571. v1.ResourceStorage: resource.MustParse("4096"),
  572. },
  573. },
  574. },
  575. },
  576. Timestamp: start1,
  577. },
  578. },
  579. expected: []metric.UpdateArgs{
  580. {
  581. MetricName: KubecostPVInfo,
  582. Labels: map[string]string{
  583. source.PVLabel: "pv1",
  584. source.ProviderIDLabel: "vol-1",
  585. source.StorageClassLabel: "storageClass1",
  586. },
  587. Value: 0,
  588. Timestamp: &start1,
  589. AdditionalInformation: nil,
  590. },
  591. {
  592. MetricName: KubePersistentVolumeCapacityBytes,
  593. Labels: map[string]string{
  594. source.PVLabel: "pv1",
  595. source.ProviderIDLabel: "vol-1",
  596. source.StorageClassLabel: "storageClass1",
  597. },
  598. Value: 4096,
  599. Timestamp: &start1,
  600. AdditionalInformation: nil,
  601. },
  602. },
  603. },
  604. }
  605. for _, tt := range tests {
  606. t.Run(tt.name, func(t *testing.T) {
  607. updateRecorder := metric.ArgRecordUpdater{}
  608. ks := &ClusterCacheScraper{
  609. updater: &updateRecorder,
  610. }
  611. for _, s := range tt.scrapes {
  612. ks.scrapePVs(s.PVs, s.Timestamp)
  613. }
  614. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  615. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  616. }
  617. for i, expected := range tt.expected {
  618. updateArg := updateRecorder.UpdateArgs[i]
  619. err := expected.Equals(updateArg)
  620. if err != nil {
  621. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  622. }
  623. }
  624. })
  625. }
  626. }
  627. func Test_kubernetesScraper_scrapeServices(t *testing.T) {
  628. start1, _ := time.Parse(time.RFC3339, Start1Str)
  629. type scrape struct {
  630. Services []*clustercache.Service
  631. Timestamp time.Time
  632. }
  633. tests := []struct {
  634. name string
  635. scrapes []scrape
  636. expected []metric.UpdateArgs
  637. }{
  638. {
  639. name: "simple",
  640. scrapes: []scrape{
  641. {
  642. Services: []*clustercache.Service{
  643. {
  644. Name: "service1",
  645. Namespace: "namespace1",
  646. SpecSelector: map[string]string{
  647. "test1": "blah",
  648. "test2": "blah2",
  649. },
  650. },
  651. },
  652. Timestamp: start1,
  653. },
  654. },
  655. expected: []metric.UpdateArgs{
  656. {
  657. MetricName: ServiceSelectorLabels,
  658. Labels: map[string]string{
  659. "service": "service1",
  660. source.NamespaceLabel: "namespace1",
  661. },
  662. Value: 0,
  663. Timestamp: &start1,
  664. AdditionalInformation: map[string]string{
  665. "label_test1": "blah",
  666. "label_test2": "blah2",
  667. },
  668. },
  669. },
  670. },
  671. }
  672. for _, tt := range tests {
  673. t.Run(tt.name, func(t *testing.T) {
  674. updateRecorder := metric.ArgRecordUpdater{}
  675. ks := &ClusterCacheScraper{
  676. updater: &updateRecorder,
  677. }
  678. for _, s := range tt.scrapes {
  679. ks.scrapeServices(s.Services, s.Timestamp)
  680. }
  681. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  682. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  683. }
  684. for i, expected := range tt.expected {
  685. updateArg := updateRecorder.UpdateArgs[i]
  686. err := expected.Equals(updateArg)
  687. if err != nil {
  688. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  689. }
  690. }
  691. })
  692. }
  693. }
  694. func Test_kubernetesScraper_scrapeStatefulSets(t *testing.T) {
  695. start1, _ := time.Parse(time.RFC3339, Start1Str)
  696. type scrape struct {
  697. StatefulSets []*clustercache.StatefulSet
  698. Timestamp time.Time
  699. }
  700. tests := []struct {
  701. name string
  702. scrapes []scrape
  703. expected []metric.UpdateArgs
  704. }{
  705. {
  706. name: "simple",
  707. scrapes: []scrape{
  708. {
  709. StatefulSets: []*clustercache.StatefulSet{
  710. {
  711. Name: "statefulSet1",
  712. Namespace: "namespace1",
  713. SpecSelector: &metav1.LabelSelector{
  714. MatchLabels: map[string]string{
  715. "test1": "blah",
  716. "test2": "blah2",
  717. },
  718. },
  719. },
  720. },
  721. Timestamp: start1,
  722. },
  723. },
  724. expected: []metric.UpdateArgs{
  725. {
  726. MetricName: StatefulSetMatchLabels,
  727. Labels: map[string]string{
  728. source.StatefulSetLabel: "statefulSet1",
  729. source.NamespaceLabel: "namespace1",
  730. },
  731. Value: 0,
  732. Timestamp: &start1,
  733. AdditionalInformation: map[string]string{
  734. "label_test1": "blah",
  735. "label_test2": "blah2",
  736. },
  737. },
  738. },
  739. },
  740. }
  741. for _, tt := range tests {
  742. t.Run(tt.name, func(t *testing.T) {
  743. updateRecorder := metric.ArgRecordUpdater{}
  744. ks := &ClusterCacheScraper{
  745. updater: &updateRecorder,
  746. }
  747. for _, s := range tt.scrapes {
  748. ks.scrapeStatefulSets(s.StatefulSets, s.Timestamp)
  749. }
  750. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  751. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  752. }
  753. for i, expected := range tt.expected {
  754. updateArg := updateRecorder.UpdateArgs[i]
  755. err := expected.Equals(updateArg)
  756. if err != nil {
  757. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  758. }
  759. }
  760. })
  761. }
  762. }
  763. func Test_kubernetesScraper_scrapeReplicaSets(t *testing.T) {
  764. start1, _ := time.Parse(time.RFC3339, Start1Str)
  765. type scrape struct {
  766. ReplicaSets []*clustercache.ReplicaSet
  767. Timestamp time.Time
  768. }
  769. tests := []struct {
  770. name string
  771. scrapes []scrape
  772. expected []metric.UpdateArgs
  773. }{
  774. {
  775. name: "simple",
  776. scrapes: []scrape{
  777. {
  778. ReplicaSets: []*clustercache.ReplicaSet{
  779. {
  780. Name: "replicaSet1",
  781. Namespace: "namespace1",
  782. OwnerReferences: []metav1.OwnerReference{
  783. {
  784. Name: "rollout1",
  785. Kind: "Rollout",
  786. },
  787. },
  788. },
  789. },
  790. Timestamp: start1,
  791. },
  792. },
  793. expected: []metric.UpdateArgs{
  794. {
  795. MetricName: KubeReplicasetOwner,
  796. Labels: map[string]string{
  797. "replicaset": "replicaSet1",
  798. source.NamespaceLabel: "namespace1",
  799. source.OwnerNameLabel: "rollout1",
  800. source.OwnerKindLabel: "Rollout",
  801. },
  802. Value: 0,
  803. Timestamp: &start1,
  804. },
  805. },
  806. },
  807. }
  808. for _, tt := range tests {
  809. t.Run(tt.name, func(t *testing.T) {
  810. updateRecorder := metric.ArgRecordUpdater{}
  811. ks := &ClusterCacheScraper{
  812. updater: &updateRecorder,
  813. }
  814. for _, s := range tt.scrapes {
  815. ks.scrapeReplicaSets(s.ReplicaSets, s.Timestamp)
  816. }
  817. if len(updateRecorder.UpdateArgs) != len(tt.expected) {
  818. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(updateRecorder.UpdateArgs))
  819. }
  820. for i, expected := range tt.expected {
  821. updateArg := updateRecorder.UpdateArgs[i]
  822. err := expected.Equals(updateArg)
  823. if err != nil {
  824. t.Errorf("Result did not match expected at index %d: %s", i, err.Error())
  825. }
  826. }
  827. })
  828. }
  829. }