Skip to content

Commit 5c1997c

Browse files
committed
Add a simple test for the Invoker main class.
This currently just command-line arguments and environment variables. PiperOrigin-RevId: 293497402
1 parent 394d65b commit 5c1997c

File tree

2 files changed

+131
-5
lines changed
  • invoker/core/src

2 files changed

+131
-5
lines changed

invoker/core/src/main/java/com/google/cloud/functions/invoker/runner/Invoker.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.nio.file.Path;
3636
import java.nio.file.Paths;
3737
import java.util.Arrays;
38+
import java.util.Map;
3839
import java.util.Objects;
3940
import java.util.Optional;
4041
import java.util.logging.Level;
@@ -105,6 +106,17 @@ private static class Options {
105106
}
106107

107108
public static void main(String[] args) throws Exception {
109+
Optional<Invoker> invoker = makeInvoker(args);
110+
if (invoker.isPresent()) {
111+
invoker.get().startServer();
112+
}
113+
}
114+
115+
static Optional<Invoker> makeInvoker(String... args) {
116+
return makeInvoker(System.getenv(), args);
117+
}
118+
119+
static Optional<Invoker> makeInvoker(Map<String, String> environment, String... args) {
108120
Options options = new Options();
109121
JCommander jCommander = JCommander.newBuilder()
110122
.addObject(options)
@@ -118,7 +130,7 @@ public static void main(String[] args) throws Exception {
118130

119131
if (options.help) {
120132
jCommander.usage();
121-
return;
133+
return Optional.empty();
122134
}
123135

124136
int port;
@@ -134,7 +146,7 @@ public static void main(String[] args) throws Exception {
134146
Optional<String> functionJarPath =
135147
Arrays.asList(
136148
options.jar,
137-
System.getenv("FUNCTION_JAR"),
149+
environment.get("FUNCTION_JAR"),
138150
Files.exists(standardFunctionJarPath) ? standardFunctionJarPath.toString() : null)
139151
.stream()
140152
.filter(Objects::nonNull)
@@ -143,10 +155,9 @@ public static void main(String[] args) throws Exception {
143155
new Invoker(
144156
port,
145157
functionTarget,
146-
// TODO: remove once function signature type is inferred from the method signature.
147-
System.getenv("FUNCTION_SIGNATURE_TYPE"),
158+
environment.get("FUNCTION_SIGNATURE_TYPE"),
148159
functionJarPath);
149-
invoker.startServer();
160+
return Optional.of(invoker);
150161
}
151162

152163
private static boolean isLocalRun() {
@@ -169,6 +180,22 @@ public Invoker(
169180
this.functionJarPath = functionJarPath;
170181
}
171182

183+
Integer getPort() {
184+
return port;
185+
}
186+
187+
String getFunctionTarget() {
188+
return functionTarget;
189+
}
190+
191+
String getFunctionSignatureType() {
192+
return functionSignatureType;
193+
}
194+
195+
Optional<String> getFunctionJarPath() {
196+
return functionJarPath;
197+
}
198+
172199
public void startServer() throws Exception {
173200
Server server = new Server(port);
174201

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.google.cloud.functions.invoker.runner;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static com.google.common.truth.Truth8.assertThat;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.io.PrintStream;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Collections;
11+
import java.util.Map;
12+
import java.util.Optional;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.junit.runners.JUnit4;
16+
17+
@RunWith(JUnit4.class)
18+
public class InvokerTest {
19+
@Test
20+
public void help() throws IOException {
21+
String help = captureOutput(() -> {
22+
Optional<Invoker> invoker = Invoker.makeInvoker("--help");
23+
assertThat(invoker).isEmpty();
24+
});
25+
assertThat(help).contains("Usage:");
26+
assertThat(help).contains("--target");
27+
}
28+
29+
@Test
30+
public void defaultPort() {
31+
Optional<Invoker> invoker = Invoker.makeInvoker();
32+
assertThat(invoker.get().getPort()).isEqualTo(8080);
33+
}
34+
35+
@Test
36+
public void explicitPort() {
37+
Optional<Invoker> invoker = Invoker.makeInvoker("--port", "1234");
38+
assertThat(invoker.get().getPort()).isEqualTo(1234);
39+
}
40+
41+
@Test
42+
public void defaultTarget() {
43+
Optional<Invoker> invoker = Invoker.makeInvoker();
44+
assertThat(invoker.get().getFunctionTarget()).isEqualTo("TestFunction.function");
45+
}
46+
47+
@Test
48+
public void explicitTarget() {
49+
Optional<Invoker> invoker = Invoker.makeInvoker("--target", "com.example.MyFunction");
50+
assertThat(invoker.get().getFunctionTarget()).isEqualTo("com.example.MyFunction");
51+
}
52+
53+
@Test
54+
public void defaultSignatureType() {
55+
Optional<Invoker> invoker = Invoker.makeInvoker();
56+
assertThat(invoker.get().getFunctionSignatureType()).isNull();
57+
}
58+
59+
@Test
60+
public void explicitSignatureType() {
61+
Map<String, String> env = Collections.singletonMap("FUNCTION_SIGNATURE_TYPE", "http");
62+
Optional<Invoker> invoker = Invoker.makeInvoker(env);
63+
assertThat(invoker.get().getFunctionSignatureType()).isEqualTo("http");
64+
}
65+
66+
@Test
67+
public void defaultJar() {
68+
Optional<Invoker> invoker = Invoker.makeInvoker();
69+
assertThat(invoker.get().getFunctionJarPath()).isEmpty();
70+
}
71+
72+
@Test
73+
public void explicitJarViaEnvironment() {
74+
Map<String, String> env = Collections.singletonMap("FUNCTION_JAR", "/foo/bar/baz.jar");
75+
Optional<Invoker> invoker = Invoker.makeInvoker(env);
76+
assertThat(invoker.get().getFunctionJarPath()).hasValue("/foo/bar/baz.jar");
77+
}
78+
79+
@Test
80+
public void explicitJarViaOption() {
81+
Optional<Invoker> invoker = Invoker.makeInvoker("--jar", "/foo/bar/baz.jar");
82+
assertThat(invoker.get().getFunctionJarPath()).hasValue("/foo/bar/baz.jar");
83+
}
84+
85+
private static String captureOutput(Runnable operation) throws IOException {
86+
PrintStream originalOut = System.out;
87+
PrintStream originalErr = System.err;
88+
ByteArrayOutputStream byteCapture = new ByteArrayOutputStream();
89+
try (PrintStream capture = new PrintStream(byteCapture)) {
90+
System.setOut(capture);
91+
System.setErr(capture);
92+
operation.run();
93+
} finally {
94+
System.setOut(originalOut);
95+
System.setErr(originalErr);
96+
}
97+
return new String(byteCapture.toByteArray(), StandardCharsets.UTF_8);
98+
}
99+
}

0 commit comments

Comments
 (0)