From b447b0cf3d13cc7b63cff42958b6d514e33ab020 Mon Sep 17 00:00:00 2001 From: Elvis Souza Date: Wed, 4 Sep 2024 16:57:26 -0300 Subject: [PATCH] Specify config file path by env and fixing arm release (#557) * Implementing env config file path option * fixing compiling errors * fixing test * fixing test * testing * updating the docs * [Gradle Release Plugin] - new version commit: '3.26.0-snapshot'. * release notes * fixing arm build mirror and ajusting test * new mirrors * formatting * refactoring * clean code --- RELEASE-NOTES.md | 3 + docs/content/3-configuration/_index.en.md | 34 +++++-- gradle.properties | 2 +- src/main/docker/qemu/Dockerfile | 9 +- .../application/ConfigFileFinderService.java | 39 ++++++++ .../config/application/ConfigPathMapper.java | 48 +++++++++ .../config/dataprovider/ConfigDAOJson.java | 15 +-- .../dataprovider/ConfigPathBuilder.java | 40 -------- .../dataprovider/mapper/ConfigFlagMapper.java | 2 +- .../config/dataprovider/vo/ConfigEnv.java | 8 +- .../config/dataprovider/vo/ConfigFlag.java | 17 +++- .../ConfigFileFinderServiceTest.java | 97 +++++++++++++++++++ .../application/ConfigPathMapperTest.java | 30 ++++++ .../dataprovider/ConfigDAOJsonTest.java | 19 +--- .../templates/ConfigFlagArgsTemplates.java | 10 ++ .../templates/ConfigFlagTemplates.java | 8 ++ .../templates/config/ConfigEnvTemplates.java | 20 ++++ src/test/resources/flags-test/001.json | 2 +- src/test/resources/flags-test/002.txt | 4 +- 19 files changed, 318 insertions(+), 89 deletions(-) create mode 100644 src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderService.java create mode 100644 src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapper.java delete mode 100644 src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigPathBuilder.java create mode 100644 src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderServiceTest.java create mode 100644 src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapperTest.java create mode 100644 src/test/java/testing/templates/config/ConfigEnvTemplates.java diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 2de6544bd..63b69e54d 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,3 +1,6 @@ +## 3.26.0 +* Option to set config file path from the ENV, see the docs. + ## 3.25.14 * Fixing SolverRemote NPE #533 diff --git a/docs/content/3-configuration/_index.en.md b/docs/content/3-configuration/_index.en.md index 93203595b..cce3bfbc0 100755 --- a/docs/content/3-configuration/_index.en.md +++ b/docs/content/3-configuration/_index.en.md @@ -13,7 +13,7 @@ Default: `8.8.8.8`. ### Web Server Port Web GUI port, Default: `5380`. -### DNS Server Port +### DNS Server Port Default: `53`. ### Log Level @@ -40,7 +40,7 @@ If should register container name / service name as a hostname. Default: false. ### Domain The container names domain used on the registered container, services. Default: `docker`. -Ex: +Ex: ```bash docker run --rm --name nginx nginx ``` @@ -61,8 +61,8 @@ Default: false. If DPS must be set as the default DNS automatically, commonly requires DPS be run as sudo/administrator permissions, this options also won't work in some cases when running inside a docker container, [see the feature details][1]. -### Host Machine Hostname -Hostname to solve machine IP, domain can be changed by Domain option. Default: `host.docker`. +### Host Machine Hostname +Hostname to solve machine IP, domain can be changed by Domain option. Default: `host.docker`. | Env | JSON | Terminal | |----------------------------|-----------------------|------------| @@ -79,7 +79,7 @@ Docker host address. Default value is SO dependent. | `MG_DOCKER_HOST` | `dockerHost` | See --help | ### Resolvconf Override Name Servers -If must comment all existing nameservers at `resolv.conf` file (Linux, MacOS) or just put DPS at the first place. +If must comment all existing nameservers at `resolv.conf` file (Linux, MacOS) or just put DPS at the first place. Default: true. | Env | JSON | Terminal | @@ -87,7 +87,7 @@ Default: true. | `MG_RESOLVCONF_OVERRIDE_NAMESERVERS` | `resolvConfOverrideNameServers` | See --help | ### Resolvconf -Linux/Mac resolvconf or systemd-resolved path to set DPS as default DNS. +Linux/Mac resolvconf or systemd-resolved path to set DPS as default DNS. Default: `/host/etc/systemd/resolved.conf,/host/etc/resolv.conf,/etc/systemd/resolved.conf,/etc/resolv.conf`. | Env | JSON | Terminal | @@ -129,6 +129,28 @@ Default: true |-----------------------------------------------|-----------------------------------------|-----------| | MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE | `dockerSolverHostMachineFallbackActive` | See -help | +## Config File Path + +The location of the external config file (as the JSON file) can be changed by setting two variables: + +### Config File Path + +Relative or absolute path to the config file. Default: `conf/config.json` + +| Env | JSON | Terminal | +|---------------------|------|-----------| +| MG_CONFIG_FILE_PATH | N/A | See -help | + +### Working Dir (Optional) + +Is the path which will be used when **ConfigFilePath** is set as a relative path. +Default: `DPS executing path, aka Working Directory`. + +| Env | JSON | Terminal | +|-------------|------|----------| +| MG_WORK_DIR | N/A | N/A | + + ## Example JSON configuration __Version 2__ diff --git a/gradle.properties b/gradle.properties index 9bf694fac..f6bed534f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=3.25.14-snapshot +version=3.26.0-snapshot diff --git a/src/main/docker/qemu/Dockerfile b/src/main/docker/qemu/Dockerfile index 917a3caec..2b556bb4b 100644 --- a/src/main/docker/qemu/Dockerfile +++ b/src/main/docker/qemu/Dockerfile @@ -1,9 +1,10 @@ FROM multiarch/qemu-user-static:latest@sha256:fe60359c92e86a43cc87b3d906006245f77bfc0565676b80004cc666e4feb9f0 -ARG DEB_MIRROR1="http://ftp.de.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.5+ds-2_amd64.deb" -ARG DEB_MIRROR2="http://ftp.de.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.5+ds-1_amd64.deb" -ARG DEB_MIRROR3="https://ftp.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.4+ds-1_amd64.deb" -RUN wget ${DEB_MIRROR1} || wget ${DEB_MIRROR2} || wget ${DEB_MIRROR3} +ARG DEB_MIRROR1="http://archive.ubuntu.com/ubuntu/pool/universe/q/qemu/qemu-user-static_8.2.2+ds-0ubuntu1.2_amd64.deb" +ARG DEB_MIRROR2="http://ftp.de.debian.org/debian/pool/main/q/qemu/qemu-user-static_8.2.5+ds-2_amd64.deb" +ARG DEB_MIRROR3="http://download.unesp.br/ubuntu/pool/universe/q/qemu/qemu-user-static_8.2.2+ds-0ubuntu1_amd64.deb" +ARG DEB_MIRROR4="https://deb.sipwise.com/debian/pool/main/q/qemu/qemu-user-static_8.2.1+ds-1~bpo12+1_amd64.deb" +RUN wget ${DEB_MIRROR1} || wget ${DEB_MIRROR2} || wget ${DEB_MIRROR3} || wget ${DEB_MIRROR4} RUN mkdir releases &&\ dpkg-deb -X qemu-user-static_*.deb releases &&\ diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderService.java b/src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderService.java new file mode 100644 index 000000000..f4c68f392 --- /dev/null +++ b/src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderService.java @@ -0,0 +1,39 @@ +package com.mageddo.dnsproxyserver.config.application; + +import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOCmdArgs; +import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOEnv; +import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigEnv; +import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag; +import com.mageddo.dnsproxyserver.utils.ObjectUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.nio.file.Path; + +@Slf4j +@Singleton +@RequiredArgsConstructor(onConstructor = @__({@Inject})) +public class ConfigFileFinderService { + + private final ConfigDAOEnv configDAOEnv; + private final ConfigDAOCmdArgs configDAOCmdArgs; + + public Path findPath(){ + final var envConfig = this.configDAOEnv.findRaw(); + final var argsConfig = this.configDAOCmdArgs.findRaw(); + + final var configFilePath = this.findConfigFilePath(envConfig, argsConfig); + + final var workDir = envConfig.getWorkingDir(); + return ConfigPathMapper.build(workDir, configFilePath); + } + + private Path findConfigFilePath(ConfigEnv envConfig, ConfigFlag argsConfig) { + return ObjectUtils.firstNonNullRequiring( + envConfig.getConfigFilePath(), + argsConfig.getConfigFileAsPath() + ); + } +} diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapper.java b/src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapper.java new file mode 100644 index 000000000..19f3b600f --- /dev/null +++ b/src/main/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapper.java @@ -0,0 +1,48 @@ +package com.mageddo.dnsproxyserver.config.application; + +import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag; +import com.mageddo.utils.Files; +import com.mageddo.utils.Runtime; +import com.mageddo.utils.Tests; +import lombok.extern.slf4j.Slf4j; + +import java.nio.file.Path; + +@Slf4j +class ConfigPathMapper { + + public static Path build(Path workDir, Path configFilePath) { + final var path = build0(workDir, configFilePath); + log.debug("status=configPathBuilt, path={}", path); + return path; + } + + private static Path build0(Path workDir, Path configPath) { + if (runningInTestsAndNoCustomConfigPath(configPath)) { + final var file = Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json"); + log.trace("status=runningInTests, usingEmptyFile={}", file); + return file; + } + if (workDir != null) { + return workDir + .resolve(configPath) + .toAbsolutePath() + ; + } + final var confRelativeToCurrDir = configPath.toAbsolutePath(); + if (Files.exists(confRelativeToCurrDir)) { + return confRelativeToCurrDir; + } + return Runtime.getRunningDir() + .resolve(configPath) + .toAbsolutePath(); + } + + private static boolean runningInTestsAndNoCustomConfigPath(Path configPath) { + return isDefaultConfigFilePath(configPath) && Tests.inTest(); + } + + private static boolean isDefaultConfigFilePath(Path configPath) { + return ConfigFlag.DEFAULT_CONFIG_FILE_AS_PATH.equals(configPath); + } +} diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java index b0305edf9..ec518f0d1 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java +++ b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java @@ -1,30 +1,25 @@ package com.mageddo.dnsproxyserver.config.dataprovider; import com.mageddo.dnsproxyserver.config.Config; +import com.mageddo.dnsproxyserver.config.application.ConfigFileFinderService; import com.mageddo.dnsproxyserver.config.dataprovider.mapper.ConfigJsonV2Mapper; -import com.mageddo.utils.Tests; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import javax.inject.Inject; import javax.inject.Singleton; import java.nio.file.Path; -import java.util.Arrays; @Slf4j @Singleton @RequiredArgsConstructor(onConstructor = @__({@Inject})) public class ConfigDAOJson implements ConfigDAO { - private final ConfigDAOEnv configDAOEnv; - private final ConfigDAOCmdArgs configDAOCmdArgs; + private final ConfigFileFinderService configFileFinderService; @Override public Config find() { - final var workDir = this.configDAOEnv.findRaw().getCurrentPath(); - final var relativeConfigFilePath = this.configDAOCmdArgs.findRaw().getConfigPath(); - final var configFileAbsolutePath = ConfigPathBuilder.build(workDir, relativeConfigFilePath); - return this.find(configFileAbsolutePath); + return this.find(this.configFileFinderService.findPath()); } public Config find(Path configPath) { @@ -33,10 +28,6 @@ public Config find(Path configPath) { return ConfigJsonV2Mapper.toConfig(jsonConfig, configPath); } - static boolean runningInTestsAndNoCustomConfigPath() { - return !Arrays.toString(ConfigDAOCmdArgs.getArgs()).contains("--conf-path") && Tests.inTest(); - } - @Override public int priority() { return 2; diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigPathBuilder.java b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigPathBuilder.java deleted file mode 100644 index 5c05057ac..000000000 --- a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigPathBuilder.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.mageddo.dnsproxyserver.config.dataprovider; - -import com.mageddo.utils.Files; -import com.mageddo.utils.Runtime; -import lombok.extern.slf4j.Slf4j; - -import java.nio.file.Path; -import java.nio.file.Paths; - -@Slf4j -public class ConfigPathBuilder { - private static Path build0(Path workDir, String configPath) { - if (ConfigDAOJson.runningInTestsAndNoCustomConfigPath()) { - final var file = Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json"); - log.trace("status=runningInTests, usingEmptyFile={}", file); - return file; - } - if (workDir != null) { - return workDir - .resolve(configPath) - .toAbsolutePath() - ; - } - final var confRelativeToCurrDir = Paths - .get(configPath) - .toAbsolutePath(); - if (Files.exists(confRelativeToCurrDir)) { - return confRelativeToCurrDir; - } - return Runtime.getRunningDir() - .resolve(configPath) - .toAbsolutePath(); - } - - public static Path build(Path workDir, String relativeConfigFilePath) { - final var path = build0(workDir, relativeConfigFilePath); - log.debug("status=configPathBuilt, path={}", path); - return path; - } -} diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/mapper/ConfigFlagMapper.java b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/mapper/ConfigFlagMapper.java index 97f19c072..ab272765d 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/mapper/ConfigFlagMapper.java +++ b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/mapper/ConfigFlagMapper.java @@ -9,7 +9,7 @@ public class ConfigFlagMapper { public static Config toConfig(ConfigFlag config) { return Config.builder() - .configPath(Files.pathOf(config.getConfigPath())) + .configPath(Files.pathOf(config.getConfigFilePath())) .registerContainerNames(config.getRegisterContainerNames()) .domain(config.getDomain()) .logFile(config.getLogToFile()) diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigEnv.java b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigEnv.java index 41e0de340..1d376933f 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigEnv.java +++ b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigEnv.java @@ -47,7 +47,9 @@ public class ConfigEnv { private static final String MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE = "MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE"; - private Path currentPath; + private static final String MG_CONFIG_FILE_PATH = "MG_CONFIG_FILE_PATH"; + + private Path workingDir; private String resolvConfPath; private String logFile; private String logLevel; @@ -61,11 +63,12 @@ public class ConfigEnv { private Boolean noRemoteServers; private Integer noEntriesResponseCode; private Boolean dockerSolverHostMachineFallbackActive; + private Path configFilePath; public static ConfigEnv fromEnv() { return ConfigEnv .builder() - .currentPath(Envs.getPathOrNull(MG_WORK_DIR)) + .workingDir(Envs.getPathOrNull(MG_WORK_DIR)) .resolvConfPath(Envs.getStringOrDefault(MG_RESOLVCONF, DEFAULT_RESOLV_CONF_PATH)) .logFile(findLogFilePath()) .logLevel(Envs.getStringOrNull(MG_LOG_LEVEL)) @@ -79,6 +82,7 @@ public static ConfigEnv fromEnv() { .noRemoteServers(Envs.getBooleanOrNull(MG_NO_REMOTE_SERVERS)) .noEntriesResponseCode(Envs.getIntegerOrNull(MG_NO_ENTRIES_RESPONSE_CODE)) .dockerSolverHostMachineFallbackActive(Envs.getBooleanOrNull(MG_DOCKER_SOLVER_HOST_MACHINE_FALLBACK_ACTIVE)) + .configFilePath(Envs.getPathOrNull(MG_CONFIG_FILE_PATH)) .build(); } diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigFlag.java b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigFlag.java index 03e8ec972..5a8e1eae6 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigFlag.java +++ b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/vo/ConfigFlag.java @@ -4,12 +4,15 @@ import com.mageddo.dnsproxyserver.config.dataprovider.ConfigPropDAO; import lombok.Getter; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import picocli.CommandLine; import picocli.CommandLine.Option; import java.io.PrintWriter; import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.concurrent.Callable; @@ -17,6 +20,9 @@ @NoArgsConstructor public class ConfigFlag implements Callable { + public static final String DEFAULT_CONFIG_FILE_PATH = "conf/config.json"; + public static final Path DEFAULT_CONFIG_FILE_AS_PATH = Paths.get(DEFAULT_CONFIG_FILE_PATH); + @Option( names = {"-version", "--version"}, description = "Shows the current version (default false)" ) @@ -46,9 +52,9 @@ public class ConfigFlag implements Callable { @Option( names = {"-conf-path", "--conf-path"}, description = "The config file path (default conf/config.json)", - defaultValue = "conf/config.json" + defaultValue = DEFAULT_CONFIG_FILE_PATH ) - private String configPath; + private String configFilePath; @Option( names = {"-service", "--service"}, @@ -236,4 +242,11 @@ public Boolean call() { return false; } + @JsonIgnore + public Path getConfigFileAsPath() { + if (StringUtils.isNotBlank(this.configFilePath)) { + return Paths.get(this.configFilePath); + } + return null; + } } diff --git a/src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderServiceTest.java b/src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderServiceTest.java new file mode 100644 index 000000000..3b359e8be --- /dev/null +++ b/src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigFileFinderServiceTest.java @@ -0,0 +1,97 @@ +package com.mageddo.dnsproxyserver.config.application; + +import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOCmdArgs; +import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOEnv; +import org.hamcrest.CoreMatchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; +import testing.templates.ConfigFlagTemplates; +import testing.templates.config.ConfigEnvTemplates; + +import java.nio.file.Paths; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.doReturn; + +@ExtendWith(MockitoExtension.class) +class ConfigFileFinderServiceTest { + + @Mock + ConfigDAOEnv configDAOEnv; + + @Mock + ConfigDAOCmdArgs configDAOCmdArgs; + + @Spy + @InjectMocks + ConfigFileFinderService service; + + @Test + void mustUseEnvConfig() { + // arrange + doReturn(ConfigEnvTemplates.withConfigFilePath()) + .when(this.configDAOEnv) + .findRaw() + ; + + doReturn(ConfigFlagTemplates.withConfigFilePath()) + .when(this.configDAOCmdArgs) + .findRaw() + ; + + // act + final var path = this.service.findPath(); + + // assert + assertTrue(path.endsWith(Paths.get("some-place/config.json")), path.toString()); + + } + + @Test + void mustUseArgsConfigWhenEnvNotSet() { + // arrange + doReturn(ConfigEnvTemplates.empty()) + .when(this.configDAOEnv) + .findRaw() + ; + + doReturn(ConfigFlagTemplates.withConfigFilePath()) + .when(this.configDAOCmdArgs) + .findRaw() + ; + + // act + final var path = this.service.findPath(); + + // assert + assertTrue(path.endsWith(Paths.get("flag-relative-path/flag-config.json")), path.toString()); + + } + + + @Test + void mustUseRandomGeneratedConfigPathWhenRunningInTestsAndNoCustomPathIsSpecified() { + // arrange + doReturn(ConfigEnvTemplates.empty()) + .when(this.configDAOEnv) + .findRaw() + ; + + doReturn(ConfigFlagTemplates.empty()) + .when(this.configDAOCmdArgs) + .findRaw() + ; + + // act + final var path = this.service.findPath(); + + // assert + assertThat(path.toString(), CoreMatchers.containsString("dns-proxy-server-junit")); + + } +} diff --git a/src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapperTest.java b/src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapperTest.java new file mode 100644 index 000000000..95b06db2c --- /dev/null +++ b/src/test/java/com/mageddo/dnsproxyserver/config/application/ConfigPathMapperTest.java @@ -0,0 +1,30 @@ +package com.mageddo.dnsproxyserver.config.application; + +import com.mageddo.dnsproxyserver.config.dataprovider.ConfigDAOCmdArgs; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import testing.templates.ConfigFlagTemplates; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.*; + +class ConfigPathMapperTest { + + @Test + void mustBuildConfPathRelativeToWorkDir(@TempDir Path tmpDir){ + // arrange + final var workDir = tmpDir.resolve("custom-work-dir"); + final var flags = ConfigFlagTemplates.defaultWithConfigPath(Paths.get("conf/config-custom.json")); + ConfigDAOCmdArgs.setArgs(flags.getArgs()); + + // act + final var configPath = ConfigPathMapper.build(workDir, flags.getConfigFileAsPath()); + + // assert + assertEquals("config-custom.json", configPath.getFileName().toString()); + assertEquals(workDir.getFileName().toString(), configPath.getParent().getParent().getFileName().toString()); + } + +} diff --git a/src/test/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJsonTest.java b/src/test/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJsonTest.java index d0240201f..ac11c6491 100644 --- a/src/test/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJsonTest.java +++ b/src/test/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJsonTest.java @@ -6,11 +6,9 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import org.mockito.junit.jupiter.MockitoExtension; -import testing.templates.ConfigFlagTemplates; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import static com.mageddo.utils.TestUtils.readAndSortJsonExcluding; import static com.mageddo.utils.TestUtils.readAsStream; @@ -26,22 +24,7 @@ class ConfigDAOJsonTest { "dockerHost" }; - final ConfigDAOJson configDAOJson = new ConfigDAOJson(null, null); - - @Test - void mustBuildConfPathRelativeToWorkDir(@TempDir Path tmpDir){ - // arrange - final var workDir = tmpDir.resolve("custom-work-dir"); - final var flags = ConfigFlagTemplates.defaultWithConfigPath(Paths.get("conf/config.json")); - ConfigDAOCmdArgs.setArgs(flags.getArgs()); - - // act - final var configPath = ConfigPathBuilder.build(workDir, flags.getConfigPath()); - - // assert - assertEquals("config.json", configPath.getFileName().toString()); - assertEquals(workDir.getFileName().toString(), configPath.getParent().getParent().getFileName().toString()); - } + final ConfigDAOJson configDAOJson = new ConfigDAOJson(null); @Test void mustReadAndRespectStoredConfigFile(@TempDir Path tmpDir) { diff --git a/src/test/java/testing/templates/ConfigFlagArgsTemplates.java b/src/test/java/testing/templates/ConfigFlagArgsTemplates.java index a1ed7c0ee..60e62c651 100644 --- a/src/test/java/testing/templates/ConfigFlagArgsTemplates.java +++ b/src/test/java/testing/templates/ConfigFlagArgsTemplates.java @@ -14,4 +14,14 @@ public static String[] withRandomPortsAndNotAsDefaultDns() { "--log-level=TRACE", }; } + + public static String[] withConfigFilePath() { + return new String[]{ + "--conf-path=flag-relative-path/flag-config.json" + }; + } + + public static String[] empty() { + return new String[]{}; + } } diff --git a/src/test/java/testing/templates/ConfigFlagTemplates.java b/src/test/java/testing/templates/ConfigFlagTemplates.java index 1d180fda4..e153c21d9 100644 --- a/src/test/java/testing/templates/ConfigFlagTemplates.java +++ b/src/test/java/testing/templates/ConfigFlagTemplates.java @@ -20,4 +20,12 @@ public static ConfigFlag withHelpFlag(){ public static ConfigFlag withVersionFlag() { return ConfigFlag.parse(new String[]{"--version"}); } + + public static ConfigFlag withConfigFilePath() { + return ConfigFlag.parse(ConfigFlagArgsTemplates.withConfigFilePath()); + } + + public static ConfigFlag empty() { + return ConfigFlag.parse(ConfigFlagArgsTemplates.empty()); + } } diff --git a/src/test/java/testing/templates/config/ConfigEnvTemplates.java b/src/test/java/testing/templates/config/ConfigEnvTemplates.java new file mode 100644 index 000000000..6af5fab03 --- /dev/null +++ b/src/test/java/testing/templates/config/ConfigEnvTemplates.java @@ -0,0 +1,20 @@ +package testing.templates.config; + +import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigEnv; + +import java.nio.file.Paths; + +public class ConfigEnvTemplates { + public static ConfigEnv withConfigFilePath() { + return ConfigEnv + .builder() + .configFilePath(Paths.get("some-place/config.json")) + .build(); + } + + public static ConfigEnv empty() { + return ConfigEnv + .builder() + .build(); + } +} diff --git a/src/test/resources/flags-test/001.json b/src/test/resources/flags-test/001.json index e0cdb2902..b49d931ab 100644 --- a/src/test/resources/flags-test/001.json +++ b/src/test/resources/flags-test/001.json @@ -5,7 +5,7 @@ "dockerHost" : null, "dockerSolverHostMachineFallbackActive" : true, "defaultDns" : true, - "configPath" : "conf/config.json", + "configFilePath" : "conf/config.json", "service" : null, "publishServicePort" : true, "logToFile" : "console", diff --git a/src/test/resources/flags-test/002.txt b/src/test/resources/flags-test/002.txt index e771a8bdb..0dc996b05 100644 --- a/src/test/resources/flags-test/002.txt +++ b/src/test/resources/flags-test/002.txt @@ -1,10 +1,10 @@ Usage:
[-default-dns] [-docker-solver-host-machine-fallback] [-dps-network] [-dps-network-auto-connect] [-help] [-no-remote-servers] [-register-container-names] [-resolvconf-override-name-servers] - [-service-publish-web-port] [-version] [-conf-path=] [-docker-host=] + [-service-publish-web-port] [-version] [-conf-path=] [-docker-host=] [-domain=] [-host-machine-hostname=] [-log-file=] [-log-level=] [-no-entries-response-code=] [-server-port=] [-service=] [-web-server-port=] - -conf-path, --conf-path= + -conf-path, --conf-path= The config file path (default conf/config.json) -default-dns, --default-dns This DNS server will be the default server for this machine (default true)