From e4d48cf69015fc2350053c8a695afc566077bcc4 Mon Sep 17 00:00:00 2001 From: Alessandro Menduni Date: Mon, 31 Oct 2022 12:00:27 +0100 Subject: [PATCH 1/3] Refactor CreateCluster to get selectedRegion as input --- frontend/src/model.tsx | 2 +- frontend/src/old-pages/Configure/Create.tsx | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/frontend/src/model.tsx b/frontend/src/model.tsx index edc20f76..675d29bc 100644 --- a/frontend/src/model.tsx +++ b/frontend/src/model.tsx @@ -97,12 +97,12 @@ function CreateCluster( clusterName: any, clusterConfig: any, region: string, + selectedRegion: string, disableRollback = false, dryrun = false, successCallback?: Callback, errorCallback?: Callback, ) { - const selectedRegion = getState(['app', 'selectedRegion']) var url = 'api?path=/v3/clusters' url += dryrun ? '&dryrun=true' : '' url += disableRollback ? '&disableRollback=true' : '' diff --git a/frontend/src/old-pages/Configure/Create.tsx b/frontend/src/old-pages/Configure/Create.tsx index e874aef8..9fab1eb6 100644 --- a/frontend/src/old-pages/Configure/Create.tsx +++ b/frontend/src/old-pages/Configure/Create.tsx @@ -49,6 +49,7 @@ function handleCreate(handleClose: any, navigate: any) { const clusterConfig = getState(configPath) || '' const dryRun = false const region = getState(['app', 'wizard', 'config', 'Region']) + const selectedRegion = getState(['app', 'selectedRegion']) var errHandler = (err: any) => { setState(['app', 'wizard', 'errors', 'create'], err) setState(['app', 'wizard', 'pending'], false) @@ -82,6 +83,7 @@ function handleCreate(handleClose: any, navigate: any) { clusterName, clusterConfig, region, + selectedRegion, disableRollback, dryRun, successHandler, @@ -96,6 +98,7 @@ function handleDryRun() { const forceUpdate = getState(['app', 'wizard', 'forceUpdate']) const clusterConfig = getState(configPath) || '' const region = getState(['app', 'wizard', 'config', 'Region']) + const selectedRegion = getState(['app', 'selectedRegion']) const disableRollback = false const dryRun = true var errHandler = (err: any) => { @@ -122,6 +125,7 @@ function handleDryRun() { clusterName, clusterConfig, region, + selectedRegion, disableRollback, dryRun, successHandler, From f85aa072925e83885cfa06e7117d60450c3c8672 Mon Sep 17 00:00:00 2001 From: Alessandro Menduni Date: Mon, 31 Oct 2022 12:11:02 +0100 Subject: [PATCH 2/3] Add spec to CreateCluster --- frontend/src/__tests__/CreateCluster.test.ts | 139 +++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 frontend/src/__tests__/CreateCluster.test.ts diff --git a/frontend/src/__tests__/CreateCluster.test.ts b/frontend/src/__tests__/CreateCluster.test.ts new file mode 100644 index 00000000..98bcf3cc --- /dev/null +++ b/frontend/src/__tests__/CreateCluster.test.ts @@ -0,0 +1,139 @@ +import {CreateCluster} from '../model' + +const mockPost = jest.fn() + +jest.mock('axios', () => ({ + create: () => ({ + post: (...args: unknown[]) => mockPost(...args), + }), +})) + +describe('given a CreateCluster command and a cluster configuration', () => { + const clusterName = 'any-name' + const clusterConfiguration = {} + const mockRegion = 'some-region' + const mockSelectedRegion = 'some-region' + + describe('when the cluster can be created successfully', () => { + beforeEach(() => { + jest.clearAllMocks() + const mockResponse = { + status: 202, + data: { + some: 'data', + }, + } + mockPost.mockResolvedValueOnce(mockResponse) + }) + + it('should emit the API request', async () => { + const expectedBody = { + clusterConfiguration, + clusterName, + } + + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + ) + expect(mockPost).toHaveBeenCalledTimes(1) + expect(mockPost).toHaveBeenCalledWith( + 'api?path=/v3/clusters®ion=some-region', + expectedBody, + expect.any(Object), + ) + }) + + it('should call the success callback', async () => { + const mockSuccessCallback = jest.fn() + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + false, + false, + mockSuccessCallback, + ) + expect(mockSuccessCallback).toHaveBeenCalledTimes(1) + expect(mockSuccessCallback).toHaveBeenCalledWith({some: 'data'}) + }) + + describe('when a dryrun is requested', () => { + const mockDryRun = true + + it('should add set the dryrun query parameter', async () => { + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + false, + mockDryRun, + ) + expect(mockPost).toHaveBeenCalledTimes(1) + expect(mockPost).toHaveBeenCalledWith( + 'api?path=/v3/clusters&dryrun=true®ion=some-region', + expect.any(Object), + expect.any(Object), + ) + }) + }) + + describe('when stack rollback on failure is disabled', () => { + const mockDisableRollback = true + + it('should add set the disableRollback query parameter', async () => { + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + mockDisableRollback, + ) + expect(mockPost).toHaveBeenCalledTimes(1) + expect(mockPost).toHaveBeenCalledWith( + 'api?path=/v3/clusters&disableRollback=true®ion=some-region', + expect.any(Object), + expect.any(Object), + ) + }) + }) + }) + + describe('when the cluster creation fails', () => { + let mockError: any + + beforeEach(() => { + mockError = { + response: { + data: { + message: 'some-error-message', + }, + }, + } + mockPost.mockRejectedValueOnce(mockError) + }) + + it('should call the error callback', async () => { + const mockErrorCallback = jest.fn() + await CreateCluster( + clusterName, + clusterConfiguration, + mockRegion, + mockSelectedRegion, + false, + false, + undefined, + mockErrorCallback, + ) + await Promise.resolve() + expect(mockErrorCallback).toHaveBeenCalledTimes(1) + expect(mockErrorCallback).toHaveBeenCalledWith({ + message: 'some-error-message', + }) + }) + }) +}) From 5db9a578dc0cb09b3ae29c2237ec93168995be04 Mon Sep 17 00:00:00 2001 From: Alessandro Menduni Date: Mon, 31 Oct 2022 12:18:54 +0100 Subject: [PATCH 3/3] Fix rollbackOnFailure not being set properly on cluster creation --- frontend/src/__tests__/CreateCluster.test.ts | 4 ++-- frontend/src/model.tsx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/src/__tests__/CreateCluster.test.ts b/frontend/src/__tests__/CreateCluster.test.ts index 98bcf3cc..8e25fc6b 100644 --- a/frontend/src/__tests__/CreateCluster.test.ts +++ b/frontend/src/__tests__/CreateCluster.test.ts @@ -85,7 +85,7 @@ describe('given a CreateCluster command and a cluster configuration', () => { describe('when stack rollback on failure is disabled', () => { const mockDisableRollback = true - it('should add set the disableRollback query parameter', async () => { + it('should add set the rollbackOnFailure query parameter', async () => { await CreateCluster( clusterName, clusterConfiguration, @@ -95,7 +95,7 @@ describe('given a CreateCluster command and a cluster configuration', () => { ) expect(mockPost).toHaveBeenCalledTimes(1) expect(mockPost).toHaveBeenCalledWith( - 'api?path=/v3/clusters&disableRollback=true®ion=some-region', + 'api?path=/v3/clusters&rollbackOnFailure=false®ion=some-region', expect.any(Object), expect.any(Object), ) diff --git a/frontend/src/model.tsx b/frontend/src/model.tsx index 675d29bc..3d42e389 100644 --- a/frontend/src/model.tsx +++ b/frontend/src/model.tsx @@ -105,7 +105,7 @@ function CreateCluster( ) { var url = 'api?path=/v3/clusters' url += dryrun ? '&dryrun=true' : '' - url += disableRollback ? '&disableRollback=true' : '' + url += disableRollback ? '&rollbackOnFailure=false' : '' url += region ? `®ion=${region}` : '' var body = {clusterName: clusterName, clusterConfiguration: clusterConfig} request('post', url, body)