allocation_helpers_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 minute = 60.0
  14. const hour = minute * 60.0
  15. var windowStart = time.Date(2020, 6, 16, 0, 0, 0, 0, time.UTC)
  16. var windowEnd = time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC)
  17. var window = opencost.NewWindow(&windowStart, &windowEnd)
  18. var startFloat = float64(windowStart.Unix())
  19. var podKey1 = podKey{
  20. namespaceKey: namespaceKey{
  21. Cluster: "cluster1",
  22. Namespace: "namespace1",
  23. },
  24. Pod: "pod1",
  25. }
  26. var podKey2 = podKey{
  27. namespaceKey: namespaceKey{
  28. Cluster: "cluster1",
  29. Namespace: "namespace1",
  30. },
  31. Pod: "pod2",
  32. }
  33. var podKey3 = podKey{
  34. namespaceKey: namespaceKey{
  35. Cluster: "cluster2",
  36. Namespace: "namespace2",
  37. },
  38. Pod: "pod3",
  39. }
  40. var podKey4 = podKey{
  41. namespaceKey: namespaceKey{
  42. Cluster: "cluster2",
  43. Namespace: "namespace2",
  44. },
  45. Pod: "pod4",
  46. }
  47. var podKeyUnmounted = podKey{
  48. namespaceKey: namespaceKey{
  49. Cluster: "cluster2",
  50. Namespace: opencost.UnmountedSuffix,
  51. },
  52. Pod: opencost.UnmountedSuffix,
  53. }
  54. var kcPVKey1 = opencost.PVKey{
  55. Cluster: "cluster1",
  56. Name: "pv1",
  57. }
  58. var kcPVKey2 = opencost.PVKey{
  59. Cluster: "cluster1",
  60. Name: "pv2",
  61. }
  62. var kcPVKey3 = opencost.PVKey{
  63. Cluster: "cluster2",
  64. Name: "pv3",
  65. }
  66. var kcPVKey4 = opencost.PVKey{
  67. Cluster: "cluster2",
  68. Name: "pv4",
  69. }
  70. var podMap1 = map[podKey]*pod{
  71. podKey1: {
  72. Window: window.Clone(),
  73. Start: time.Date(2020, 6, 16, 0, 0, 0, 0, time.UTC),
  74. End: time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC),
  75. Key: podKey1,
  76. Allocations: nil,
  77. },
  78. podKey2: {
  79. Window: window.Clone(),
  80. Start: time.Date(2020, 6, 16, 12, 0, 0, 0, time.UTC),
  81. End: time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC),
  82. Key: podKey2,
  83. Allocations: nil,
  84. },
  85. podKey3: {
  86. Window: window.Clone(),
  87. Start: time.Date(2020, 6, 16, 6, 30, 0, 0, time.UTC),
  88. End: time.Date(2020, 6, 17, 18, 12, 33, 0, time.UTC),
  89. Key: podKey3,
  90. Allocations: nil,
  91. },
  92. podKey4: {
  93. Window: window.Clone(),
  94. Start: time.Date(2020, 6, 16, 0, 0, 0, 0, time.UTC),
  95. End: time.Date(2020, 6, 17, 13, 0, 0, 0, time.UTC),
  96. Key: podKey4,
  97. Allocations: nil,
  98. },
  99. podKeyUnmounted: {
  100. Window: window.Clone(),
  101. Start: *window.Start(),
  102. End: *window.End(),
  103. Key: podKeyUnmounted,
  104. Allocations: map[string]*opencost.Allocation{
  105. opencost.UnmountedSuffix: {
  106. Name: fmt.Sprintf("%s/%s/%s/%s", podKeyUnmounted.Cluster, podKeyUnmounted.Namespace, podKeyUnmounted.Pod, opencost.UnmountedSuffix),
  107. Properties: &opencost.AllocationProperties{
  108. Cluster: podKeyUnmounted.Cluster,
  109. Node: "",
  110. Container: opencost.UnmountedSuffix,
  111. Namespace: podKeyUnmounted.Namespace,
  112. Pod: podKeyUnmounted.Pod,
  113. Services: []string{"LB1"},
  114. },
  115. Window: window,
  116. Start: *window.Start(),
  117. End: *window.End(),
  118. LoadBalancerCost: 0.60,
  119. LoadBalancerCostAdjustment: 0,
  120. PVs: opencost.PVAllocations{
  121. kcPVKey2: &opencost.PVAllocation{
  122. ByteHours: 24 * Gi,
  123. Cost: 2.25,
  124. },
  125. },
  126. },
  127. },
  128. },
  129. }
  130. var pvKey1 = pvKey{
  131. Cluster: "cluster1",
  132. PersistentVolume: "pv1",
  133. }
  134. var pvKey2 = pvKey{
  135. Cluster: "cluster1",
  136. PersistentVolume: "pv2",
  137. }
  138. var pvKey3 = pvKey{
  139. Cluster: "cluster2",
  140. PersistentVolume: "pv3",
  141. }
  142. var pvKey4 = pvKey{
  143. Cluster: "cluster2",
  144. PersistentVolume: "pv4",
  145. }
  146. var pvMap1 = map[pvKey]*pv{
  147. pvKey1: {
  148. Start: windowStart,
  149. End: windowEnd.Add(time.Hour * -6),
  150. Bytes: 20 * Gi,
  151. CostPerGiBHour: 0.05,
  152. Cluster: "cluster1",
  153. Name: "pv1",
  154. StorageClass: "class1",
  155. },
  156. pvKey2: {
  157. Start: windowStart,
  158. End: windowEnd,
  159. Bytes: 100 * Gi,
  160. CostPerGiBHour: 0.05,
  161. Cluster: "cluster1",
  162. Name: "pv2",
  163. StorageClass: "class1",
  164. },
  165. pvKey3: {
  166. Start: windowStart.Add(time.Hour * 6),
  167. End: windowEnd.Add(time.Hour * -6),
  168. Bytes: 50 * Gi,
  169. CostPerGiBHour: 0.03,
  170. Cluster: "cluster2",
  171. Name: "pv3",
  172. StorageClass: "class2",
  173. },
  174. pvKey4: {
  175. Start: windowStart,
  176. End: windowEnd.Add(time.Hour * -6),
  177. Bytes: 30 * Gi,
  178. CostPerGiBHour: 0.05,
  179. Cluster: "cluster2",
  180. Name: "pv4",
  181. StorageClass: "class1",
  182. },
  183. }
  184. /* pv/pvc Helpers */
  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. /* Helper Helpers */
  363. func TestGetUnmountedPodForCluster(t *testing.T) {
  364. testCases := map[string]struct {
  365. window opencost.Window
  366. podMap map[podKey]*pod
  367. cluster string
  368. expected *pod
  369. }{
  370. "create new": {
  371. window: window.Clone(),
  372. podMap: podMap1,
  373. cluster: "cluster1",
  374. expected: &pod{
  375. Window: window.Clone(),
  376. Start: *window.Start(),
  377. End: *window.End(),
  378. Key: getUnmountedPodKey("cluster1"),
  379. Allocations: map[string]*opencost.Allocation{
  380. opencost.UnmountedSuffix: {
  381. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster1", opencost.UnmountedSuffix, opencost.UnmountedSuffix, opencost.UnmountedSuffix),
  382. Properties: &opencost.AllocationProperties{
  383. Cluster: "cluster1",
  384. Node: "",
  385. Container: opencost.UnmountedSuffix,
  386. Namespace: opencost.UnmountedSuffix,
  387. Pod: opencost.UnmountedSuffix,
  388. },
  389. Window: window,
  390. Start: *window.Start(),
  391. End: *window.End(),
  392. },
  393. },
  394. },
  395. },
  396. "get existing": {
  397. window: window.Clone(),
  398. podMap: podMap1,
  399. cluster: "cluster2",
  400. expected: &pod{
  401. Window: window.Clone(),
  402. Start: *window.Start(),
  403. End: *window.End(),
  404. Key: getUnmountedPodKey("cluster2"),
  405. Allocations: map[string]*opencost.Allocation{
  406. opencost.UnmountedSuffix: {
  407. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster2", opencost.UnmountedSuffix, opencost.UnmountedSuffix, opencost.UnmountedSuffix),
  408. Properties: &opencost.AllocationProperties{
  409. Cluster: "cluster2",
  410. Node: "",
  411. Container: opencost.UnmountedSuffix,
  412. Namespace: opencost.UnmountedSuffix,
  413. Pod: opencost.UnmountedSuffix,
  414. Services: []string{"LB1"},
  415. },
  416. Window: window,
  417. Start: *window.Start(),
  418. End: *window.End(),
  419. LoadBalancerCost: .60,
  420. LoadBalancerCostAdjustment: 0,
  421. PVs: opencost.PVAllocations{
  422. kcPVKey2: &opencost.PVAllocation{
  423. ByteHours: 24 * Gi,
  424. Cost: 2.25,
  425. },
  426. },
  427. },
  428. },
  429. },
  430. },
  431. }
  432. for name, testCase := range testCases {
  433. t.Run(name, func(t *testing.T) {
  434. actual := getUnmountedPodForCluster(testCase.window, testCase.podMap, testCase.cluster)
  435. if !actual.equal(testCase.expected) {
  436. t.Errorf("Unmounted pod does not match expectation")
  437. }
  438. })
  439. }
  440. }
  441. func TestCalculateStartAndEnd(t *testing.T) {
  442. testCases := map[string]struct {
  443. resolution time.Duration
  444. expectedStart time.Time
  445. expectedEnd time.Time
  446. result *source.QueryResult
  447. }{
  448. "1 hour resolution, 1 hour window": {
  449. resolution: time.Hour,
  450. expectedStart: windowStart,
  451. expectedEnd: windowStart.Add(time.Hour),
  452. result: &source.QueryResult{
  453. Values: []*util.Vector{
  454. {
  455. Timestamp: startFloat,
  456. },
  457. {
  458. Timestamp: startFloat + (minute * 60),
  459. },
  460. },
  461. },
  462. },
  463. "30 minute resolution, 1 hour window": {
  464. resolution: time.Minute * 30,
  465. expectedStart: windowStart,
  466. expectedEnd: windowStart.Add(time.Hour),
  467. result: &source.QueryResult{
  468. Values: []*util.Vector{
  469. {
  470. Timestamp: startFloat,
  471. },
  472. {
  473. Timestamp: startFloat + (minute * 30),
  474. },
  475. {
  476. Timestamp: startFloat + (minute * 60),
  477. },
  478. },
  479. },
  480. },
  481. "15 minute resolution, 45 minute window": {
  482. resolution: time.Minute * 15,
  483. expectedStart: windowStart,
  484. expectedEnd: windowStart.Add(time.Minute * 45),
  485. result: &source.QueryResult{
  486. Values: []*util.Vector{
  487. {
  488. Timestamp: startFloat + (minute * 0),
  489. },
  490. {
  491. Timestamp: startFloat + (minute * 15),
  492. },
  493. {
  494. Timestamp: startFloat + (minute * 30),
  495. },
  496. {
  497. Timestamp: startFloat + (minute * 45),
  498. },
  499. },
  500. },
  501. },
  502. "1 minute resolution, 5 minute window": {
  503. resolution: time.Minute,
  504. expectedStart: windowStart.Add(time.Minute * 15),
  505. expectedEnd: windowStart.Add(time.Minute * 20),
  506. result: &source.QueryResult{
  507. Values: []*util.Vector{
  508. {
  509. Timestamp: startFloat + (minute * 15),
  510. },
  511. {
  512. Timestamp: startFloat + (minute * 16),
  513. },
  514. {
  515. Timestamp: startFloat + (minute * 17),
  516. },
  517. {
  518. Timestamp: startFloat + (minute * 18),
  519. },
  520. {
  521. Timestamp: startFloat + (minute * 19),
  522. },
  523. {
  524. Timestamp: startFloat + (minute * 20),
  525. },
  526. },
  527. },
  528. },
  529. "1 minute resolution, 1 minute window": {
  530. resolution: time.Minute,
  531. expectedStart: windowStart.Add(time.Minute * 14).Add(time.Second * 30),
  532. expectedEnd: windowStart.Add(time.Minute * 15).Add(time.Second * 30),
  533. result: &source.QueryResult{
  534. Values: []*util.Vector{
  535. {
  536. Timestamp: startFloat + (minute * 15),
  537. },
  538. },
  539. },
  540. },
  541. "1 minute resolution, 1 minute window, at window start": {
  542. resolution: time.Minute,
  543. expectedStart: windowStart,
  544. expectedEnd: windowStart.Add(time.Second * 30),
  545. result: &source.QueryResult{
  546. Values: []*util.Vector{
  547. {
  548. Timestamp: startFloat,
  549. },
  550. },
  551. },
  552. },
  553. }
  554. for name, testCase := range testCases {
  555. t.Run(name, func(t *testing.T) {
  556. start, end := calculateStartAndEnd(testCase.result.Values, testCase.resolution, window)
  557. if !start.Equal(testCase.expectedStart) {
  558. t.Errorf("start does not match: expected %v; got %v", testCase.expectedStart, start)
  559. }
  560. if !end.Equal(testCase.expectedEnd) {
  561. t.Errorf("end does not match: expected %v; got %v", testCase.expectedEnd, end)
  562. }
  563. })
  564. }
  565. }