-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathLambdaLoggingAspectTest.java
181 lines (145 loc) · 7.01 KB
/
LambdaLoggingAspectTest.java
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
179
180
181
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.logging.internal;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.ThreadContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import software.amazon.lambda.handlers.PowerLogToolEnabled;
import software.amazon.lambda.handlers.PowerLogToolEnabledForStream;
import software.amazon.lambda.handlers.PowerToolDisabled;
import software.amazon.lambda.handlers.PowerToolDisabledForStream;
import software.amazon.lambda.handlers.PowerToolLogEventEnabled;
import software.amazon.lambda.handlers.PowerToolLogEventEnabledForStream;
import software.amazon.lambda.internal.LambdaHandlerProcessor;
import static org.apache.commons.lang3.reflect.FieldUtils.writeStaticField;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
class LambdaLoggingAspectTest {
private static final int EXPECTED_CONTEXT_SIZE = 6;
private RequestStreamHandler requestStreamHandler;
private RequestHandler<Object, Object> requestHandler;
@Mock
private Context context;
@BeforeEach
void setUp() throws IllegalAccessException {
initMocks(this);
ThreadContext.clearAll();
writeStaticField(LambdaHandlerProcessor.class, "IS_COLD_START", null, true);
setupContext();
requestHandler = new PowerLogToolEnabled();
requestStreamHandler = new PowerLogToolEnabledForStream();
}
@Test
void shouldSetLambdaContextWhenEnabled() {
requestHandler.handleRequest(new Object(), context);
assertThat(ThreadContext.getImmutableContext())
.hasSize(EXPECTED_CONTEXT_SIZE)
.containsEntry(DefaultLambdaFields.FUNCTION_ARN.getName(), "testArn")
.containsEntry(DefaultLambdaFields.FUNCTION_MEMORY_SIZE.getName(), "10")
.containsEntry(DefaultLambdaFields.FUNCTION_VERSION.getName(), "1")
.containsEntry(DefaultLambdaFields.FUNCTION_NAME.getName(), "testFunction")
.containsKey("coldStart")
.containsKey("service");
}
@Test
void shouldSetLambdaContextForStreamHandlerWhenEnabled() throws IOException {
requestStreamHandler = new PowerLogToolEnabledForStream();
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context);
assertThat(ThreadContext.getImmutableContext())
.hasSize(EXPECTED_CONTEXT_SIZE)
.containsEntry(DefaultLambdaFields.FUNCTION_ARN.getName(), "testArn")
.containsEntry(DefaultLambdaFields.FUNCTION_MEMORY_SIZE.getName(), "10")
.containsEntry(DefaultLambdaFields.FUNCTION_VERSION.getName(), "1")
.containsEntry(DefaultLambdaFields.FUNCTION_NAME.getName(), "testFunction")
.containsKey("coldStart")
.containsKey("service");
}
@Test
void shouldSetColdStartFlag() throws IOException {
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context);
assertThat(ThreadContext.getImmutableContext())
.hasSize(6)
.containsEntry("coldStart", "true");
requestStreamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context);
assertThat(ThreadContext.getImmutableContext())
.hasSize(EXPECTED_CONTEXT_SIZE)
.containsEntry("coldStart", "false");
}
@Test
void shouldNotSetLambdaContextWhenDisabled() {
requestHandler = new PowerToolDisabled();
requestHandler.handleRequest(new Object(), context);
assertThat(ThreadContext.getImmutableContext())
.isEmpty();
}
@Test
void shouldNotSetLambdaContextForStreamHandlerWhenDisabled() throws IOException {
requestStreamHandler = new PowerToolDisabledForStream();
requestStreamHandler.handleRequest(null, null, context);
assertThat(ThreadContext.getImmutableContext())
.isEmpty();
}
@Test
void shouldHaveNoEffectIfNotUsedOnLambdaHandler() {
PowerLogToolEnabled handler = new PowerLogToolEnabled();
handler.anotherMethod();
assertThat(ThreadContext.getImmutableContext())
.isEmpty();
}
@Test
void shouldLogEventForHandler() {
requestHandler = new PowerToolLogEventEnabled();
requestHandler.handleRequest(new Object(), context);
assertThat(ThreadContext.getImmutableContext())
.hasSize(EXPECTED_CONTEXT_SIZE);
}
@Test
void shouldLogEventForStreamAndLambdaStreamIsValid() throws IOException {
requestStreamHandler = new PowerToolLogEventEnabledForStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
Map<String, String> testPayload = new HashMap<>();
testPayload.put("test", "payload");
requestStreamHandler.handleRequest(new ByteArrayInputStream(new ObjectMapper().writeValueAsBytes(testPayload)), output, context);
assertThat(new String(output.toByteArray(), StandardCharsets.UTF_8))
.isEqualTo("{\"test\":\"payload\"}");
assertThat(ThreadContext.getImmutableContext())
.hasSize(EXPECTED_CONTEXT_SIZE);
}
@Test
void shouldLogServiceNameWhenEnvVarSet() throws IllegalAccessException {
writeStaticField(LambdaHandlerProcessor.class, "SERVICE_NAME", "testService", true);
requestHandler.handleRequest(new Object(), context);
assertThat(ThreadContext.getImmutableContext())
.hasSize(EXPECTED_CONTEXT_SIZE)
.containsEntry("service", "testService");
}
private void setupContext() {
when(context.getFunctionName()).thenReturn("testFunction");
when(context.getInvokedFunctionArn()).thenReturn("testArn");
when(context.getFunctionVersion()).thenReturn("1");
when(context.getMemoryLimitInMB()).thenReturn(10);
}
}