|
| 1 | +import { after, before, describe, it } from 'node:test'; |
| 2 | +import { execa } from 'execa'; |
| 3 | +import path from 'path'; |
| 4 | +import { TestBranch, amplifyAppPool } from '../amplify_app_pool.js'; |
| 5 | +import { BackendIdentifier } from '@aws-amplify/plugin-types'; |
| 6 | +import { |
| 7 | + CloudFormationClient, |
| 8 | + DeleteStackCommand, |
| 9 | +} from '@aws-sdk/client-cloudformation'; |
| 10 | +import fsp from 'fs/promises'; |
| 11 | +import { e2eToolingClientConfig } from '../e2e_tooling_client_config.js'; |
| 12 | +import { NpmProxyController } from '../npm_proxy_controller.js'; |
| 13 | +import assert from 'assert'; |
| 14 | +import os from 'os'; |
| 15 | +import { generateClientConfig } from '@aws-amplify/client-config'; |
| 16 | +import { BackendIdentifierConversions } from '@aws-amplify/platform-core'; |
| 17 | +import { amplifyAtTag } from '../constants.js'; |
| 18 | + |
| 19 | +void describe('client config backwards compatibility', () => { |
| 20 | + let branchBackendIdentifier: BackendIdentifier; |
| 21 | + let testBranch: TestBranch; |
| 22 | + let cfnClient: CloudFormationClient; |
| 23 | + let tempDir: string; |
| 24 | + let baselineDir: string; |
| 25 | + let baselineNpmProxyController: NpmProxyController; |
| 26 | + let currentNpmProxyController: NpmProxyController; |
| 27 | + |
| 28 | + before(async () => { |
| 29 | + assert.ok( |
| 30 | + process.env.BASELINE_DIR, |
| 31 | + 'BASELINE_DIR environment variable must be set and point to amplify-backend repo at baseline version' |
| 32 | + ); |
| 33 | + baselineDir = process.env.BASELINE_DIR; |
| 34 | + |
| 35 | + tempDir = await fsp.mkdtemp( |
| 36 | + path.join(os.tmpdir(), 'test-amplify-outputs-backwards-compatibility') |
| 37 | + ); |
| 38 | + |
| 39 | + console.log(`Temp dir is ${tempDir}`); |
| 40 | + |
| 41 | + cfnClient = new CloudFormationClient(e2eToolingClientConfig); |
| 42 | + baselineNpmProxyController = new NpmProxyController(baselineDir); |
| 43 | + currentNpmProxyController = new NpmProxyController(); |
| 44 | + testBranch = await amplifyAppPool.createTestBranch(); |
| 45 | + branchBackendIdentifier = { |
| 46 | + namespace: testBranch.appId, |
| 47 | + name: testBranch.branchName, |
| 48 | + type: 'branch', |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + after(async () => { |
| 53 | + await cfnClient.send( |
| 54 | + new DeleteStackCommand({ |
| 55 | + StackName: BackendIdentifierConversions.toStackName( |
| 56 | + branchBackendIdentifier |
| 57 | + ), |
| 58 | + }) |
| 59 | + ); |
| 60 | + await fsp.rm(tempDir, { recursive: true }); |
| 61 | + |
| 62 | + await baselineNpmProxyController.tearDown(); |
| 63 | + await currentNpmProxyController.tearDown(); |
| 64 | + }); |
| 65 | + |
| 66 | + const deploy = async (): Promise<void> => { |
| 67 | + await execa( |
| 68 | + 'npx', |
| 69 | + [ |
| 70 | + 'ampx', |
| 71 | + 'pipeline-deploy', |
| 72 | + '--branch', |
| 73 | + branchBackendIdentifier.name, |
| 74 | + '--appId', |
| 75 | + branchBackendIdentifier.namespace, |
| 76 | + ], |
| 77 | + { |
| 78 | + cwd: tempDir, |
| 79 | + stdio: 'inherit', |
| 80 | + env: { |
| 81 | + CI: 'true', |
| 82 | + }, |
| 83 | + } |
| 84 | + ); |
| 85 | + }; |
| 86 | + |
| 87 | + const reinstallDependencies = async (): Promise<void> => { |
| 88 | + await fsp.rm(path.join(tempDir, 'node_modules'), { |
| 89 | + recursive: true, |
| 90 | + force: true, |
| 91 | + }); |
| 92 | + await fsp.unlink(path.join(tempDir, 'package-lock.json')); |
| 93 | + |
| 94 | + await execa('npm', ['install'], { |
| 95 | + cwd: tempDir, |
| 96 | + stdio: 'inherit', |
| 97 | + }); |
| 98 | + }; |
| 99 | + |
| 100 | + const assertGenerateClientConfigAPI = async ( |
| 101 | + type: 'baseline' | 'current' |
| 102 | + ) => { |
| 103 | + try { |
| 104 | + assert.ok( |
| 105 | + await generateClientConfig(branchBackendIdentifier, '1'), |
| 106 | + `outputs v1 failed to be generated for an app created with ${type} library version` |
| 107 | + ); |
| 108 | + } catch (e) { |
| 109 | + throw new Error( |
| 110 | + `outputs v1 failed to be generated for an app created with ${type} library version. Error: ${JSON.stringify( |
| 111 | + e |
| 112 | + )}` |
| 113 | + ); |
| 114 | + } |
| 115 | + try { |
| 116 | + assert.ok( |
| 117 | + await generateClientConfig(branchBackendIdentifier, '1.1'), |
| 118 | + `outputs v1.1 failed to be generated for an app created with ${type} library version` |
| 119 | + ); |
| 120 | + } catch (e) { |
| 121 | + throw new Error( |
| 122 | + `outputs v1.1 failed to be generated for an app created with ${type} library version. Error: ${JSON.stringify( |
| 123 | + e |
| 124 | + )}` |
| 125 | + ); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + const assertGenerateClientConfigCommand = async ( |
| 130 | + type: 'baseline' | 'current' |
| 131 | + ) => { |
| 132 | + await execa( |
| 133 | + 'npx', |
| 134 | + [ |
| 135 | + 'ampx', |
| 136 | + 'generate', |
| 137 | + 'outputs', |
| 138 | + '--stack', |
| 139 | + BackendIdentifierConversions.toStackName(branchBackendIdentifier), |
| 140 | + ], |
| 141 | + { |
| 142 | + cwd: tempDir, |
| 143 | + stdio: 'inherit', |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + const fileSize = ( |
| 148 | + await fsp.stat(path.join(tempDir, 'amplify_outputs.json')) |
| 149 | + ).size; |
| 150 | + assert.ok( |
| 151 | + fileSize > 100, // Validate that it's not just a shim |
| 152 | + `outputs file should not be empty when generating for a ${ |
| 153 | + type === 'baseline' ? 'new' : 'old' |
| 154 | + } new app with the ${type} version` |
| 155 | + ); |
| 156 | + }; |
| 157 | + |
| 158 | + void it('outputs generation should be backwards and forward compatible', async () => { |
| 159 | + // build an app using previous (baseline) version |
| 160 | + await baselineNpmProxyController.setUp(); |
| 161 | + await execa('npm', ['create', amplifyAtTag, '--yes'], { |
| 162 | + cwd: tempDir, |
| 163 | + stdio: 'inherit', |
| 164 | + }); |
| 165 | + |
| 166 | + // Replace backend.ts to add custom outputs without version as well. |
| 167 | + await fsp.writeFile( |
| 168 | + path.join(tempDir, 'amplify', 'backend.ts'), |
| 169 | + `import { defineBackend } from '@aws-amplify/backend'; |
| 170 | +import { auth } from './auth/resource'; |
| 171 | +import { data } from './data/resource'; |
| 172 | +
|
| 173 | +const backend = defineBackend({ |
| 174 | + auth, |
| 175 | + data, |
| 176 | +}); |
| 177 | +
|
| 178 | +backend.addOutput({ |
| 179 | + custom: { |
| 180 | + someCustomOutput: 'someCustomOutputValue', |
| 181 | + }, |
| 182 | +}); |
| 183 | +` |
| 184 | + ); |
| 185 | + await deploy(); |
| 186 | + await baselineNpmProxyController.tearDown(); |
| 187 | + |
| 188 | + // Generate the outputs using the current version for apps built with baseline version |
| 189 | + |
| 190 | + // 1. via CLI command |
| 191 | + await currentNpmProxyController.setUp(); |
| 192 | + await reinstallDependencies(); |
| 193 | + await assertGenerateClientConfigCommand('current'); |
| 194 | + |
| 195 | + // 2. via API. |
| 196 | + await assertGenerateClientConfigAPI('current'); |
| 197 | + |
| 198 | + // Re-deploy the app using the current version now |
| 199 | + await deploy(); |
| 200 | + |
| 201 | + // Generate the outputs using the baseline version for apps built with current version |
| 202 | + |
| 203 | + // 1. via CLI command |
| 204 | + await currentNpmProxyController.tearDown(); |
| 205 | + await baselineNpmProxyController.setUp(); |
| 206 | + await reinstallDependencies(); |
| 207 | + await assertGenerateClientConfigCommand('baseline'); |
| 208 | + |
| 209 | + // 2. via API. |
| 210 | + await assertGenerateClientConfigAPI('baseline'); |
| 211 | + }); |
| 212 | +}); |
0 commit comments