-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathLocalStackContainerTest.cs
More file actions
178 lines (140 loc) · 6.13 KB
/
LocalStackContainerTest.cs
File metadata and controls
178 lines (140 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace Testcontainers.LocalStack;
public abstract class LocalStackContainerTest : IAsyncLifetime
{
private const string AwsService = "Service";
private readonly LocalStackContainer _localStackContainer;
static LocalStackContainerTest()
{
Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", CommonCredentials.AwsAccessKey);
Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", CommonCredentials.AwsSecretKey);
}
private LocalStackContainerTest(LocalStackContainer localStackContainer)
{
_localStackContainer = localStackContainer;
}
public async ValueTask InitializeAsync()
{
await _localStackContainer.StartAsync()
.ConfigureAwait(false);
}
public async ValueTask DisposeAsync()
{
await DisposeAsyncCore()
.ConfigureAwait(false);
GC.SuppressFinalize(this);
}
[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
[Trait(AwsService, "cloudwatch")]
public async Task CreateLogReturnsHttpStatusCodeOk()
{
// Given
var config = new AmazonCloudWatchLogsConfig();
config.ServiceURL = _localStackContainer.GetConnectionString();
using var client = new AmazonCloudWatchLogsClient(config);
var logGroupRequest = new CreateLogGroupRequest(Guid.NewGuid().ToString("D"));
// When
var logGroupResponse = await client.CreateLogGroupAsync(logGroupRequest, TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// Then
Assert.Equal(HttpStatusCode.OK, logGroupResponse.HttpStatusCode);
Assert.Equal(_localStackContainer.GetConnectionString(), _localStackContainer.GetConnectionString(ConnectionMode.Host));
}
[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
[Trait(AwsService, "dynamodb")]
public async Task GetItemReturnsPutItem()
{
// Given
var id = Guid.NewGuid().ToString("D");
var tableName = Guid.NewGuid().ToString("D");
var config = new AmazonDynamoDBConfig();
config.ServiceURL = _localStackContainer.GetConnectionString();
using var client = new AmazonDynamoDBClient(config);
var tableRequest = new CreateTableRequest();
tableRequest.TableName = tableName;
tableRequest.AttributeDefinitions = new List<AttributeDefinition> { new AttributeDefinition("Id", ScalarAttributeType.S) };
tableRequest.KeySchema = new List<KeySchemaElement> { new KeySchemaElement("Id", KeyType.HASH) };
tableRequest.ProvisionedThroughput = new ProvisionedThroughput(10, 5);
var putItemRequest = new PutItemRequest();
putItemRequest.TableName = tableName;
putItemRequest.Item = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { S = id } } };
var getItemRequest = new GetItemRequest();
getItemRequest.TableName = tableName;
getItemRequest.Key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { S = id } } };
// When
_ = await client.CreateTableAsync(tableRequest, TestContext.Current.CancellationToken)
.ConfigureAwait(true);
_ = await client.PutItemAsync(putItemRequest, TestContext.Current.CancellationToken)
.ConfigureAwait(true);
var itemResponse = await client.GetItemAsync(getItemRequest, TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// Then
Assert.Equal(id, itemResponse.Item.Values.Single().S);
}
[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
[Trait(AwsService, "s3")]
public async Task ListBucketsReturnsHttpStatusCodeOk()
{
// Given
var config = new AmazonS3Config();
config.ServiceURL = _localStackContainer.GetConnectionString();
using var client = new AmazonS3Client(config);
// When
var buckets = await client.ListBucketsAsync(TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// Then
Assert.Equal(HttpStatusCode.OK, buckets.HttpStatusCode);
}
[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
[Trait(AwsService, "sns")]
public async Task CreateTopicReturnsHttpStatusCodeOk()
{
// Given
var config = new AmazonSimpleNotificationServiceConfig();
config.ServiceURL = _localStackContainer.GetConnectionString();
using var client = new AmazonSimpleNotificationServiceClient(config);
// When
var topicResponse = await client.CreateTopicAsync(Guid.NewGuid().ToString("D"), TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// Then
Assert.Equal(HttpStatusCode.OK, topicResponse.HttpStatusCode);
}
[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
[Trait(AwsService, "sqs")]
public async Task CreateQueueReturnsHttpStatusCodeOk()
{
// Given
var config = new AmazonSQSConfig();
config.ServiceURL = _localStackContainer.GetConnectionString();
using var client = new AmazonSQSClient(config);
// When
var queueResponse = await client.CreateQueueAsync(Guid.NewGuid().ToString("D"), TestContext.Current.CancellationToken)
.ConfigureAwait(true);
// Then
Assert.Equal(HttpStatusCode.OK, queueResponse.HttpStatusCode);
}
protected virtual ValueTask DisposeAsyncCore()
{
return _localStackContainer.DisposeAsync();
}
[UsedImplicitly]
public sealed class LocalStackDefaultConfiguration : LocalStackContainerTest
{
public LocalStackDefaultConfiguration()
: base(new LocalStackBuilder(TestSession.GetImageFromDockerfile()).Build())
{
}
}
[UsedImplicitly]
public sealed class LocalStackV1Configuration : LocalStackContainerTest
{
public LocalStackV1Configuration()
: base(new LocalStackBuilder(TestSession.GetImageFromDockerfile(stage: "v1_4_0")).Build())
{
}
}
}