28
28
import com .google .common .truth .Expect ;
29
29
import com .google .gson .Gson ;
30
30
import com .google .gson .JsonObject ;
31
+ import com .google .gson .JsonParser ;
32
+ import com .google .gson .stream .JsonReader ;
31
33
import io .cloudevents .CloudEvent ;
32
34
import io .cloudevents .core .builder .CloudEventBuilder ;
33
35
import io .cloudevents .core .format .EventFormat ;
39
41
import java .io .IOException ;
40
42
import java .io .InputStream ;
41
43
import java .io .InputStreamReader ;
44
+ import java .io .StringReader ;
42
45
import java .io .UncheckedIOException ;
43
46
import java .net .ServerSocket ;
44
47
import java .net .URI ;
@@ -89,6 +92,8 @@ public class IntegrationTest {
89
92
@ Rule public final TestName testName = new TestName ();
90
93
91
94
private static final String SERVER_READY_STRING = "Started ServerConnector" ;
95
+ private static final String EXECUTION_ID_HTTP_HEADER = "HTTP_FUNCTION_EXECUTION_ID" ;
96
+ private static final String EXECUTION_ID = "1234abcd" ;
92
97
93
98
private static final ExecutorService EXECUTOR = Executors .newCachedThreadPool ();
94
99
@@ -286,14 +291,18 @@ public void exceptionHttp() throws Exception {
286
291
String exceptionExpectedOutput =
287
292
"\" severity\" : \" ERROR\" , \" logging.googleapis.com/sourceLocation\" : {\" file\" :"
288
293
+ " \" com/google/cloud/functions/invoker/HttpFunctionExecutor.java\" , \" method\" :"
289
- + " \" service\" }, \" message\" : \" Failed to execute"
294
+ + " \" service\" }, \" execution_id\" : \" "
295
+ + EXECUTION_ID
296
+ + "\" ,"
297
+ + " \" message\" : \" Failed to execute"
290
298
+ " com.google.cloud.functions.invoker.testfunctions.ExceptionHttp\\ n"
291
299
+ "java.lang.RuntimeException: exception thrown for test" ;
292
300
testHttpFunction (
293
301
fullTarget ("ExceptionHttp" ),
294
302
ImmutableList .of (
295
303
TestCase .builder ()
296
304
.setExpectedResponseCode (500 )
305
+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
297
306
.setExpectedOutput (exceptionExpectedOutput )
298
307
.build ()));
299
308
}
@@ -303,7 +312,10 @@ public void exceptionBackground() throws Exception {
303
312
String exceptionExpectedOutput =
304
313
"\" severity\" : \" ERROR\" , \" logging.googleapis.com/sourceLocation\" : {\" file\" :"
305
314
+ " \" com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java\" , \" method\" :"
306
- + " \" service\" }, \" message\" : \" Failed to execute"
315
+ + " \" service\" }, \" execution_id\" : \" "
316
+ + EXECUTION_ID
317
+ + "\" , "
318
+ + "\" message\" : \" Failed to execute"
307
319
+ " com.google.cloud.functions.invoker.testfunctions.ExceptionBackground\\ n"
308
320
+ "java.lang.RuntimeException: exception thrown for test" ;
309
321
@@ -317,6 +329,7 @@ public void exceptionBackground() throws Exception {
317
329
ImmutableList .of (
318
330
TestCase .builder ()
319
331
.setRequestText (gcfRequestText )
332
+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
320
333
.setExpectedResponseCode (500 )
321
334
.setExpectedOutput (exceptionExpectedOutput )
322
335
.build ()),
@@ -359,25 +372,37 @@ public void stackDriverLogging() throws Exception {
359
372
+ "\" logging.googleapis.com/sourceLocation\" : "
360
373
+ "{\" file\" : \" com/google/cloud/functions/invoker/testfunctions/Log.java\" ,"
361
374
+ " \" method\" : \" service\" },"
375
+ + " \" execution_id\" : \" "
376
+ + EXECUTION_ID
377
+ + "\" ,"
362
378
+ " \" message\" : \" blim\" }" ;
363
379
TestCase simpleTestCase =
364
- TestCase .builder ().setUrl ("/?message=blim" ).setExpectedOutput (simpleExpectedOutput ).build ();
380
+ TestCase .builder ()
381
+ .setUrl ("/?message=blim" )
382
+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
383
+ .setExpectedOutput (simpleExpectedOutput )
384
+ .build ();
365
385
String quotingExpectedOutput = "\" message\" : \" foo\\ nbar\\ \" " ;
366
386
TestCase quotingTestCase =
367
387
TestCase .builder ()
368
388
.setUrl ("/?message=" + URLEncoder .encode ("foo\n bar\" " , "UTF-8" ))
389
+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
369
390
.setExpectedOutput (quotingExpectedOutput )
370
391
.build ();
371
392
String exceptionExpectedOutput =
372
393
"{\" severity\" : \" ERROR\" , "
373
394
+ "\" logging.googleapis.com/sourceLocation\" : "
374
395
+ "{\" file\" : \" com/google/cloud/functions/invoker/testfunctions/Log.java\" , "
375
396
+ "\" method\" : \" service\" }, "
397
+ + "\" execution_id\" : \" "
398
+ + EXECUTION_ID
399
+ + "\" , "
376
400
+ "\" message\" : \" oops\\ njava.lang.Exception: disaster\\ n"
377
401
+ " at com.google.cloud.functions.invoker.testfunctions.Log.service(Log.java:" ;
378
402
TestCase exceptionTestCase =
379
403
TestCase .builder ()
380
404
.setUrl ("/?message=oops&level=severe&exception=disaster" )
405
+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
381
406
.setExpectedOutput (exceptionExpectedOutput )
382
407
.build ();
383
408
testHttpFunction (
@@ -753,7 +778,11 @@ private void testFunction(
753
778
for (TestCase testCase : testCases ) {
754
779
testCase
755
780
.expectedOutput ()
756
- .ifPresent (output -> expect .that (serverProcess .output ()).contains (output ));
781
+ .ifPresent (
782
+ (output ) -> {
783
+ expect .that (serverProcess .output ()).contains (output );
784
+ parseLogJson (serverProcess .output ());
785
+ });
757
786
}
758
787
// Wait for the output monitor task to terminate. If it threw an exception, we will get an
759
788
// ExecutionException here.
@@ -842,7 +871,9 @@ private ServerProcess startServer(
842
871
"FUNCTION_SIGNATURE_TYPE" ,
843
872
signatureType .toString (),
844
873
"FUNCTION_TARGET" ,
845
- target );
874
+ target ,
875
+ "LOG_EXECUTION_ID" ,
876
+ "true" );
846
877
processBuilder .environment ().putAll (environment );
847
878
processBuilder .environment ().putAll (environmentVariables );
848
879
Process serverProcess = processBuilder .start ();
@@ -879,4 +910,12 @@ private void monitorOutput(
879
910
throw new UncheckedIOException (e );
880
911
}
881
912
}
913
+
914
+ // Attempt to parse Json object, throws on parse failure
915
+ private void parseLogJson (String json ) throws RuntimeException {
916
+ System .out .println ("trying to parse the following object " );
917
+ System .out .println (json );
918
+ JsonReader reader = new JsonReader (new StringReader (json ));
919
+ JsonParser .parseReader (reader );
920
+ }
882
921
}
0 commit comments