allocation_helpers_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. package costmodel
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/pkg/kubecost"
  5. "github.com/opencost/opencost/pkg/prom"
  6. "github.com/opencost/opencost/pkg/util"
  7. "testing"
  8. "time"
  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 = kubecost.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: kubecost.UnmountedSuffix,
  51. },
  52. Pod: kubecost.UnmountedSuffix,
  53. }
  54. var kcPVKey1 = kubecost.PVKey{
  55. Cluster: "cluster1",
  56. Name: "pv1",
  57. }
  58. var kcPVKey2 = kubecost.PVKey{
  59. Cluster: "cluster1",
  60. Name: "pv2",
  61. }
  62. var kcPVKey3 = kubecost.PVKey{
  63. Cluster: "cluster2",
  64. Name: "pv3",
  65. }
  66. var kcPVKey4 = kubecost.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]*kubecost.Allocation{
  105. kubecost.UnmountedSuffix: {
  106. Name: fmt.Sprintf("%s/%s/%s/%s", podKeyUnmounted.Cluster, podKeyUnmounted.Namespace, podKeyUnmounted.Pod, kubecost.UnmountedSuffix),
  107. Properties: &kubecost.AllocationProperties{
  108. Cluster: podKeyUnmounted.Cluster,
  109. Node: "",
  110. Container: kubecost.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: kubecost.PVAllocations{
  121. kcPVKey2: &kubecost.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 + (hour * 6),
  256. },
  257. {
  258. Timestamp: startFloat + (hour * 12),
  259. },
  260. {
  261. Timestamp: startFloat + (hour * 18),
  262. },
  263. },
  264. },
  265. {
  266. Metric: map[string]interface{}{
  267. "cluster_id": "cluster1",
  268. "persistentvolume": "pv2",
  269. },
  270. Values: []*util.Vector{
  271. {
  272. Timestamp: startFloat + (hour * 6),
  273. },
  274. {
  275. Timestamp: startFloat + (hour * 12),
  276. },
  277. {
  278. Timestamp: startFloat + (hour * 18),
  279. },
  280. {
  281. Timestamp: startFloat + (hour * 24),
  282. },
  283. },
  284. },
  285. {
  286. Metric: map[string]interface{}{
  287. "cluster_id": "cluster2",
  288. "persistentvolume": "pv3",
  289. },
  290. Values: []*util.Vector{
  291. {
  292. Timestamp: startFloat + (hour * 12),
  293. },
  294. {
  295. Timestamp: startFloat + (hour * 18),
  296. },
  297. },
  298. },
  299. {
  300. Metric: map[string]interface{}{
  301. "cluster_id": "cluster2",
  302. "persistentvolume": "pv4",
  303. },
  304. Values: []*util.Vector{
  305. {
  306. Timestamp: startFloat + (hour * 6),
  307. },
  308. {
  309. Timestamp: startFloat + (hour * 12),
  310. },
  311. {
  312. Timestamp: startFloat + (hour * 18),
  313. },
  314. },
  315. },
  316. },
  317. expected: pvMap1NoBytes,
  318. },
  319. }
  320. for name, testCase := range testCases {
  321. t.Run(name, func(t *testing.T) {
  322. pvMap := make(map[pvKey]*pv)
  323. buildPVMap(testCase.resolution, pvMap, testCase.resultsPVCostPerGiBHour, testCase.resultsActiveMinutes)
  324. if len(pvMap) != len(testCase.expected) {
  325. t.Errorf("pv map does not have the expected length %d : %d", len(pvMap), len(testCase.expected))
  326. }
  327. for thisPVKey, expectedPV := range testCase.expected {
  328. actualPV, ok := pvMap[thisPVKey]
  329. if !ok {
  330. t.Errorf("pv map is missing key %s", thisPVKey)
  331. }
  332. if !actualPV.equal(expectedPV) {
  333. t.Errorf("pv does not match with key %s", thisPVKey)
  334. }
  335. }
  336. })
  337. }
  338. }
  339. /* Helper Helpers */
  340. func TestGetUnmountedPodForCluster(t *testing.T) {
  341. testCases := map[string]struct {
  342. window kubecost.Window
  343. podMap map[podKey]*pod
  344. cluster string
  345. expected *pod
  346. }{
  347. "create new": {
  348. window: window.Clone(),
  349. podMap: podMap1,
  350. cluster: "cluster1",
  351. expected: &pod{
  352. Window: window.Clone(),
  353. Start: *window.Start(),
  354. End: *window.End(),
  355. Key: getUnmountedPodKey("cluster1"),
  356. Allocations: map[string]*kubecost.Allocation{
  357. kubecost.UnmountedSuffix: {
  358. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster1", kubecost.UnmountedSuffix, kubecost.UnmountedSuffix, kubecost.UnmountedSuffix),
  359. Properties: &kubecost.AllocationProperties{
  360. Cluster: "cluster1",
  361. Node: "",
  362. Container: kubecost.UnmountedSuffix,
  363. Namespace: kubecost.UnmountedSuffix,
  364. Pod: kubecost.UnmountedSuffix,
  365. },
  366. Window: window,
  367. Start: *window.Start(),
  368. End: *window.End(),
  369. },
  370. },
  371. },
  372. },
  373. "get existing": {
  374. window: window.Clone(),
  375. podMap: podMap1,
  376. cluster: "cluster2",
  377. expected: &pod{
  378. Window: window.Clone(),
  379. Start: *window.Start(),
  380. End: *window.End(),
  381. Key: getUnmountedPodKey("cluster2"),
  382. Allocations: map[string]*kubecost.Allocation{
  383. kubecost.UnmountedSuffix: {
  384. Name: fmt.Sprintf("%s/%s/%s/%s", "cluster2", kubecost.UnmountedSuffix, kubecost.UnmountedSuffix, kubecost.UnmountedSuffix),
  385. Properties: &kubecost.AllocationProperties{
  386. Cluster: "cluster2",
  387. Node: "",
  388. Container: kubecost.UnmountedSuffix,
  389. Namespace: kubecost.UnmountedSuffix,
  390. Pod: kubecost.UnmountedSuffix,
  391. Services: []string{"LB1"},
  392. },
  393. Window: window,
  394. Start: *window.Start(),
  395. End: *window.End(),
  396. LoadBalancerCost: .60,
  397. LoadBalancerCostAdjustment: 0,
  398. PVs: kubecost.PVAllocations{
  399. kcPVKey2: &kubecost.PVAllocation{
  400. ByteHours: 24 * Gi,
  401. Cost: 2.25,
  402. },
  403. },
  404. },
  405. },
  406. },
  407. },
  408. }
  409. for name, testCase := range testCases {
  410. t.Run(name, func(t *testing.T) {
  411. actual := getUnmountedPodForCluster(testCase.window, testCase.podMap, testCase.cluster)
  412. if !actual.equal(testCase.expected) {
  413. t.Errorf("Unmounted pod does not match expectation")
  414. }
  415. })
  416. }
  417. }
  418. func TestCalculateStartAndEnd(t *testing.T) {
  419. testCases := map[string]struct {
  420. resolution time.Duration
  421. expectedStart time.Time
  422. expectedEnd time.Time
  423. result *prom.QueryResult
  424. }{
  425. "1 hour resolution, 1 hour window": {
  426. resolution: time.Hour,
  427. expectedStart: windowStart,
  428. expectedEnd: windowStart.Add(time.Hour),
  429. result: &prom.QueryResult{
  430. Values: []*util.Vector{
  431. {
  432. Timestamp: startFloat + (minute * 60),
  433. },
  434. },
  435. },
  436. },
  437. "30 minute resolution, 1 hour window": {
  438. resolution: time.Minute * 30,
  439. expectedStart: windowStart,
  440. expectedEnd: windowStart.Add(time.Hour),
  441. result: &prom.QueryResult{
  442. Values: []*util.Vector{
  443. {
  444. Timestamp: startFloat + (minute * 30),
  445. },
  446. {
  447. Timestamp: startFloat + (minute * 60),
  448. },
  449. },
  450. },
  451. },
  452. "15 minute resolution, 45 minute window": {
  453. resolution: time.Minute * 15,
  454. expectedStart: windowStart.Add(time.Minute * -15),
  455. expectedEnd: windowStart.Add(time.Minute * 30),
  456. result: &prom.QueryResult{
  457. Values: []*util.Vector{
  458. {
  459. Timestamp: startFloat + (minute * 0),
  460. },
  461. {
  462. Timestamp: startFloat + (minute * 15),
  463. },
  464. {
  465. Timestamp: startFloat + (minute * 30),
  466. },
  467. },
  468. },
  469. },
  470. }
  471. for name, testCase := range testCases {
  472. t.Run(name, func(t *testing.T) {
  473. start, end := calculateStartAndEnd(testCase.result, testCase.resolution)
  474. if !start.Equal(testCase.expectedStart) {
  475. t.Errorf("start to not match expected %v : %v", start, testCase.expectedStart)
  476. }
  477. if !end.Equal(testCase.expectedEnd) {
  478. t.Errorf("end to not match expected %v : %v", end, testCase.expectedEnd)
  479. }
  480. })
  481. }
  482. }