Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion __tests__/ut/commands/sync_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import fs from 'fs';
import fs_extra from 'fs-extra';
import downloads from '@serverless-devs/downloads';

// Mock the ScalingPolicy import that's causing issues
jest.mock('@alicloud/fc20230330', () => ({
ScalingPolicy: jest.fn().mockImplementation((config) => config),
}));

// Mock dependencies
jest.mock('../../../src/resources/fc', () => {
// Define GetApiType enum for the mock
Expand Down Expand Up @@ -108,6 +113,7 @@ describe('Sync', () => {
listTriggers: jest.fn(),
getAsyncInvokeConfig: jest.fn(),
getFunctionProvisionConfig: jest.fn(),
getFunctionScalingConfig: jest.fn(),
getFunctionConcurrency: jest.fn(),
getVpcBinding: jest.fn(),
getFunctionCode: jest.fn(),
Expand Down Expand Up @@ -158,6 +164,14 @@ describe('Sync', () => {
vpcIds: ['vpc-12345'],
});

mockFcInstance.getFunctionScalingConfig.mockResolvedValue({
currentInstances: 1,
minInstances: 1,
targetInstances: 1,
currentError: '',
functionArn: 'arn:acs:fc:cn-hangzhou:123456789:functions/test-function',
});

mockFcInstance.getFunctionCode.mockResolvedValue({
url: 'https://test.oss-cn-hangzhou.aliyuncs.com/code.zip',
});
Expand Down Expand Up @@ -306,6 +320,7 @@ describe('Sync', () => {
expect(mockFcInstance.listTriggers).toHaveBeenCalled();
expect(mockFcInstance.getAsyncInvokeConfig).toHaveBeenCalled();
expect(mockFcInstance.getFunctionProvisionConfig).toHaveBeenCalled();
expect(mockFcInstance.getFunctionScalingConfig).toHaveBeenCalled();
expect(mockFcInstance.getFunctionConcurrency).toHaveBeenCalled();
expect(mockFcInstance.getVpcBinding).toHaveBeenCalled();
expect(result).toHaveProperty('ymlPath');
Expand Down Expand Up @@ -345,6 +360,26 @@ describe('Sync', () => {

expect(downloads).toHaveBeenCalled();
});

it('should handle scaling config successfully', async () => {
mockFcInstance.getFunctionScalingConfig.mockResolvedValue({
currentInstances: 1,
minInstances: 1,
targetInstances: 1,
currentError: '',
functionArn: 'arn:acs:fc:cn-hangzhou:123456789:functions/test-function',
});

const sync = new Sync(mockInputs);
const result = await sync.run();

expect(mockFcInstance.getFunctionScalingConfig).toHaveBeenCalledWith(
'test-function',
'LATEST',
);
expect(result).toHaveProperty('ymlPath');
expect(result).toHaveProperty('codePath');
});
});

describe('write', () => {
Expand Down Expand Up @@ -385,6 +420,13 @@ describe('Sync', () => {
currentError: '',
functionArn: 'arn:acs:fc:cn-hangzhou:123456789:functions/test-function',
};
const scalingConfig = {
currentInstances: 1,
minInstances: 1,
targetInstances: 1,
currentError: '',
functionArn: 'arn:acs:fc:cn-hangzhou:123456789:functions/test-function',
};

const result = await sync.write(
functionConfig,
Expand All @@ -393,6 +435,7 @@ describe('Sync', () => {
vpcBindingConfig,
concurrencyConfig,
provisionConfig,
scalingConfig,
);

expect(fs_extra.removeSync).toHaveBeenCalledWith(
Expand Down Expand Up @@ -422,7 +465,7 @@ describe('Sync', () => {
},
};

await sync.write(functionConfig, [], {}, {}, {}, {});
await sync.write(functionConfig, [], {}, {}, {}, {}, {});

expect(downloads).not.toHaveBeenCalled();
expect(fs_extra.removeSync).not.toHaveBeenCalled();
Expand All @@ -443,9 +486,48 @@ describe('Sync', () => {
{},
{},
{},
{},
);

expect(fs.mkdirSync).toHaveBeenCalledWith('/custom/target', { recursive: true });
});

it('should handle scaling configuration in write method', async () => {
const sync = new Sync(mockInputs);
const functionConfig = {
functionName: 'test-function',
runtime: 'nodejs18',
handler: 'index.handler',
code: {
location: 'https://test.oss-cn-hangzhou.aliyuncs.com/code.zip',
},
};
const triggers = [];
const asyncInvokeConfig = {};
const vpcBindingConfig = {};
const concurrencyConfig = {};
const provisionConfig = {};
const scalingConfig = {
currentInstances: 2,
minInstances: 1,
targetInstances: 3,
currentError: '',
functionArn: 'arn:acs:fc:cn-hangzhou:123456789:functions/test-function',
};

const result = await sync.write(
functionConfig,
triggers,
asyncInvokeConfig,
vpcBindingConfig,
concurrencyConfig,
provisionConfig,
scalingConfig,
);

expect(fs.writeFileSync).toHaveBeenCalled();
expect(result).toHaveProperty('ymlPath');
expect(result).toHaveProperty('codePath');
});
});
});
27 changes: 26 additions & 1 deletion src/subCommands/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default class Sync {
let vpcBindingConfig = {};
let concurrencyConfig = {};
let provisionConfig = {};
let scalingConfig = {};
try {
asyncInvokeConfig = await this.fcSdk.getAsyncInvokeConfig(
this.functionName,
Expand All @@ -122,6 +123,12 @@ export default class Sync {
// eslint-disable-next-line no-empty
}

try {
scalingConfig = await this.fcSdk.getFunctionScalingConfig(this.functionName, 'LATEST');
} catch (ex) {
// eslint-disable-next-line no-empty
}

try {
concurrencyConfig = await this.fcSdk.getFunctionConcurrency(this.functionName);
} catch (ex) {
Expand All @@ -143,6 +150,7 @@ export default class Sync {
vpcBindingConfig,
concurrencyConfig,
provisionConfig,
scalingConfig,
);
}

Expand All @@ -153,6 +161,7 @@ export default class Sync {
vpcBindingConfig: any,
concurrencyConfig: any,
provisionConfig: any,
scalingConfig: any,
) {
const syncFolderName = 'sync-clone';

Expand Down Expand Up @@ -207,12 +216,28 @@ export default class Sync {
if (!_.isEmpty(asyncInvokeConfig)) {
props.asyncInvokeConfig = asyncInvokeConfig;
}

if (!_.isEmpty(provisionConfig)) {
_.unset(provisionConfig, 'current');
_.unset(provisionConfig, 'currentError');
_.unset(provisionConfig, 'functionArn');
props.provisionConfig = provisionConfig;
const isElasticInstance =
provisionConfig.alwaysAllocateCPU === true && provisionConfig.alwaysAllocateGPU === true;

if (isElasticInstance) {
props.provisionConfig = provisionConfig;
} else if (!_.isEmpty(scalingConfig)) {
_.unset(scalingConfig, 'currentError');
_.unset(scalingConfig, 'currentInstances');
_.unset(scalingConfig, 'targetInstances');
_.unset(scalingConfig, 'enableOnDemandScaling');
_.unset(scalingConfig, 'functionArn');
props.scalingConfig = scalingConfig;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这 else 的逻辑是不是只需要保留 scalingconfig, delete 掉 provisionConfig 就好呢

throw new Error('ScalingConfig not found');
}
}

if (!_.isEmpty(concurrencyConfig)) {
_.unset(concurrencyConfig, 'functionArn');
props.concurrencyConfig = concurrencyConfig;
Expand Down
Loading