1
1
using System . Text . Json ;
2
+ using Amazon . CDK . AWS . CodeDeploy ;
2
3
using Amazon . CloudWatch ;
3
4
using Amazon . CloudWatch . Model ;
4
5
using Amazon . Lambda ;
7
8
using Amazon . Lambda . Model ;
8
9
using TestUtils ;
9
10
using Xunit . Abstractions ;
11
+ using Environment = Amazon . Lambda . Model . Environment ;
10
12
11
13
namespace Function . Tests ;
12
14
@@ -15,7 +17,7 @@ public class FunctionTests
15
17
{
16
18
private readonly ITestOutputHelper _testOutputHelper ;
17
19
private readonly AmazonLambdaClient _lambdaClient ;
18
- private string _functionName ;
20
+ private string ? _functionName ;
19
21
20
22
public FunctionTests ( ITestOutputHelper testOutputHelper )
21
23
{
@@ -29,7 +31,9 @@ public FunctionTests(ITestOutputHelper testOutputHelper)
29
31
[ InlineData ( "E2ETestLambda_ARM_AOT_NET8_metrics" ) ]
30
32
public async Task AotFunctionTest ( string functionName )
31
33
{
32
- await TestFunction ( functionName ) ;
34
+ _functionName = functionName ;
35
+ await ForceColdStart ( ) ;
36
+ await TestFunction ( ) ;
33
37
}
34
38
35
39
[ Theory ]
@@ -39,15 +43,16 @@ public async Task AotFunctionTest(string functionName)
39
43
[ InlineData ( "E2ETestLambda_ARM_NET8_metrics" ) ]
40
44
public async Task FunctionTest ( string functionName )
41
45
{
42
- await TestFunction ( functionName ) ;
46
+ _functionName = functionName ;
47
+ await ForceColdStart ( ) ;
48
+ await TestFunction ( ) ;
43
49
}
44
50
45
- internal async Task TestFunction ( string functionName )
51
+ internal async Task TestFunction ( )
46
52
{
47
- _functionName = functionName ;
48
53
var request = new InvokeRequest
49
54
{
50
- FunctionName = functionName ,
55
+ FunctionName = _functionName ,
51
56
InvocationType = InvocationType . RequestResponse ,
52
57
Payload = await File . ReadAllTextAsync ( "../../../../../../../../payload.json" ) ,
53
58
LogType = LogType . Tail
@@ -86,7 +91,7 @@ private void ValidateResponse(InvokeResponse response, bool isColdStart)
86
91
// Assert Output log from Lambda execution
87
92
AssertOutputLog ( response , isColdStart ) ;
88
93
}
89
-
94
+
90
95
private void AssertOutputLog ( InvokeResponse response , bool expectedColdStart )
91
96
{
92
97
var logResult = System . Text . Encoding . UTF8 . GetString ( Convert . FromBase64String ( response . LogResult ) ) ;
@@ -108,7 +113,7 @@ private void AssertOutputLog(InvokeResponse response, bool expectedColdStart)
108
113
AssertMetricsDimensionsMetadata ( output [ 1 ] ) ;
109
114
}
110
115
}
111
-
116
+
112
117
private async Task AssertCloudWatch ( )
113
118
{
114
119
using var cloudWatchClient = new AmazonCloudWatchClient ( ) ;
@@ -130,15 +135,35 @@ private async Task AssertCloudWatch()
130
135
}
131
136
]
132
137
} ;
133
-
138
+
134
139
var response = await cloudWatchClient . ListMetricsAsync ( request ) ;
135
140
141
+ Assert . Equal ( 7 , response . Metrics . Count ) ;
142
+
136
143
foreach ( var metric in response . Metrics )
137
144
{
138
- _testOutputHelper . WriteLine ( $ "Namespace: { metric . Namespace } , MetricName: { metric . MetricName } ") ;
139
- foreach ( var dimension in metric . Dimensions )
145
+ Assert . Equal ( "Test" , metric . Namespace ) ;
146
+
147
+ switch ( metric . MetricName )
140
148
{
141
- _testOutputHelper . WriteLine ( $ " Dimension: { dimension . Name } = { dimension . Value } ") ;
149
+ case "ColdStart" :
150
+ case "SingleMetric" :
151
+ Assert . Equal ( 2 , metric . Dimensions . Count ) ;
152
+ Assert . Contains ( metric . Dimensions , d => d . Name == "Service" && d . Value == "Test" ) ;
153
+ Assert . Contains ( metric . Dimensions , d => d . Name == "FunctionName" && d . Value == _functionName ) ;
154
+ break ;
155
+ case "Invocation" :
156
+ case "Memory with Environment dimension" :
157
+ case "Standard resolution" :
158
+ case "High resolution" :
159
+ case "Default resolution" :
160
+ Assert . Equal ( 5 , metric . Dimensions . Count ) ;
161
+ Assert . Contains ( metric . Dimensions , d => d . Name == "Service" && d . Value == "Test" ) ;
162
+ Assert . Contains ( metric . Dimensions , d => d . Name == "FunctionName" && d . Value == _functionName ) ;
163
+ Assert . Contains ( metric . Dimensions , d => d . Name == "Memory" && d . Value == "MemoryLimitInMB" ) ;
164
+ Assert . Contains ( metric . Dimensions , d => d . Name == "Environment" && d . Value == "Prod" ) ;
165
+ Assert . Contains ( metric . Dimensions , d => d . Name == "Another" && d . Value == "One" ) ;
166
+ break ;
142
167
}
143
168
}
144
169
}
@@ -189,7 +214,8 @@ private void AssertMetricsDimensionsMetadata(string output)
189
214
Assert . Equal ( "Service" , dimensionsElement [ 0 ] [ 0 ] . GetString ( ) ) ;
190
215
Assert . Equal ( "Environment" , dimensionsElement [ 0 ] [ 1 ] . GetString ( ) ) ;
191
216
Assert . Equal ( "Another" , dimensionsElement [ 0 ] [ 2 ] . GetString ( ) ) ;
192
- Assert . Equal ( "Memory" , dimensionsElement [ 0 ] [ 3 ] . GetString ( ) ) ;
217
+ Assert . Equal ( "FunctionName" , dimensionsElement [ 0 ] [ 3 ] . GetString ( ) ) ;
218
+ Assert . Equal ( "Memory" , dimensionsElement [ 0 ] [ 4 ] . GetString ( ) ) ;
193
219
194
220
Assert . True ( root . TryGetProperty ( "Service" , out JsonElement serviceElement ) ) ;
195
221
Assert . Equal ( "Test" , serviceElement . GetString ( ) ) ;
@@ -200,6 +226,9 @@ private void AssertMetricsDimensionsMetadata(string output)
200
226
Assert . True ( root . TryGetProperty ( "Another" , out JsonElement anotherElement ) ) ;
201
227
Assert . Equal ( "One" , anotherElement . GetString ( ) ) ;
202
228
229
+ Assert . True ( root . TryGetProperty ( "FunctionName" , out JsonElement functionNameElement ) ) ;
230
+ Assert . Equal ( _functionName , functionNameElement . GetString ( ) ) ;
231
+
203
232
Assert . True ( root . TryGetProperty ( "Memory" , out JsonElement memoryElement ) ) ;
204
233
Assert . Equal ( "MemoryLimitInMB" , memoryElement . GetString ( ) ) ;
205
234
@@ -271,4 +300,23 @@ private void AssertColdStart(string output)
271
300
Assert . True ( root . TryGetProperty ( "ColdStart" , out JsonElement coldStartElement ) ) ;
272
301
Assert . Equal ( 1 , coldStartElement . GetInt32 ( ) ) ;
273
302
}
303
+
304
+ private async Task ForceColdStart ( )
305
+ {
306
+ var updateRequest = new UpdateFunctionConfigurationRequest
307
+ {
308
+ FunctionName = _functionName ,
309
+ Environment = new Environment
310
+ {
311
+ Variables = new Dictionary < string , string >
312
+ {
313
+ { "ForceColdStart" , Guid . NewGuid ( ) . ToString ( ) }
314
+ }
315
+ }
316
+ } ;
317
+
318
+ _ = await _lambdaClient . UpdateFunctionConfigurationAsync ( updateRequest ) ;
319
+
320
+ await Task . Delay ( 2000 ) ;
321
+ }
274
322
}
0 commit comments