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
44 changes: 44 additions & 0 deletions .github/workflows/check-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Check Code Formatting

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
check-format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16.x'

- name: Configure NPM
run: |
npm config set registry https://registry.npmjs.org
npm config set '//packages.aliyun.com/670e108663cd360abfe4be65/npm/npm-registry/:_authToken' ${{secrets.NPM_TOKEN}}

- name: Install dependencies
run: npm install

- name: Run fix command
run: npm run fix

- name: Check for uncommitted changes
id: check_changes
run: |
git diff --exit-code -- '*.ts' '*.tsx'
continue-on-error: true

- name: Fail if changes detected
if: steps.check_changes.outcome == 'failure'
run: |
echo "Error: TypeScript files were modified by 'npm run fix'. Please run 'npm run fix' locally and commit the changes."
echo "Modified files:"
git diff --name-only -- '*.ts' '*.tsx'
exit 1
2 changes: 2 additions & 0 deletions __tests__/e2e/nodejs/s_auto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ resources:
initializer:
handler: index.initializer
timeout: 10
role: acs:ram::${config('AccountID')}:role/aliyunaliyunfcdefaultrole

vpcConfig: auto
nasConfig: auto
logConfig: auto
ossMountConfig: auto

asyncInvokeConfig:
destinationConfig:
Expand Down
24 changes: 24 additions & 0 deletions __tests__/e2e/nodejs/test-auto-code/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
const fs = require('fs');

exports.initializer = (context, callback) => {
console.log('initializing');
callback(null, '');
};

module.exports.handler = function (event, context, callback) {
console.log(event.toString());
console.log(JSON.stringify(context));
const functionName = context.function.name;
console.log(`functionName: ${functionName}`);
const nasDir = `/mnt/${functionName}`;
const ossDir = `/mnt/oss_${functionName}`;
const nasFile = `${nasDir}/test.txt`;
const ossFile = `${ossDir}/test.txt`;
if (fs.existsSync(nasFile)) {
const content = fs.readFileSync(nasFile, 'utf8');
console.log(`nasFile content: ${content}`);
} else {
fs.writeFileSync(nasFile, 'hello world');
console.log(`nasFile created: ${nasFile}`);
}
if (fs.existsSync(ossFile)) {
const content = fs.readFileSync(ossFile, 'utf8');
console.log(`ossFile content: ${content}`);
} else {
fs.writeFileSync(ossFile, 'hello world');
console.log(`ossFile created: ${ossFile}`);
}
callback(null, 'hello world');
};
103 changes: 103 additions & 0 deletions __tests__/ut/resources/oss_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import OSS from '../../../src/resources/oss';
import { ICredentials } from '@serverless-devs/component-interface';
import logger from '../../../src/logger';
import * as utils from '../../../src/utils';

// Mocks
jest.mock('@serverless-cd/srm-aliyun-oss', () => {
return {
__esModule: true,
default: jest.fn(),
};
});
jest.mock('../../../src/logger');
jest.mock('../../../src/utils');

const OssMock = jest.requireMock('@serverless-cd/srm-aliyun-oss').default;
const isAppCenterMock = utils.isAppCenter as jest.Mock;

describe('OSS', () => {
let mockCredentials: ICredentials;
let oss: OSS;

beforeEach(() => {
mockCredentials = {
AccountID: 'test-account-id',
AccessKeyID: 'test-access-key-id',
AccessKeySecret: 'test-access-key-secret',
SecurityToken: 'test-security-token',
} as ICredentials;

// Setup mocks
isAppCenterMock.mockReturnValue(false);

// Mock logger methods
logger.debug = jest.fn();
logger.info = jest.fn();
logger.spin = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
});

describe('constructor', () => {
it('should initialize correctly with credentials and endpoint', () => {
const region = 'cn-hangzhou';
const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com';

oss = new OSS(region, mockCredentials, ossEndpoint);

expect(oss.client).toBeDefined();
});
});

describe('deploy', () => {
it('should deploy OSS resource and return ossBucket', async () => {
const region = 'cn-hangzhou';
const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
const expectedOssBucket = 'test-oss-bucket';

// Mock Oss client
const mockClient = {
initOss: jest.fn().mockResolvedValue({ ossBucket: expectedOssBucket }),
};
OssMock.mockImplementation(() => mockClient);

oss = new OSS(region, mockCredentials, ossEndpoint);

const result = await oss.deploy();

expect(mockClient.initOss).toHaveBeenCalledWith(expect.any(Object));
expect(result).toEqual({ ossBucket: expectedOssBucket });
expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('init oss:'));
expect(logger.spin).toHaveBeenCalledWith(
'creating',
'oss',
`region: ${region}; ossBucket: ${expectedOssBucket}`,
);
});

it('should log info message when isAppCenter returns true', async () => {
const region = 'cn-hangzhou';
const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
const expectedOssBucket = 'test-oss-bucket';

// Mock isAppCenter to return true
isAppCenterMock.mockReturnValue(true);

// Mock Oss client
const mockClient = {
initOss: jest.fn().mockResolvedValue({ ossBucket: expectedOssBucket }),
};
OssMock.mockImplementation(() => mockClient);

oss = new OSS(region, mockCredentials, ossEndpoint);

await oss.deploy();

expect(mockClient.initOss).toHaveBeenCalledWith(expect.any(Object));
expect(logger.info).toHaveBeenCalledWith(`created oss region: ${region};`);
});
});
});
Loading
Loading