Skip to content

Commit 809f16e

Browse files
authored
fix: optimize deploy web type function (#70)
1 parent b0150e6 commit 809f16e

File tree

11 files changed

+324
-144
lines changed

11 files changed

+324
-144
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ name: scfdemo # (必填) 创建的实例名称,请修改成您的实例名称
6969
inputs:
7070
name: ${name}-${stage}-${app} #函数名称
7171
src: ./ #代码路径
72-
handler: index.main_handler #入口
72+
type: 'web' # 部署Web函数时需指定该值
73+
handler: index.main_handler #入口(部署非Web函数时生效)
7374
runtime: Nodejs10.15 # 云函数运行时的环境
7475
region: ap-guangzhou # 云函数所在区域
7576
events: # 触发器
@@ -93,14 +94,16 @@ serverless.yml 文件包含的信息:
9394

9495
inputs 下的参数为组件配置参数。一个最简单 SCF 组件参数配置由以下几部分:
9596

96-
| 参数名 | 说明 |
97-
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
98-
| name | 云函数名称。由于云函数又是资源 ID,为了保证资源的唯一性,建议采用 `${name}-${stage}-${app}` 变量方式。 |
99-
| src | 代码路径。 |
100-
| handler | 函数处理方法名称 。 |
101-
| runtime | 云函数运行环境,目前支持: Python2.7、Python3.6、Nodejs6.10、Nodejs8.9、Nodejs10.15、Nodejs12.16、PHP5、PHP7、Go1、Java8 和 CustomRuntime。 |
102-
| region | 云函数所在的区域。 |
103-
| events | 触发器。 支持的触发器为:timer、apigw、cos、cmq、ckafka 。 |
97+
| 参数名 | 说明 |
98+
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
99+
| name | 云函数名称。由于云函数又是资源 ID,为了保证资源的唯一性,建议采用 `${name}-${stage}-${app}` 变量方式。 |
100+
| src | 代码路径。 |
101+
| type | 函数类型,默认为事件函数。支持的类型为:event(事件函数),web(Web 函数)。 |
102+
| handler | 函数处理方法名称 。 |
103+
| entryFile | 函数入口文件名。(函数类型为 web 且无 scf_bootstrap 文件时生效) |
104+
| runtime | 云函数运行环境,目前支持: Python2.7、Python3.6、Nodejs6.10、Nodejs8.9、Nodejs10.15、Nodejs12.16、PHP5、PHP7、Go1、Java8 和 CustomRuntime。 |
105+
| region | 云函数所在的区域。 |
106+
| events | 触发器。 支持的触发器为:timer、apigw、cos、cmq、ckafka 。 |
104107

105108
### 账号权限
106109

__tests__/index.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,41 @@ describe('Scf', () => {
5959
inputs
6060
}
6161

62+
const webFuncYaml = {
63+
org: appId,
64+
app: 'appDemo',
65+
component: 'scf@dev',
66+
name: `scf-web-integration-tests-${generateId()}`,
67+
stage: 'dev',
68+
inputs: {
69+
name: `scf-web-integration-tests-${generateId()}`,
70+
src: {
71+
src: path.join(__dirname, '../example/web/src'),
72+
exclude: ['.env']
73+
},
74+
type: 'web',
75+
region: 'ap-chengdu',
76+
runtime: 'Nodejs12.16',
77+
events: [
78+
{
79+
apigw: {
80+
parameters: {
81+
protocols: ['http', 'https'],
82+
description: 'The service of Serverless Framework',
83+
environment: 'test',
84+
endpoints: [
85+
{
86+
path: '/',
87+
method: 'ANY'
88+
}
89+
]
90+
}
91+
}
92+
}
93+
]
94+
}
95+
}
96+
6297
const sdk = getServerlessSdk(instanceYaml.org, appId)
6398

6499
let lastVersion = '$LATEST'
@@ -370,4 +405,60 @@ describe('Scf', () => {
370405

371406
expect(result.instance.instanceStatus).toEqual('inactive')
372407
})
408+
409+
it('deploy web function', async () => {
410+
const instance = await sdk.deploy(webFuncYaml, credentials)
411+
412+
expect(instance).toBeDefined()
413+
expect(instance.instanceName).toEqual(webFuncYaml.name)
414+
415+
const { outputs } = instance
416+
// get src from template by default
417+
expect(outputs.functionName).toEqual(webFuncYaml.inputs.name)
418+
expect(outputs.runtime).toEqual(webFuncYaml.inputs.runtime)
419+
expect(outputs.triggers).toBeDefined()
420+
expect(outputs.triggers.length).toBe(1)
421+
422+
const { triggers } = outputs
423+
const apiTrigger = triggers[0]
424+
425+
expect(apiTrigger).toEqual({
426+
NeedCreate: expect.any(Boolean),
427+
created: expect.any(Boolean),
428+
serviceId: expect.stringContaining('service-'),
429+
serviceName: 'serverless',
430+
subDomain: expect.stringContaining('.cd.apigw.tencentcs.com'),
431+
protocols: 'http&https',
432+
environment: 'test',
433+
url: expect.stringContaining('http'),
434+
apiList: [
435+
{
436+
created: expect.any(Boolean),
437+
path: '/',
438+
method: 'ANY',
439+
apiId: expect.stringContaining('api-'),
440+
apiName: 'index',
441+
authType: 'NONE',
442+
businessType: 'NORMAL',
443+
internalDomain: expect.any(String),
444+
url: expect.stringContaining('http'),
445+
isBase64Encoded: false
446+
}
447+
],
448+
urls: expect.any(Array)
449+
})
450+
})
451+
452+
it('remove web function', async () => {
453+
await sleep(5000)
454+
await sdk.remove(webFuncYaml, credentials)
455+
const result = await sdk.getInstance(
456+
webFuncYaml.org,
457+
webFuncYaml.stage,
458+
webFuncYaml.app,
459+
webFuncYaml.name
460+
)
461+
462+
expect(result.instance.instanceStatus).toEqual('inactive')
463+
})
373464
})

0 commit comments

Comments
 (0)