Skip to content

Commit 31bc271

Browse files
authored
Add integration test (#11)
* chore(test): add integration test and update isDisabled config * chore: update jest version to support node8.x * chore: remove process.env variables of test * chore: update test case
1 parent a1d1af1 commit 31bc271

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"access": "public"
77
},
88
"scripts": {
9-
"test": "npm run lint && npm run prettier",
9+
"int-test": "jest ./tests/integration.test.js --testEnvironment node",
10+
"test": "npm run lint && npm run prettier && npm run int-test",
1011
"commitlint": "commitlint -f HEAD@{15}",
1112
"lint": "eslint --ext .js,.ts,.tsx .",
1213
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
@@ -44,13 +45,15 @@
4445
"@semantic-release/git": "^9.0.0",
4546
"@semantic-release/npm": "^7.0.4",
4647
"@semantic-release/release-notes-generator": "^9.0.1",
48+
"@serverless/platform-client-china": "^1.0.19",
4749
"babel-eslint": "^10.1.0",
4850
"dotenv": "^8.2.0",
4951
"eslint": "^6.8.0",
5052
"eslint-config-prettier": "^6.10.0",
5153
"eslint-plugin-import": "^2.20.1",
5254
"eslint-plugin-prettier": "^3.1.2",
5355
"husky": "^4.2.3",
56+
"jest": "^25.0.1",
5457
"lint-staged": "^10.0.8",
5558
"prettier": "^1.19.1",
5659
"semantic-release": "^17.0.4"

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
267267

268268
// 对apigw inputs进行标准化
269269
const apigatewayConf = inputs.apigatewayConf ? inputs.apigatewayConf : {}
270+
apigatewayConf.isDisabled = apigatewayConf.isDisabled === true
270271
apigatewayConf.fromClientRemark = fromClientRemark
271272
apigatewayConf.serviceName = inputs.serviceName
272273
apigatewayConf.description = `Serverless Framework Tencent-${capitalString(

tests/integration.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { generateId, getServerlessSdk } = require('./utils')
2+
3+
// set enough timeout for deployment to finish
4+
jest.setTimeout(300000)
5+
6+
// the yaml file we're testing against
7+
const instanceYaml = {
8+
org: 'orgDemo',
9+
app: 'appDemo',
10+
component: 'flask@dev',
11+
name: `flask-integration-tests-${generateId()}`,
12+
stage: 'dev',
13+
inputs: {
14+
runtime: 'Python3.6',
15+
region: 'ap-guangzhou',
16+
apigatewayConf: { environment: 'test' }
17+
}
18+
}
19+
20+
// get credentials from process.env but need to init empty credentials object
21+
const credentials = {
22+
tencent: {}
23+
}
24+
25+
// get serverless construct sdk
26+
const sdk = getServerlessSdk(instanceYaml.org)
27+
28+
it('should successfully deploy flask app', async () => {
29+
const instance = await sdk.deploy(instanceYaml, { tencent: {} })
30+
expect(instance).toBeDefined()
31+
expect(instance.instanceName).toEqual(instanceYaml.name)
32+
// get src from template by default
33+
expect(instance.outputs.templateUrl).toBeDefined()
34+
expect(instance.outputs).toBeDefined()
35+
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
36+
expect(instance.outputs.scf).toBeDefined()
37+
expect(instance.outputs.scf.runtime).toEqual(instanceYaml.inputs.runtime)
38+
expect(instance.outputs.apigw).toBeDefined()
39+
expect(instance.outputs.apigw.environment).toEqual(instanceYaml.inputs.apigatewayConf.environment)
40+
})
41+
42+
it('should successfully remove flask app', async () => {
43+
await sdk.remove(instanceYaml, credentials)
44+
result = await sdk.getInstance(
45+
instanceYaml.org,
46+
instanceYaml.stage,
47+
instanceYaml.app,
48+
instanceYaml.name
49+
)
50+
51+
// remove action won't delete the service cause the apigw have the api binded
52+
expect(result.instance.instanceStatus).toEqual('inactive')
53+
})

tests/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { ServerlessSDK } = require('@serverless/platform-client-china')
2+
3+
/*
4+
* Generate random id
5+
*/
6+
const generateId = () =>
7+
Math.random()
8+
.toString(36)
9+
.substring(6)
10+
11+
/*
12+
* Initializes and returns an instance of the serverless sdk
13+
* @param ${string} orgName - the serverless org name.
14+
*/
15+
const getServerlessSdk = (orgName) => {
16+
const sdk = new ServerlessSDK({
17+
context: {
18+
orgName
19+
}
20+
})
21+
return sdk
22+
}
23+
24+
module.exports = { generateId, getServerlessSdk }

0 commit comments

Comments
 (0)