Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Retry some tests using NUnit #1797

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions Libraries/test/TestServerlessApp.IntegrationTests/CustomResponse.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using Xunit;
using NUnit.Framework;

namespace TestServerlessApp.IntegrationTests
{
[Collection("Integration Tests")]
public class CustomResponse
[TestFixture]
public class CustomResponse : IntegrationTestsSetup
{
private readonly IntegrationTestContextFixture _fixture;

public CustomResponse(IntegrationTestContextFixture fixture)
{
_fixture = fixture;
}

[Fact]
private static int attempt = 0;

[Test]
[Retry(15)]
public async Task OkResponseWithHeader_Returns200Status()
{
var response = await _fixture.HttpClient.GetAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/1");
response.EnsureSuccessStatusCode();
Assert.Equal("All Good", await response.Content.ReadAsStringAsync());
attempt++;
Console.WriteLine($"Attempt {attempt}");
var response = await HttpClient.GetAsync($"{RestApiUrlPrefix}/okresponsewithheader/1");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(await response.Content.ReadAsStringAsync(), Is.EqualTo("All Good"));
}

[Fact]
[Test]
[Retry(15)]
public async Task OkResponseWithHeader_ReturnsValidationErrors()
{
var response = await _fixture.HttpClient.GetAsync($"{_fixture.RestApiUrlPrefix}/okresponsewithheader/hello");
Assert.Equal(400, (int)response.StatusCode);
attempt++;
Console.WriteLine($"Attempt {attempt}");
var response = await HttpClient.GetAsync($"{RestApiUrlPrefix}/okresponsewithheader/hello");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var content = await response.Content.ReadAsStringAsync();
var errorJson = JObject.Parse(content);

var expectedErrorMessage = "1 validation error(s) detected: Value hello at 'x' failed to satisfy constraint: Input string was not in a correct format.";
Assert.Equal(expectedErrorMessage, errorJson["message"]);
Assert.That(errorJson["message"].ToString(), Is.EqualTo(expectedErrorMessage));
}

[Fact]
[Test]
[Retry(15)]
public async Task OkResponseWithCustomSerializer_Returns200Status()
{
var response = await _fixture.HttpClient.GetAsync($"{_fixture.HttpApiUrlPrefix}/okresponsewithcustomserializerasync/John/Doe");
response.EnsureSuccessStatusCode();
attempt++;
Console.WriteLine($"Attempt {attempt}");
var response = await HttpClient.GetAsync($"{HttpApiUrlPrefix}/okresponsewithcustomserializerasync/John/Doe");
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));

var content = await response.Content.ReadAsStringAsync();
var person = JObject.Parse(content);
Assert.Equal("John", person["FIRST_NAME"]);
Assert.Equal("Doe", person["LAST_NAME"]);
Assert.That(person["FIRST_NAME"].ToString(), Is.EqualTo("John"));
Assert.That(person["LAST_NAME"].ToString(), Is.EqualTo("Doe"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.CloudFormation;
using Amazon.CloudWatchLogs;
using Amazon.Lambda;
using Amazon.S3;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using TestServerlessApp.IntegrationTests.Helpers;

namespace TestServerlessApp.IntegrationTests;

public class IntegrationTestsSetup
{
private readonly CloudFormationHelper _cloudFormationHelper;
private readonly S3Helper _s3Helper;

private string _stackName;
private string _bucketName;

public readonly LambdaHelper LambdaHelper;
public readonly CloudWatchHelper CloudWatchHelper;
public readonly HttpClient HttpClient;

public string RestApiUrlPrefix;
public string HttpApiUrlPrefix;
public string TestQueueARN;
public List<LambdaFunction> LambdaFunctions;

public IntegrationTestsSetup()
{
_cloudFormationHelper = new CloudFormationHelper(new AmazonCloudFormationClient(Amazon.RegionEndpoint.USWest2));
_s3Helper = new S3Helper(new AmazonS3Client(Amazon.RegionEndpoint.USWest2));
LambdaHelper = new LambdaHelper(new AmazonLambdaClient(Amazon.RegionEndpoint.USWest2));
CloudWatchHelper = new CloudWatchHelper(new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2));
HttpClient = new HttpClient();
}

[OneTimeSetUp]
public async Task OneTimeSetUp()
{
var scriptPath = Path.Combine("..", "..", "..", "DeploymentScript.ps1");
await CommandLineWrapper.RunAsync($"pwsh {scriptPath}");

_stackName = GetStackName();
_bucketName = GetBucketName();
Assert.That(string.IsNullOrEmpty(_stackName), Is.False);
Assert.That(string.IsNullOrEmpty(_bucketName), Is.False);

RestApiUrlPrefix = await _cloudFormationHelper.GetOutputValueAsync(_stackName, "RestApiURL");
HttpApiUrlPrefix = await _cloudFormationHelper.GetOutputValueAsync(_stackName, "HttpApiURL");
TestQueueARN = await _cloudFormationHelper.GetOutputValueAsync(_stackName, "TestQueueARN");
LambdaFunctions = await LambdaHelper.FilterByCloudFormationStackAsync(_stackName);

var stackStatus = await _cloudFormationHelper.GetStackStatusAsync(_stackName);
Assert.That(stackStatus, Is.EqualTo(StackStatus.CREATE_COMPLETE));
Assert.That(await _s3Helper.BucketExistsAsync(_bucketName), Is.True);
Assert.That(LambdaFunctions.Count, Is.EqualTo(28));
Assert.That(string.IsNullOrEmpty(RestApiUrlPrefix), Is.False);
Assert.That(string.IsNullOrEmpty(RestApiUrlPrefix), Is.False);

await LambdaHelper.WaitTillNotPending(LambdaFunctions.Select(x => x.Name).ToList());
}

[OneTimeTearDown]
public async Task OneTimeTearDown()
{

await _cloudFormationHelper.DeleteStackAsync(_stackName);
Assert.That(await _cloudFormationHelper.IsDeletedAsync(_stackName), Is.True, $"The stack '{_stackName}' still exists and will have to be manually deleted from the AWS console.");

await _s3Helper.DeleteBucketAsync(_bucketName);
Assert.That(await _s3Helper.BucketExistsAsync(_bucketName), Is.False, $"The bucket '{_bucketName}' still exists and will have to be manually deleted from the AWS console.");

var filePath = Path.Combine("..", "..", "..", "..", "TestServerlessApp", "aws-lambda-tools-defaults.json");
var token = JObject.Parse(await File.ReadAllTextAsync(filePath));
token["s3-bucket"] = "test-serverless-app";
token["stack-name"] = "test-serverless-app";
await File.WriteAllTextAsync(filePath, token.ToString(Formatting.Indented));
}

private string GetStackName()
{
var filePath = Path.Combine("..", "..", "..", "..", "TestServerlessApp", "aws-lambda-tools-defaults.json");
var token = JObject.Parse(File.ReadAllText(filePath))["stack-name"];
return token.ToObject<string>();
}

private string GetBucketName()
{
var filePath = Path.Combine("..", "..", "..", "..", "TestServerlessApp", "aws-lambda-tools-defaults.json");
var token = JObject.Parse(File.ReadAllText(filePath))["s3-bucket"];
return token.ToObject<string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<!-- AWSSDK.SecurityToken is needed at runtime for environments which uses assume-role operation for credentials -->
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.1.99" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="4.2.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading