|
| 1 | +import { readFileSync } from 'fs'; |
| 2 | +import { resolve } from 'path'; |
| 3 | +import { load } from 'js-yaml'; |
| 4 | +import { describe, it, expect } from 'vitest'; |
| 5 | +import { ArtifactExporter } from '../../../src/artifactexporter/ArtifactExporter'; |
| 6 | +import { DocumentType } from '../../../src/document/Document'; |
| 7 | +import { createMockComponents } from '../../utils/MockServerComponents'; |
| 8 | + |
| 9 | +describe('Upload Artifact to S3', () => { |
| 10 | + describe('YAML template', () => { |
| 11 | + it('should identify template artifacts', () => { |
| 12 | + const mockComponents = createMockComponents(); |
| 13 | + const mockS3Service = mockComponents.s3Service; |
| 14 | + |
| 15 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_folder_artifact.yaml'); |
| 16 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 17 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.YAML, templatePath, templateContent); |
| 18 | + const artifacts = exporter.getTemplateArtifacts(); |
| 19 | + |
| 20 | + expect(artifacts).toHaveLength(1); |
| 21 | + expect(artifacts[0].resourceType).toBe('AWS::Serverless::Function'); |
| 22 | + expect(artifacts[0].filePath).toBe('./artifact/code'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should zip and upload folder artifact to S3', async () => { |
| 26 | + const mockComponents = createMockComponents(); |
| 27 | + const mockS3Service = mockComponents.s3Service; |
| 28 | + mockS3Service.putObject.resolves({ VersionId: 'v1', $metadata: {} }); |
| 29 | + |
| 30 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_folder_artifact.yaml'); |
| 31 | + const originalContent = readFileSync(templatePath, 'utf8'); |
| 32 | + const originalTemplate = load(originalContent) as any; |
| 33 | + |
| 34 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.YAML, templatePath, originalContent); |
| 35 | + const exportedTemplate = (await exporter.export('test-bucket', 'test-prefix')) as any; |
| 36 | + |
| 37 | + expect(mockS3Service.putObject.called).toBe(true); |
| 38 | + const [filePath, s3Url] = mockS3Service.putObject.getCall(0).args; |
| 39 | + expect(filePath).toMatch(/\.zip$/); |
| 40 | + expect(s3Url).toMatch(/^s3:\/\/test-bucket\/test-prefix\/artifact\//); |
| 41 | + |
| 42 | + expect(exportedTemplate.Resources.MyFunction.Properties.CodeUri).toMatch( |
| 43 | + /^s3:\/\/test-bucket\/test-prefix\/artifact\//, |
| 44 | + ); |
| 45 | + |
| 46 | + delete originalTemplate.Resources.MyFunction.Properties.CodeUri; |
| 47 | + delete exportedTemplate.Resources.MyFunction.Properties.CodeUri; |
| 48 | + |
| 49 | + expect(JSON.stringify(exportedTemplate)).toBe(JSON.stringify(originalTemplate)); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should upload file artifact to S3', async () => { |
| 53 | + const mockComponents = createMockComponents(); |
| 54 | + const mockS3Service = mockComponents.s3Service; |
| 55 | + mockS3Service.putObject.resolves({ VersionId: 'v1', $metadata: {} }); |
| 56 | + |
| 57 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_file_artifact.yaml'); |
| 58 | + const originalContent = readFileSync(templatePath, 'utf8'); |
| 59 | + const originalTemplate = load(originalContent) as any; |
| 60 | + |
| 61 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.YAML, templatePath, originalContent); |
| 62 | + const exportedTemplate = (await exporter.export('test-bucket', 'test-prefix')) as any; |
| 63 | + |
| 64 | + expect(exportedTemplate.Resources.MyApi.Properties.DefinitionUri).toMatch( |
| 65 | + /^s3:\/\/test-bucket\/test-prefix\/artifact\//, |
| 66 | + ); |
| 67 | + |
| 68 | + delete originalTemplate.Resources.MyApi.Properties.DefinitionUri; |
| 69 | + delete exportedTemplate.Resources.MyApi.Properties.DefinitionUri; |
| 70 | + |
| 71 | + expect(JSON.stringify(exportedTemplate)).toBe(JSON.stringify(originalTemplate)); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should do nothing when no artifacts exist', async () => { |
| 75 | + const mockComponents = createMockComponents(); |
| 76 | + const mockS3Service = mockComponents.s3Service; |
| 77 | + |
| 78 | + const templatePath = resolve(__dirname, '../../resources/templates/simple.yaml'); |
| 79 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 80 | + const originalTemplate = load(templateContent) as any; |
| 81 | + |
| 82 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.YAML, templatePath, templateContent); |
| 83 | + const exportedTemplate = (await exporter.export('test-bucket', 'test-prefix')) as any; |
| 84 | + |
| 85 | + expect(mockS3Service.putObject.called).toBe(false); |
| 86 | + |
| 87 | + expect(JSON.stringify(exportedTemplate)).toBe(JSON.stringify(originalTemplate)); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle S3 upload failures', async () => { |
| 91 | + const mockComponents = createMockComponents(); |
| 92 | + const mockS3Service = mockComponents.s3Service; |
| 93 | + mockS3Service.putObject.rejects(new Error('S3 upload failed')); |
| 94 | + |
| 95 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_folder_artifact.yaml'); |
| 96 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 97 | + |
| 98 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.YAML, templatePath, templateContent); |
| 99 | + |
| 100 | + await expect(exporter.export('test-bucket', 'test-prefix')).rejects.toThrow('S3 upload failed'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should throw exception for broken templates', () => { |
| 104 | + const mockComponents = createMockComponents(); |
| 105 | + const mockS3Service = mockComponents.s3Service; |
| 106 | + |
| 107 | + const templatePath = resolve(__dirname, '../../resources/templates/broken.yaml'); |
| 108 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 109 | + |
| 110 | + expect(() => { |
| 111 | + new ArtifactExporter(mockS3Service, DocumentType.YAML, templatePath, templateContent); |
| 112 | + }).toThrow(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('JSON template', () => { |
| 117 | + it('should identify template artifacts', () => { |
| 118 | + const mockComponents = createMockComponents(); |
| 119 | + const mockS3Service = mockComponents.s3Service; |
| 120 | + |
| 121 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_folder_artifact.json'); |
| 122 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 123 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.JSON, templatePath, templateContent); |
| 124 | + const artifacts = exporter.getTemplateArtifacts(); |
| 125 | + |
| 126 | + expect(artifacts).toHaveLength(1); |
| 127 | + expect(artifacts[0].resourceType).toBe('AWS::Serverless::Function'); |
| 128 | + expect(artifacts[0].filePath).toBe('./artifact/code'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should zip and upload folder artifact to S3', async () => { |
| 132 | + const mockComponents = createMockComponents(); |
| 133 | + const mockS3Service = mockComponents.s3Service; |
| 134 | + mockS3Service.putObject.resolves({ VersionId: 'v1', $metadata: {} }); |
| 135 | + |
| 136 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_folder_artifact.json'); |
| 137 | + const originalContent = readFileSync(templatePath, 'utf8'); |
| 138 | + const originalTemplate = JSON.parse(originalContent); |
| 139 | + |
| 140 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.JSON, templatePath, originalContent); |
| 141 | + const exportedTemplate = (await exporter.export('test-bucket', 'test-prefix')) as any; |
| 142 | + |
| 143 | + expect(mockS3Service.putObject.called).toBe(true); |
| 144 | + const [filePath, s3Url] = mockS3Service.putObject.getCall(0).args; |
| 145 | + expect(filePath).toMatch(/\.zip$/); |
| 146 | + expect(s3Url).toMatch(/^s3:\/\/test-bucket\/test-prefix\/artifact\//); |
| 147 | + |
| 148 | + expect(exportedTemplate.Resources.MyFunction.Properties.CodeUri).toMatch( |
| 149 | + /^s3:\/\/test-bucket\/test-prefix\/artifact\//, |
| 150 | + ); |
| 151 | + |
| 152 | + delete originalTemplate.Resources.MyFunction.Properties.CodeUri; |
| 153 | + delete exportedTemplate.Resources.MyFunction.Properties.CodeUri; |
| 154 | + |
| 155 | + expect(JSON.stringify(exportedTemplate)).toBe(JSON.stringify(originalTemplate)); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should upload file artifact to S3', async () => { |
| 159 | + const mockComponents = createMockComponents(); |
| 160 | + const mockS3Service = mockComponents.s3Service; |
| 161 | + mockS3Service.putObject.resolves({ VersionId: 'v1', $metadata: {} }); |
| 162 | + |
| 163 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_file_artifact.json'); |
| 164 | + const originalContent = readFileSync(templatePath, 'utf8'); |
| 165 | + const originalTemplate = JSON.parse(originalContent); |
| 166 | + |
| 167 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.JSON, templatePath, originalContent); |
| 168 | + const exportedTemplate = (await exporter.export('test-bucket', 'test-prefix')) as any; |
| 169 | + |
| 170 | + expect(exportedTemplate.Resources.MyApi.Properties.DefinitionUri).toMatch( |
| 171 | + /^s3:\/\/test-bucket\/test-prefix\/artifact\//, |
| 172 | + ); |
| 173 | + |
| 174 | + delete originalTemplate.Resources.MyApi.Properties.DefinitionUri; |
| 175 | + delete exportedTemplate.Resources.MyApi.Properties.DefinitionUri; |
| 176 | + |
| 177 | + expect(JSON.stringify(exportedTemplate)).toBe(JSON.stringify(originalTemplate)); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should do nothing when no artifacts exist', async () => { |
| 181 | + const mockComponents = createMockComponents(); |
| 182 | + const mockS3Service = mockComponents.s3Service; |
| 183 | + |
| 184 | + const templatePath = resolve(__dirname, '../../resources/templates/simple.json'); |
| 185 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 186 | + const originalTemplate = JSON.parse(templateContent); |
| 187 | + |
| 188 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.JSON, templatePath, templateContent); |
| 189 | + const exportedTemplate = (await exporter.export('test-bucket', 'test-prefix')) as any; |
| 190 | + |
| 191 | + expect(mockS3Service.putObject.called).toBe(false); |
| 192 | + |
| 193 | + expect(JSON.stringify(exportedTemplate)).toBe(JSON.stringify(originalTemplate)); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should handle S3 upload failures', async () => { |
| 197 | + const mockComponents = createMockComponents(); |
| 198 | + const mockS3Service = mockComponents.s3Service; |
| 199 | + mockS3Service.putObject.rejects(new Error('S3 upload failed')); |
| 200 | + |
| 201 | + const templatePath = resolve(__dirname, '../../resources/templates/template_with_folder_artifact.json'); |
| 202 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 203 | + |
| 204 | + const exporter = new ArtifactExporter(mockS3Service, DocumentType.JSON, templatePath, templateContent); |
| 205 | + |
| 206 | + await expect(exporter.export('test-bucket', 'test-prefix')).rejects.toThrow('S3 upload failed'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should throw exception for broken templates', () => { |
| 210 | + const mockComponents = createMockComponents(); |
| 211 | + const mockS3Service = mockComponents.s3Service; |
| 212 | + |
| 213 | + const templatePath = resolve(__dirname, '../../resources/templates/broken.json'); |
| 214 | + const templateContent = readFileSync(templatePath, 'utf8'); |
| 215 | + |
| 216 | + expect(() => { |
| 217 | + new ArtifactExporter(mockS3Service, DocumentType.JSON, templatePath, templateContent); |
| 218 | + }).toThrow(); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments