Skip to content

Commit

Permalink
Few little improvements to testing (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper authored Nov 20, 2024
1 parent 85292d4 commit 351aa86
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@SpringBootTest(
classes = Greeter.class,
properties = {"greetingPrefix=Something something "})
@RestateTest(restateContainerImage = "ghcr.io/restatedev/restate:main")
@RestateTest(containerImage = "ghcr.io/restatedev/restate:main")
public class SdkTestingIntegrationTest {

@Autowired @BindService private Greeter greeter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,29 @@ public class ManualRestateRunner
this.runtimeContainer
// We expose these ports only to enable port checks
.withExposedPorts(RESTATE_INGRESS_ENDPOINT_PORT, RESTATE_ADMIN_ENDPOINT_PORT)
// Let's have a high logging level by default to avoid spamming too much, it can be
// overriden by the user
.withEnv("RUST_LOG", "warn")
.withEnv(additionalEnv)
// These envs should not be overriden by additionalEnv
.withEnv("RESTATE_META__REST_ADDRESS", "0.0.0.0:" + RESTATE_ADMIN_ENDPOINT_PORT)
.withEnv(
"RESTATE_WORKER__INGRESS__BIND_ADDRESS", "0.0.0.0:" + RESTATE_INGRESS_ENDPOINT_PORT)
.withNetworkAliases(RESTATE_RUNTIME)
// Configure wait strategy on health paths
.setWaitStrategy(
.waitingFor(
new WaitAllStrategy()
.withStrategy(Wait.forHttp("/health").forPort(RESTATE_ADMIN_ENDPOINT_PORT))
.withStrategy(
Wait.forHttp("/restate/health").forPort(RESTATE_INGRESS_ENDPOINT_PORT)));
Wait.forHttp("/restate/health").forPort(RESTATE_INGRESS_ENDPOINT_PORT)))
.withLogConsumer(
outputFrame -> {
switch (outputFrame.getType()) {
case STDOUT, STDERR ->
LOG.debug("[restate] {}", outputFrame.getUtf8StringWithoutLineEnding());
case END -> LOG.debug("[restate] END");
}
});

if (configFile != null) {
this.runtimeContainer.withCopyToContainer(Transferable.of(configFile), "/config.yaml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package dev.restate.sdk.testing;

import java.util.List;
import java.util.regex.Pattern;
import org.junit.jupiter.api.extension.*;
import org.junit.platform.commons.support.AnnotationSupport;

Expand Down Expand Up @@ -74,7 +75,19 @@ private RestateRunner initializeRestateRunner(ExtensionContext extensionContext)
// Build runner discovering services to bind
var runnerBuilder = RestateRunnerBuilder.create();
servicesToBind.forEach(runnerBuilder::bind);
runnerBuilder.withRestateContainerImage(testAnnotation.restateContainerImage());
runnerBuilder.withRestateContainerImage(testAnnotation.containerImage());
if (testAnnotation.environment() != null) {
for (String env : testAnnotation.environment()) {
String[] splitEnv = env.split(Pattern.quote("="), 2);
if (splitEnv.length != 2) {
throw new IllegalStateException(
"Environment variables should be passed in the form of NAME=VALUE. Found an invalid env: '"
+ env
+ "'");
}
runnerBuilder.withAdditionalEnv(splitEnv[0], splitEnv[1]);
}
}
return runnerBuilder.buildRunner();
}
}
14 changes: 11 additions & 3 deletions sdk-testing/src/main/java/dev/restate/sdk/testing/RestateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
* href="https://java.testcontainers.org/">Testcontainers</a>, and register the services.
*
* <p>This extension is scoped per test class, meaning that the restate runner will be shared among
* test methods. Because of the aforementioned issue, the extension sets the {@link TestInstance}
* {@link TestInstance.Lifecycle#PER_CLASS} automatically.
* test methods. Because of this behaviour, the extension sets the {@link TestInstance} as {@link
* TestInstance.Lifecycle#PER_CLASS} automatically.
*
* <p>Use the annotations {@link RestateClient}, {@link RestateURL} and {@link RestateAdminClient}
* to interact with the deployed environment:
Expand All @@ -66,6 +66,14 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public @interface RestateTest {

/**
* Environment variables in form {@literal key=value} that should be added to the deployed Restate
* container.
*
* @return the environment variables to add
*/
String[] environment() default {};

/** Restate container image to use */
String restateContainerImage() default "docker.io/restatedev/restate:latest";
String containerImage() default "docker.io/restatedev/restate:latest";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

@RestateTest(restateContainerImage = "ghcr.io/restatedev/restate:main")
@RestateTest(containerImage = "ghcr.io/restatedev/restate:main")
class CounterTest {

@BindService private final Counter counter = new Counter();
Expand Down

0 comments on commit 351aa86

Please sign in to comment.