allocation_helpers_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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/util"
  8. "github.com/opencost/opencost/pkg/prom"
  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 []*prom.QueryResult
  196. resultsActiveMinutes []*prom.QueryResult
  197. expected map[pvKey]*pv
  198. }{
  199. "pvMap1": {
  200. resolution: time.Hour * 6,
  201. resultsPVCostPerGiBHour: []*prom.QueryResult{
  202. {
  203. Metric: map[string]interface{}{
  204. "cluster_id": "cluster1",
  205. "volumename": "pv1",
  206. },
  207. Values: []*util.Vector{
  208. {
  209. Value: 0.05,
  210. },
  211. },
  212. },
  213. {
  214. Metric: map[string]interface{}{
  215. "cluster_id": "cluster1",
  216. "volumename": "pv2",
  217. },
  218. Values: []*util.Vector{
  219. {
  220. Value: 0.05,
  221. },
  222. },
  223. },
  224. {
  225. Metric: map[string]interface{}{
  226. "cluster_id": "cluster2",
  227. "volumename": "pv3",
  228. },
  229. Values: []*util.Vector{
  230. {
  231. Value: 0.03,
  232. },
  233. },
  234. },
  235. {
  236. Metric: map[string]interface{}{
  237. "cluster_id": "cluster2",
  238. "volumename": "pv4",
  239. },
  240. Values: []*util.Vector{
  241. {
  242. Value: 0.05,
  243. },
  244. },
  245. },
  246. },
  247. resultsActiveMinutes: []*prom.QueryResult{
  248. {
  249. Metric: map[string]interface{}{
  250. "cluster_id": "cluster1",
  251. "persistentvolume": "pv1",
  252. },
  253. Values: []*util.Vector{
  254. {
  255. Timestamp: startFloat,
  256. },
  257. {
  258. Timestamp: startFloat + (hour * 6),
  259. },
  260. {
  261. Timestamp: startFloat + (hour * 12),
  262. },
  263. {
  264. Timestamp: startFloat + (hour * 18),
  265. },
  266. },
  267. },
  268. {
  269. Metric: map[string]interface{}{
  270. "cluster_id": "cluster1",
  271. "persistentvolume": "pv2",
  272. },
  273. Values: []*util.Vector{
  274. {
  275. Timestamp: startFloat,
  276. },
  277. {
  278. Timestamp: startFloat + (hour * 6),
  279. },
  280. {
  281. Timestamp: startFloat + (hour * 12),
  282. },
  283. {
  284. Timestamp: startFloat + (hour * 18),
  285. },
  286. {
  287. Timestamp: startFloat + (hour * 24),
  288. },
  289. },
  290. },
  291. {
  292. Metric: map[string]interface{}{
  293. "cluster_id": "cluster2",
  294. "persistentvolume": "pv3",
  295. },
  296. Values: []*util.Vector{
  297. {
  298. Timestamp: startFloat + (hour * 6),
  299. },
  300. {
  301. Timestamp: startFloat + (hour * 12),
  302. },
  303. {
  304. Timestamp: startFloat + (hour * 18),
  305. },
  306. },
  307. },
  308. {
  309. Metric: map[string]interface{}{
  310. "cluster_id": "cluster2",
  311. "persistentvolume": "pv4",
  312. },
  313. Values: []*util.Vector{
  314. {
  315. Timestamp: startFloat,
  316. },
  317. {
  318. Timestamp: startFloat + (hour * 6),
  319. },
  320. {
  321. Timestamp: startFloat + (hour * 12),
  322. },
  323. {
  324. Timestamp: startFloat + (hour * 18),
  325. },
  326. },
  327. },
  328. },
  329. expected: pvMap1NoBytes,
  330. },
  331. }
  332. for name, testCase := range testCases {
  333. t.Run(name, func(t *testing.T) {
  334. pvMap := make(map[pvKey]*pv)
  335. buildPVMap(testCase.resolution, pvMap, testCase.resultsPVCostPerGiBHour, testCase.resultsActiveMinutes, []*prom.QueryResult{}, window)
  336. if len(pvMap) != len(testCase.expected) {
  337. t.Errorf("pv map does not have the expected length %d : %d", len(pvMap), len(testCase.expected))
  338. }
  339. for thisPVKey, expectedPV := range testCase.expected {
  340. actualPV, ok := pvMap[thisPVKey]
  341. if !ok {
  342. t.Errorf("pv map is missing key %s", thisPVKey)
  343. }
  344. if !actualPV.equal(expectedPV) {
  345. t.Errorf("pv does not match with key %s: %s != %s", thisPVKey, opencost.NewClosedWindow(actualPV.Start, actualPV.End), opencost.NewClosedWindow(expectedPV.Start, expectedPV.End))
  346. }
  347. }
  348. })
  349. }
  350. }
  351. /* Helper Helpers */
  352. func TestGetUnmountedPodForCluster(t *testing.T) {
  353. testCases := map[string]struct {
  354. window opencost.Window
  355. podMap map[podKey]*pod
  356. cluster string
  357. expected *pod
  358. }{
  359. "create new": {
  360. window: window.Clone(),
  361. podMap: podMap1,
  362. cluster: "cluster1",
  363. expected: &pod{
  364. Window: window.Clone(),
  365. Start: *window.Start(),
  366. End: *window.End(),
  367. Key: getUnmountedPodKey("cluster1"),
  368. Allocations: map[string]*opencost.Allocation{
  369. opencost.UnmountedSuffix: {
  370. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster1", opencost.UnmountedSuffix, opencost.UnmountedSuffix, opencost.UnmountedSuffix),
  371. Properties: &opencost.AllocationProperties{
  372. Cluster: "cluster1",
  373. Node: "",
  374. Container: opencost.UnmountedSuffix,
  375. Namespace: opencost.UnmountedSuffix,
  376. Pod: opencost.UnmountedSuffix,
  377. },
  378. Window: window,
  379. Start: *window.Start(),
  380. End: *window.End(),
  381. },
  382. },
  383. },
  384. },
  385. "get existing": {
  386. window: window.Clone(),
  387. podMap: podMap1,
  388. cluster: "cluster2",
  389. expected: &pod{
  390. Window: window.Clone(),
  391. Start: *window.Start(),
  392. End: *window.End(),
  393. Key: getUnmountedPodKey("cluster2"),
  394. Allocations: map[string]*opencost.Allocation{
  395. opencost.UnmountedSuffix: {
  396. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster2", opencost.UnmountedSuffix, opencost.UnmountedSuffix, opencost.UnmountedSuffix),
  397. Properties: &opencost.AllocationProperties{
  398. Cluster: "cluster2",
  399. Node: "",
  400. Container: opencost.UnmountedSuffix,
  401. Namespace: opencost.UnmountedSuffix,
  402. Pod: opencost.UnmountedSuffix,
  403. Services: []string{"LB1"},
  404. },
  405. Window: window,
  406. Start: *window.Start(),
  407. End: *window.End(),
  408. LoadBalancerCost: .60,
  409. LoadBalancerCostAdjustment: 0,
  410. PVs: opencost.PVAllocations{
  411. kcPVKey2: &opencost.PVAllocation{
  412. ByteHours: 24 * Gi,
  413. Cost: 2.25,
  414. },
  415. },
  416. },
  417. },
  418. },
  419. },
  420. }
  421. for name, testCase := range testCases {
  422. t.Run(name, func(t *testing.T) {
  423. actual := getUnmountedPodForCluster(testCase.window, testCase.podMap, testCase.cluster)
  424. if !actual.equal(testCase.expected) {
  425. t.Errorf("Unmounted pod does not match expectation")
  426. }
  427. })
  428. }
  429. }
  430. func TestCalculateStartAndEnd(t *testing.T) {
  431. testCases := map[string]struct {
  432. resolution time.Duration
  433. expectedStart time.Time
  434. expectedEnd time.Time
  435. result *prom.QueryResult
  436. }{
  437. "1 hour resolution, 1 hour window": {
  438. resolution: time.Hour,
  439. expectedStart: windowStart,
  440. expectedEnd: windowStart.Add(time.Hour),
  441. result: &prom.QueryResult{
  442. Values: []*util.Vector{
  443. {
  444. Timestamp: startFloat,
  445. },
  446. {
  447. Timestamp: startFloat + (minute * 60),
  448. },
  449. },
  450. },
  451. },
  452. "30 minute resolution, 1 hour window": {
  453. resolution: time.Minute * 30,
  454. expectedStart: windowStart,
  455. expectedEnd: windowStart.Add(time.Hour),
  456. result: &prom.QueryResult{
  457. Values: []*util.Vector{
  458. {
  459. Timestamp: startFloat,
  460. },
  461. {
  462. Timestamp: startFloat + (minute * 30),
  463. },
  464. {
  465. Timestamp: startFloat + (minute * 60),
  466. },
  467. },
  468. },
  469. },
  470. "15 minute resolution, 45 minute window": {
  471. resolution: time.Minute * 15,
  472. expectedStart: windowStart,
  473. expectedEnd: windowStart.Add(time.Minute * 45),
  474. result: &prom.QueryResult{
  475. Values: []*util.Vector{
  476. {
  477. Timestamp: startFloat + (minute * 0),
  478. },
  479. {
  480. Timestamp: startFloat + (minute * 15),
  481. },
  482. {
  483. Timestamp: startFloat + (minute * 30),
  484. },
  485. {
  486. Timestamp: startFloat + (minute * 45),
  487. },
  488. },
  489. },
  490. },
  491. "1 minute resolution, 5 minute window": {
  492. resolution: time.Minute,
  493. expectedStart: windowStart.Add(time.Minute * 15),
  494. expectedEnd: windowStart.Add(time.Minute * 20),
  495. result: &prom.QueryResult{
  496. Values: []*util.Vector{
  497. {
  498. Timestamp: startFloat + (minute * 15),
  499. },
  500. {
  501. Timestamp: startFloat + (minute * 16),
  502. },
  503. {
  504. Timestamp: startFloat + (minute * 17),
  505. },
  506. {
  507. Timestamp: startFloat + (minute * 18),
  508. },
  509. {
  510. Timestamp: startFloat + (minute * 19),
  511. },
  512. {
  513. Timestamp: startFloat + (minute * 20),
  514. },
  515. },
  516. },
  517. },
  518. "1 minute resolution, 1 minute window": {
  519. resolution: time.Minute,
  520. expectedStart: windowStart.Add(time.Minute * 14).Add(time.Second * 30),
  521. expectedEnd: windowStart.Add(time.Minute * 15).Add(time.Second * 30),
  522. result: &prom.QueryResult{
  523. Values: []*util.Vector{
  524. {
  525. Timestamp: startFloat + (minute * 15),
  526. },
  527. },
  528. },
  529. },
  530. "1 minute resolution, 1 minute window, at window start": {
  531. resolution: time.Minute,
  532. expectedStart: windowStart,
  533. expectedEnd: windowStart.Add(time.Second * 30),
  534. result: &prom.QueryResult{
  535. Values: []*util.Vector{
  536. {
  537. Timestamp: startFloat,
  538. },
  539. },
  540. },
  541. },
  542. }
  543. for name, testCase := range testCases {
  544. t.Run(name, func(t *testing.T) {
  545. start, end := calculateStartAndEnd(testCase.result, testCase.resolution, window)
  546. if !start.Equal(testCase.expectedStart) {
  547. t.Errorf("start does not match: expected %v; got %v", testCase.expectedStart, start)
  548. }
  549. if !end.Equal(testCase.expectedEnd) {
  550. t.Errorf("end does not match: expected %v; got %v", testCase.expectedEnd, end)
  551. }
  552. })
  553. }
  554. }