Skip to content

Commit

Permalink
updating configuration configs
Browse files Browse the repository at this point in the history
Signed-off-by: salaboy <Salaboy@gmail.com>
  • Loading branch information
salaboy committed Sep 27, 2024
1 parent def7c5d commit 570a311
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package io.dapr.testcontainers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
Expand All @@ -28,6 +30,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -38,14 +41,15 @@

public class DaprContainer extends GenericContainer<DaprContainer> {

private static final Logger log = LoggerFactory.getLogger(DaprContainer.class);
private static final int DAPRD_DEFAULT_HTTP_PORT = 3500;
private static final int DAPRD_DEFAULT_GRPC_PORT = 50001;
private static final WaitStrategy WAIT_STRATEGY = Wait.forHttp("/v1.0/healthz/outbound")
.forPort(DAPRD_DEFAULT_HTTP_PORT)
.forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399);

private final Set<Component> components = new HashSet<>();
private final Set<Configuration> configurations = new HashSet<>();
private Configuration configuration;
private final Set<Subscription> subscriptions = new HashSet<>();
private DaprProtocol protocol = DaprProtocol.HTTP;
private String appName;
Expand Down Expand Up @@ -87,8 +91,8 @@ public Set<Subscription> getSubscriptions() {
return subscriptions;
}

public Set<Configuration> getConfigurations() {
return configurations;
public Configuration getConfiguration() {
return configuration;
}

public DaprContainer withAppPort(Integer port) {
Expand Down Expand Up @@ -155,7 +159,7 @@ public DaprContainer withComponent(Path path) {
}

public DaprContainer withConfiguration(Configuration configuration) {
configurations.add(configuration);
this.configuration = configuration;
return this;
}

Expand Down Expand Up @@ -300,11 +304,21 @@ protected void configure() {
cmds.add(Integer.toString(appPort));
}

if (configuration != null) {
cmds.add("--config");
cmds.add("/dapr-resources/" + configuration.getName() + ".yaml");
}

cmds.add("--log-level");
cmds.add(daprLogLevel.toString());
cmds.add("-components-path");
cmds.add("--resources-path");
cmds.add("/dapr-resources");
withCommand(cmds.toArray(new String[]{}));

String[] cmdArray = cmds.toArray(new String[]{});
log.info("> `daprd` Command: \n");
log.info("\t" + Arrays.toString(cmdArray) + "\n");

withCommand(cmdArray);

if (components.isEmpty()) {
components.add(new Component("kvstore", "state.in-memory", "v1", Collections.emptyMap()));
Expand All @@ -320,7 +334,7 @@ protected void configure() {
withCopyToContainer(Transferable.of(componentYaml), "/dapr-resources/" + component.getName() + ".yaml");
}

for (Configuration configuration : configurations) {
if (configuration != null) {
String configurationYaml = configurationToYaml(configuration);
withCopyToContainer(Transferable.of(configurationYaml), "/dapr-resources/" + configuration.getName() + ".yaml");
}
Expand All @@ -333,19 +347,43 @@ protected void configure() {
dependsOn(placementContainer);
}

/**
* Get a Yaml representation of a Subscription.
* @param subscription A Dapr Subscription.
* @return String representing the Subscription in Yaml format
*/
public String subscriptionToYaml(Subscription subscription) {
Map<String, Object> subscriptionMap = subscriptionToMap(subscription);
return yaml.dumpAsMap(subscriptionMap);
String subscriptionYaml = yaml.dumpAsMap(subscriptionMap);
log.info("> Subscription YAML: \n");
log.info("\t\n" + subscriptionYaml + "\n");
return subscriptionYaml;
}

/**
* Get a Yaml representation of a Component.
* @param component A Dapr Subscription.
* @return String representing the Component in Yaml format
*/
public String componentToYaml(Component component) {
Map<String, Object> componentMap = componentToMap(component);
return yaml.dumpAsMap(componentMap);
String componentYaml = yaml.dumpAsMap(componentMap);
log.info("> Component YAML: \n");
log.info("\t\n" + componentYaml + "\n");
return componentYaml;
}

/**
* Get a Yaml representation of a Configuration.
* @param configuration A Dapr Subscription.
* @return String representing the Configuration in Yaml format
*/
public String configurationToYaml(Configuration configuration) {
Map<String, Object> configurationMap = configurationToMap(configuration);
return yaml.dumpAsMap(configurationMap);
String configurationYaml = yaml.dumpAsMap(configurationMap);
log.info("> Configuration YAML: \n");
log.info("\t\n" + configurationYaml + "\n");
return configurationYaml;
}

public String getAppName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -107,22 +108,19 @@ public void configurationSerializationTest() {
"localhost:4317", false, "grpc")))
.withAppChannelAddress("host.testcontainers.internal");

Set<Configuration> configurations = dapr.getConfigurations();
assertEquals(1, configurations.size());
Configuration configuration = dapr.getConfiguration();
assertNotNull(configuration);

String configurationYaml = dapr.configurationToYaml(configurations.iterator().next());
String configurationYaml = dapr.configurationToYaml(configuration);
String expectedConfigurationYaml = "metadata:\n" + " name: my-config\n"
+ "apiVersion: dapr.io/v1alpha1\n"
+ "kind: Configuration\n"
+ "spec:\n"
+ " tracing:\n"
// + " samplingRate: \"1\"\n"
+ " stdout: true\n"
+ " samplingRate: '1'\n"
+ " otel:\n"
// + " endpointAddress: \"localhost:4317\"\n"
+ " endpointAddress: localhost:4317\n"
// + " protocol: \"grpc\"\n"
+ " protocol: grpc\n"
+ " isSecure: false\n";
assertEquals(expectedConfigurationYaml, configurationYaml);
Expand Down

0 comments on commit 570a311

Please sign in to comment.