|
| 1 | +import { |
| 2 | + EbDeployInputs, |
| 3 | + EbDeployOutputs, |
| 4 | + EventConnectionItem, |
| 5 | + EventConnectionOutputs, |
| 6 | +} from '../../src/modules/eb/interface'; |
| 7 | +import { EventBridge } from '../../src'; |
| 8 | +import { getQcsResourceId } from '../../src/utils'; |
| 9 | + |
| 10 | +describe('eb', () => { |
| 11 | + const credentials = { |
| 12 | + SecretId: process.env.TENCENT_SECRET_ID, |
| 13 | + SecretKey: process.env.TENCENT_SECRET_KEY, |
| 14 | + }; |
| 15 | + const eb = new EventBridge(credentials); |
| 16 | + const inputs: EbDeployInputs = { |
| 17 | + type: 'Cloud', |
| 18 | + eventBusName: 'unit-test-eb', |
| 19 | + description: 'test eb deploy', |
| 20 | + uin: process.env.TENCENT_UIN, |
| 21 | + // 目前仅支持广州区 |
| 22 | + region: 'ap-guangzhou', |
| 23 | + connections: [ |
| 24 | + { |
| 25 | + connectionName: 'test-conn-01', |
| 26 | + description: 'test connection binding', |
| 27 | + enable: true, |
| 28 | + type: 'apigw', |
| 29 | + // e.g: |
| 30 | + // connectionDescription: { |
| 31 | + // serviceId: 'service-abcd123', |
| 32 | + // gwParams: { |
| 33 | + // Protocol: 'HTTP', |
| 34 | + // Method: 'POST', |
| 35 | + // }, |
| 36 | + // }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + rules: [ |
| 40 | + { |
| 41 | + ruleName: 'test-rule-01', |
| 42 | + eventPattern: '{\n "source": ["apigw.cloud.tencent"]\n}', |
| 43 | + enable: true, |
| 44 | + description: 'test rule deploy', |
| 45 | + type: 'Cloud', |
| 46 | + targets: [ |
| 47 | + { |
| 48 | + type: 'scf', |
| 49 | + functionName: 'serverless-unit-test', |
| 50 | + functionNamespace: 'default', |
| 51 | + functionVersion: '$DEFAULT', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + ], |
| 56 | + }; |
| 57 | + let outputs: EbDeployOutputs; |
| 58 | + let testConnInfo: EventConnectionOutputs; |
| 59 | + |
| 60 | + test('[Auto Connection] should deploy a event bridge success', async () => { |
| 61 | + outputs = await eb.deploy(inputs); |
| 62 | + expect(outputs.type).toEqual(inputs.type); |
| 63 | + expect(outputs.eventBusName).toEqual(inputs.eventBusName); |
| 64 | + expect(outputs.description).toEqual(inputs.description); |
| 65 | + inputs.eventBusId = outputs.eventBusId; |
| 66 | + |
| 67 | + expect(outputs.connections).toHaveLength(1); |
| 68 | + expect(outputs.connections[0].connectionName).toEqual(inputs.connections[0].connectionName); |
| 69 | + expect(outputs.connections[0].type).toEqual(inputs.connections[0].type); |
| 70 | + expect(outputs.connections[0].connectionDescription.APIGWParams).toEqual({ |
| 71 | + Protocol: 'HTTP', |
| 72 | + Method: 'POST', |
| 73 | + }); |
| 74 | + // 获取自动创建的API网关,便于后续测试 |
| 75 | + inputs.connections[0].connectionId = outputs.connections[0].connectionId; |
| 76 | + const qcsItems = outputs.connections[0].connectionDescription.ResourceDescription.split('/'); |
| 77 | + const tempServiceId = qcsItems[qcsItems.length - 1]; |
| 78 | + inputs.connections[0].connectionDescription = { |
| 79 | + serviceId: tempServiceId, |
| 80 | + gwParams: outputs.connections[0].connectionDescription.APIGWParams, |
| 81 | + }; |
| 82 | + testConnInfo = outputs.connections[0]; |
| 83 | + |
| 84 | + expect(outputs.rules).toHaveLength(1); |
| 85 | + expect(outputs.rules[0].ruleName).toEqual(inputs.rules[0].ruleName); |
| 86 | + expect(outputs.rules[0].type).toEqual(inputs.rules[0].type); |
| 87 | + expect(outputs.rules[0].description).toEqual(inputs.rules[0].description); |
| 88 | + expect(outputs.rules[0].eventPattern).toEqual(inputs.rules[0].eventPattern); |
| 89 | + expect(outputs.rules[0].targets).toHaveLength(1); |
| 90 | + inputs.rules[0].ruleId = outputs.rules[0].ruleId; |
| 91 | + |
| 92 | + const inputTarget = inputs.rules[0].targets[0]; |
| 93 | + expect(outputs.rules[0].targets[0].type).toEqual(inputTarget.type); |
| 94 | + const resourceId = getQcsResourceId( |
| 95 | + 'scf', |
| 96 | + 'ap-guangzhou', |
| 97 | + process.env.TENCENT_UIN, |
| 98 | + `namespace/${inputTarget.functionNamespace}/function/${inputTarget.functionName}/${inputTarget.functionVersion}`, |
| 99 | + ); |
| 100 | + expect(outputs.rules[0].targets[0].targetDescription.resourceDescription).toEqual(resourceId); |
| 101 | + inputs.rules[0].targets[0].targetId = outputs.rules[0].targets[0].targetId; |
| 102 | + }); |
| 103 | + |
| 104 | + test('[Auto Connection] should update event bridge success', async () => { |
| 105 | + inputs.eventBusName = 'new-eb-01'; |
| 106 | + const newConn = { |
| 107 | + connectionName: 'test-conn-02', |
| 108 | + description: 'test connection binding', |
| 109 | + type: 'apigw', |
| 110 | + enable: true, |
| 111 | + connectionDescription: { |
| 112 | + serviceId: inputs.connections[0].connectionDescription.serviceId, |
| 113 | + gwParams: { Protocol: 'HTTP', Method: 'GET' }, |
| 114 | + }, |
| 115 | + }; |
| 116 | + inputs.connections.push(newConn as EventConnectionItem); |
| 117 | + inputs.connections[0].connectionName = 'new-conn-01'; |
| 118 | + inputs.rules[0].ruleName = 'new-rule-01'; |
| 119 | + |
| 120 | + outputs = await eb.deploy(inputs); |
| 121 | + expect(outputs.eventBusName).toEqual(inputs.eventBusName); |
| 122 | + expect(outputs.rules[0].ruleName).toEqual(inputs.rules[0].ruleName); |
| 123 | + expect(outputs.connections).toHaveLength(2); |
| 124 | + expect(outputs.connections[0].connectionName).toEqual(inputs.connections[0].connectionName); |
| 125 | + expect(outputs.connections[0].connectionDescription.APIGWParams).toEqual( |
| 126 | + inputs.connections[0].connectionDescription.gwParams, |
| 127 | + ); |
| 128 | + const firstTargetResDesc = getQcsResourceId( |
| 129 | + 'apigw', |
| 130 | + 'ap-guangzhou', |
| 131 | + process.env.TENCENT_UIN, |
| 132 | + `serviceid/${inputs.connections[0].connectionDescription.serviceId}`, |
| 133 | + ); |
| 134 | + expect(outputs.connections[0].connectionDescription.ResourceDescription).toEqual( |
| 135 | + firstTargetResDesc, |
| 136 | + ); |
| 137 | + |
| 138 | + expect(outputs.connections[1].connectionName).toEqual(inputs.connections[1].connectionName); |
| 139 | + expect(outputs.connections[1].type).toEqual(inputs.connections[1].type); |
| 140 | + const targetResDesc = getQcsResourceId( |
| 141 | + 'apigw', |
| 142 | + 'ap-guangzhou', |
| 143 | + process.env.TENCENT_UIN, |
| 144 | + `serviceid/${inputs.connections[1].connectionDescription.serviceId}`, |
| 145 | + ); |
| 146 | + expect(outputs.connections[1].connectionDescription.ResourceDescription).toEqual(targetResDesc); |
| 147 | + expect(outputs.connections[1].connectionDescription.APIGWParams).toEqual( |
| 148 | + inputs.connections[1].connectionDescription.gwParams, |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + test('[Auto Connection] should remove event bridge success', async () => { |
| 153 | + const res = await eb.remove(outputs.eventBusId); |
| 154 | + expect(res).toEqual(true); |
| 155 | + }); |
| 156 | + |
| 157 | + test('[Spec Connection] should deploy event bridge success', async () => { |
| 158 | + const qcsItems = testConnInfo.connectionDescription.ResourceDescription.split('/'); |
| 159 | + const tempServiceId = qcsItems[qcsItems.length - 1]; |
| 160 | + const newInput: EbDeployInputs = { |
| 161 | + type: 'Cloud', |
| 162 | + eventBusName: 'test-eb-02', |
| 163 | + description: 'test eb deploy', |
| 164 | + uin: process.env.TENCENT_UIN, |
| 165 | + region: 'ap-guangzhou', |
| 166 | + connections: [ |
| 167 | + { |
| 168 | + connectionName: 'test-conn-01', |
| 169 | + description: 'test connection binding', |
| 170 | + enable: true, |
| 171 | + type: 'apigw', |
| 172 | + connectionDescription: { |
| 173 | + serviceId: tempServiceId, |
| 174 | + gwParams: testConnInfo.connectionDescription.APIGWParams, |
| 175 | + }, |
| 176 | + }, |
| 177 | + ], |
| 178 | + rules: [ |
| 179 | + { |
| 180 | + ruleName: 'test-rule', |
| 181 | + eventPattern: '{\n "source": ["apigw.cloud.tencent"]\n}', |
| 182 | + enable: true, |
| 183 | + description: 'test rule deploy', |
| 184 | + type: 'Cloud', |
| 185 | + targets: [ |
| 186 | + { |
| 187 | + type: 'scf', |
| 188 | + functionName: 'serverless-unit-test', |
| 189 | + functionNamespace: 'default', |
| 190 | + functionVersion: '$DEFAULT', |
| 191 | + }, |
| 192 | + ], |
| 193 | + }, |
| 194 | + ], |
| 195 | + }; |
| 196 | + outputs = await eb.deploy(newInput); |
| 197 | + expect(outputs.eventBusName).toEqual(newInput.eventBusName); |
| 198 | + const targetResDesc = getQcsResourceId( |
| 199 | + 'apigw', |
| 200 | + 'ap-guangzhou', |
| 201 | + process.env.TENCENT_UIN, |
| 202 | + `serviceid/${newInput.connections[0].connectionDescription.serviceId}`, |
| 203 | + ); |
| 204 | + expect(outputs.connections[0].connectionDescription.ResourceDescription).toEqual(targetResDesc); |
| 205 | + expect(outputs.connections[0].connectionDescription.APIGWParams).toEqual( |
| 206 | + newInput.connections[0].connectionDescription.gwParams, |
| 207 | + ); |
| 208 | + expect(outputs.rules).toHaveLength(1); |
| 209 | + expect(outputs.rules[0].ruleName).toEqual(newInput.rules[0].ruleName); |
| 210 | + expect(outputs.rules[0].targets).toHaveLength(1); |
| 211 | + }); |
| 212 | + |
| 213 | + test('[Spec Connection] should remove event bridge success', async () => { |
| 214 | + const res = await eb.remove(outputs.eventBusId); |
| 215 | + expect(res).toEqual(true); |
| 216 | + }); |
| 217 | + |
| 218 | + test('[Without Connections and Rules] should create event success', async () => { |
| 219 | + const newInput: EbDeployInputs = { |
| 220 | + type: 'Cloud', |
| 221 | + eventBusName: 'test-eb-03', |
| 222 | + description: 'test eb deploy', |
| 223 | + uin: process.env.TENCENT_UIN, |
| 224 | + region: 'ap-guangzhou', |
| 225 | + }; |
| 226 | + outputs = await eb.deploy(newInput); |
| 227 | + expect(outputs.eventBusName).toEqual(newInput.eventBusName); |
| 228 | + }); |
| 229 | + |
| 230 | + test('[Without Connections and Rules] should remove event success', async () => { |
| 231 | + const res = await eb.remove(outputs.eventBusId); |
| 232 | + expect(res).toEqual(true); |
| 233 | + }); |
| 234 | +}); |
0 commit comments