From 0e8ba9236d3b56309645c46905e3801d56dd0273 Mon Sep 17 00:00:00 2001 From: Nisha Lad Date: Thu, 11 Apr 2019 12:09:15 +0100 Subject: [PATCH 1/5] WIP adding logframer and tests to fdk-java, before each function invocation --- .../fn/runtime/MethodFunctionInvoker.java | 23 +++++++++++++++++++ .../fn/testing/FnTestingRuleTest.java | 16 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java b/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java index 0f46d749..e0463d55 100644 --- a/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java +++ b/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java @@ -17,6 +17,27 @@ */ public class MethodFunctionInvoker implements FunctionInvoker { + /* + * If enabled, print the logging framing content + */ + public void logFramer(FunctionRuntimeContext rctx, InputEvent evt) { + String framer = rctx.getConfigurationByKey("FN_LOGFRAME_NAME").orElse(""); + + if (framer != "") { + String valueSrc = rctx.getConfigurationByKey("FN_LOGFRAME_HDR").orElse(""); + + if (valueSrc != "") { + String id = evt.getHeaders().get(valueSrc).orElse(""); + if (id != "") { + System.out.println(framer + "=" + id); + System.err.println(framer + "=" + id); + } + } + } + + } + + /** * Invoke the function wrapped by this loader * @@ -33,6 +54,8 @@ public Optional tryInvoke(InvocationContext ctx, InputEvent evt) th Object rawResult; + logFramer(runtimeContext, evt); + try { rawResult = method.getTargetMethod().invoke(ctx.getRuntimeContext().getInvokeInstance().orElse(null), userFunctionParams); } catch (IllegalAccessException | InvocationTargetException e) { diff --git a/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java b/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java index 03df8764..430e76c4 100644 --- a/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java +++ b/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java @@ -250,6 +250,22 @@ public void shouldHandleBodyAsInputStream() throws IOException { Assertions.assertThat(capturedBodies.get(0)).isEqualTo("FOO BAR".getBytes()); } + @Test + public void shouldPrintLogFrame() throws Exception { + fn.setConfig("FN_LOGFRAME_NAME", "containerID"); + fn.setConfig("FN_LOGFRAME_HDR", "fnID"); + fn.givenEvent().withBody("Hello World").enqueue(); + + fn.thenRun(TestFn.class, "copyConfiguration"); + Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID"); + Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID"); + + FnResult result = fn.getOnlyResult(); + Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World"); + // assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World"); + } + + // TODO move this to HTTP gateway // @Test // public void shouldLeaveQueryParamtersOffIfNotSpecified() { From 7cff542944c34ff49e59ad20e1211df0b7c0c971 Mon Sep 17 00:00:00 2001 From: Nisha Lad Date: Thu, 11 Apr 2019 15:08:07 +0100 Subject: [PATCH 2/5] WIP debugging test --- .../fn/runtime/EndToEndInvokeTest.java | 20 ++++++++++++++ .../fn/testing/FnTestingRuleTest.java | 27 ++++++++++--------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java b/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java index 4be57f20..4616c1c3 100644 --- a/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java +++ b/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java @@ -211,6 +211,26 @@ public void shouldReadBytesOnDefaultCodec() throws Exception { } + @Test + public void shouldPrintLogFrame() throws Exception { + fn.setConfig("FN_LOGFRAME_NAME", "containerID"); + fn.setConfig("FN_LOGFRAME_HDR", "fnID"); + fn.givenEvent().enqueue(); + TestFn.setOutput("Hello world"); + + fn.thenRun(TestFn.class, "setOutput"); + assertThat(fn.getOnlyOutputAsString()).isEqualTo("Hello world!"); + + // fn.getConfig("FN_LOGFRAME_NAME").isE + // fn.thenRun(TestFn.class, "echoInput"); + // assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID"); + // assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID"); + + // FnResult result = fn.getResults().get(0); + // Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World"); + // assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World"); + } + @Test public void shouldWriteBytesOnDefaultCodec() throws Exception { diff --git a/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java b/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java index 430e76c4..ff8deb3d 100644 --- a/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java +++ b/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java @@ -250,20 +250,21 @@ public void shouldHandleBodyAsInputStream() throws IOException { Assertions.assertThat(capturedBodies.get(0)).isEqualTo("FOO BAR".getBytes()); } - @Test - public void shouldPrintLogFrame() throws Exception { - fn.setConfig("FN_LOGFRAME_NAME", "containerID"); - fn.setConfig("FN_LOGFRAME_HDR", "fnID"); - fn.givenEvent().withBody("Hello World").enqueue(); - - fn.thenRun(TestFn.class, "copyConfiguration"); - Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID"); - Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID"); + // @Test + // public void shouldPrintLogFrame() throws Exception { + // fn.setConfig("FN_LOGFRAME_NAME", "containerID"); + // fn.setConfig("FN_LOGFRAME_HDR", "fnID"); + // fn.givenEvent().withBody("Hello World").enqueue(); + + // fn.thenRun(TestFn.class, "copyConfiguration"); + // // fn.thenRun(TestFn.class, "echoInput"); + // Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID"); + // Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID"); - FnResult result = fn.getOnlyResult(); - Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World"); - // assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World"); - } + // FnResult result = fn.getResults().get(0); + // Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World"); + // // assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World"); + // } // TODO move this to HTTP gateway From 5b58491f17c31dd5bc22b6ed1a3ada86720fb25c Mon Sep 17 00:00:00 2001 From: jan grant Date: Thu, 11 Apr 2019 15:30:48 +0100 Subject: [PATCH 3/5] Quick fix..? --- .../fn/runtime/MethodFunctionInvoker.java | 9 ++++----- .../fnproject/fn/runtime/EndToEndInvokeTest.java | 14 +++----------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java b/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java index e0463d55..197c9723 100644 --- a/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java +++ b/runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java @@ -22,10 +22,10 @@ public class MethodFunctionInvoker implements FunctionInvoker { */ public void logFramer(FunctionRuntimeContext rctx, InputEvent evt) { String framer = rctx.getConfigurationByKey("FN_LOGFRAME_NAME").orElse(""); - + if (framer != "") { String valueSrc = rctx.getConfigurationByKey("FN_LOGFRAME_HDR").orElse(""); - + if (valueSrc != "") { String id = evt.getHeaders().get(valueSrc).orElse(""); if (id != "") { @@ -34,10 +34,9 @@ public void logFramer(FunctionRuntimeContext rctx, InputEvent evt) { } } } - } - - + + /** * Invoke the function wrapped by this loader * diff --git a/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java b/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java index 4616c1c3..a1d1d457 100644 --- a/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java +++ b/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java @@ -215,20 +215,12 @@ public void shouldReadBytesOnDefaultCodec() throws Exception { public void shouldPrintLogFrame() throws Exception { fn.setConfig("FN_LOGFRAME_NAME", "containerID"); fn.setConfig("FN_LOGFRAME_HDR", "fnID"); - fn.givenEvent().enqueue(); - TestFn.setOutput("Hello world"); + fn.givenEvent().withHeader("fnID", "uhfieuwfhieuwh").withBody( "Hello world!").enqueue(); - fn.thenRun(TestFn.class, "setOutput"); + fn.thenRun(TestFn.class, "fnEcho"); assertThat(fn.getOnlyOutputAsString()).isEqualTo("Hello world!"); + assertThat(fn.getStdErrAsString()).isEqualTo(""); - // fn.getConfig("FN_LOGFRAME_NAME").isE - // fn.thenRun(TestFn.class, "echoInput"); - // assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID"); - // assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID"); - - // FnResult result = fn.getResults().get(0); - // Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World"); - // assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World"); } From 736813e96e4bed88963974496ec5c1d0fc0fea3e Mon Sep 17 00:00:00 2001 From: Nisha Lad Date: Thu, 11 Apr 2019 16:00:17 +0100 Subject: [PATCH 4/5] checking if env variables are logged out to stdout and stderr, stdout gets redirected to stderr hence the duplication in the assertion --- .../java/com/fnproject/fn/runtime/EndToEndInvokeTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java b/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java index a1d1d457..6f666ca2 100644 --- a/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java +++ b/runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java @@ -215,11 +215,12 @@ public void shouldReadBytesOnDefaultCodec() throws Exception { public void shouldPrintLogFrame() throws Exception { fn.setConfig("FN_LOGFRAME_NAME", "containerID"); fn.setConfig("FN_LOGFRAME_HDR", "fnID"); - fn.givenEvent().withHeader("fnID", "uhfieuwfhieuwh").withBody( "Hello world!").enqueue(); + fn.givenEvent().withHeader("fnID", "fnIDVal").withBody( "Hello world!").enqueue(); fn.thenRun(TestFn.class, "fnEcho"); assertThat(fn.getOnlyOutputAsString()).isEqualTo("Hello world!"); - assertThat(fn.getStdErrAsString()).isEqualTo(""); + // stdout gets redirected to stderr - hence printing out twice + assertThat(fn.getStdErrAsString()).isEqualTo("containerID=fnIDVal\ncontainerID=fnIDVal\n"); } From b5da35da39e76f79af3a90f3f98d0c9c3e24b906 Mon Sep 17 00:00:00 2001 From: Nisha Lad Date: Thu, 11 Apr 2019 16:05:04 +0100 Subject: [PATCH 5/5] removing comments of test in wrong file --- .../fnproject/fn/testing/FnTestingRuleTest.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java b/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java index ff8deb3d..03df8764 100644 --- a/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java +++ b/testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java @@ -250,23 +250,6 @@ public void shouldHandleBodyAsInputStream() throws IOException { Assertions.assertThat(capturedBodies.get(0)).isEqualTo("FOO BAR".getBytes()); } - // @Test - // public void shouldPrintLogFrame() throws Exception { - // fn.setConfig("FN_LOGFRAME_NAME", "containerID"); - // fn.setConfig("FN_LOGFRAME_HDR", "fnID"); - // fn.givenEvent().withBody("Hello World").enqueue(); - - // fn.thenRun(TestFn.class, "copyConfiguration"); - // // fn.thenRun(TestFn.class, "echoInput"); - // Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID"); - // Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID"); - - // FnResult result = fn.getResults().get(0); - // Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World"); - // // assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World"); - // } - - // TODO move this to HTTP gateway // @Test // public void shouldLeaveQueryParamtersOffIfNotSpecified() {