Skip to content

Commit 49ed4f0

Browse files
committed
test: add jest integration tests
1 parent 6d1d224 commit 49ed4f0

36 files changed

+1087
-796
lines changed

.env.example

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# yugasun
2+
TENCENT_UIN=xxx
3+
TENCENT_APP_ID=xxx
4+
TENCENT_SECRET_ID=xxx
5+
TENCENT_SECRET_KEY=xxx
6+
7+
# bucket for code
8+
BUCKET=serverless-test
9+
10+
# domain for test
11+
DOMAIN=abc.com
12+
SUB_DOMAIN=test.abc.com
13+
14+
# for pg
15+
REGION=ap-guangzhou
16+
ZONE=ap-guangzhou-2
17+
VPC_ID=vpc-xxx
18+
SUBNET_ID=subnet-xxx
19+
20+
# VPC in ap-guangzhou-3 for cfs
21+
CFS_VPC_ID=vpc-xxx
22+
CFS_SUBNET_ID=subnet-xxx

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ dist
1717
.idea
1818
build
1919
.env*
20+
.env.test
21+
!.env.example
2022
env.js
2123
package-lock.json
22-
yarn.lock
24+
yarn.lock

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ support type:
3434

3535
Most of time, we just use `feat` and `fix`.
3636

37+
## Test
38+
39+
For running integration tests we should setup some environment variables in `.env.test` locally or `secrets` in CI.
40+
41+
Just copy `.env.example` to `.env.test`, then change to test account.
42+
3743
## License
3844

3945
Copyright (c) 2019-present Tencent Cloud, Inc.

__tests__/apigw.test.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
const { Apigw } = require('../src');
2+
3+
describe('apigw', () => {
4+
const credentials = {
5+
SecretId: process.env.TENCENT_SECRET_ID,
6+
SecretKey: process.env.TENCENT_SECRET_KEY,
7+
};
8+
const inputs = {
9+
protocols: ['http', 'https'],
10+
serviceName: 'serverless_test',
11+
environment: 'release',
12+
netTypes: ['OUTER'],
13+
// customDomains: [
14+
// {
15+
// domain: 'test.yugasun.com',
16+
// // TODO: change to your certId
17+
// certificateId: 'cWOJJjax',
18+
// isDefaultMapping: false,
19+
// pathMappingSet: [
20+
// {
21+
// path: '/',
22+
// environment: 'release',
23+
// },
24+
// ],
25+
// protocols: ['http', 'https'],
26+
// },
27+
// ],
28+
endpoints: [
29+
{
30+
apiId: 'api-i84p7rla',
31+
path: '/',
32+
protocol: 'HTTP',
33+
method: 'GET',
34+
apiName: 'index',
35+
function: {
36+
functionName: 'egg-function',
37+
},
38+
usagePlan: {
39+
usagePlanId: 'usagePlan-8bbr8pup',
40+
usagePlanName: 'slscmp',
41+
usagePlanDesc: 'sls create',
42+
maxRequestNum: 1000,
43+
},
44+
auth: {
45+
serviceTimeout: 15,
46+
secretName: 'authName',
47+
secretIds: ['xxx'],
48+
},
49+
},
50+
{
51+
path: '/mo',
52+
protocol: 'HTTP',
53+
method: 'GET',
54+
apiName: 'mo',
55+
serviceType: 'MOCK',
56+
serviceMockReturnMessage: 'test mock response',
57+
},
58+
{
59+
path: '/auto',
60+
protocol: 'HTTP',
61+
apiName: 'auto-http',
62+
method: 'GET',
63+
serviceType: 'HTTP',
64+
serviceConfig: {
65+
url: 'http://www.baidu.com',
66+
path: '/test',
67+
method: 'GET',
68+
},
69+
},
70+
{
71+
path: '/ws',
72+
protocol: 'WEBSOCKET',
73+
apiName: 'ws-test',
74+
method: 'GET',
75+
serviceType: 'WEBSOCKET',
76+
serviceConfig: {
77+
url: 'ws://yugasun.com',
78+
path: '/',
79+
method: 'GET',
80+
},
81+
},
82+
{
83+
path: '/wsf',
84+
protocol: 'WEBSOCKET',
85+
apiName: 'ws-scf',
86+
method: 'GET',
87+
serviceType: 'SCF',
88+
function: {
89+
functionNamespace: 'default',
90+
functionQualifier: '$DEFAULT',
91+
transportFunctionName: 'fullstack-api',
92+
registerFunctionName: 'myRestAPI',
93+
},
94+
},
95+
],
96+
};
97+
const apigw = new Apigw(credentials, process.env.REGION);
98+
let outputs;
99+
100+
test('should deploy a apigw success', async () => {
101+
outputs = await apigw.deploy(inputs);
102+
expect(outputs).toEqual({
103+
created: true,
104+
serviceId: expect.stringContaining('service-'),
105+
serviceName: 'serverless_test',
106+
subDomain: expect.stringContaining('.apigw.tencentcs.com'),
107+
protocols: inputs.protocols,
108+
environment: 'release',
109+
apiList: [{
110+
path: '/',
111+
bindType: 'API',
112+
internalDomain: null,
113+
method: 'GET',
114+
apiName: 'index',
115+
apiId: expect.stringContaining('api-'),
116+
created: true,
117+
usagePlan: {
118+
created: true,
119+
secrets: {
120+
'created': false,
121+
'secretIds': [],
122+
},
123+
usagePlanId: expect.stringContaining('usagePlan-'),
124+
},
125+
},{
126+
path: '/mo',
127+
method: 'GET',
128+
apiName: 'mo',
129+
internalDomain: null,
130+
apiId: expect.stringContaining('api-'),
131+
created: true,
132+
},{
133+
path: '/auto',
134+
method: 'GET',
135+
apiName: 'auto-http',
136+
internalDomain: null,
137+
apiId: expect.stringContaining('api-'),
138+
created: true,
139+
},{
140+
path: '/ws',
141+
method: 'GET',
142+
apiName: 'ws-test',
143+
internalDomain: null,
144+
apiId: expect.stringContaining('api-'),
145+
created: true,
146+
},{
147+
path: '/wsf',
148+
method: 'GET',
149+
apiName: 'ws-scf',
150+
internalDomain: expect.stringContaining('http://set-websocket.cb-common.apigateway.tencentyun.com'),
151+
apiId: expect.stringContaining('api-'),
152+
created: true,
153+
}],
154+
});
155+
156+
});
157+
158+
test('should remove apigw success', async () => {
159+
await apigw.remove(outputs);
160+
const detail = await apigw.request({
161+
Action: 'DescribeService',
162+
ServiceId: outputs.serviceId,
163+
});
164+
165+
expect(detail).toBeNull();
166+
});
167+
});
168+

__tests__/cam.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { Cam } = require('../src');
2+
3+
describe('Cam', () => {
4+
const credentials = {
5+
SecretId: process.env.TENCENT_SECRET_ID,
6+
SecretKey: process.env.TENCENT_SECRET_KEY,
7+
};
8+
const roleName = 'role-test';
9+
const policy = JSON.stringify({
10+
version: '2.0',
11+
statement: [
12+
{
13+
action: 'name/sts:AssumeRole',
14+
effect: 'allow',
15+
principal: {
16+
service: ['cloudaudit.cloud.tencent.com', 'cls.cloud.tencent.com'],
17+
},
18+
},
19+
],
20+
});
21+
const cam = new Cam(credentials, process.env.REGION);
22+
23+
test('should create role success', async () => {
24+
await cam.CreateRole(roleName, policy);
25+
const { RoleInfo } = await cam.GetRole(roleName);
26+
const exist = await cam.isRoleExist(roleName, {});
27+
expect(RoleInfo.RoleName).toBe(roleName);
28+
expect(exist).toBe(true);
29+
});
30+
31+
test('should delete role success', async () => {
32+
await cam.DeleteRole(roleName, {});
33+
try {
34+
await cam.GetRole(roleName);
35+
} catch (e) {
36+
expect(e.code).toBe('InvalidParameter.RoleNotExist');
37+
}
38+
});
39+
40+
test('SCFExcuteRole should exist', async () => {
41+
const res = await cam.CheckSCFExcuteRole();
42+
expect(res).toBe(true);
43+
});
44+
});
45+

__tests__/cdn.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { Cdn } = require('../src');
2+
3+
describe('Cdn', () => {
4+
const credentials = {
5+
SecretId: process.env.TENCENT_SECRET_ID,
6+
SecretKey: process.env.TENCENT_SECRET_KEY,
7+
};
8+
const inputs = {
9+
async: true,
10+
area: 'overseas',
11+
domain: 'test.yuga.chat',
12+
hostType: 'cos',
13+
origin: {
14+
origins: ['up6pwd9-89hm718-xxx.cos-website.ap-guangzhou.myqcloud.com'],
15+
originType: 'cos',
16+
originPullProtocol: 'https',
17+
},
18+
serviceType: 'web',
19+
https: {
20+
switch: 'on',
21+
http2: 'on',
22+
certInfo: {
23+
certId: 'cWOJJjax',
24+
},
25+
},
26+
forceRedirect: {
27+
switch: 'on',
28+
redirectType: 'https',
29+
redirectStatusCode: 301,
30+
},
31+
};
32+
const cdn = new Cdn(credentials, process.env.REGION);
33+
34+
test('should deploy CDN success', async () => {
35+
const res = await cdn.deploy(inputs);
36+
expect(res).toEqual({
37+
created: true,
38+
https: true,
39+
domain: inputs.domain,
40+
origins: inputs.origin.origins,
41+
cname: `${inputs.domain}.cdn.dnsv1.com`,
42+
inputCache: JSON.stringify(inputs),
43+
resourceId: expect.stringContaining('cdn-'),
44+
});
45+
});
46+
});
47+

__tests__/cfs.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { sleep } = require('@ygkit/request');
2+
const { Cfs } = require('../src');
3+
const apis = require('../src/modules/cfs/apis');
4+
5+
describe('Cfs', () => {
6+
const credentials = {
7+
SecretId: process.env.TENCENT_SECRET_ID,
8+
SecretKey: process.env.TENCENT_SECRET_KEY,
9+
};
10+
const inputs = {
11+
fsName: 'cfs-test',
12+
region: 'ap-guangzhou',
13+
zone: 'ap-guangzhou-3',
14+
netInterface: 'VPC',
15+
vpc: {
16+
vpcId: process.env.CFS_VPC_ID,
17+
subnetId: process.env.CFS_SUBNET_ID,
18+
},
19+
};
20+
const cfs = new Cfs(credentials, process.env.REGION);
21+
22+
test('should deploy CFS success', async () => {
23+
const res = await cfs.deploy(inputs);
24+
expect(res).toEqual({
25+
region: process.env.REGION,
26+
fsName: inputs.fsName,
27+
pGroupId: 'pgroupbasic',
28+
netInterface: 'VPC',
29+
protocol: 'NFS',
30+
storageType: 'SD',
31+
fileSystemId: expect.stringContaining('cfs-'),
32+
});
33+
inputs.fileSystemId = res.fileSystemId;
34+
});
35+
36+
test('should remove CFS success', async () => {
37+
await sleep(1000);
38+
const res = await cfs.remove(inputs);
39+
const detail = await apis.getCfs(cfs.capi, inputs.fileSystemId);
40+
expect(res).toEqual({});
41+
expect(detail).toBeUndefined();
42+
});
43+
});
44+

0 commit comments

Comments
 (0)