|
| 1 | +import fs from "fs"; |
| 2 | + |
| 3 | +import { |
| 4 | + transformArgsToDict, |
| 5 | + replaceStacknameOpt, |
| 6 | + CYAN_STRING_FORMAT, |
| 7 | +} from "../../src/services/severlessConfigParser/helpers"; |
| 8 | +import ServerlessConfigParser from "../../src/services/severlessConfigParser/serverlessConfigParser"; |
| 9 | + |
| 10 | +const TEST_YAML_FILE = ` |
| 11 | +service: |
| 12 | + name: test-\${opt:testArg1} |
| 13 | +
|
| 14 | +package: |
| 15 | + exclude: |
| 16 | + - .git/** |
| 17 | + - .gitignore |
| 18 | +
|
| 19 | +plugins: |
| 20 | + - serverless-webpack |
| 21 | +
|
| 22 | +provider: |
| 23 | + name: aws |
| 24 | + runtime: nodejs10.x |
| 25 | + region: eu-west-1 |
| 26 | + stage: \${opt:stage, 'dev'} |
| 27 | + usagePlan: |
| 28 | + quota: |
| 29 | + limit: 5000 |
| 30 | + offset: 2 |
| 31 | + period: MONTH |
| 32 | + throttle: |
| 33 | + burstLimit: 200 |
| 34 | + rateLimit: 100 |
| 35 | + iamRoleStatements: |
| 36 | + - Effect: Allow |
| 37 | + Action: |
| 38 | + - dynamodb:DescribeTable |
| 39 | + - dynamodb:Query |
| 40 | + - dynamodb:Scan |
| 41 | + - dynamodb:GetItem |
| 42 | + - dynamodb:PutItem |
| 43 | + - dynamodb:UpdateItem |
| 44 | + - dynamodb:DeleteItem |
| 45 | + Resource: |
| 46 | + - 'Fn::GetAtt': [Table, Arn] |
| 47 | + environment: |
| 48 | + tableName: \${self:custom.dynamodbTableName} |
| 49 | +
|
| 50 | +functions: |
| 51 | + hello: |
| 52 | + handler: hello.main |
| 53 | + memorySize: 1024 |
| 54 | + timeout: 6 |
| 55 | + events: |
| 56 | + - http: |
| 57 | + method: get |
| 58 | + path: hello |
| 59 | + authorizer: |
| 60 | + type: COGNITO_USER_POOLS |
| 61 | + authorizerId: |
| 62 | + Ref: ApiGatewayAuthorizer |
| 63 | + create: |
| 64 | + handler: create.main |
| 65 | + events: |
| 66 | + - http: |
| 67 | + method: post |
| 68 | + path: items |
| 69 | + cors: true |
| 70 | + authorizer: |
| 71 | + type: COGNITO_USER_POOLS |
| 72 | + authorizerId: |
| 73 | + Ref: ApiGatewayAuthorizer |
| 74 | + list: |
| 75 | + handler: list.main |
| 76 | + events: |
| 77 | + - http: |
| 78 | + method: get |
| 79 | + path: items |
| 80 | + cors: true |
| 81 | + authorizer: |
| 82 | + type: COGNITO_USER_POOLS |
| 83 | + authorizerId: |
| 84 | + Ref: ApiGatewayAuthorizer |
| 85 | + get: |
| 86 | + handler: get.main |
| 87 | + events: |
| 88 | + - http: |
| 89 | + method: get |
| 90 | + path: items/{id} |
| 91 | + cors: true |
| 92 | + authorizer: |
| 93 | + type: COGNITO_USER_POOLS |
| 94 | + authorizerId: |
| 95 | + Ref: ApiGatewayAuthorizer |
| 96 | + update: |
| 97 | + handler: update.main |
| 98 | + events: |
| 99 | + - http: |
| 100 | + method: put |
| 101 | + path: items/{id} |
| 102 | + cors: true |
| 103 | + authorizer: |
| 104 | + type: COGNITO_USER_POOLS |
| 105 | + authorizerId: |
| 106 | + Ref: ApiGatewayAuthorizer |
| 107 | + delete: |
| 108 | + handler: delete.main |
| 109 | + events: |
| 110 | + - http: |
| 111 | + method: delete |
| 112 | + path: items/{id} |
| 113 | + cors: true |
| 114 | + authorizer: |
| 115 | + type: COGNITO_USER_POOLS |
| 116 | + authorizerId: |
| 117 | + Ref: ApiGatewayAuthorizer |
| 118 | +`; |
| 119 | + |
| 120 | +// eslint-disable-next-line no-template-curly-in-string |
| 121 | +const STACK_NAME_WITH_OPT = "test-${opt:testArg1}"; |
| 122 | +const STACK_NAME_WITHOUT_OPT = "test-backend"; |
| 123 | +const CMD_VAR_1 = "--testArg1"; |
| 124 | +const CMD_VAL_1 = "backend"; |
| 125 | +const CMD_VAR_2 = "-testArg2"; |
| 126 | +const CMD_VAL_2 = "arg2"; |
| 127 | +const INVALID_ARGUMENT_EXIT_CODE = 9; |
| 128 | + |
| 129 | +jest.mock("fs"); |
| 130 | + |
| 131 | +describe("Serverless Config Options", () => { |
| 132 | + /* |
| 133 | + * Transform Args |
| 134 | + */ |
| 135 | + it("should transform 1 arg (double dash) to dict", () => { |
| 136 | + const args = [CMD_VAR_1, CMD_VAL_1]; |
| 137 | + const result = { testArg1: CMD_VAL_1 }; |
| 138 | + expect(transformArgsToDict(args)).toStrictEqual(result); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should transform 1 arg (single dash) to dict", () => { |
| 142 | + const args = [CMD_VAR_2, CMD_VAL_2]; |
| 143 | + const result = { testArg2: CMD_VAL_2 }; |
| 144 | + expect(transformArgsToDict(args)).toStrictEqual(result); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should not remove dashes from middle of option in service name", () => { |
| 148 | + const args = ["--env-name", CMD_VAL_1]; |
| 149 | + const result = { "env-name": CMD_VAL_1 }; |
| 150 | + expect(transformArgsToDict(args)).toStrictEqual(result); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should transform multiple args (mixed dash) to dict", () => { |
| 154 | + const args = [CMD_VAR_1, CMD_VAL_1, CMD_VAR_2, CMD_VAL_2]; |
| 155 | + const result = { |
| 156 | + testArg1: CMD_VAL_1, |
| 157 | + testArg2: CMD_VAL_2, |
| 158 | + }; |
| 159 | + expect(transformArgsToDict(args)).toStrictEqual(result); |
| 160 | + }); |
| 161 | + |
| 162 | + /* |
| 163 | + * Replace service.name Opt |
| 164 | + */ |
| 165 | + it("should replace the service name option with stored option", () => { |
| 166 | + const testOptions = { testArg1: CMD_VAL_1 }; |
| 167 | + expect(replaceStacknameOpt(STACK_NAME_WITH_OPT, testOptions)).toBe( |
| 168 | + STACK_NAME_WITHOUT_OPT |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should not replace the stack name where there is not opt variable in the YAML configuration file", () => { |
| 173 | + const testOptions = { testArg1: CMD_VAL_1 }; |
| 174 | + expect(replaceStacknameOpt(STACK_NAME_WITHOUT_OPT, testOptions)).toBe( |
| 175 | + STACK_NAME_WITHOUT_OPT |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should output an error message when an opt varibale exists in serverless configuration but no option is passed", () => { |
| 180 | + console.error = jest.fn(); |
| 181 | + process.exit = jest.fn(); |
| 182 | + replaceStacknameOpt(STACK_NAME_WITH_OPT, []); |
| 183 | + |
| 184 | + expect(console.error).toHaveBeenCalledWith( |
| 185 | + CYAN_STRING_FORMAT, |
| 186 | + `Your project requires stack name option ${CMD_VAR_1} to be passed when starting sls-dev-tools` |
| 187 | + ); |
| 188 | + expect(process.exit).toHaveBeenCalledWith(INVALID_ARGUMENT_EXIT_CODE); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe("Serverless Config Parsing", () => { |
| 193 | + let SLS; |
| 194 | + |
| 195 | + beforeAll(() => { |
| 196 | + fs.readFileSync.mockReturnValue(TEST_YAML_FILE); |
| 197 | + fs.existsSync.mockReturnValue(true); |
| 198 | + |
| 199 | + const program = { |
| 200 | + args: [CMD_VAR_1, CMD_VAL_1], |
| 201 | + location: "~/Dev/testProject/backend", |
| 202 | + }; |
| 203 | + |
| 204 | + SLS = new ServerlessConfigParser(program); |
| 205 | + }); |
| 206 | + |
| 207 | + it("should read YAML file and replace opt in service.name with stored arg", () => { |
| 208 | + expect(SLS.getStackName("dev")).toBe("test-backend-dev"); |
| 209 | + }); |
| 210 | + |
| 211 | + it("should get a stackname given a stage", () => { |
| 212 | + const stage = "test"; |
| 213 | + expect(SLS.getStackName(stage)).toBe("test-backend-test"); |
| 214 | + }); |
| 215 | +}); |
0 commit comments