io.dapr.testcontainers.DaprContainer Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/
package io.dapr.testcontainers;
import io.dapr.testcontainers.converter.ComponentYamlConverter;
import io.dapr.testcontainers.converter.ConfigurationYamlConverter;
import io.dapr.testcontainers.converter.SubscriptionYamlConverter;
import io.dapr.testcontainers.converter.YamlConverter;
import io.dapr.testcontainers.converter.YamlMapperFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.DockerImageName;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DaprContainer extends GenericContainer {
private static final int DAPRD_DEFAULT_HTTP_PORT = 3500;
private static final int DAPRD_DEFAULT_GRPC_PORT = 50001;
private static final DaprProtocol DAPR_PROTOCOL = DaprProtocol.HTTP;
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("daprio/daprd");
private static final Yaml YAML_MAPPER = YamlMapperFactory.create();
private static final YamlConverter COMPONENT_CONVERTER = new ComponentYamlConverter(YAML_MAPPER);
private static final YamlConverter SUBSCRIPTION_CONVERTER = new SubscriptionYamlConverter(YAML_MAPPER);
private static final YamlConverter CONFIGURATION_CONVERTER = new ConfigurationYamlConverter(
YAML_MAPPER);
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 components = new HashSet<>();
private final Set subscriptions = new HashSet<>();
private DaprLogLevel daprLogLevel = DaprLogLevel.INFO;
private String appChannelAddress = "localhost";
private String placementService = "placement";
private String placementDockerImageName = "daprio/placement";
private Configuration configuration;
private DaprPlacementContainer placementContainer;
private String appName;
private Integer appPort;
private boolean shouldReusePlacement;
/**
* Creates a new Dapr container.
* @param dockerImageName Docker image name.
*/
public DaprContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
withAccessToHost(true);
withExposedPorts(DAPRD_DEFAULT_HTTP_PORT, DAPRD_DEFAULT_GRPC_PORT);
setWaitStrategy(WAIT_STRATEGY);
}
/**
* Creates a new Dapr container.
* @param image Docker image name.
*/
public DaprContainer(String image) {
this(DockerImageName.parse(image));
}
public Configuration getConfiguration() {
return configuration;
}
public Set getComponents() {
return components;
}
public Set getSubscriptions() {
return subscriptions;
}
public DaprContainer withAppPort(Integer port) {
this.appPort = port;
return this;
}
public DaprContainer withAppChannelAddress(String appChannelAddress) {
this.appChannelAddress = appChannelAddress;
return this;
}
public DaprContainer withConfiguration(Configuration configuration) {
this.configuration = configuration;
return this;
}
public DaprContainer withPlacementService(String placementService) {
this.placementService = placementService;
return this;
}
public DaprContainer withAppName(String appName) {
this.appName = appName;
return this;
}
public DaprContainer withDaprLogLevel(DaprLogLevel daprLogLevel) {
this.daprLogLevel = daprLogLevel;
return this;
}
public DaprContainer withSubscription(Subscription subscription) {
subscriptions.add(subscription);
return this;
}
public DaprContainer withPlacementImage(String placementDockerImageName) {
this.placementDockerImageName = placementDockerImageName;
return this;
}
public DaprContainer withReusablePlacement(boolean reuse) {
this.shouldReusePlacement = reuse;
return this;
}
public DaprContainer withPlacementContainer(DaprPlacementContainer placementContainer) {
this.placementContainer = placementContainer;
return this;
}
public DaprContainer withComponent(Component component) {
components.add(component);
return this;
}
/**
* Adds a Dapr component from a YAML file.
* @param path Path to the YAML file.
* @return This container.
*/
public DaprContainer withComponent(Path path) {
try {
Map component = this.YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);
String type = (String) component.get("type");
Map metadata = (Map) component.get("metadata");
String name = (String) metadata.get("name");
Map spec = (Map) component.get("spec");
String version = (String) spec.get("version");
List