Merge branch 'config-file-check' into 'master'

Check for config file existence

See merge request domaindrivenarchitecture/provs!2
This commit is contained in:
Pat Dyn 2022-11-30 09:28:12 +00:00
commit 805fe029dc
6 changed files with 74 additions and 8 deletions

View file

@ -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
}
}

View file

@ -0,0 +1,5 @@
package org.domaindrivenarchitecture.provs.configuration.domain
interface ConfigFileRepository {
fun assertExists(configFileName: ConfigFileName?)
}

View file

@ -0,0 +1,13 @@
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("Config file ${configFileName.fileName} not found. Please check if path is correct.")
}
}
}

View file

@ -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)
val k3sConfigReprovision = k3sConfig.copy(reprovision = cli.reprovision || k3sConfig.reprovision)
provisionK3s(k3sConfigReprovision, grafanaConfigResolved, cli.applicationFileName)

View file

@ -0,0 +1,41 @@
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
val invalidFileName = ApplicationFileName("iDontExist")
val repo: ApplicationFileRepository = DefaultApplicationFileRepository()
// then
val exception = assertThrows<RuntimeException>(
"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
}
}

View file

@ -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<RuntimeException>(
@ -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