Skip to content

Commit

Permalink
consul-connect: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslansennov committed Nov 27, 2024
1 parent 79bb096 commit 5d548ce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.consul.ConsulContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import java.io.IOException;
Expand All @@ -23,10 +24,12 @@ public class ConsulInstance extends ConsulContainer {
private final ConsulDatacenter dc;

private JsonObject configuration = new JsonObject();
private final SemVer semVer;

private ConsulInstance(String image, String version, ConsulDatacenter dc) {
super(image + ":" + version);
super(DockerImageName.parse(image + ":" + version).asCompatibleSubstituteFor("consul"));
this.dc = dc;
this.semVer = new SemVer(version);
}

public static ConsulInstance.Builder builder() {
Expand All @@ -41,6 +44,10 @@ public static ConsulInstance.Builder defaultConsulBuilder(ConsulDatacenter dc) {
.caFile("server-cert-ca-chain.pem");
}

public SemVer semVer() {
return semVer;
}

@Override
public void stop() {
try {
Expand Down Expand Up @@ -115,8 +122,8 @@ private static boolean isMacOS() {

public static class Builder {
private static final String CONSUL_LOCAL_CONFIG_ENV = "CONSUL_LOCAL_CONFIG";
private static final String DEFAULT_IMAGE = "consul";
private static final String DEFAULT_VERSION = "1.13.7";
private static final String DEFAULT_IMAGE = "hashicorp/consul";
private static final String DEFAULT_VERSION = "1.18.1";
private static final int DNS_PORT = 8600;
private static final int HTTP_PORT = 8500;
private static final int HTTPS_PORT = 8501;
Expand Down Expand Up @@ -206,6 +213,9 @@ public ConsulInstance build() {
cfg.put("bind_addr", "0.0.0.0");

SemVer semVer = new SemVer(version);
if (semVer.compareTo(new SemVer("1.8.0")) >= 0) {
cfg.put("connect", new JsonObject().put("enabled", true));
}

if (semVer.compareTo(new SemVer("1.4.0")) >= 0) {
JsonObject master;
Expand Down Expand Up @@ -233,6 +243,10 @@ public ConsulInstance build() {
ports.put("server", RPC_PORT);
}

if (semVer.compareTo(new SemVer("1.14.0")) >= 0) {
ports.put("grpc_tls", 8503);
}

if (semVer.compareTo(new SemVer("0.8.0")) >= 0) {
cfg.put("acl_enforce_version_8", false);
}
Expand Down
29 changes: 23 additions & 6 deletions src/test/java/io/vertx/ext/consul/tests/suite/AclPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,40 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static io.vertx.ext.consul.tests.Utils.getAsync;

@RunWith(VertxUnitRunner.class)
public class AclPolicyTest extends ConsulTestBase {

private static final Set<String> buildIn;

static {
buildIn = new HashSet<>();
buildIn.add("global-management");
buildIn.add("builtin/global-read-only");
}

private List<AclPolicy> filter(List<AclPolicy> policies) {
return policies.stream()
.filter(aclPolicy -> !buildIn.contains(aclPolicy.getName()))
.collect(Collectors.toList());
}

@Test
public void lifecycle(TestContext tc) {
List<AclPolicy> policies = getAsync(() -> masterClient.getAclPolicies());
tc.assertEquals(3, policies.size());
List<AclPolicy> policies = filter(getAsync(() -> masterClient.getAclPolicies()));
tc.assertEquals(2, policies.size());
AclPolicy policy = new AclPolicy()
.setName("created_policy")
.setRules("key \"bar/\" { policy = \"read\" }");
String createdId = getAsync(() -> masterClient.createAclPolicy(policy));
List<AclPolicy> newPolicies = getAsync(() -> masterClient.getAclPolicies());
tc.assertEquals(4, newPolicies.size());
List<AclPolicy> newPolicies = filter(getAsync(() -> masterClient.getAclPolicies()));
tc.assertEquals(3, newPolicies.size());
AclPolicy read = getAsync(() -> masterClient.readPolicy(createdId));
tc.assertEquals(createdId, read.getId());
tc.assertEquals(policy.getRules(), read.getRules());
Expand All @@ -41,7 +58,7 @@ public void lifecycle(TestContext tc) {
tc.assertEquals(updateOptions.getRules(), readByName.getRules());
Boolean isDeleted = getAsync(() -> masterClient.deletePolicy(createdId));
tc.assertTrue(isDeleted);
List<AclPolicy> afterDeleting = getAsync(() -> masterClient.getAclPolicies());
tc.assertEquals(3, afterDeleting.size());
List<AclPolicy> afterDeleting = filter(getAsync(() -> masterClient.getAclPolicies()));
tc.assertEquals(2, afterDeleting.size());
}
}
39 changes: 39 additions & 0 deletions src/test/java/io/vertx/ext/consul/tests/suite/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.ext.consul.*;
import io.vertx.ext.consul.connect.ConnectOptions;
import io.vertx.ext.consul.connect.ProxyOptions;
import io.vertx.ext.consul.connect.SidecarServiceOptions;
import io.vertx.ext.consul.connect.UpstreamOptions;
import io.vertx.ext.consul.tests.Utils;
import io.vertx.ext.consul.tests.instance.SemVer;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import org.junit.Test;
Expand All @@ -33,6 +38,7 @@
import static io.vertx.ext.consul.tests.RandomObjects.randomServiceOptions;
import static io.vertx.ext.consul.tests.Utils.*;
import static io.vertx.test.core.TestUtils.randomAlphaString;
import static io.vertx.test.core.TestUtils.randomPortInt;

/**
* @author <a href="mailto:ruslan.sennov@gmail.com">Ruslan Sennov</a>
Expand All @@ -54,6 +60,39 @@ private static String checkService(TestContext tc, List<Service> list, String na
return serviceId;
}

@Test
public void connect(TestContext tc) {
if (consul.semVer().compareTo(new SemVer("1.8.0")) < 0) {
return;
}
Async async = tc.async();
String serviceName = randomAlphaString(10);
ConnectOptions connectOpts = new ConnectOptions()
.setSidecarService(new SidecarServiceOptions()
.setPort(randomPortInt())
.setProxy(new ProxyOptions()
.setUpstreams(Collections.singletonList(
new UpstreamOptions()
.setDestinationName("dest_name")
.setLocalBindPort(3333)
))));
ServiceOptions opts = randomServiceOptions()
.setName(serviceName)
.setId(null)
.setConnectOptions(connectOpts);
writeClient.registerService(opts).onComplete(tc.asyncAssertSuccess(reg -> {
writeClient.localServices().onComplete(tc.asyncAssertSuccess(services -> {
tc.assertEquals(services.size(), 2); // service and sidecar
tc.assertEquals(services.stream().filter(s -> s.getName().startsWith(serviceName)).count(), 2L);
tc.assertEquals(services.stream().filter(s -> s.getName().endsWith("-sidecar-proxy")).count(), 1L);
writeClient.deregisterService(serviceName).onComplete(tc.asyncAssertSuccess(deregister -> {
async.countDown();
}));
}));
}));
async.await();
}

@Test
public void createLocalService(TestContext tc) {
String serviceName = randomAlphaString(10);
Expand Down

0 comments on commit 5d548ce

Please sign in to comment.