Skip to content

Commit 78fde58

Browse files
Merge pull request #3 from serverless-tencent/testing
add test case
2 parents 3485a35 + 35bc457 commit 78fde58

22 files changed

+904
-14
lines changed

deploy/lib/deployFunction.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
const sinon = require('sinon');
4+
const DeployFunction = require('./deployFunction');
5+
6+
describe('DeployFunction@Library', () => {
7+
let deployFunction;
8+
let deployFunctionStub;
9+
let updateFunctionCodeStub;
10+
let createFunctionStub;
11+
let getFunctionStub;
12+
let updateConfigurationStub;
13+
let createTagsStub;
14+
let uploadService2CosStub;
15+
16+
let options;
17+
18+
beforeEach(() => {
19+
options = {
20+
region: 'ap-guangzhou',
21+
};
22+
deployFunction = new DeployFunction('appid', 'secret_id', 'secret_key', options);
23+
24+
deployFunctionStub = sinon.stub(deployFunction, 'deploy')
25+
.returns(Promise.resolve());
26+
updateFunctionCodeStub = sinon.stub(deployFunction, 'updateFunctionCode')
27+
.returns(Promise.resolve());
28+
createFunctionStub = sinon.stub(deployFunction, 'createFunction')
29+
.returns(Promise.resolve());
30+
getFunctionStub = sinon.stub(deployFunction, 'getFunction')
31+
.returns(Promise.resolve());
32+
updateConfigurationStub = sinon.stub(deployFunction, 'updateConfiguration')
33+
.returns(Promise.resolve());
34+
createTagsStub = sinon.stub(deployFunction, 'createTags')
35+
.returns(Promise.resolve());
36+
uploadService2CosStub = sinon.stub(deployFunction, 'uploadService2Cos')
37+
.returns(Promise.resolve());
38+
39+
});
40+
41+
afterEach(() => {
42+
deployFunction.deploy.restore();
43+
deployFunction.updateFunctionCode.restore();
44+
deployFunction.createFunction.restore();
45+
deployFunction.getFunction.restore();
46+
deployFunction.updateConfiguration.restore();
47+
deployFunction.createTags.restore();
48+
deployFunction.uploadService2Cos.restore();
49+
});
50+
51+
it('should make the deploy function accessible', () => {
52+
deployFunction.should.to.be.an.instanceof(DeployFunction);
53+
});
54+
55+
it('should run library deploy function', () => deployFunction
56+
.deploy().then(()=>{
57+
deployFunctionStub.calledOnce.should.equal(true)
58+
}));
59+
60+
it('should run library update function code', () => deployFunction
61+
.updateFunctionCode().then(()=>{
62+
updateFunctionCodeStub.calledOnce.should.equal(true)
63+
}));
64+
65+
it('should run library create function', () => deployFunction
66+
.createFunction().then(()=>{
67+
createFunctionStub.calledOnce.should.equal(true)
68+
}));
69+
70+
it('should run library get function', () => deployFunction
71+
.getFunction().then(()=>{
72+
getFunctionStub.calledOnce.should.equal(true)
73+
}));
74+
75+
it('should run library update configuration', () => deployFunction
76+
.updateConfiguration().then(()=>{
77+
updateConfigurationStub.calledOnce.should.equal(true)
78+
}));
79+
80+
it('should run library create tags', () => deployFunction
81+
.createTags().then(()=>{
82+
createTagsStub.calledOnce.should.equal(true)
83+
}));
84+
it('should run library upload service to cos', () => deployFunction
85+
.uploadService2Cos().then(()=>{
86+
uploadService2CosStub.calledOnce.should.equal(true)
87+
}));
88+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const sinon = require('sinon');
4+
const DeployListFunctions = require('./deployListFunctions');
5+
6+
describe('DeployListFunctions@Library', () => {
7+
let deployListFunctions;
8+
let deployListFunctionsStub;
9+
10+
let options;
11+
12+
beforeEach(() => {
13+
options = {
14+
region: 'ap-guangzhou',
15+
};
16+
deployListFunctions = new DeployListFunctions('appid', 'secret_id', 'secret_key', options);
17+
18+
deployListFunctionsStub = sinon.stub(deployListFunctions, 'functionsList')
19+
.returns(Promise.resolve());
20+
});
21+
22+
afterEach(() => {
23+
deployListFunctions.functionsList.restore();
24+
});
25+
26+
it('should make the info function accessible', () => {
27+
deployListFunctions.should.to.be.an.instanceof(DeployListFunctions);
28+
});
29+
30+
it('should run library functions list', () => deployListFunctions
31+
.functionsList().then(()=>{
32+
deployListFunctionsStub.calledOnce.should.equal(true)
33+
}));
34+
});

deploy/lib/deployTrigger.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const sinon = require('sinon');
4+
const DeployTrigger = require('./deployTrigger');
5+
6+
describe('DeployTrigger@Library', () => {
7+
let deployTrigger;
8+
let deployTriggerCreateStub;
9+
10+
let options;
11+
12+
beforeEach(() => {
13+
options = {
14+
region: 'ap-guangzhou',
15+
};
16+
deployTrigger = new DeployTrigger('appid', 'secret_id', 'secret_key', options);
17+
18+
deployTriggerCreateStub = sinon.stub(deployTrigger, 'create')
19+
.returns(Promise.resolve());
20+
});
21+
22+
afterEach(() => {
23+
deployTrigger.create.restore();
24+
});
25+
26+
it('should make the info function accessible', () => {
27+
deployTrigger.should.to.be.an.instanceof(DeployTrigger);
28+
});
29+
30+
it('should run library create trigger', () => deployTrigger
31+
.create().then(()=>{
32+
deployTriggerCreateStub.calledOnce.should.equal(true)
33+
}));
34+
});

deploy/tencentDeploy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class TencentDeploy {
2727
.then(this.validate)
2828
.then(this.setDefaults),
2929

30-
'deploy:deploy': this.deploy.bind(this),
30+
'deploy:deploy': () => BbPromise.bind(this)
31+
.then(this.deploy),
3132
};
3233
}
3334

deploy/tencentDeploy.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const os = require('os');
5+
const sinon = require('sinon');
6+
const TencentProvider = require('../provider/tencentProvider');
7+
const Serverless = require('../test/serverless');
8+
const TencentDeploy = require('./tencentDeploy');
9+
10+
describe('TencentDeploy', () => {
11+
let serverless;
12+
let options;
13+
let tencentDeploy;
14+
let readFileSyncStub;
15+
let homedirStub;
16+
17+
beforeEach(() => {
18+
serverless = new Serverless();
19+
options = {
20+
stage: 'dev',
21+
region: 'ap-guangzhou',
22+
function: 'test'
23+
};
24+
25+
readFileSyncStub = sinon.stub(fs, 'readFileSync')
26+
.returns(`[default]
27+
tencent_secret_key = PYR4a0HSZ******eVvHRe
28+
tencent_secret_id = AKIDoM*****mxsfOirI
29+
tencent_appid = 12561*****`);
30+
homedirStub = sinon.stub(os, 'homedir')
31+
.returns('/root');
32+
33+
serverless.setProvider('tencent', new TencentProvider(serverless, options));
34+
tencentDeploy = new TencentDeploy(serverless, options);
35+
});
36+
37+
afterEach(() => {
38+
fs.readFileSync.restore();
39+
os.homedir.restore();
40+
});
41+
42+
describe('#constructor()', () => {
43+
it('should set the serverless instance', () => {
44+
tencentDeploy.serverless.should.equal(serverless);
45+
});
46+
47+
it('should set options if provided', () => {
48+
tencentDeploy.options.should.equal(options);
49+
});
50+
51+
it('should make the provider accessible', () => {
52+
tencentDeploy.provider.should.to.be.an.instanceof(TencentProvider);
53+
});
54+
55+
describe('hooks', () => {
56+
let validateStub;
57+
let setDefaultsStub;
58+
let tencentDeployStub;
59+
60+
beforeEach(() => {
61+
validateStub = sinon.stub(tencentDeploy, 'validate')
62+
.returns(Promise.resolve());
63+
setDefaultsStub = sinon.stub(tencentDeploy, 'setDefaults')
64+
.returns(Promise.resolve());
65+
tencentDeployStub = sinon.stub(tencentDeploy, 'deploy')
66+
.returns(Promise.resolve());
67+
});
68+
69+
afterEach(() => {
70+
tencentDeploy.validate.restore();
71+
tencentDeploy.setDefaults.restore();
72+
tencentDeploy.deploy.restore();
73+
});
74+
75+
it('should run "before:deploy:deploy" promise chain', () => tencentDeploy
76+
.hooks['before:deploy:deploy']().then(() => {
77+
validateStub.calledOnce.should.equal(true);
78+
setDefaultsStub.calledAfter(validateStub).should.equal(true);
79+
}));
80+
81+
it('should run "deploy:deploy" promise chain', () => tencentDeploy
82+
.hooks['deploy:deploy']().then(() => {
83+
tencentDeployStub.calledOnce.should.equal(true);
84+
}));
85+
});
86+
});
87+
});

deploy/tencentDeployFunction.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ class TencentDeployFunction {
2626
'deploy:function:initialize': () => BbPromise.bind(this)
2727
.then(this.validate)
2828
.then(this.setDefaults),
29-
'deploy:function:packageFunction': this.packageFunction.bind(this),
30-
'deploy:function:deploy': this.deploy.bind(this),
29+
'deploy:function:packageFunction': () => BbPromise.bind(this)
30+
.then(this.packageFunction),
31+
'deploy:function:deploy': () => BbPromise.bind(this)
32+
.then(this.deploy),
3133
};
3234
}
3335

deploy/tencentDeployFunction.test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const os = require('os');
5+
const sinon = require('sinon');
6+
const TencentProvider = require('../provider/tencentProvider');
7+
const Serverless = require('../test/serverless');
8+
const TencentDeployFunction = require('./tencentDeployFunction');
9+
10+
describe('TencentDeployFunction', () => {
11+
let serverless;
12+
let options;
13+
let tencentDeployFunction;
14+
let readFileSyncStub;
15+
let homedirStub;
16+
17+
beforeEach(() => {
18+
serverless = new Serverless();
19+
options = {
20+
stage: 'dev',
21+
region: 'ap-guangzhou',
22+
function: 'test'
23+
};
24+
25+
readFileSyncStub = sinon.stub(fs, 'readFileSync')
26+
.returns(`[default]
27+
tencent_secret_key = PYR4a0HSZ******eVvHRe
28+
tencent_secret_id = AKIDoM*****mxsfOirI
29+
tencent_appid = 12561*****`);
30+
homedirStub = sinon.stub(os, 'homedir')
31+
.returns('/root');
32+
33+
serverless.setProvider('tencent', new TencentProvider(serverless, options));
34+
tencentDeployFunction = new TencentDeployFunction(serverless, options);
35+
});
36+
37+
afterEach(() => {
38+
fs.readFileSync.restore();
39+
os.homedir.restore();
40+
});
41+
42+
describe('#constructor()', () => {
43+
it('should set the serverless instance', () => {
44+
tencentDeployFunction.serverless.should.equal(serverless);
45+
});
46+
47+
it('should set options if provided', () => {
48+
tencentDeployFunction.options.should.equal(options);
49+
});
50+
51+
it('should make the provider accessible', () => {
52+
tencentDeployFunction.provider.should.to.be.an.instanceof(TencentProvider);
53+
});
54+
55+
describe('hooks', () => {
56+
let validateStub;
57+
let setDefaultsStub;
58+
let tencentDeployFunctionStub;
59+
let tencentPackageFunctionStub;
60+
61+
beforeEach(() => {
62+
validateStub = sinon.stub(tencentDeployFunction, 'validate')
63+
.returns(Promise.resolve());
64+
setDefaultsStub = sinon.stub(tencentDeployFunction, 'setDefaults')
65+
.returns(Promise.resolve());
66+
tencentDeployFunctionStub = sinon.stub(tencentDeployFunction, 'deploy')
67+
.returns(Promise.resolve());
68+
tencentPackageFunctionStub = sinon.stub(tencentDeployFunction, 'packageFunction')
69+
.returns(Promise.resolve());
70+
});
71+
72+
afterEach(() => {
73+
tencentDeployFunction.validate.restore();
74+
tencentDeployFunction.setDefaults.restore();
75+
tencentDeployFunction.deploy.restore();
76+
});
77+
78+
it('should run "deploy:function:initialize" promise chain', () => tencentDeployFunction
79+
.hooks['deploy:function:initialize']().then(() => {
80+
validateStub.calledOnce.should.equal(true);
81+
setDefaultsStub.calledAfter(validateStub).should.equal(true);
82+
}));
83+
84+
it('should run "deploy:function:packageFunction" promise chain', () => tencentDeployFunction
85+
.hooks['deploy:function:packageFunction']().then(() => {
86+
tencentPackageFunctionStub.calledOnce.should.equal(true);
87+
}));
88+
89+
it('should run "deploy:function:deploy" promise chain', () => tencentDeployFunction
90+
.hooks['deploy:function:deploy']().then(() => {
91+
tencentDeployFunctionStub.calledOnce.should.equal(true);
92+
}));
93+
});
94+
});
95+
});

deploy/tencentDeployList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class TencentDeployList {
2323
'before:deploy:list:log': () => BbPromise.bind(this)
2424
.then(this.validate)
2525
.then(this.setDefaults),
26-
'deploy:list:log': this.serviceList.bind(this),
26+
'deploy:list:log': () => BbPromise.bind(this)
27+
.then(this.serviceList),
2728
};
2829
}
2930

0 commit comments

Comments
 (0)