DiskManagement.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { describe, expect, test } from 'vitest'
  2. import {
  3. calculateBaselineIopsForComputeSize,
  4. calculateComputeSizeRequiredForIops,
  5. calculateDiskSizePrice,
  6. calculateIOPSPrice,
  7. calculateMaxIopsAllowedForDiskSizeWithGp3,
  8. calculateMaxIopsForComputeSize,
  9. calculateThroughputPrice,
  10. mapAddOnVariantIdToComputeSize,
  11. mapComputeSizeNameToAddonVariantId,
  12. } from './DiskManagement.utils'
  13. import { DiskType } from './ui/DiskManagement.constants'
  14. describe('DiskManagement utils', () => {
  15. describe('mapComputeSizeNameToAddonVariantId', () => {
  16. test('maps known infra sizes to addon variant ids', () => {
  17. expect(mapComputeSizeNameToAddonVariantId('4xlarge')).toBe('ci_4xlarge')
  18. })
  19. test('falls back to nano for unknown infra sizes', () => {
  20. // @ts-expect-error intentional invalid value for runtime guard
  21. expect(mapComputeSizeNameToAddonVariantId('unknown-size')).toBe('ci_nano')
  22. })
  23. })
  24. describe('mapAddOnVariantIdToComputeSize', () => {
  25. test('maps known addon ids to display names', () => {
  26. expect(mapAddOnVariantIdToComputeSize('ci_4xlarge')).toBe('4XL')
  27. })
  28. test('falls back to Nano on invalid addon id', () => {
  29. // @ts-expect-error intentional invalid value for runtime guard
  30. expect(mapAddOnVariantIdToComputeSize('ci_invalid')).toBe('Nano')
  31. })
  32. })
  33. describe('calculateBaselineIopsForComputeSize / calculateMaxIopsForComputeSize', () => {
  34. test('returns 0 for invalid compute ids', () => {
  35. expect(calculateBaselineIopsForComputeSize('invalid')).toBe(0)
  36. expect(calculateMaxIopsForComputeSize('invalid')).toBe(0)
  37. })
  38. test('returns baseline and max for valid compute ids', () => {
  39. expect(calculateBaselineIopsForComputeSize('ci_2xlarge')).toBe(12000)
  40. expect(calculateMaxIopsForComputeSize('ci_2xlarge')).toBe(20000)
  41. })
  42. })
  43. describe('calculateComputeSizeRequiredForIops', () => {
  44. test('returns smallest size that satisfies requested IOPS', () => {
  45. expect(calculateComputeSizeRequiredForIops(500)).toBe('ci_nano')
  46. expect(calculateComputeSizeRequiredForIops(19000)).toBe('ci_large')
  47. expect(calculateComputeSizeRequiredForIops(45000)).toBe('ci_12xlarge')
  48. })
  49. test('falls back to largest size when exceeding known max', () => {
  50. const fallback = calculateComputeSizeRequiredForIops(500000)
  51. expect([
  52. 'ci_48xlarge',
  53. 'ci_48xlarge_optimized_cpu',
  54. 'ci_48xlarge_optimized_memory',
  55. 'ci_48xlarge_high_memory',
  56. ]).toContain(fallback)
  57. })
  58. })
  59. })
  60. describe('calculateMaxIopsAllowedForDiskSizeWithGp3', () => {
  61. // Regression: old code returned `3000 * size`, letting a 2 GB disk request 6000 IOPS
  62. // which the platform rejects. The real ceiling is 500 IOPS/GB capped at 16 000.
  63. test('caps a sub-6 GB disk at the 3000 IOPS floor (not 3000 × size)', () => {
  64. expect(calculateMaxIopsAllowedForDiskSizeWithGp3(2)).toBe(3000)
  65. })
  66. test('caps large disks at 16 000 IOPS', () => {
  67. expect(calculateMaxIopsAllowedForDiskSizeWithGp3(100)).toBe(16000)
  68. })
  69. })
  70. describe('DiskManagement.utils.ts:calculateDiskSizePrice', () => {
  71. test('GP3 with 8GB to GP3 with 10GB for pro plan', () => {
  72. const result = calculateDiskSizePrice({
  73. planId: 'pro',
  74. oldSize: 8,
  75. oldStorageType: DiskType.GP3,
  76. newSize: 10,
  77. newStorageType: DiskType.GP3,
  78. })
  79. expect(result.oldPrice).toBe('0.00')
  80. expect(result.newPrice).toBe('0.25')
  81. })
  82. test('IO2 with 8GB to IO2 with 10GB for pro plan', () => {
  83. const result = calculateDiskSizePrice({
  84. planId: 'pro',
  85. oldSize: 8,
  86. oldStorageType: DiskType.IO2,
  87. newSize: 10,
  88. newStorageType: DiskType.IO2,
  89. })
  90. expect(result.oldPrice).toBe('1.56')
  91. expect(result.newPrice).toBe('1.95')
  92. })
  93. test('GP3 with 8GB to GP3 with 10GB, with 2 replicas', () => {
  94. const result = calculateDiskSizePrice({
  95. planId: 'pro',
  96. oldSize: 8,
  97. oldStorageType: DiskType.GP3,
  98. newSize: 10,
  99. newStorageType: DiskType.GP3,
  100. numReplicas: 2,
  101. })
  102. expect(result.oldPrice).toBe('2.50')
  103. expect(result.newPrice).toBe('3.38')
  104. })
  105. })
  106. describe('DiskManagement.utils.ts:calculateIOPSPrice', () => {
  107. test('GP3 with 3000 to IO2 with 3000', () => {
  108. const result = calculateIOPSPrice({
  109. oldStorageType: DiskType.GP3,
  110. oldProvisionedIOPS: 3000,
  111. newStorageType: DiskType.IO2,
  112. newProvisionedIOPS: 3000,
  113. })
  114. expect(result.oldPrice).toBe('0.00')
  115. expect(result.newPrice).toBe('357.00')
  116. })
  117. test('GP3 with 3000 to GP3 with 5000', () => {
  118. const result = calculateIOPSPrice({
  119. oldStorageType: DiskType.GP3,
  120. oldProvisionedIOPS: 3000,
  121. newStorageType: DiskType.GP3,
  122. newProvisionedIOPS: 5000,
  123. })
  124. expect(result.oldPrice).toBe('0.00')
  125. expect(result.newPrice).toBe('48.00')
  126. })
  127. test('IO2 with 3000 to IO2 with 5000', () => {
  128. const result = calculateIOPSPrice({
  129. oldStorageType: DiskType.IO2,
  130. oldProvisionedIOPS: 3000,
  131. newStorageType: DiskType.IO2,
  132. newProvisionedIOPS: 5000,
  133. })
  134. expect(result.oldPrice).toBe('357.00')
  135. expect(result.newPrice).toBe('595.00')
  136. })
  137. })
  138. describe('DiskManagement.utils.ts:calculateThroughputPrice', () => {
  139. test('GP3 with 125 MB/s 150 MB/s', () => {
  140. const result = calculateThroughputPrice({
  141. storageType: DiskType.GP3,
  142. oldThroughput: 125,
  143. newThroughput: 150,
  144. })
  145. expect(result.oldPrice).toBe('0.00')
  146. expect(result.newPrice).toBe('2.38')
  147. })
  148. test('IO1 with 125 MB/s 150 MB/s', () => {
  149. const result = calculateThroughputPrice({
  150. storageType: DiskType.IO2,
  151. oldThroughput: 125,
  152. newThroughput: 150,
  153. })
  154. expect(result.oldPrice).toBe('0.00')
  155. expect(result.newPrice).toBe('0.00')
  156. })
  157. })