allocation_helpers_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package costmodel
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "github.com/opencost/opencost/core/pkg/source"
  8. "github.com/opencost/opencost/core/pkg/util"
  9. )
  10. const Ki = 1024
  11. const Mi = Ki * 1024
  12. const Gi = Mi * 1024
  13. const second = 1.0
  14. const minute = second * 60.0
  15. const hour = minute * 60.0
  16. var windowStart = time.Date(2020, 6, 16, 0, 0, 0, 0, time.UTC)
  17. var windowEnd = time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC)
  18. var window = opencost.NewWindow(&windowStart, &windowEnd)
  19. var startFloat = float64(windowStart.Unix())
  20. var podKey1 = podKey{
  21. namespaceKey: namespaceKey{
  22. Cluster: "cluster1",
  23. Namespace: "namespace1",
  24. },
  25. Pod: "pod1",
  26. }
  27. var podKey2 = podKey{
  28. namespaceKey: namespaceKey{
  29. Cluster: "cluster1",
  30. Namespace: "namespace1",
  31. },
  32. Pod: "pod2",
  33. }
  34. var podKey3 = podKey{
  35. namespaceKey: namespaceKey{
  36. Cluster: "cluster2",
  37. Namespace: "namespace2",
  38. },
  39. Pod: "pod3",
  40. }
  41. var podKey4 = podKey{
  42. namespaceKey: namespaceKey{
  43. Cluster: "cluster2",
  44. Namespace: "namespace2",
  45. },
  46. Pod: "pod4",
  47. }
  48. var podKeyUnmounted = podKey{
  49. namespaceKey: namespaceKey{
  50. Cluster: "cluster2",
  51. Namespace: opencost.UnmountedSuffix,
  52. },
  53. Pod: opencost.UnmountedSuffix,
  54. }
  55. var kcPVKey1 = opencost.PVKey{
  56. Cluster: "cluster1",
  57. Name: "pv1",
  58. }
  59. var kcPVKey2 = opencost.PVKey{
  60. Cluster: "cluster1",
  61. Name: "pv2",
  62. }
  63. var kcPVKey3 = opencost.PVKey{
  64. Cluster: "cluster2",
  65. Name: "pv3",
  66. }
  67. var kcPVKey4 = opencost.PVKey{
  68. Cluster: "cluster2",
  69. Name: "pv4",
  70. }
  71. var podMap1 = map[podKey]*pod{
  72. podKey1: {
  73. Window: window.Clone(),
  74. Start: time.Date(2020, 6, 16, 0, 0, 0, 0, time.UTC),
  75. End: time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC),
  76. Key: podKey1,
  77. Allocations: nil,
  78. },
  79. podKey2: {
  80. Window: window.Clone(),
  81. Start: time.Date(2020, 6, 16, 12, 0, 0, 0, time.UTC),
  82. End: time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC),
  83. Key: podKey2,
  84. Allocations: nil,
  85. },
  86. podKey3: {
  87. Window: window.Clone(),
  88. Start: time.Date(2020, 6, 16, 6, 30, 0, 0, time.UTC),
  89. End: time.Date(2020, 6, 17, 18, 12, 33, 0, time.UTC),
  90. Key: podKey3,
  91. Allocations: nil,
  92. },
  93. podKey4: {
  94. Window: window.Clone(),
  95. Start: time.Date(2020, 6, 16, 0, 0, 0, 0, time.UTC),
  96. End: time.Date(2020, 6, 17, 13, 0, 0, 0, time.UTC),
  97. Key: podKey4,
  98. Allocations: nil,
  99. },
  100. podKeyUnmounted: {
  101. Window: window.Clone(),
  102. Start: *window.Start(),
  103. End: *window.End(),
  104. Key: podKeyUnmounted,
  105. Allocations: map[string]*opencost.Allocation{
  106. opencost.UnmountedSuffix: {
  107. Name: fmt.Sprintf("%s/%s/%s/%s", podKeyUnmounted.Cluster, podKeyUnmounted.Namespace, podKeyUnmounted.Pod, opencost.UnmountedSuffix),
  108. Properties: &opencost.AllocationProperties{
  109. Cluster: podKeyUnmounted.Cluster,
  110. Node: "",
  111. Container: opencost.UnmountedSuffix,
  112. Namespace: podKeyUnmounted.Namespace,
  113. Pod: podKeyUnmounted.Pod,
  114. Services: []string{"LB1"},
  115. },
  116. Window: window,
  117. Start: *window.Start(),
  118. End: *window.End(),
  119. LoadBalancerCost: 0.60,
  120. LoadBalancerCostAdjustment: 0,
  121. PVs: opencost.PVAllocations{
  122. kcPVKey2: &opencost.PVAllocation{
  123. ByteHours: 24 * Gi,
  124. Cost: 2.25,
  125. },
  126. },
  127. },
  128. },
  129. },
  130. }
  131. var pvKey1 = pvKey{
  132. Cluster: "cluster1",
  133. PersistentVolume: "pv1",
  134. }
  135. var pvKey2 = pvKey{
  136. Cluster: "cluster1",
  137. PersistentVolume: "pv2",
  138. }
  139. var pvKey3 = pvKey{
  140. Cluster: "cluster2",
  141. PersistentVolume: "pv3",
  142. }
  143. var pvKey4 = pvKey{
  144. Cluster: "cluster2",
  145. PersistentVolume: "pv4",
  146. }
  147. var pvMap1 = map[pvKey]*pv{
  148. pvKey1: {
  149. Start: windowStart,
  150. End: windowEnd.Add(time.Hour * -6),
  151. Bytes: 20 * Gi,
  152. CostPerGiBHour: 0.05,
  153. Cluster: "cluster1",
  154. Name: "pv1",
  155. StorageClass: "class1",
  156. },
  157. pvKey2: {
  158. Start: windowStart,
  159. End: windowEnd,
  160. Bytes: 100 * Gi,
  161. CostPerGiBHour: 0.05,
  162. Cluster: "cluster1",
  163. Name: "pv2",
  164. StorageClass: "class1",
  165. },
  166. pvKey3: {
  167. Start: windowStart.Add(time.Hour * 6),
  168. End: windowEnd.Add(time.Hour * -6),
  169. Bytes: 50 * Gi,
  170. CostPerGiBHour: 0.03,
  171. Cluster: "cluster2",
  172. Name: "pv3",
  173. StorageClass: "class2",
  174. },
  175. pvKey4: {
  176. Start: windowStart,
  177. End: windowEnd.Add(time.Hour * -6),
  178. Bytes: 30 * Gi,
  179. CostPerGiBHour: 0.05,
  180. Cluster: "cluster2",
  181. Name: "pv4",
  182. StorageClass: "class1",
  183. },
  184. }
  185. func TestBuildPVMap(t *testing.T) {
  186. pvMap1NoBytes := make(map[pvKey]*pv, len(pvMap1))
  187. for thisPVKey, thisPV := range pvMap1 {
  188. clonePV := thisPV.clone()
  189. clonePV.Bytes = 0.0
  190. clonePV.StorageClass = ""
  191. pvMap1NoBytes[thisPVKey] = clonePV
  192. }
  193. testCases := map[string]struct {
  194. resolution time.Duration
  195. resultsPVCostPerGiBHour []*source.QueryResult
  196. resultsActiveMinutes []*source.QueryResult
  197. expected map[pvKey]*pv
  198. }{
  199. "pvMap1": {
  200. resolution: time.Hour * 6,
  201. resultsPVCostPerGiBHour: []*source.QueryResult{
  202. source.NewQueryResult(
  203. map[string]interface{}{
  204. "cluster_id": "cluster1",
  205. "volumename": "pv1",
  206. },
  207. []*util.Vector{
  208. {
  209. Value: 0.05,
  210. },
  211. },
  212. source.DefaultResultKeys(),
  213. ),
  214. source.NewQueryResult(
  215. map[string]interface{}{
  216. "cluster_id": "cluster1",
  217. "volumename": "pv2",
  218. },
  219. []*util.Vector{
  220. {
  221. Value: 0.05,
  222. },
  223. },
  224. source.DefaultResultKeys(),
  225. ),
  226. source.NewQueryResult(
  227. map[string]interface{}{
  228. "cluster_id": "cluster2",
  229. "volumename": "pv3",
  230. },
  231. []*util.Vector{
  232. {
  233. Value: 0.03,
  234. },
  235. },
  236. source.DefaultResultKeys(),
  237. ),
  238. source.NewQueryResult(
  239. map[string]interface{}{
  240. "cluster_id": "cluster2",
  241. "volumename": "pv4",
  242. },
  243. []*util.Vector{
  244. {
  245. Value: 0.05,
  246. },
  247. },
  248. source.DefaultResultKeys(),
  249. ),
  250. },
  251. resultsActiveMinutes: []*source.QueryResult{
  252. source.NewQueryResult(
  253. map[string]interface{}{
  254. "cluster_id": "cluster1",
  255. "persistentvolume": "pv1",
  256. },
  257. []*util.Vector{
  258. {
  259. Timestamp: startFloat,
  260. },
  261. {
  262. Timestamp: startFloat + (hour * 6),
  263. },
  264. {
  265. Timestamp: startFloat + (hour * 12),
  266. },
  267. {
  268. Timestamp: startFloat + (hour * 18),
  269. },
  270. },
  271. source.DefaultResultKeys(),
  272. ),
  273. source.NewQueryResult(
  274. map[string]interface{}{
  275. "cluster_id": "cluster1",
  276. "persistentvolume": "pv2",
  277. },
  278. []*util.Vector{
  279. {
  280. Timestamp: startFloat,
  281. },
  282. {
  283. Timestamp: startFloat + (hour * 6),
  284. },
  285. {
  286. Timestamp: startFloat + (hour * 12),
  287. },
  288. {
  289. Timestamp: startFloat + (hour * 18),
  290. },
  291. {
  292. Timestamp: startFloat + (hour * 24),
  293. },
  294. },
  295. source.DefaultResultKeys(),
  296. ),
  297. source.NewQueryResult(
  298. map[string]interface{}{
  299. "cluster_id": "cluster2",
  300. "persistentvolume": "pv3",
  301. },
  302. []*util.Vector{
  303. {
  304. Timestamp: startFloat + (hour * 6),
  305. },
  306. {
  307. Timestamp: startFloat + (hour * 12),
  308. },
  309. {
  310. Timestamp: startFloat + (hour * 18),
  311. },
  312. },
  313. source.DefaultResultKeys(),
  314. ),
  315. source.NewQueryResult(
  316. map[string]interface{}{
  317. "cluster_id": "cluster2",
  318. "persistentvolume": "pv4",
  319. },
  320. []*util.Vector{
  321. {
  322. Timestamp: startFloat,
  323. },
  324. {
  325. Timestamp: startFloat + (hour * 6),
  326. },
  327. {
  328. Timestamp: startFloat + (hour * 12),
  329. },
  330. {
  331. Timestamp: startFloat + (hour * 18),
  332. },
  333. },
  334. source.DefaultResultKeys(),
  335. ),
  336. },
  337. expected: pvMap1NoBytes,
  338. },
  339. }
  340. for name, testCase := range testCases {
  341. t.Run(name, func(t *testing.T) {
  342. pvMap := make(map[pvKey]*pv)
  343. pvCostResults := source.DecodeAll(testCase.resultsPVCostPerGiBHour, source.DecodePVPricePerGiBHourResult)
  344. pvActiveMinsResults := source.DecodeAll(testCase.resultsActiveMinutes, source.DecodePVActiveMinutesResult)
  345. pvInfoResult := []*source.PVInfoResult{}
  346. buildPVMap(testCase.resolution, pvMap, pvCostResults, pvActiveMinsResults, pvInfoResult, window)
  347. if len(pvMap) != len(testCase.expected) {
  348. t.Errorf("pv map does not have the expected length %d : %d", len(pvMap), len(testCase.expected))
  349. }
  350. for thisPVKey, expectedPV := range testCase.expected {
  351. actualPV, ok := pvMap[thisPVKey]
  352. if !ok {
  353. t.Errorf("pv map is missing key %s", thisPVKey)
  354. }
  355. if !actualPV.equal(expectedPV) {
  356. t.Errorf("pv does not match with key %s: %s != %s", thisPVKey, opencost.NewClosedWindow(actualPV.Start, actualPV.End), opencost.NewClosedWindow(expectedPV.Start, expectedPV.End))
  357. }
  358. }
  359. })
  360. }
  361. }
  362. func TestGetUnmountedPodForCluster(t *testing.T) {
  363. testCases := map[string]struct {
  364. window opencost.Window
  365. podMap map[podKey]*pod
  366. cluster string
  367. expected *pod
  368. }{
  369. "create new": {
  370. window: window.Clone(),
  371. podMap: podMap1,
  372. cluster: "cluster1",
  373. expected: &pod{
  374. Window: window.Clone(),
  375. Start: *window.Start(),
  376. End: *window.End(),
  377. Key: getUnmountedPodKey("cluster1"),
  378. Allocations: map[string]*opencost.Allocation{
  379. opencost.UnmountedSuffix: {
  380. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster1", opencost.UnmountedSuffix, opencost.UnmountedSuffix, opencost.UnmountedSuffix),
  381. Properties: &opencost.AllocationProperties{
  382. Cluster: "cluster1",
  383. Node: "",
  384. Container: opencost.UnmountedSuffix,
  385. Namespace: opencost.UnmountedSuffix,
  386. Pod: opencost.UnmountedSuffix,
  387. },
  388. Window: window,
  389. Start: *window.Start(),
  390. End: *window.End(),
  391. },
  392. },
  393. },
  394. },
  395. "get existing": {
  396. window: window.Clone(),
  397. podMap: podMap1,
  398. cluster: "cluster2",
  399. expected: &pod{
  400. Window: window.Clone(),
  401. Start: *window.Start(),
  402. End: *window.End(),
  403. Key: getUnmountedPodKey("cluster2"),
  404. Allocations: map[string]*opencost.Allocation{
  405. opencost.UnmountedSuffix: {
  406. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster2", opencost.UnmountedSuffix, opencost.UnmountedSuffix, opencost.UnmountedSuffix),
  407. Properties: &opencost.AllocationProperties{
  408. Cluster: "cluster2",
  409. Node: "",
  410. Container: opencost.UnmountedSuffix,
  411. Namespace: opencost.UnmountedSuffix,
  412. Pod: opencost.UnmountedSuffix,
  413. Services: []string{"LB1"},
  414. },
  415. Window: window,
  416. Start: *window.Start(),
  417. End: *window.End(),
  418. LoadBalancerCost: .60,
  419. LoadBalancerCostAdjustment: 0,
  420. PVs: opencost.PVAllocations{
  421. kcPVKey2: &opencost.PVAllocation{
  422. ByteHours: 24 * Gi,
  423. Cost: 2.25,
  424. },
  425. },
  426. },
  427. },
  428. },
  429. },
  430. }
  431. for name, testCase := range testCases {
  432. t.Run(name, func(t *testing.T) {
  433. actual := getUnmountedPodForCluster(testCase.window, testCase.podMap, testCase.cluster)
  434. if !actual.equal(testCase.expected) {
  435. t.Errorf("Unmounted pod does not match expectation")
  436. }
  437. })
  438. }
  439. }
  440. func TestCalculateStartAndEnd(t *testing.T) {
  441. testCases := map[string]struct {
  442. resolution time.Duration // User defined config when querying Prometheus
  443. window opencost.Window // User defined config when querying Allocations/Assets
  444. expectedStart time.Time
  445. expectedEnd time.Time
  446. result *source.QueryResult
  447. }{
  448. // Example: avg(node_total_hourly_cost{}) by (node, provider_id)[1h:1h]
  449. "1 hour resolution, 1 hour window": {
  450. resolution: time.Hour,
  451. window: opencost.NewClosedWindow(windowStart, windowStart.Add(time.Hour)),
  452. expectedStart: windowStart,
  453. expectedEnd: windowStart.Add(time.Hour),
  454. result: &source.QueryResult{
  455. Values: []*util.Vector{
  456. {
  457. Timestamp: startFloat,
  458. },
  459. {
  460. Timestamp: startFloat + (minute * 60),
  461. },
  462. },
  463. },
  464. },
  465. // Example: avg(node_total_hourly_cost{}) by (node, provider_id)[1h:30m]
  466. "30 minute resolution, 1 hour window": {
  467. resolution: time.Minute * 30,
  468. window: opencost.NewClosedWindow(windowStart, windowStart.Add(time.Hour)),
  469. expectedStart: windowStart,
  470. expectedEnd: windowStart.Add(time.Hour),
  471. result: &source.QueryResult{
  472. Values: []*util.Vector{
  473. {
  474. Timestamp: startFloat,
  475. },
  476. {
  477. Timestamp: startFloat + (minute * 30),
  478. },
  479. {
  480. Timestamp: startFloat + (minute * 60),
  481. },
  482. },
  483. },
  484. },
  485. // Example: avg(node_total_hourly_cost{}) by (node, provider_id)[45m:15m]
  486. "15 minute resolution, 45 minute window": {
  487. resolution: time.Minute * 15,
  488. window: opencost.NewClosedWindow(windowStart, windowStart.Add(time.Minute*45)),
  489. expectedStart: windowStart,
  490. expectedEnd: windowStart.Add(time.Minute * 45),
  491. result: &source.QueryResult{
  492. Values: []*util.Vector{
  493. {
  494. Timestamp: startFloat + (minute * 0),
  495. },
  496. {
  497. Timestamp: startFloat + (minute * 15),
  498. },
  499. {
  500. Timestamp: startFloat + (minute * 30),
  501. },
  502. {
  503. Timestamp: startFloat + (minute * 45),
  504. },
  505. },
  506. },
  507. },
  508. "1 minute resolution, 5 minute window": {
  509. resolution: time.Minute,
  510. window: opencost.NewClosedWindow(windowStart.Add(time.Minute*15), windowStart.Add(time.Minute*20)),
  511. expectedStart: windowStart.Add(time.Minute * 15),
  512. expectedEnd: windowStart.Add(time.Minute * 20),
  513. result: &source.QueryResult{
  514. Values: []*util.Vector{
  515. {
  516. Timestamp: startFloat + (minute * 15),
  517. },
  518. {
  519. Timestamp: startFloat + (minute * 16),
  520. },
  521. {
  522. Timestamp: startFloat + (minute * 17),
  523. },
  524. {
  525. Timestamp: startFloat + (minute * 18),
  526. },
  527. {
  528. Timestamp: startFloat + (minute * 19),
  529. },
  530. {
  531. Timestamp: startFloat + (minute * 20),
  532. },
  533. },
  534. },
  535. },
  536. // Example: avg(node_total_hourly_cost{}) by (node, provider_id)[1m:1m]
  537. "1 minute resolution, 1 minute window": {
  538. resolution: time.Minute,
  539. window: opencost.NewClosedWindow(windowStart.Add(time.Minute*14).Add(time.Second*30), windowStart.Add(time.Minute*15).Add(time.Second*30)),
  540. expectedStart: windowStart.Add(time.Minute * 14).Add(time.Second * 30),
  541. expectedEnd: windowStart.Add(time.Minute * 15).Add(time.Second * 30),
  542. result: &source.QueryResult{
  543. Values: []*util.Vector{
  544. {
  545. Timestamp: startFloat + (minute * 15),
  546. },
  547. },
  548. },
  549. },
  550. // Example: avg(node_total_hourly_cost{}) by (node, provider_id)[1m:1m]
  551. "1 minute resolution, 1 minute window, at window start": {
  552. resolution: time.Minute,
  553. window: opencost.NewClosedWindow(windowStart, windowStart.Add(time.Second*30)),
  554. expectedStart: windowStart,
  555. expectedEnd: windowStart.Add(time.Second * 30),
  556. result: &source.QueryResult{
  557. Values: []*util.Vector{
  558. {
  559. Timestamp: startFloat,
  560. },
  561. },
  562. },
  563. },
  564. }
  565. for name, testCase := range testCases {
  566. t.Run(name, func(t *testing.T) {
  567. start, end := calculateStartAndEnd(testCase.result.Values, testCase.resolution, window)
  568. if !start.Equal(testCase.expectedStart) {
  569. t.Errorf("start does not match: expected %v; got %v", testCase.expectedStart, start)
  570. }
  571. if !end.Equal(testCase.expectedEnd) {
  572. t.Errorf("end does not match: expected %v; got %v", testCase.expectedEnd, end)
  573. }
  574. })
  575. }
  576. }