From 9ceb74515d46f159e2120d22960b09a7f65a5185 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Sun, 20 Aug 2023 10:51:13 +0200 Subject: [PATCH] refactor & fix execution of onlyModules, add tests --- .../provs/desktop/domain/DesktopOnlyModule.kt | 5 + .../provs/desktop/domain/DesktopService.kt | 167 +++++++++--------- .../application/ProvWithSudoKtTest.kt | 5 +- .../domain/CliTargetCommandTest.kt | 6 +- .../desktop/application/ApplicationKtTest.kt | 8 +- .../desktop/domain/DesktopServiceKtTest.kt | 74 +++++++- 6 files changed, 163 insertions(+), 102 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopOnlyModule.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopOnlyModule.kt index 87aced7..69cdbb2 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopOnlyModule.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopOnlyModule.kt @@ -2,4 +2,9 @@ package org.domaindrivenarchitecture.provs.desktop.domain enum class DesktopOnlyModule { FIREFOX, VERIFY + ; + + fun isIn(list: List): Boolean { + return list.any { it.equals(this.name, ignoreCase = true) } + } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt index 0e351bb..7b1787e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt @@ -1,5 +1,7 @@ package org.domaindrivenarchitecture.provs.desktop.domain +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopOnlyModule.FIREFOX +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopOnlyModule.VERIFY import org.domaindrivenarchitecture.provs.desktop.infrastructure.* import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.ubuntu.git.provisionGit @@ -42,21 +44,29 @@ internal fun Prov.provisionDesktop( onlyModules: List? ) = task { validatePrecondition() - provisionBasicDesktop(gpg, ssh, gitUserName, gitEmail, onlyModules) - if (desktopType == DesktopType.OFFICE) { - provisionOfficeDesktop(onlyModules) - if (onlyModules == null) { + if (onlyModules == null) { + provisionBasicDesktop(gpg, ssh, gitUserName, gitEmail) + + if (desktopType == DesktopType.OFFICE) { + provisionOfficeDesktop() verifyOfficeSetup() } - } - if (desktopType == DesktopType.IDE) { - if (onlyModules == null) { + if (desktopType == DesktopType.IDE) { provisionOfficeDesktop() provisionIdeDesktop() verifyIdeSetup() - } else { - provisionIdeDesktop(onlyModules) + } + } else { + if (FIREFOX.isIn(onlyModules)) { + installPpaFirefox() + } + if (VERIFY.isIn(onlyModules)) { + if (desktopType == DesktopType.OFFICE) { + verifyOfficeSetup() + } else if (desktopType == DesktopType.IDE) { + verifyIdeSetup() + } } } } @@ -67,93 +77,78 @@ fun Prov.validatePrecondition() { } } -fun Prov.provisionIdeDesktop(onlyModules: List? = null) { - if (onlyModules == null) { - aptInstall(OPEN_VPM) - aptInstall(OPENCONNECT) - aptInstall(VPNC) - - // DevEnvs - installDocker() - aptInstall(JAVA) - aptInstall(CLOJURE_TOOLS) - installShadowCljs() - installDevOps() - provisionPython() - - // IDEs - installVSC("python", "clojure") - installIntelliJ() - } else if (onlyModules.contains(DesktopOnlyModule.VERIFY.name.lowercase())) { - verifyIdeSetup() - } else if (onlyModules.contains(DesktopOnlyModule.FIREFOX.name.lowercase())) { - installPpaFirefox() - } -} - -fun Prov.provisionOfficeDesktop(onlyModules: List? = null) { - if (onlyModules == null) { - aptInstall(ZIP_UTILS) - aptInstall(SPELLCHECKING_DE) - aptInstall(BROWSER) - aptInstall(EMAIL_CLIENT) - installDeltaChat() - aptInstall(OFFICE_SUITE) - installZimWiki() - installNextcloudClient() - aptInstall(COMPARE_TOOLS) - - // optional as installation of these tools often fail and they are not considered mandatory - optional { - aptInstall(DRAWING_TOOLS) - } - } else if (onlyModules.contains(DesktopOnlyModule.VERIFY.name.lowercase())) { - verifyOfficeSetup() - } else if (onlyModules.contains(DesktopOnlyModule.FIREFOX.name.lowercase())) { - installPpaFirefox() - } -} fun Prov.provisionBasicDesktop( gpg: KeyPair?, ssh: SshKeyPair?, gitUserName: String?, gitEmail: String?, - onlyModules: List? ) { - if (onlyModules == null) { - aptInstall(KEY_MANAGEMENT) - aptInstall(VERSION_MANAGEMENT) - aptInstall(NETWORK_TOOLS) - aptInstall(SCREEN_TOOLS) - aptInstall(KEY_MANAGEMENT_GUI) - aptInstall(PASSWORD_TOOLS) - aptInstall(OS_ANALYSIS) - aptInstall(BASH_UTILS) - aptInstall(CLIP_TOOLS) - aptPurge( - "remove-power-management xfce4-power-manager " + - "xfce4-power-manager-plugins xfce4-power-manager-data" - ) - aptPurge("abiword gnumeric") - aptPurge("popularity-contest") + aptInstall(KEY_MANAGEMENT) + aptInstall(VERSION_MANAGEMENT) + aptInstall(NETWORK_TOOLS) + aptInstall(SCREEN_TOOLS) + aptInstall(KEY_MANAGEMENT_GUI) + aptInstall(PASSWORD_TOOLS) + aptInstall(OS_ANALYSIS) + aptInstall(BASH_UTILS) + aptInstall(CLIP_TOOLS) + aptPurge( + "remove-power-management xfce4-power-manager " + + "xfce4-power-manager-plugins xfce4-power-manager-data" + ) + aptPurge("abiword gnumeric") + aptPurge("popularity-contest") - provisionKeys(gpg, ssh) - provisionGit(gitUserName ?: whoami(), gitEmail, gpg?.let { gpgFingerprint(it.publicKey.plain()) }) + provisionKeys(gpg, ssh) + provisionGit(gitUserName ?: whoami(), gitEmail, gpg?.let { gpgFingerprint(it.publicKey.plain()) }) - installPpaFirefox() - installGopass() - configureGopass(publicGpgKey = gpg?.publicKey) - installGopassJsonApi() - downloadGopassBridge() + installPpaFirefox() + installGopass() + configureGopass(publicGpgKey = gpg?.publicKey) + installGopassJsonApi() + downloadGopassBridge() - installRedshift() - configureRedshift() + installRedshift() + configureRedshift() - configureNoSwappiness() - configureBash() - installVirtualBoxGuestAdditions() - } else if (onlyModules.contains(DesktopOnlyModule.FIREFOX.name.lowercase())) { - installPpaFirefox() + configureNoSwappiness() + configureBash() + installVirtualBoxGuestAdditions() +} + +fun Prov.provisionOfficeDesktop() { + aptInstall(ZIP_UTILS) + aptInstall(SPELLCHECKING_DE) + aptInstall(BROWSER) + aptInstall(EMAIL_CLIENT) + installDeltaChat() + aptInstall(OFFICE_SUITE) + installZimWiki() + installNextcloudClient() + aptInstall(COMPARE_TOOLS) + + // optional, as installation of these tools often fail and as they are not considered mandatory + optional { + aptInstall(DRAWING_TOOLS) } } + + +fun Prov.provisionIdeDesktop() { + aptInstall(OPEN_VPM) + aptInstall(OPENCONNECT) + aptInstall(VPNC) + + // DevEnvs + installDocker() + aptInstall(JAVA) + aptInstall(CLOJURE_TOOLS) + installShadowCljs() + installDevOps() + provisionPython() + + // IDEs + installVSC("python", "clojure") + installIntelliJ() +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt index a895618..8337d66 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/application/ProvWithSudoKtTest.kt @@ -2,7 +2,7 @@ package org.domaindrivenarchitecture.provs.configuration.application import io.mockk.every import io.mockk.mockkStatic -import io.mockk.unmockkStatic +import io.mockk.unmockkAll import org.domaindrivenarchitecture.provs.framework.core.* import org.domaindrivenarchitecture.provs.framework.core.cli.getPasswordToConfigureSudoWithoutPassword import org.domaindrivenarchitecture.provs.framework.core.docker.provideContainer @@ -49,7 +49,8 @@ class ProvWithSudoKtTest { assertFalse(canSudo1) assertTrue(canSudo2) - unmockkStatic(::getPasswordToConfigureSudoWithoutPassword) + // cleanup + unmockkAll() } @ExtensiveContainerTest diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/CliTargetCommandTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/CliTargetCommandTest.kt index ba2f91a..24a5607 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/CliTargetCommandTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/CliTargetCommandTest.kt @@ -31,10 +31,8 @@ internal class TargetCliCommandKtTest { @AfterAll @JvmStatic internal fun afterAll() { - unmockkObject(Prov) - unmockkStatic(::local) - unmockkStatic(::remote) - unmockkStatic(::getPasswordToConfigureSudoWithoutPassword) + // cleanup + unmockkAll() } } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt index 4b2af6a..3971e23 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/ApplicationKtTest.kt @@ -65,12 +65,8 @@ internal class ApplicationKtTest { @AfterAll @JvmStatic internal fun afterAll() { - unmockkObject(Prov) - unmockkStatic(::local) - unmockkStatic(::remote) - unmockkStatic(::getConfig) - unmockkStatic(Prov::provisionDesktop) - unmockkStatic(::getPasswordToConfigureSudoWithoutPassword) + // cleanup + unmockkAll() } } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopServiceKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopServiceKtTest.kt index 5e7878f..4dc43a0 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopServiceKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopServiceKtTest.kt @@ -1,12 +1,14 @@ package org.domaindrivenarchitecture.provs.desktop.domain -import org.domaindrivenarchitecture.provs.framework.core.ProgressType -import org.domaindrivenarchitecture.provs.framework.core.Prov +import io.mockk.* +import org.domaindrivenarchitecture.provs.desktop.infrastructure.installPpaFirefox +import org.domaindrivenarchitecture.provs.desktop.infrastructure.verifyIdeSetup +import org.domaindrivenarchitecture.provs.desktop.infrastructure.verifyOfficeSetup +import org.domaindrivenarchitecture.provs.framework.core.* import org.domaindrivenarchitecture.provs.framework.core.docker.provideContainer -import org.domaindrivenarchitecture.provs.framework.core.local import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerStartMode import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerUbuntuHostProcessor -import org.domaindrivenarchitecture.provs.framework.core.remote +import org.domaindrivenarchitecture.provs.framework.core.processors.DummyProcessor import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile import org.domaindrivenarchitecture.provs.test.defaultTestContainer import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest @@ -44,6 +46,70 @@ internal class DesktopServiceKtTest { } } + @Test + fun provisionDesktop_with_onlyModules_firefox_installs_firefox() { + // given + val prov = Prov.newInstance(DummyProcessor()) + mockkStatic(Prov::installPpaFirefox) // mocks all function in file Firefox.kt + every { any().installPpaFirefox() } returns ProvResult(true, cmd = "mocked") + + // when + prov.provisionDesktop(DesktopType.IDE, onlyModules = listOf("firefox")) + + // then + verify(exactly = 1) { any().installPpaFirefox() } + + // cleanup + unmockkAll() + } + + @Test + fun provisionDesktop_ide_with_onlyModules_verify_performs_verification() { + // given + val prov = Prov.newInstance(DummyProcessor()) + mockkStatic(Prov::verifyIdeSetup) + mockkStatic(Prov::verifyOfficeSetup) + mockkStatic(Prov::provisionBasicDesktop) // mocks function provisionBasicDesktop and all other functions in same file + every { any().verifyIdeSetup() } returns ProvResult(true, cmd = "mocked") + every { any().verifyOfficeSetup() } returns ProvResult(true, cmd = "mocked") + every { any().provisionBasicDesktop(any(), any(), any(), any()) } + + // when + prov.provisionDesktop(DesktopType.IDE, onlyModules = listOf("verify")) + + // then + verify(exactly = 1) { any().verifyIdeSetup() } + verify(exactly = 0) { any().verifyOfficeSetup() } + verify(exactly = 0) { any().provisionBasicDesktop(any(), any(), any(), any()) } + + // cleanup + unmockkAll() + } + + @Test + fun provisionDesktop_office_with_onlyModules_verify_performs_verification() { + // given + val prov = Prov.newInstance(DummyProcessor()) + mockkStatic(Prov::verifyIdeSetup) + mockkStatic(Prov::verifyOfficeSetup) + mockkStatic(Prov::provisionBasicDesktop) + every { any().verifyIdeSetup() } returns ProvResult(true, cmd = "mocked") + every { any().verifyOfficeSetup() } returns ProvResult(true, cmd = "mocked") + every { any().provisionBasicDesktop(any(), any(), any(), any()) } + + // when + prov.provisionDesktop(DesktopType.OFFICE, onlyModules = listOf("verify")) + + // then + verify(exactly = 0) { any().verifyIdeSetup() } + verify(exactly = 1) { any().verifyOfficeSetup() } + verify(exactly = 0) { any().provisionBasicDesktop(any(), any(), any(), any()) } + + // cleanup + unmockkAll() + } + + @ExtensiveContainerTest @Disabled("Takes very long, enable if you want to test a desktop setup") fun provisionDesktop() {