|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using Amazon.CloudFormation; |
| 5 | +using Amazon.APIGateway; |
| 6 | +using Amazon.ApiGatewayV2; |
| 7 | +using Amazon.Lambda.TestTool.IntegrationTests.Helpers; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace Amazon.Lambda.TestTool.IntegrationTests |
| 11 | +{ |
| 12 | + public class ApiGatewayIntegrationTestFixture : IAsyncLifetime |
| 13 | + { |
| 14 | + public CloudFormationHelper CloudFormationHelper { get; private set; } |
| 15 | + public ApiGatewayHelper ApiGatewayHelper { get; private set; } |
| 16 | + public ApiGatewayTestHelper ApiGatewayTestHelper { get; private set; } |
| 17 | + |
| 18 | + public string StackName { get; private set; } |
| 19 | + public string RestApiId { get; private set; } |
| 20 | + public string HttpApiV1Id { get; private set; } |
| 21 | + public string HttpApiV2Id { get; private set; } |
| 22 | + public string ReturnRawRequestBodyV2Id { get; private set; } |
| 23 | + public string RestApiUrl { get; private set; } |
| 24 | + public string HttpApiV1Url { get; private set; } |
| 25 | + public string HttpApiV2Url { get; private set; } |
| 26 | + public string ReturnRawRequestBodyHttpApiV2Url { get; private set; } |
| 27 | + |
| 28 | + public ApiGatewayIntegrationTestFixture() |
| 29 | + { |
| 30 | + var regionEndpoint = RegionEndpoint.USWest2; |
| 31 | + CloudFormationHelper = new CloudFormationHelper(new AmazonCloudFormationClient(regionEndpoint)); |
| 32 | + ApiGatewayHelper = new ApiGatewayHelper( |
| 33 | + new AmazonAPIGatewayClient(regionEndpoint), |
| 34 | + new AmazonApiGatewayV2Client(regionEndpoint) |
| 35 | + ); |
| 36 | + ApiGatewayTestHelper = new ApiGatewayTestHelper(); |
| 37 | + StackName = string.Empty; |
| 38 | + RestApiId = string.Empty; |
| 39 | + HttpApiV1Id = string.Empty; |
| 40 | + HttpApiV2Id = string.Empty; |
| 41 | + ReturnRawRequestBodyV2Id = string.Empty; |
| 42 | + RestApiUrl = string.Empty; |
| 43 | + HttpApiV1Url = string.Empty; |
| 44 | + HttpApiV2Url = string.Empty; |
| 45 | + ReturnRawRequestBodyHttpApiV2Url = string.Empty; |
| 46 | + } |
| 47 | + |
| 48 | + public async Task InitializeAsync() |
| 49 | + { |
| 50 | + StackName = $"Test-{Guid.NewGuid().ToString("N").Substring(0, 5)}"; |
| 51 | + |
| 52 | + string templateBody = ReadCloudFormationTemplate("cloudformation-template-apigateway.yaml"); |
| 53 | + await CloudFormationHelper.CreateStackAsync(StackName, templateBody); |
| 54 | + |
| 55 | + await WaitForStackCreationComplete(); |
| 56 | + await RetrieveStackOutputs(); |
| 57 | + await WaitForApisAvailability(); |
| 58 | + } |
| 59 | + |
| 60 | + private string ReadCloudFormationTemplate(string fileName) |
| 61 | + { |
| 62 | + var assembly = Assembly.GetExecutingAssembly(); |
| 63 | + var resourceName = $"{assembly.GetName().Name}.{fileName}"; |
| 64 | + using (var stream = assembly.GetManifestResourceStream(resourceName)) |
| 65 | + { |
| 66 | + if (stream == null) |
| 67 | + { |
| 68 | + throw new FileNotFoundException($"CloudFormation template file '{fileName}' not found in assembly resources."); |
| 69 | + } |
| 70 | + |
| 71 | + using (StreamReader reader = new StreamReader(stream)) |
| 72 | + { |
| 73 | + return reader.ReadToEnd(); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private async Task WaitForStackCreationComplete() |
| 79 | + { |
| 80 | + while (true) |
| 81 | + { |
| 82 | + var status = await CloudFormationHelper.GetStackStatusAsync(StackName); |
| 83 | + if (status == StackStatus.CREATE_COMPLETE) |
| 84 | + { |
| 85 | + break; |
| 86 | + } |
| 87 | + if (status.ToString().EndsWith("FAILED") || status == StackStatus.DELETE_COMPLETE) |
| 88 | + { |
| 89 | + throw new Exception($"Stack creation failed. Status: {status}"); |
| 90 | + } |
| 91 | + await Task.Delay(10000); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private async Task RetrieveStackOutputs() |
| 96 | + { |
| 97 | + RestApiId = await CloudFormationHelper.GetOutputValueAsync(StackName, "RestApiId"); |
| 98 | + RestApiUrl = await CloudFormationHelper.GetOutputValueAsync(StackName, "RestApiUrl"); |
| 99 | + |
| 100 | + HttpApiV1Id = await CloudFormationHelper.GetOutputValueAsync(StackName, "HttpApiV1Id"); |
| 101 | + HttpApiV1Url = await CloudFormationHelper.GetOutputValueAsync(StackName, "HttpApiV1Url"); |
| 102 | + |
| 103 | + HttpApiV2Id = await CloudFormationHelper.GetOutputValueAsync(StackName, "HttpApiV2Id"); |
| 104 | + HttpApiV2Url = await CloudFormationHelper.GetOutputValueAsync(StackName, "HttpApiV2Url"); |
| 105 | + |
| 106 | + ReturnRawRequestBodyV2Id = await CloudFormationHelper.GetOutputValueAsync(StackName, "ReturnRawRequestBodyHttpApiId"); |
| 107 | + ReturnRawRequestBodyHttpApiV2Url = await CloudFormationHelper.GetOutputValueAsync(StackName, "ReturnRawRequestBodyHttpApiUrl"); |
| 108 | + } |
| 109 | + |
| 110 | + private async Task WaitForApisAvailability() |
| 111 | + { |
| 112 | + await ApiGatewayHelper.WaitForApiAvailability(RestApiId, RestApiUrl, false); |
| 113 | + await ApiGatewayHelper.WaitForApiAvailability(HttpApiV1Id, HttpApiV1Url, true); |
| 114 | + await ApiGatewayHelper.WaitForApiAvailability(HttpApiV2Id, HttpApiV2Url, true); |
| 115 | + await ApiGatewayHelper.WaitForApiAvailability(ReturnRawRequestBodyV2Id, ReturnRawRequestBodyHttpApiV2Url, true); |
| 116 | + } |
| 117 | + |
| 118 | + public async Task DisposeAsync() |
| 119 | + { |
| 120 | + await CloudFormationHelper.DeleteStackAsync(StackName); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments