From b4c907ec5e01a5bbb1047e4f028e2dec14ba1728 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Mon, 25 Nov 2024 21:37:23 +0100 Subject: [PATCH] [skip ci] add possibility for monthly reboot as "only" without provisioning server --- .../provs/server/domain/k3s/K3sService.kt | 9 ++++ .../server/domain/k3s/ServerSubModule.kt | 3 +- .../server/application/ApplicationKtTest.kt | 52 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt 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 a41a96c..6d5049c 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 @@ -26,6 +26,7 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task { } else { provisionGrafana(cli.onlyModules, grafanaConfigResolved) provisionHetznerCSI(cli.onlyModules, hcloudConfigResolved) + scheduleMonthlyReboot(cli.onlyModules) } } @@ -106,3 +107,11 @@ private fun Prov.provisionHetznerCSI( } } + +fun Prov.scheduleMonthlyReboot( + onlyModules: List?, +) = task { + if (onlyModules != null && onlyModules.contains(ServerOnlyModule.MONTHLY_REBOOT.name.lowercase())) { + scheduleMonthlyReboot() + } +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ServerSubModule.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ServerSubModule.kt index 07613f2..a0cc55c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ServerSubModule.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/ServerSubModule.kt @@ -2,5 +2,6 @@ package org.domaindrivenarchitecture.provs.server.domain.k3s enum class ServerOnlyModule { GRAFANA, - HETZNER_CSI + HETZNER_CSI, + MONTHLY_REBOOT, } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt new file mode 100644 index 0000000..3fc9dc7 --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt @@ -0,0 +1,52 @@ +package org.domaindrivenarchitecture.provs.server.application + +import io.mockk.* +import org.domaindrivenarchitecture.provs.framework.core.Prov +import org.domaindrivenarchitecture.provs.framework.core.ProvResult +import org.domaindrivenarchitecture.provs.framework.core.local +import org.domaindrivenarchitecture.provs.framework.core.processors.DummyProcessor +import org.domaindrivenarchitecture.provs.framework.core.remote +import org.domaindrivenarchitecture.provs.framework.ubuntu.cron.infrastructure.scheduleMonthlyReboot +import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s +import org.junit.jupiter.api.Test + + +class ApplicationKtTest { + @Test + fun test_main_with_only_monthly_reboot() { + // given + val dummyProv = Prov.newInstance(DummyProcessor()) + + mockkObject(Prov) + every { Prov.newInstance(any(), any(), any(), any()) } returns dummyProv + + mockkStatic(::local) + every { local() } returns dummyProv + + mockkStatic(::remote) + every { remote(any(), any(), any(), any()) } returns dummyProv + + mockkStatic(Prov::scheduleMonthlyReboot) + every { any().scheduleMonthlyReboot() } returns ProvResult( + true, + cmd = "mocked command" + ) + + mockkStatic(Prov::provisionK3s) + every { any().provisionK3s(any(), any(), any(), any()) } returns ProvResult( + true, + cmd = "mocked command" + ) + + // when + main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "monthly_reboot")) + + //then + verify(exactly = 1) { any().scheduleMonthlyReboot() } + verify(exactly = 0) { any().provisionK3s(any(), any(), any(), any()) } + + // cleanup + unmockkAll() + } + +} \ No newline at end of file