Skip to content

Commit 5b0a410

Browse files
authored
Merge pull request #85 from devsapp/oss-auto
fix: support ossConfig auto
2 parents a93c0ab + 1aa1da1 commit 5b0a410

13 files changed

Lines changed: 614 additions & 83 deletions

File tree

.github/workflows/check-format.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Check Code Formatting
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
check-format:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 2
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: '16.x'
20+
21+
- name: Configure NPM
22+
run: |
23+
npm config set registry https://registry.npmjs.org
24+
npm config set '//packages.aliyun.com/670e108663cd360abfe4be65/npm/npm-registry/:_authToken' ${{secrets.NPM_TOKEN}}
25+
26+
- name: Install dependencies
27+
run: npm install
28+
29+
- name: Run fix command
30+
run: npm run fix
31+
32+
- name: Check for uncommitted changes
33+
id: check_changes
34+
run: |
35+
git diff --exit-code -- '*.ts' '*.tsx'
36+
continue-on-error: true
37+
38+
- name: Fail if changes detected
39+
if: steps.check_changes.outcome == 'failure'
40+
run: |
41+
echo "Error: TypeScript files were modified by 'npm run fix'. Please run 'npm run fix' locally and commit the changes."
42+
echo "Modified files:"
43+
git diff --name-only -- '*.ts' '*.tsx'
44+
exit 1

__tests__/e2e/nodejs/s_auto.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ resources:
2020
initializer:
2121
handler: index.initializer
2222
timeout: 10
23+
role: acs:ram::${config('AccountID')}:role/aliyunaliyunfcdefaultrole
2324

2425
vpcConfig: auto
2526
nasConfig: auto
2627
logConfig: auto
28+
ossMountConfig: auto
2729

2830
asyncInvokeConfig:
2931
destinationConfig:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1+
const fs = require('fs');
2+
13
exports.initializer = (context, callback) => {
24
console.log('initializing');
35
callback(null, '');
46
};
57

68
module.exports.handler = function (event, context, callback) {
9+
console.log(event.toString());
10+
console.log(JSON.stringify(context));
11+
const functionName = context.function.name;
12+
console.log(`functionName: ${functionName}`);
13+
const nasDir = `/mnt/${functionName}`;
14+
const ossDir = `/mnt/oss_${functionName}`;
15+
const nasFile = `${nasDir}/test.txt`;
16+
const ossFile = `${ossDir}/test.txt`;
17+
if (fs.existsSync(nasFile)) {
18+
const content = fs.readFileSync(nasFile, 'utf8');
19+
console.log(`nasFile content: ${content}`);
20+
} else {
21+
fs.writeFileSync(nasFile, 'hello world');
22+
console.log(`nasFile created: ${nasFile}`);
23+
}
24+
if (fs.existsSync(ossFile)) {
25+
const content = fs.readFileSync(ossFile, 'utf8');
26+
console.log(`ossFile content: ${content}`);
27+
} else {
28+
fs.writeFileSync(ossFile, 'hello world');
29+
console.log(`ossFile created: ${ossFile}`);
30+
}
731
callback(null, 'hello world');
832
};

__tests__/ut/resources/oss_test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import OSS from '../../../src/resources/oss';
2+
import { ICredentials } from '@serverless-devs/component-interface';
3+
import logger from '../../../src/logger';
4+
import * as utils from '../../../src/utils';
5+
6+
// Mocks
7+
jest.mock('@serverless-cd/srm-aliyun-oss', () => {
8+
return {
9+
__esModule: true,
10+
default: jest.fn(),
11+
};
12+
});
13+
jest.mock('../../../src/logger');
14+
jest.mock('../../../src/utils');
15+
16+
const OssMock = jest.requireMock('@serverless-cd/srm-aliyun-oss').default;
17+
const isAppCenterMock = utils.isAppCenter as jest.Mock;
18+
19+
describe('OSS', () => {
20+
let mockCredentials: ICredentials;
21+
let oss: OSS;
22+
23+
beforeEach(() => {
24+
mockCredentials = {
25+
AccountID: 'test-account-id',
26+
AccessKeyID: 'test-access-key-id',
27+
AccessKeySecret: 'test-access-key-secret',
28+
SecurityToken: 'test-security-token',
29+
} as ICredentials;
30+
31+
// Setup mocks
32+
isAppCenterMock.mockReturnValue(false);
33+
34+
// Mock logger methods
35+
logger.debug = jest.fn();
36+
logger.info = jest.fn();
37+
logger.spin = jest.fn();
38+
});
39+
40+
afterEach(() => {
41+
jest.clearAllMocks();
42+
});
43+
44+
describe('constructor', () => {
45+
it('should initialize correctly with credentials and endpoint', () => {
46+
const region = 'cn-hangzhou';
47+
const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
48+
49+
oss = new OSS(region, mockCredentials, ossEndpoint);
50+
51+
expect(oss.client).toBeDefined();
52+
});
53+
});
54+
55+
describe('deploy', () => {
56+
it('should deploy OSS resource and return ossBucket', async () => {
57+
const region = 'cn-hangzhou';
58+
const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
59+
const expectedOssBucket = 'test-oss-bucket';
60+
61+
// Mock Oss client
62+
const mockClient = {
63+
initOss: jest.fn().mockResolvedValue({ ossBucket: expectedOssBucket }),
64+
};
65+
OssMock.mockImplementation(() => mockClient);
66+
67+
oss = new OSS(region, mockCredentials, ossEndpoint);
68+
69+
const result = await oss.deploy();
70+
71+
expect(mockClient.initOss).toHaveBeenCalledWith(expect.any(Object));
72+
expect(result).toEqual({ ossBucket: expectedOssBucket });
73+
expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('init oss:'));
74+
expect(logger.spin).toHaveBeenCalledWith(
75+
'creating',
76+
'oss',
77+
`region: ${region}; ossBucket: ${expectedOssBucket}`,
78+
);
79+
});
80+
81+
it('should log info message when isAppCenter returns true', async () => {
82+
const region = 'cn-hangzhou';
83+
const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
84+
const expectedOssBucket = 'test-oss-bucket';
85+
86+
// Mock isAppCenter to return true
87+
isAppCenterMock.mockReturnValue(true);
88+
89+
// Mock Oss client
90+
const mockClient = {
91+
initOss: jest.fn().mockResolvedValue({ ossBucket: expectedOssBucket }),
92+
};
93+
OssMock.mockImplementation(() => mockClient);
94+
95+
oss = new OSS(region, mockCredentials, ossEndpoint);
96+
97+
await oss.deploy();
98+
99+
expect(mockClient.initOss).toHaveBeenCalledWith(expect.any(Object));
100+
expect(logger.info).toHaveBeenCalledWith(`created oss region: ${region};`);
101+
});
102+
});
103+
});

0 commit comments

Comments
 (0)