Skip to content

Commit

Permalink
improve: add more test for rosClient
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Dec 9, 2024
1 parent 810526e commit cffdd38
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 30000,
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};

Expand Down
88 changes: 88 additions & 0 deletions tests/common/rosClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { logger, rosStackDeploy } from '../../src/common';
import { context } from '../fixtures/contextFixture';
import { lang } from '../../src/lang';

const mockedCreateStack = jest.fn();
const mockedUpdateStack = jest.fn();
const mockedListStacks = jest.fn();
const mockedGetStack = jest.fn();

jest.mock('@alicloud/ros20190910', () => ({
...jest.requireActual('@alicloud/ros20190910'),
__esModule: true,
default: jest.fn().mockImplementation(() => ({
createStack: () => mockedCreateStack(),
updateStack: () => mockedUpdateStack(),
listStacks: () => mockedListStacks(),
getStack: () => mockedGetStack(),
})),
}));

jest.mock('../../src/common/logger');

describe('Unit test for rosClient', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should create a new stack if it does not exist', async () => {
mockedListStacks.mockResolvedValue({ statusCode: 200, body: { stacks: [] } });
mockedCreateStack.mockResolvedValue({ body: { stackId: 'newStackId' } });
mockedGetStack.mockResolvedValue({ body: { status: 'CREATE_COMPLETE' } });

await rosStackDeploy('newStack', {}, context);

expect(mockedCreateStack).toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('createStack success'));
});

it('should update an existing stack if it exists', async () => {
mockedListStacks.mockResolvedValue({
statusCode: 200,
body: { stacks: [{ stackId: 'existingStackId', Status: 'CREATE_COMPLETE' }] },
});
mockedUpdateStack.mockResolvedValue({ body: { stackId: 'existingStackId' } });
mockedGetStack.mockResolvedValue({ body: { status: 'UPDATE_COMPLETE' } });

await rosStackDeploy('existingStack', {}, context);

expect(mockedUpdateStack).toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('stackUpdate success'));
});

it('should throw an error if the stack is in progress', async () => {
mockedListStacks.mockResolvedValue({
statusCode: 200,
body: { stacks: [{ stackId: 'inProgressStackId', Status: 'CREATE_IN_PROGRESS' }] },
});

await expect(rosStackDeploy('inProgressStack', {}, context)).rejects.toThrow(
'fail to update stack, because stack status is CREATE_IN_PROGRESS',
);
});

it('should notify user with warning logs when update completely same stack', async () => {
mockedListStacks.mockResolvedValue({
statusCode: 200,
body: { stacks: [{ stackId: 'existingStackId', Status: 'CREATE_COMPLETE' }] },
});
mockedUpdateStack.mockRejectedValueOnce({
data: { statusCode: 400, Message: 'Update the completely same stack' },
});
mockedGetStack.mockResolvedValue({ body: { status: 'UPDATE_COMPLETE' } });

await rosStackDeploy('existingStack', {}, context);

expect(logger.warn).toHaveBeenCalledWith(`${lang.__('UPDATE_COMPLETELY_SAME_STACK')}`);
});

it('should throw error when deploy stack failed', async () => {
mockedListStacks.mockResolvedValue({ statusCode: 200, body: { stacks: [] } });
mockedCreateStack.mockResolvedValueOnce({ body: { stackId: 'newStackId' } });
mockedGetStack.mockResolvedValue({ body: { status: 'ROLLBACK_COMPLETE' } });

await expect(rosStackDeploy('newStack', {}, context)).rejects.toThrow(
`Stack operation failed with status: ROLLBACK_COMPLETE`,
);
});
});

0 comments on commit cffdd38

Please sign in to comment.