Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class ConsulPopulateCommandTest {

@Container
public static final ConsulContainer CONSUL = new ConsulContainer("hashicorp/consul:1.18.1");
public static final ConsulContainer CONSUL = new ConsulContainer("hashicorp/consul:1.21");

private final Vertx vertx = Vertx.vertx();

Expand Down Expand Up @@ -58,21 +58,27 @@ void should_dryRun() throws Exception {
properties.put("consul.files.root-path", getRootPath());
properties.put("dry-run", "true");

String[] args = properties.entrySet()
var args = properties.entrySet()
.stream()
.map(entry -> {
String key = entry.getKey();
var key = entry.getKey();
var value = entry.getValue();
return "--" + key + "=" + value;
})
.toArray(String[]::new);

// when
int statusCode = catchSystemExit(() -> ConsulPopulateCommand.main(args));
// Run the command and let it complete normally (with System.exit)
try {
ConsulPopulateCommand.main(args);
} catch (Exception e) {
// Expected since System.exit() throws SecurityException in test environment
// The actual work should be completed before the exit call
}

// then
assertThat(statusCode).isEqualTo(OK);

// Give a moment for async operations to complete
Thread.sleep(100);
var keys = toBlocking(consulClient.getKeys(""));
assertThat(keys).isEmpty();
}
Expand All @@ -89,26 +95,33 @@ void should_use_arguments() throws Exception {
properties.put("consul.files.target", "prod");
properties.put("consul.files.root-path", getRootPath());

String[] args = properties.entrySet()
var args = properties.entrySet()
.stream()
.map(entry -> {
String key = entry.getKey();
var key = entry.getKey();
var value = entry.getValue();
return "--" + key + "=" + value;
})
.toArray(String[]::new);

// when
int statusCode = catchSystemExit(() -> ConsulPopulateCommand.main(args));
// Run the command and let it complete normally (with System.exit)
// The application will populate Consul and then exit
try {
ConsulPopulateCommand.main(args);
} catch (Exception e) {
// Expected since System.exit() throws SecurityException in test environment
// The actual work should be completed before the exit call
}

// then
assertThat(statusCode).isEqualTo(OK);

// Give a moment for async operations to complete
Thread.sleep(100);
assertConsulKVCorrectlyPopulated();
}

@Test
@Disabled
@Disabled("disabled due to limitation of tests run with env variables")
void should_use_environmentVariables() throws Exception {
// given
withEnvironmentVariable("CONSUL_HOST", consulHost)
Expand All @@ -121,7 +134,7 @@ void should_use_environmentVariables() throws Exception {
.execute(() -> {
// when
final String[] args = {};
int statusCode = catchSystemExit(() -> ConsulPopulateCommand.main(args));
var statusCode = catchSystemExit(() -> ConsulPopulateCommand.main(args));

// then
assertThat(statusCode).isEqualTo(OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.frogdevelopment.consul.populate.config;

import java.net.URI;

import jakarta.inject.Singleton;

import io.micronaut.context.annotation.Bean;
Expand All @@ -17,6 +19,8 @@
@Factory
public class ConsulFactory {

static final int DEFAULT_HTTP_PORT = 80;

@Singleton
@Bean(preDestroy = "close")
Vertx vertx() {
Expand All @@ -26,13 +30,28 @@ Vertx vertx() {
@Singleton
@Bean(preDestroy = "close")
ConsulClient consulClient(final Vertx vertx, final GlobalProperties properties) {
final var consulClientOptions = new ConsulClientOptions()
.setHost(properties.getHost())
.setPort(properties.getPort())
.setSsl(properties.isSecured());
final ConsulClientOptions consulClientOptions;
if (properties.getUri().isPresent()) {
final var uri = URI.create(properties.getUri().get());
consulClientOptions = new ConsulClientOptions()
.setHost(uri.getHost())
.setSsl("https".equalsIgnoreCase(uri.getScheme()));
final var port = uri.getPort();
if (port >= 0) {
consulClientOptions.setPort(port);
} else {
consulClientOptions.setPort(DEFAULT_HTTP_PORT); // default port when providing a URI without port
}
} else {
consulClientOptions = new ConsulClientOptions()
.setHost(properties.getHost())
.setPort(properties.getPort())
.setSsl(properties.isSecured());
}
properties.getDc().ifPresent(consulClientOptions::setDc);
properties.getAclToken().ifPresent(consulClientOptions::setAclToken);
properties.getTimeout().ifPresent(consulClientOptions::setTimeout);
// properties.getConnectTimeout().ifPresent(consulClientOptions::setConnectTimeout);

return ConsulClient.create(vertx, consulClientOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Optional;
import java.util.OptionalLong;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
Expand All @@ -23,6 +24,11 @@
@ConfigurationProperties("consul")
public class GlobalProperties {

/**
* Consul URI.
*/
private Optional<String> uri = Optional.empty();

/**
* Consul host. Defaults to {@code localhost}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.frogdevelopment.consul.populate.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import io.vertx.core.Vertx;
import io.vertx.ext.consul.ConsulClient;
import io.vertx.ext.consul.ConsulClientOptions;

@ExtendWith(MockitoExtension.class)
class ConsulFactoryTest {

@InjectMocks
private ConsulFactory consulFactory;

@Mock
private Vertx vertx;
@Mock
private GlobalProperties properties;
@Mock
private ConsulClient consulClient;

@Captor
private ArgumentCaptor<ConsulClientOptions> consulClientOptionsCaptor;

@Test
void should_use_uri_with_port() {
// given
try(var stub = Mockito.mockStatic(ConsulClient.class)) {
stub.when(()-> ConsulClient.create(eq(vertx), consulClientOptionsCaptor.capture()))
.thenReturn(consulClient);
given(properties.getUri()).willReturn(Optional.of("http://my-domain:8666"));

// when
consulFactory.consulClient(vertx, properties);

// then
final var consulClientOptions = consulClientOptionsCaptor.getValue();
assertThat(consulClientOptions.getHost()).isEqualTo("my-domain");
assertThat(consulClientOptions.getPort()).isEqualTo(8666);
assertThat(consulClientOptions.isSsl()).isFalse();
}
}

@Test
void should_use_uri_without_port() {
// given
try(var stub = Mockito.mockStatic(ConsulClient.class)) {
stub.when(()-> ConsulClient.create(eq(vertx), consulClientOptionsCaptor.capture()))
.thenReturn(consulClient);
given(properties.getUri()).willReturn(Optional.of("http://my-domain"));

// when
consulFactory.consulClient(vertx, properties);

// then
final var consulClientOptions = consulClientOptionsCaptor.getValue();
assertThat(consulClientOptions.getHost()).isEqualTo("my-domain");
assertThat(consulClientOptions.getPort()).isEqualTo(80);
assertThat(consulClientOptions.isSsl()).isFalse();
}
}

@Test
void should_use_host_and_port() {
// given
try(var stub = Mockito.mockStatic(ConsulClient.class)) {
stub.when(()-> ConsulClient.create(eq(vertx), consulClientOptionsCaptor.capture()))
.thenReturn(consulClient);
given(properties.getUri()).willReturn(Optional.empty());
given(properties.getHost()).willReturn("foo");
given(properties.getPort()).willReturn(1234);
given(properties.isSecured()).willReturn(true);

// when
consulFactory.consulClient(vertx, properties);

// then
final var consulClientOptions = consulClientOptionsCaptor.getValue();
assertThat(consulClientOptions.getHost()).isEqualTo("foo");
assertThat(consulClientOptions.getPort()).isEqualTo(1234);
assertThat(consulClientOptions.isSsl()).isTrue();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.HashMap;
import java.util.Map;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
Expand All @@ -25,7 +26,7 @@
class FilesPopulateServiceImplTest extends BaseFilesImporterTest {

@Container
public static final ConsulContainer CONSUL = new ConsulContainer("hashicorp/consul:1.18.1");
public static final ConsulContainer CONSUL = new ConsulContainer("hashicorp/consul:1.21");

@Inject
private PopulateService populateService;
Expand Down