|
1 | 1 | import { AzureNamingService } from "./namingService" |
| 2 | +import { ServerlessAzureConfig } from "../models/serverless"; |
2 | 3 |
|
3 | 4 | describe("Naming Service", () => { |
4 | | - |
5 | 5 | it("Creates a short name for an azure region", () => { |
6 | 6 | const expected = "ausse"; |
7 | 7 | const actual = AzureNamingService.createShortAzureRegionName("australiasoutheast"); |
@@ -86,4 +86,57 @@ describe("Naming Service", () => { |
86 | 86 | const actual = AzureNamingService.getNormalizedRegionName(expected); |
87 | 87 | expect(actual).toEqual(expected); |
88 | 88 | }); |
| 89 | + |
| 90 | + it("deployment name is generated correctly", () => { |
| 91 | + const config: ServerlessAzureConfig = { |
| 92 | + functions: [], |
| 93 | + plugins: [], |
| 94 | + provider: { |
| 95 | + prefix: "sls", |
| 96 | + name: "azure", |
| 97 | + region: "westus", |
| 98 | + stage: "dev", |
| 99 | + }, |
| 100 | + service: "test-api" |
| 101 | + }; |
| 102 | + |
| 103 | + const timestamp = Date.now(); |
| 104 | + const deploymentName = AzureNamingService.getDeploymentName(config, `t${timestamp}`); |
| 105 | + |
| 106 | + expect(deploymentName).toEqual(`slswusdevtestapi-DEPLOYMENT-t${timestamp}`); |
| 107 | + assertValidDeploymentName(config, deploymentName, timestamp); |
| 108 | + }); |
| 109 | + |
| 110 | + it("deployment name with long suffix or service name generated correctly", () => { |
| 111 | + const config: ServerlessAzureConfig = { |
| 112 | + functions: [], |
| 113 | + plugins: [], |
| 114 | + provider: { |
| 115 | + prefix: "sls-long-prefix-name", |
| 116 | + name: "azure", |
| 117 | + region: "westus", |
| 118 | + stage: "multicloud", |
| 119 | + }, |
| 120 | + service: "extra-long-service-name" |
| 121 | + }; |
| 122 | + |
| 123 | + const timestamp = Date.now(); |
| 124 | + const deploymentName = AzureNamingService.getDeploymentName(config, `t${timestamp}`); |
| 125 | + |
| 126 | + expect(deploymentName).toEqual(`slswusmulext-DEPLOYMENT-t${timestamp}`); |
| 127 | + assertValidDeploymentName(config, deploymentName, timestamp); |
| 128 | + }); |
| 129 | + |
| 130 | + function assertValidDeploymentName(config: ServerlessAzureConfig, value: string, timestamp: number) { |
| 131 | + expect(value.length).toBeLessThanOrEqual(64); |
| 132 | + expect(value).toContain(timestamp); |
| 133 | + expect(value).toContain(AzureNamingService.createShortAzureRegionName(config.provider.region)); |
| 134 | + expect(value).toContain(createSafeString(config.provider.prefix)); |
| 135 | + expect(value).toContain(createSafeString(config.provider.stage)); |
| 136 | + expect(value).toContain(createSafeString(config.service)); |
| 137 | + } |
| 138 | + |
| 139 | + function createSafeString(value: string) { |
| 140 | + return value.replace(/\W+/g, "").toLowerCase().substr(0, 3); |
| 141 | + }; |
89 | 142 | }); |
0 commit comments