Skip to content

Commit 06f4b86

Browse files
authored
Instance state changes asynchronously in mock API (#2167)
* add delays, some tests fail * make the tests pass
1 parent a25e84e commit 06f4b86

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

mock-api/msw/handlers.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,20 @@ export const handlers = makeHandlers({
474474
project_id: project.id,
475475
...pick(body, 'name', 'description', 'hostname', 'memory', 'ncpus'),
476476
...getTimestamps(),
477-
run_state: 'running',
477+
run_state: 'creating',
478478
time_run_state_updated: new Date().toISOString(),
479479
}
480+
481+
setTimeout(() => {
482+
newInstance.run_state = 'starting'
483+
}, 1000)
484+
485+
setTimeout(() => {
486+
newInstance.run_state = 'running'
487+
}, 5000)
488+
480489
db.instances.push(newInstance)
490+
481491
return json(newInstance, { status: 201 })
482492
},
483493
instanceView: ({ path, query }) => lookup.instance({ ...path, ...query }),
@@ -599,7 +609,7 @@ export const handlers = makeHandlers({
599609

600610
setTimeout(() => {
601611
instance.run_state = 'running'
602-
}, 3000)
612+
}, 1000)
603613

604614
return json(instance, { status: 202 })
605615
},
@@ -609,13 +619,21 @@ export const handlers = makeHandlers({
609619
},
610620
instanceStart({ path, query }) {
611621
const instance = lookup.instance({ ...path, ...query })
612-
instance.run_state = 'running'
622+
instance.run_state = 'starting'
623+
624+
setTimeout(() => {
625+
instance.run_state = 'running'
626+
}, 1000)
613627

614628
return json(instance, { status: 202 })
615629
},
616630
instanceStop({ path, query }) {
617631
const instance = lookup.instance({ ...path, ...query })
618-
instance.run_state = 'stopped'
632+
instance.run_state = 'stopping'
633+
634+
setTimeout(() => {
635+
instance.run_state = 'stopped'
636+
}, 1000)
619637

620638
return json(instance, { status: 202 })
621639
},

test/e2e/instance.e2e.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { expect, test } from './utils'
8+
import { expect, refreshInstance, sleep, test } from './utils'
99

1010
test('can delete a failed instance', async ({ page }) => {
1111
await page.goto('/projects/mock-project/instances')
@@ -36,6 +36,9 @@ test('can stop and delete a running instance', async ({ page }) => {
3636
await page.getByRole('menuitem', { name: 'Stop' }).click()
3737
await page.getByRole('button', { name: 'Confirm' }).click()
3838

39+
await sleep(3000)
40+
await refreshInstance(page)
41+
3942
// now it's stopped
4043
await expect(row.getByRole('cell', { name: /stopped/ })).toBeVisible()
4144

@@ -58,6 +61,9 @@ test('can stop a starting instance', async ({ page }) => {
5861
await page.getByRole('menuitem', { name: 'Stop' }).click()
5962
await page.getByRole('button', { name: 'Confirm' }).click()
6063

64+
await sleep(3000)
65+
await refreshInstance(page)
66+
6167
await expect(row.getByRole('cell', { name: /stopped/ })).toBeVisible()
6268
})
6369

test/e2e/network-interface-create.e2e.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
*/
88
import { test } from '@playwright/test'
99

10-
import { expect, expectRowVisible } from './utils'
10+
import { expect, expectRowVisible, stopInstance } from './utils'
1111

1212
test('can create a NIC with a specified IP address', async ({ page }) => {
1313
// go to an instance’s Network Interfaces page
1414
await page.goto('/projects/mock-project/instances/db1/network-interfaces')
1515

16-
// stop the instance
17-
await page.getByRole('button', { name: 'Instance actions' }).click()
18-
await page.getByRole('menuitem', { name: 'Stop' }).click()
19-
await page.getByRole('button', { name: 'Confirm' }).click()
16+
await stopInstance(page)
2017

2118
// open the add network interface side modal
2219
await page.getByRole('button', { name: 'Add network interface' }).click()
@@ -43,10 +40,7 @@ test('can create a NIC with a blank IP address', async ({ page }) => {
4340
// go to an instance’s Network Interfaces page
4441
await page.goto('/projects/mock-project/instances/db1/network-interfaces')
4542

46-
// stop the instance
47-
await page.getByRole('button', { name: 'Instance actions' }).click()
48-
await page.getByRole('menuitem', { name: 'Stop' }).click()
49-
await page.getByRole('button', { name: 'Confirm' }).click()
43+
await stopInstance(page)
5044

5145
// open the add network interface side modal
5246
await page.getByRole('button', { name: 'Add network interface' }).click()

test/e2e/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,17 @@ export async function expectRowVisible(
104104
}
105105

106106
export async function stopInstance(page: Page) {
107-
await page.click('role=button[name="Instance actions"]')
108-
await page.click('role=menuitem[name="Stop"]')
109-
await page.click('role=button[name="Confirm"]')
107+
await page.getByRole('button', { name: 'Instance actions' }).click()
108+
await page.getByRole('menuitem', { name: 'Stop' }).click()
109+
await page.getByRole('button', { name: 'Confirm' }).click()
110110
await closeToast(page)
111+
await sleep(1200)
112+
await refreshInstance(page)
113+
await expect(page.getByText('statusstopped')).toBeVisible()
114+
}
115+
116+
export async function refreshInstance(page: Page) {
117+
await page.getByRole('button', { name: 'Refresh data' }).click()
111118
}
112119

113120
/**

test/e2e/z-index.e2e.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { expect, test } from '@playwright/test'
99

10-
import { expectObscured } from './utils'
10+
import { expectObscured, stopInstance } from './utils'
1111

1212
test('Dropdown content can scroll off page and doesn’t hide TopBar', async ({ page }) => {
1313
// load the page
@@ -45,10 +45,7 @@ test('Dropdown content in SidebarModal shows on screen', async ({ page }) => {
4545
// go to an instance’s Network Interfaces page
4646
await page.goto('/projects/mock-project/instances/db1/network-interfaces')
4747

48-
// stop the instance
49-
await page.getByRole('button', { name: 'Instance actions' }).click()
50-
await page.getByRole('menuitem', { name: 'Stop' }).click()
51-
await page.getByRole('button', { name: 'Confirm' }).click()
48+
await stopInstance(page)
5249

5350
// open the add network interface side modal
5451
await page.getByRole('button', { name: 'Add network interface' }).click()

0 commit comments

Comments
 (0)