Skip to content

Commit

Permalink
Add dns check, fix #283
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe committed Sep 11, 2024
1 parent 40aac07 commit d453b52
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 6 deletions.
37 changes: 37 additions & 0 deletions src/main/java/io/kokuwa/maven/k3s/mojo/RunMojo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.kokuwa.maven.k3s.mojo;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
Expand Down Expand Up @@ -204,9 +206,36 @@ public class RunMojo extends K3sMojo {
@Parameter(property = "k3s.skipRun", defaultValue = "false")
private boolean skipRun;

/**
* Check if dns resolves custom domains.
*
* @since 1.4.0
*/
@Parameter(property = "k3s.dnsResolverCheck", defaultValue = "true")
private boolean dnsResolverCheck;

/**
* Custom domain to resolve.
*
* @since 1.4.0
*/
@Parameter(property = "k3s.dnsResolverDomain", defaultValue = "k3s-maven-plugin.127.0.0.1.nip.io")
private String dnsResolverDomain;

@Override
public void execute() throws MojoExecutionException {

// check dns

if (dnsResolverCheck) {
try {
var address = InetAddress.getByName(dnsResolverDomain).getHostAddress();
getLog().debug("DNS resolved " + dnsResolverDomain + " to " + address + ".");
} catch (UnknownHostException e) {
getLog().warn("DNS was unable to resolve " + dnsResolverDomain + ". Custom domains may not work!");
}
}

if (isSkip(skipRun)) {
return;
}
Expand Down Expand Up @@ -404,4 +433,12 @@ public void setClusterCidr(String clusterCidr) {
public void setServiceCidr(String serviceCidr) {
this.serviceCidr = serviceCidr;
}

public void setDnsResolverCheck(boolean dnsResolverCheck) {
this.dnsResolverCheck = dnsResolverCheck;
}

public void setDnsResolverDomain(String dnsResolverDomain) {
this.dnsResolverDomain = dnsResolverDomain;
}
}
35 changes: 35 additions & 0 deletions src/test/java/io/kokuwa/maven/k3s/mojo/RunMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.lang.System.Logger.Level;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import io.kokuwa.maven.k3s.test.AbstractTest;
import io.kokuwa.maven.k3s.test.TestLog;
import io.kokuwa.maven.k3s.util.Await;
import io.kokuwa.maven.k3s.util.Task;

Expand Down Expand Up @@ -151,4 +154,36 @@ void withRegistriesMissing(RunMojo runMojo) throws MojoExecutionException {
assertEquals("Registries file '" + file.getAbsolutePath() + "' not found.", actualMessage, "exception message");
assertFalse(runMojo.getMarker().consumeStarted(), "no started marker expected");
}

@DisplayName("dns: skipped")
@Test
void checkDnsSkipped(RunMojo runMojo, TestLog log) {
runMojo.setSkip(true);
runMojo.setDnsResolverCheck(false);
assertDoesNotThrow(runMojo::execute);
assertTrue(log.getMessages(Level.DEBUG).isEmpty());
assertTrue(log.getMessages(Level.WARNING).isEmpty());
}

@DisplayName("dns: success")
@Test
void checkDnsSuccess(RunMojo runMojo, TestLog log) {
runMojo.setSkip(true);
assertDoesNotThrow(runMojo::execute);
assertEquals(List.of("DNS resolved k3s-maven-plugin.127.0.0.1.nip.io to 127.0.0.1."),
log.getMessages(Level.DEBUG));
assertTrue(log.getMessages(Level.WARNING).isEmpty());
}

@DisplayName("dns: failure")
@Test
void checkDnsFailure(RunMojo runMojo, TestLog log) {
runMojo.setSkip(true);
runMojo.setDnsResolverDomain("nope.example.org");
assertDoesNotThrow(runMojo::execute);
assertTrue(log.getMessages(Level.DEBUG).isEmpty());
assertEquals(
List.of("DNS was unable to resolve nope.example.org. Custom domains may not work!"),
log.getMessages(Level.WARNING));
}
}
4 changes: 2 additions & 2 deletions src/test/java/io/kokuwa/maven/k3s/test/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.net.http.HttpResponse;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.ClassOrderer;
Expand All @@ -37,12 +36,13 @@ public abstract class AbstractTest {

@BeforeEach
@AfterEach
void reset(TestInfo test, Log log, Docker newDocker) throws MojoExecutionException {
void reset(TestInfo test, TestLog log, Docker newDocker) throws MojoExecutionException {
log.info("Reset test: " + test.getTestMethod().orElse(null));
this.docker = newDocker;
this.docker.removeContainer();
this.docker.removeVolume();
this.docker.removeImage(helloWorld());
log.clear();
}

public static String helloWorld() {
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/io/kokuwa/maven/k3s/test/MojoExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.codehaus.plexus.util.InterpolationFilterReader;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.xml.XmlStreamReader;
Expand All @@ -22,7 +21,6 @@
import org.junit.jupiter.api.extension.ParameterResolver;

import io.kokuwa.maven.k3s.mojo.K3sMojo;
import io.kokuwa.maven.k3s.util.DebugLog;
import io.kokuwa.maven.k3s.util.Docker;

/**
Expand All @@ -34,7 +32,7 @@ public class MojoExtension implements ParameterResolver, BeforeAllCallback {

private static final String containerName = "k3s-maven-plugin";
private static final String volumeName = "k3s-maven-plugin-junit";
private static final Log log = new DebugLog(new SystemStreamLog(), false);
private static final TestLog log = new TestLog(false);
private static final Docker docker = new Docker(containerName, volumeName, log);
private static final Set<MojoDescriptor> mojos = new HashSet<>();

Expand All @@ -56,14 +54,15 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
var type = parameterContext.getParameter().getType();
return mojos.stream().map(MojoDescriptor::getImplementation).anyMatch(type.getName()::equals)
|| type.equals(Log.class)
|| type.equals(TestLog.class)
|| type.equals(Docker.class);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {

var type = parameterContext.getParameter().getType();
if (type.equals(Log.class)) {
if (type.equals(Log.class) || type.equals(TestLog.class)) {
return log;
}
if (type.equals(Docker.class)) {
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/io/kokuwa/maven/k3s/test/TestLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.kokuwa.maven.k3s.test;

import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.logging.SystemStreamLog;

import io.kokuwa.maven.k3s.util.DebugLog;

/**
* Wrapper for maven logger to capture output.
*
* @since 1.4.0
*/
public class TestLog extends DebugLog {

private final Map<Level, List<CharSequence>> messages = new HashMap<>();

public TestLog(boolean debug) {
super(new SystemStreamLog(), debug);
}

public void clear() {
messages.clear();
}

public List<CharSequence> getMessages(Level level) {
return messages.getOrDefault(level, List.of());
}

// capture

@Override
public void debug(CharSequence content) {
super.debug(content);
messages.computeIfAbsent(Level.DEBUG, k -> new ArrayList<CharSequence>()).add(content);
}

@Override
public void warn(CharSequence content) {
super.warn(content);
messages.computeIfAbsent(Level.WARNING, k -> new ArrayList<CharSequence>()).add(content);
}
}

0 comments on commit d453b52

Please sign in to comment.