Skip to content

Commit 4639b03

Browse files
committed
test it, rework helpers to avoid mistakes
1 parent f0176c7 commit 4639b03

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

app/forms/disk-create.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { Radio } from '~/ui/lib/Radio'
3737
import { RadioGroup } from '~/ui/lib/RadioGroup'
3838
import { Slash } from '~/ui/lib/Slash'
3939
import { toLocaleDateString } from '~/util/date'
40+
import { roundUpDiskSize } from '~/util/math'
4041
import { bytesToGiB, GiB } from '~/util/units'
4142

4243
const blankDiskSource: DiskSource = {
@@ -227,8 +228,7 @@ const DiskSourceField = ({
227228
const image = images.find((i) => i.id === id)! // if it's selected, it must be present
228229
const imageSizeGiB = image.size / GiB
229230
if (diskSizeField.value < imageSizeGiB) {
230-
const nearest10 = Math.ceil(imageSizeGiB / 10) * 10
231-
diskSizeField.onChange(Math.min(nearest10, MAX_DISK_SIZE_GiB))
231+
diskSizeField.onChange(roundUpDiskSize(imageSizeGiB))
232232
}
233233
}}
234234
/>
@@ -288,8 +288,7 @@ const SnapshotSelectField = ({ control }: { control: Control<DiskCreate> }) => {
288288
const snapshot = snapshots.find((i) => i.id === id)! // if it's selected, it must be present
289289
const snapshotSizeGiB = snapshot.size / GiB
290290
if (diskSizeField.value < snapshotSizeGiB) {
291-
const nearest10 = Math.ceil(snapshotSizeGiB / 10) * 10
292-
diskSizeField.onChange(Math.min(nearest10, MAX_DISK_SIZE_GiB))
291+
diskSizeField.onChange(roundUpDiskSize(snapshotSizeGiB))
293292
}
294293
}}
295294
/>

app/util/math.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import * as R from 'remeda'
1010

11+
import { MAX_DISK_SIZE_GiB } from '~/api'
12+
1113
/**
1214
* Get the two parts of a number (before decimal and after-and-including
1315
* decimal) as strings. Round to 2 decimal points if necessary.
@@ -99,3 +101,11 @@ export function displayBigNum(
99101
export function nearest10(num: number): number {
100102
return Math.ceil(num / 10) * 10
101103
}
104+
105+
/**
106+
* Calculate disk size based on image or snapshot size. We round up to the
107+
* nearest 10, but also cap it at the max disk size so that, for example, a 1023
108+
* GiB image doesn't produce a 1030 GiB disk, which is not valid.
109+
*/
110+
export const roundUpDiskSize = (imageSizeGiB: number) =>
111+
Math.min(nearest10(imageSizeGiB), MAX_DISK_SIZE_GiB)

mock-api/snapshot.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { v4 as uuid } from 'uuid'
1010

1111
import type { Snapshot } from '@oxide/api'
1212

13+
import { GiB } from '~/util/units'
14+
1315
import { disks } from './disk'
1416
import type { Json } from './json-type'
1517
import { project } from './project'
@@ -81,7 +83,18 @@ export const snapshots: Json<Snapshot>[] = [
8183
project_id: project.id,
8284
time_created: new Date().toISOString(),
8385
time_modified: new Date().toISOString(),
84-
size: 1024 * 1024 * 1024 * 20,
86+
size: 20 * GiB,
87+
disk_id: disks[3].id,
88+
state: 'ready',
89+
},
90+
{
91+
id: '3b252b04-d896-49d3-bae3-0ac102eed9b4',
92+
name: 'snapshot-max-size',
93+
description: '',
94+
project_id: project.id,
95+
time_created: new Date().toISOString(),
96+
time_modified: new Date().toISOString(),
97+
size: 1023 * GiB, // the biggest size allowed
8598
disk_id: disks[3].id,
8699
state: 'ready',
87100
},

test/e2e/disks.e2e.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
expectNoToast,
1212
expectRowVisible,
1313
expectToast,
14-
expectVisible,
1514
test,
1615
} from './utils'
1716

@@ -60,14 +59,16 @@ test('Disk snapshot error', async ({ page }) => {
6059
test.describe('Disk create', () => {
6160
test.beforeEach(async ({ page }) => {
6261
await page.goto('/projects/mock-project/disks-new')
62+
await expect(page.getByRole('dialog', { name: 'Create disk' })).toBeVisible()
6363
await page.getByRole('textbox', { name: 'Name' }).fill('a-new-disk')
6464
})
6565

6666
test.afterEach(async ({ page }) => {
6767
await page.getByRole('button', { name: 'Create disk' }).click()
6868

69+
await expect(page.getByRole('dialog', { name: 'Create disk' })).toBeHidden()
6970
await expectToast(page, 'Disk a-new-disk created')
70-
await expectVisible(page, ['role=cell[name="a-new-disk"]'])
71+
await expect(page.getByRole('cell', { name: 'a-new-disk' })).toBeVisible()
7172
})
7273

7374
// expects are in the afterEach
@@ -83,6 +84,13 @@ test.describe('Disk create', () => {
8384
await page.getByRole('option', { name: 'delete-500' }).click()
8485
})
8586

87+
// max-size snapshot required a fix
88+
test('from max-size snapshot', async ({ page }) => {
89+
await page.getByRole('radio', { name: 'Snapshot' }).click()
90+
await page.getByRole('button', { name: 'Source snapshot' }).click()
91+
await page.getByRole('option', { name: 'snapshot-max' }).click()
92+
})
93+
8694
test('from image', async ({ page }) => {
8795
await page.getByRole('radio', { name: 'Image' }).click()
8896
await page.getByRole('button', { name: 'Source image' }).click()

0 commit comments

Comments
 (0)