32
32
import org .mockito .Mockito ;
33
33
34
34
public class GCPResourceProviderTest {
35
-
35
+ private static final String DUMMY_PROJECT_ID = "123456789" ;
36
36
private final ConfigProperties mockConfigProps = Mockito .mock (ConfigProperties .class );
37
37
private final Map <String , String > mockGKECommonAttributes =
38
38
new HashMap <>() {
@@ -47,7 +47,6 @@ private DetectedPlatform generateMockGCEPlatform() {
47
47
Map <String , String > mockAttributes =
48
48
new HashMap <>() {
49
49
{
50
- put (GCE_PROJECT_ID , "test-project-id" );
51
50
put (GCE_CLOUD_REGION , "australia-southeast1" );
52
51
put (GCE_AVAILABILITY_ZONE , "australia-southeast1-b" );
53
52
put (GCE_INSTANCE_ID , "random-id" );
@@ -60,6 +59,7 @@ private DetectedPlatform generateMockGCEPlatform() {
60
59
Mockito .when (mockGCEPlatform .getSupportedPlatform ())
61
60
.thenReturn (GCPPlatformDetector .SupportedPlatform .GOOGLE_COMPUTE_ENGINE );
62
61
Mockito .when (mockGCEPlatform .getAttributes ()).thenReturn (mockAttributes );
62
+ Mockito .when (mockGCEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
63
63
return mockGCEPlatform ;
64
64
}
65
65
@@ -76,6 +76,7 @@ private DetectedPlatform generateMockGKEPlatform(String gkeClusterLocationType)
76
76
Mockito .when (mockGKEPlatform .getSupportedPlatform ())
77
77
.thenReturn (GCPPlatformDetector .SupportedPlatform .GOOGLE_KUBERNETES_ENGINE );
78
78
Mockito .when (mockGKEPlatform .getAttributes ()).thenReturn (mockAttributes );
79
+ Mockito .when (mockGKEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
79
80
return mockGKEPlatform ;
80
81
}
81
82
@@ -101,6 +102,7 @@ private DetectedPlatform generateMockServerlessPlatform(
101
102
DetectedPlatform mockServerlessPlatform = Mockito .mock (DetectedPlatform .class );
102
103
Mockito .when (mockServerlessPlatform .getSupportedPlatform ()).thenReturn (platform );
103
104
Mockito .when (mockServerlessPlatform .getAttributes ()).thenReturn (mockAttributes );
105
+ Mockito .when (mockServerlessPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
104
106
return mockServerlessPlatform ;
105
107
}
106
108
@@ -119,6 +121,7 @@ private DetectedPlatform generateMockGAEPlatform() {
119
121
Mockito .when (mockGAEPlatform .getSupportedPlatform ())
120
122
.thenReturn (GCPPlatformDetector .SupportedPlatform .GOOGLE_APP_ENGINE );
121
123
Mockito .when (mockGAEPlatform .getAttributes ()).thenReturn (mockAttributes );
124
+ Mockito .when (mockGAEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
122
125
return mockGAEPlatform ;
123
126
}
124
127
@@ -146,15 +149,15 @@ public void testGCEResourceAttributesMapping() {
146
149
147
150
Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
148
151
152
+ assertEquals (
153
+ mockPlatform .getProjectId (),
154
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
149
155
assertEquals (
150
156
ResourceAttributes .CloudPlatformValues .GCP_COMPUTE_ENGINE ,
151
157
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_PLATFORM ));
152
158
assertEquals (
153
159
ResourceAttributes .CloudProviderValues .GCP ,
154
160
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_PROVIDER ));
155
- assertEquals (
156
- mockPlatform .getAttributes ().get (GCE_PROJECT_ID ),
157
- gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
158
161
assertEquals (
159
162
mockPlatform .getAttributes ().get (GCE_INSTANCE_ID ),
160
163
gotResource .getAttributes ().get (ResourceAttributes .HOST_ID ));
@@ -188,11 +191,14 @@ public void testGKEResourceAttributesMapping_LocationTypeRegion() {
188
191
Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
189
192
190
193
verifyGKEMapping (gotResource , mockPlatform );
194
+ assertEquals (
195
+ mockPlatform .getProjectId (),
196
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
191
197
assertNull (gotResource .getAttributes ().get (ResourceAttributes .CLOUD_AVAILABILITY_ZONE ));
192
198
assertEquals (
193
199
mockPlatform .getAttributes ().get (GKE_CLUSTER_LOCATION ),
194
200
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_REGION ));
195
- assertEquals (5 , gotResource .getAttributes ().size ());
201
+ assertEquals (6 , gotResource .getAttributes ().size ());
196
202
}
197
203
198
204
@ Test
@@ -204,11 +210,14 @@ public void testGKEResourceAttributesMapping_LocationTypeZone() {
204
210
Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
205
211
206
212
verifyGKEMapping (gotResource , mockPlatform );
213
+ assertEquals (
214
+ mockPlatform .getProjectId (),
215
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
207
216
assertNull (gotResource .getAttributes ().get (ResourceAttributes .CLOUD_REGION ));
208
217
assertEquals (
209
218
mockPlatform .getAttributes ().get (GKE_CLUSTER_LOCATION ),
210
219
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_AVAILABILITY_ZONE ));
211
- assertEquals (5 , gotResource .getAttributes ().size ());
220
+ assertEquals (6 , gotResource .getAttributes ().size ());
212
221
}
213
222
214
223
@ Test
@@ -221,15 +230,19 @@ public void testGKEResourceAttributesMapping_LocationTypeInvalid() {
221
230
DetectedPlatform mockPlatform = Mockito .mock (DetectedPlatform .class );
222
231
Mockito .when (mockPlatform .getSupportedPlatform ())
223
232
.thenReturn (GCPPlatformDetector .SupportedPlatform .GOOGLE_KUBERNETES_ENGINE );
233
+ Mockito .when (mockPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
224
234
Mockito .when (mockPlatform .getAttributes ()).thenReturn (mockGKEAttributes );
225
235
Mockito .when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
226
236
227
237
Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
228
238
229
239
verifyGKEMapping (gotResource , mockPlatform );
240
+ assertEquals (
241
+ mockPlatform .getProjectId (),
242
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
230
243
assertNull (gotResource .getAttributes ().get (ResourceAttributes .CLOUD_REGION ));
231
244
assertNull (gotResource .getAttributes ().get (ResourceAttributes .CLOUD_AVAILABILITY_ZONE ));
232
- assertEquals (4 , gotResource .getAttributes ().size ());
245
+ assertEquals (5 , gotResource .getAttributes ().size ());
233
246
}
234
247
235
248
@ Test
@@ -241,9 +254,12 @@ public void testGKEResourceAttributesMapping_LocationMissing() {
241
254
Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
242
255
243
256
verifyGKEMapping (gotResource , mockPlatform );
257
+ assertEquals (
258
+ mockPlatform .getProjectId (),
259
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
244
260
assertNull (gotResource .getAttributes ().get (ResourceAttributes .CLOUD_REGION ));
245
261
assertNull (gotResource .getAttributes ().get (ResourceAttributes .CLOUD_AVAILABILITY_ZONE ));
246
- assertEquals (4 , gotResource .getAttributes ().size ());
262
+ assertEquals (5 , gotResource .getAttributes ().size ());
247
263
}
248
264
249
265
private void verifyGKEMapping (Resource gotResource , DetectedPlatform detectedPlatform ) {
@@ -272,8 +288,11 @@ public void testGCRResourceAttributesMapping() {
272
288
assertEquals (
273
289
ResourceAttributes .CloudPlatformValues .GCP_CLOUD_RUN ,
274
290
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_PLATFORM ));
291
+ assertEquals (
292
+ mockPlatform .getProjectId (),
293
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
275
294
verifyServerlessMapping (gotResource , mockPlatform );
276
- assertEquals (7 , gotResource .getAttributes ().size ());
295
+ assertEquals (8 , gotResource .getAttributes ().size ());
277
296
}
278
297
279
298
@ Test
@@ -288,8 +307,11 @@ public void testGCFResourceAttributeMapping() {
288
307
assertEquals (
289
308
ResourceAttributes .CloudPlatformValues .GCP_CLOUD_FUNCTIONS ,
290
309
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_PLATFORM ));
310
+ assertEquals (
311
+ mockPlatform .getProjectId (),
312
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
291
313
verifyServerlessMapping (gotResource , mockPlatform );
292
- assertEquals (7 , gotResource .getAttributes ().size ());
314
+ assertEquals (8 , gotResource .getAttributes ().size ());
293
315
}
294
316
295
317
private void verifyServerlessMapping (Resource gotResource , DetectedPlatform detectedPlatform ) {
@@ -326,6 +348,9 @@ public void testGAEResourceAttributeMapping() {
326
348
assertEquals (
327
349
ResourceAttributes .CloudProviderValues .GCP ,
328
350
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_PROVIDER ));
351
+ assertEquals (
352
+ mockPlatform .getProjectId (),
353
+ gotResource .getAttributes ().get (ResourceAttributes .CLOUD_ACCOUNT_ID ));
329
354
assertEquals (
330
355
mockPlatform .getAttributes ().get (GAE_MODULE_NAME ),
331
356
gotResource .getAttributes ().get (ResourceAttributes .FAAS_NAME ));
@@ -341,7 +366,7 @@ public void testGAEResourceAttributeMapping() {
341
366
assertEquals (
342
367
mockPlatform .getAttributes ().get (GAE_CLOUD_REGION ),
343
368
gotResource .getAttributes ().get (ResourceAttributes .CLOUD_REGION ));
344
- assertEquals (7 , gotResource .getAttributes ().size ());
369
+ assertEquals (8 , gotResource .getAttributes ().size ());
345
370
}
346
371
347
372
@ Test
0 commit comments