Skip to content

Commit

Permalink
fix: fx the tests
Browse files Browse the repository at this point in the history
Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll committed Dec 21, 2024
1 parent 6dcf678 commit 3cecef4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
19 changes: 3 additions & 16 deletions tests/fixtures/deployFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ export const largeCodeRos = {
Properties: {
Code: {
OssBucketName: {
'Fn::GetAtt': ['my-demo-service_artifacts_bucket', 'Name'],
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
},
OssObjectName: 'hello_fn/43cb4c356149762dbe507fc1baede172-large-artifact.zip',
},
Expand All @@ -656,6 +656,7 @@ export const largeCodeRos = {
Timeout: 10,
},
Type: 'ALIYUN::FC3::Function',
DependsOn: ['my-demo-service_artifacts_code_deployment'],
},
'my-demo-service_apigroup': {
Properties: {
Expand All @@ -669,25 +670,11 @@ export const largeCodeRos = {
},
Type: 'ALIYUN::ApiGateway::Group',
},
'my-demo-service_artifacts_bucket': {
Properties: {
AccessControl: 'private',
BucketName: 'my-demo-service-artifacts-bucket',
DeletionForce: false,
EnableOssHdfsService: false,
RedundancyType: 'LRS',
ServerSideEncryptionConfiguration: {
SSEAlgorithm: 'KMS',
},
},
Type: 'ALIYUN::OSS::Bucket',
},
'my-demo-service_artifacts_code_deployment': {
DependsOn: ['my-demo-service_artifacts_bucket'],
Properties: {
Parameters: {
destinationBucket: {
'Fn::GetAtt': ['my-demo-service_artifacts_bucket', 'Name'],
'Fn::Sub': 'si-bootstrap-artifacts-${ALIYUN::AccountId}-${ALIYUN::Region}',
},
retainOnCreate: false,
sources: [
Expand Down
38 changes: 26 additions & 12 deletions tests/stack/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ import { cloneDeep, set } from 'lodash';
const mockedRosStackDeploy = jest.fn();
const mockedResolveCode = jest.fn();
const mockedPublishAssets = jest.fn();
const mockedGetIamInfo = jest.fn();

jest.mock('../../src/common', () => ({
...jest.requireActual('../../src/common'),
rosStackDeploy: (...args: unknown[]) => mockedRosStackDeploy(...args),
publishAssets: (...args: unknown[]) => mockedPublishAssets(...args),
resolveCode: (path: string) => mockedResolveCode(path),
getIamInfo: (...args: unknown[]) => mockedGetIamInfo(...args),
}));

describe('Unit tests for stack deployment', () => {
beforeEach(() => {
mockedResolveCode.mockReturnValueOnce('resolved-code');
mockedPublishAssets.mockResolvedValueOnce('published-assets-bucket');
mockedGetIamInfo.mockResolvedValueOnce({ accountId: '123456789012', region: 'cn-hangzhou' });
});
afterEach(() => {
mockedRosStackDeploy.mockRestore();
mockedResolveCode.mockRestore();
mockedPublishAssets.mockRestore();
mockedGetIamInfo.mockRestore();
});

it('should deploy generated stack when iac is valid', async () => {
Expand All @@ -45,7 +49,7 @@ describe('Unit tests for stack deployment', () => {

await deployStack(stackName, oneFcOneGatewayIac, { stackName } as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, oneFcOneGatewayRos, { stackName });
});

Expand All @@ -55,7 +59,7 @@ describe('Unit tests for stack deployment', () => {

await deployStack(stackName, minimumIac, { stackName } as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, minimumRos, { stackName });
});

Expand All @@ -65,7 +69,7 @@ describe('Unit tests for stack deployment', () => {

await deployStack(stackName, oneFcIac, { stackName } as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, oneFcRos, { stackName });
});

Expand All @@ -75,7 +79,7 @@ describe('Unit tests for stack deployment', () => {

await deployStack(options.stackName, oneFcIacWithStage, options as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(
options.stackName,
oneFcWithStageRos,
Expand All @@ -89,7 +93,7 @@ describe('Unit tests for stack deployment', () => {

await deployStack(options.stackName, oneFcIacWithStage, options as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(
options.stackName,
set(
Expand All @@ -107,7 +111,7 @@ describe('Unit tests for stack deployment', () => {

await deployStack(options.stackName, referredServiceIac, options as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(
options.stackName,
referredServiceRos,
Expand All @@ -130,7 +134,7 @@ describe('Unit tests for stack deployment', () => {
);

expect(mockedPublishAssets).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedPublishAssets).toHaveBeenCalledWith(
expect.objectContaining({
dockerImages: {},
Expand All @@ -141,20 +145,30 @@ describe('Unit tests for stack deployment', () => {

{ stackName },
);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, largeCodeRos, { stackName });
expect(mockedRosStackDeploy.mock.calls[1]).toEqual([
stackName,
largeCodeRos,
{
stackName,
},
]);
});

describe('unit test for deploy of databases', () => {
it('should deploy elasticsearch serverless when database minimum fields provided', async () => {
const stackName = 'my-demo-es-serverless-stack';
mockedRosStackDeploy.mockResolvedValueOnce(stackName);
mockedRosStackDeploy.mockResolvedValue(stackName);

await deployStack(stackName, esServerlessMinimumIac, { stackName } as ActionContext);

expect(mockedRosStackDeploy).toHaveBeenCalledTimes(1);
expect(mockedRosStackDeploy).toHaveBeenCalledWith(stackName, esServerlessMinimumRos, {
expect(mockedRosStackDeploy).toHaveBeenCalledTimes(2);
expect(mockedRosStackDeploy.mock.calls[1]).toEqual([
stackName,
});
esServerlessMinimumRos,
{
stackName,
},
]);
});
});
});

0 comments on commit 3cecef4

Please sign in to comment.