From 5cf191568e1df6a75b439a24d19f53afbe836c87 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 23 Nov 2022 14:41:10 +0100 Subject: [PATCH 1/4] Introduce check for config file --- .../provs/server/domain/k3s/ConfigFileName.kt | 9 +++++++++ .../provs/server/domain/k3s/ConfigFileRepository.kt | 5 +++++ 2 files changed, 14 insertions(+) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt new file mode 100644 index 0000000..d716f5b --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt @@ -0,0 +1,9 @@ +package org.domaindrivenarchitecture.provs.server.domain.k3s + +import java.io.File + +data class ConfigFileName(val fileName: String) { + fun fullqualified() : String { + return File(fileName).absoluteFile.absolutePath + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt new file mode 100644 index 0000000..029f6c9 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt @@ -0,0 +1,5 @@ +package org.domaindrivenarchitecture.provs.server.domain.k3s + +interface ConfigFileRepository { + fun assertExists(configFileName: ConfigFileName?) +} \ No newline at end of file From 0ef50ea233717a10d310b89e979799e6d8ee9b6a Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 23 Nov 2022 15:03:02 +0100 Subject: [PATCH 2/4] Refactor into configuration package Implement existence check in k3sService. --- .../provs/configuration/domain/ConfigFileName.kt | 5 +++++ .../domain}/ConfigFileRepository.kt | 2 +- .../infrastructure/DefaultConfigFileRepository.kt | 14 ++++++++++++++ .../provs/server/domain/k3s/ConfigFileName.kt | 9 --------- .../provs/server/domain/k3s/K3sService.kt | 2 ++ 5 files changed, 22 insertions(+), 10 deletions(-) rename src/main/kotlin/org/domaindrivenarchitecture/provs/{server/domain/k3s => configuration/domain}/ConfigFileRepository.kt (58%) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt index 0ca0cee..7c3de6e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt @@ -1,6 +1,11 @@ package org.domaindrivenarchitecture.provs.configuration.domain +import java.io.File + class ConfigFileName(fileName: String) { val fileName = fileName.trim() + fun fullqualified() : String { + return File(fileName).absoluteFile.absolutePath + } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt similarity index 58% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt index 029f6c9..49232e4 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt @@ -1,4 +1,4 @@ -package org.domaindrivenarchitecture.provs.server.domain.k3s +package org.domaindrivenarchitecture.provs.configuration.domain interface ConfigFileRepository { fun assertExists(configFileName: ConfigFileName?) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt new file mode 100644 index 0000000..a228ec1 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt @@ -0,0 +1,14 @@ +package org.domaindrivenarchitecture.provs.configuration.infrastructure + +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkLocalFile +import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName +import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileRepository + +class DefaultConfigFileRepository : ConfigFileRepository { + + override fun assertExists(configFileName: ConfigFileName?) { + if (configFileName != null && !checkLocalFile(configFileName.fullqualified())) { + throw RuntimeException("Application file ${configFileName.fileName} not found. Please check if path is correct.") + } + } +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt deleted file mode 100644 index d716f5b..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ConfigFileName.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.domaindrivenarchitecture.provs.server.domain.k3s - -import java.io.File - -data class ConfigFileName(val fileName: String) { - fun fullqualified() : String { - return File(fileName).absoluteFile.absolutePath - } -} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt index bc5474c..a65c5f6 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt @@ -1,5 +1,6 @@ package org.domaindrivenarchitecture.provs.server.domain.k3s +import org.domaindrivenarchitecture.provs.configuration.infrastructure.DefaultConfigFileRepository import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.server.domain.k8s_grafana_agent.GrafanaAgentConfigResolved import org.domaindrivenarchitecture.provs.server.domain.k8s_grafana_agent.provisionGrafanaAgent @@ -14,6 +15,7 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task { if (cli.onlyModules == null ) { val k3sConfig: K3sConfig = getK3sConfig(cli.configFileName) DefaultApplicationFileRepository().assertExists(cli.applicationFileName) + DefaultConfigFileRepository().assertExists(cli.configFileName) if (cli.reprovision || k3sConfig.reprovision) { deprovisionK3sInfra() From 546f2faf3ad2aeb4eb7dc52c5d04ea87efc93282 Mon Sep 17 00:00:00 2001 From: az Date: Wed, 30 Nov 2022 09:56:15 +0100 Subject: [PATCH 3/4] [skip ci] add tests DefaultConfigFileRepository.kt --- .../DefaultConfigFileRepository.kt | 2 +- .../DefaultApplicationFileRepositoryKtTest.kt | 16 ++++---- .../DefaultConfigFileRepositoryKtTest.kt | 41 +++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt index a228ec1..b08f3d6 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt @@ -8,7 +8,7 @@ class DefaultConfigFileRepository : ConfigFileRepository { override fun assertExists(configFileName: ConfigFileName?) { if (configFileName != null && !checkLocalFile(configFileName.fullqualified())) { - throw RuntimeException("Application file ${configFileName.fileName} not found. Please check if path is correct.") + throw RuntimeException("Config file ${configFileName.fileName} not found. Please check if path is correct.") } } } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt index 86c1123..eb57bd6 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt @@ -1,10 +1,10 @@ package org.domaindrivenarchitecture.provs.server.infrastructure -import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName -import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository +import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName +import org.domaindrivenarchitecture.provs.configuration.infrastructure.DefaultConfigFileRepository +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import org.junit.jupiter.api.Assertions.assertEquals import java.io.File internal class DefaultApplicationFileRepositoryKtTest { @@ -12,8 +12,8 @@ internal class DefaultApplicationFileRepositoryKtTest { @Test fun assertExistsThrowsRuntimeException() { // when - val invalidFileName = ApplicationFileName("iDontExist") - val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + val invalidFileName = ConfigFileName("iDontExist") + val repo = DefaultConfigFileRepository() // then val exception = assertThrows( @@ -21,7 +21,7 @@ internal class DefaultApplicationFileRepositoryKtTest { ) { repo.assertExists(invalidFileName) } assertEquals( - "Application file iDontExist not found. Please check if path is correct.", + "Config file iDontExist not found. Please check if path is correct.", exception.message) } @@ -31,8 +31,8 @@ internal class DefaultApplicationFileRepositoryKtTest { val validFileName = "src/test/resources/existing_file" // when - val validFile = ApplicationFileName(File(validFileName).path) - val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + val validFile = ConfigFileName(File(validFileName).path) + val repo = DefaultConfigFileRepository() repo.assertExists(validFile) // then diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt new file mode 100644 index 0000000..91b2981 --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt @@ -0,0 +1,41 @@ +package org.domaindrivenarchitecture.provs.server.infrastructure + +import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName +import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.Assertions.assertEquals +import java.io.File + +internal class DefaultConfigFileRepositoryKtTest { + + @Test + fun assertExistsThrowsRuntimeException() { + // when + val invalidFileName = ApplicationFileName("iDontExist") + val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + + // then + val exception = assertThrows( + "Should not find the file." + ) { repo.assertExists(invalidFileName) } + + assertEquals( + "Application file iDontExist not found. Please check if path is correct.", + exception.message) + } + + @Test + fun assertExistsPasses() { + // given + val validFileName = "src/test/resources/existing_file" + + // when + val validFile = ApplicationFileName(File(validFileName).path) + val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + repo.assertExists(validFile) + + // then + // no exception is thrown + } +} \ No newline at end of file From c9b3c2a1eb1b0735e070042e0a5599de8b2a5cd0 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 30 Nov 2022 10:25:54 +0100 Subject: [PATCH 4/4] [skip ci] mv Config file tests to config test package --- .../provs/configuration/domain/ConfigFileRepository.kt | 2 +- .../infrastructure/DefaultConfigFileRepository.kt | 1 - .../infrastructure/DefaultConfigFileRepositoryKtTest.kt | 6 +++--- 3 files changed, 4 insertions(+), 5 deletions(-) rename src/test/kotlin/org/domaindrivenarchitecture/provs/{server => configuration}/infrastructure/DefaultConfigFileRepositoryKtTest.kt (88%) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt index 49232e4..ad9eedf 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt @@ -2,4 +2,4 @@ package org.domaindrivenarchitecture.provs.configuration.domain interface ConfigFileRepository { fun assertExists(configFileName: ConfigFileName?) -} \ No newline at end of file +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt index b08f3d6..7ce2000 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt @@ -5,7 +5,6 @@ import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileRepository class DefaultConfigFileRepository : ConfigFileRepository { - override fun assertExists(configFileName: ConfigFileName?) { if (configFileName != null && !checkLocalFile(configFileName.fullqualified())) { throw RuntimeException("Config file ${configFileName.fileName} not found. Please check if path is correct.") diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepositoryKtTest.kt similarity index 88% rename from src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt rename to src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepositoryKtTest.kt index 91b2981..6319fdb 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultConfigFileRepositoryKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepositoryKtTest.kt @@ -1,14 +1,14 @@ -package org.domaindrivenarchitecture.provs.server.infrastructure +package org.domaindrivenarchitecture.provs.configuration.infrastructure import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository +import org.domaindrivenarchitecture.provs.server.infrastructure.DefaultApplicationFileRepository import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.Assertions.assertEquals import java.io.File internal class DefaultConfigFileRepositoryKtTest { - @Test fun assertExistsThrowsRuntimeException() { // when @@ -38,4 +38,4 @@ internal class DefaultConfigFileRepositoryKtTest { // then // no exception is thrown } -} \ No newline at end of file +}