refactor createProvInstance & add tests
This commit is contained in:
parent
b3bdc26e3f
commit
130a9b4786
4 changed files with 102 additions and 25 deletions
|
@ -79,6 +79,7 @@ dependencies {
|
||||||
api "ch.qos.logback:logback-core:1.2.5"
|
api "ch.qos.logback:logback-core:1.2.5"
|
||||||
|
|
||||||
testFixturesApi group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'
|
testFixturesApi group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'
|
||||||
|
testImplementation("io.mockk:mockk:1.12.0")
|
||||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
|
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.domaindrivenarchitecture.provs.core
|
package org.domaindrivenarchitecture.provs.core
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
open class Secret(private val value: String) {
|
open class Secret(private val value: String) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
|
@ -8,6 +10,12 @@ open class Secret(private val value: String) {
|
||||||
fun plain() : String {
|
fun plain() : String {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
return (this === other) || ((other is Secret) && (this.value == other.value))
|
||||||
|
}
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
return Objects.hash(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -93,30 +93,7 @@ internal fun createProvInstance(
|
||||||
if (cliCommand.isValidLocalhost()) {
|
if (cliCommand.isValidLocalhost()) {
|
||||||
return local()
|
return local()
|
||||||
} else if (cliCommand.isValidRemote()) {
|
} else if (cliCommand.isValidRemote()) {
|
||||||
val host = cliCommand.remoteHost!!
|
return createProvInstanceRemote(cliCommand.remoteHost!!, cliCommand.userName!!, cliCommand.sshWithKey, password, remoteHostSetSudoWithoutPasswordRequired)
|
||||||
val remoteUser = cliCommand.userName!!
|
|
||||||
|
|
||||||
val prov =
|
|
||||||
if (cliCommand.sshWithKey) {
|
|
||||||
remote(host, remoteUser)
|
|
||||||
} else {
|
|
||||||
require(
|
|
||||||
password != null,
|
|
||||||
{ "No password available for provisioning without ssh keys. Either specify provisioning by ssh-keys or provide password." })
|
|
||||||
remote(host, remoteUser, password)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!prov.currentUserCanSudo()) {
|
|
||||||
if (remoteHostSetSudoWithoutPasswordRequired) {
|
|
||||||
require(
|
|
||||||
password != null,
|
|
||||||
{ "User ${prov.whoami()} not able to sudo on remote machine without password and no password available for the user." })
|
|
||||||
prov.makeUserSudoerWithNoSudoPasswordRequired(password)
|
|
||||||
} else {
|
|
||||||
throw IllegalStateException("User ${prov.whoami()} not able to sudo on remote machine without password and option not set to enable user to sudo without password.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return prov
|
|
||||||
} else {
|
} else {
|
||||||
throw IllegalArgumentException("Error: neither a valid localHost nor a valid remoteHost was specified! Use option -h for help.")
|
throw IllegalArgumentException("Error: neither a valid localHost nor a valid remoteHost was specified! Use option -h for help.")
|
||||||
}
|
}
|
||||||
|
@ -126,8 +103,38 @@ internal fun createProvInstance(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun createProvInstanceRemote(
|
||||||
|
host: String,
|
||||||
|
remoteUser: String,
|
||||||
|
sshWithKey: Boolean,
|
||||||
|
password: Secret?,
|
||||||
|
remoteHostSetSudoWithoutPasswordRequired: Boolean
|
||||||
|
): Prov {
|
||||||
|
val prov =
|
||||||
|
if (sshWithKey) {
|
||||||
|
remote(host, remoteUser)
|
||||||
|
} else {
|
||||||
|
require(
|
||||||
|
password != null,
|
||||||
|
{ "No password available for provisioning without ssh keys. Either specify provisioning by ssh-keys or provide password." })
|
||||||
|
remote(host, remoteUser, password)
|
||||||
|
}
|
||||||
|
|
||||||
private fun retrievePassword(cliCommand: CliCommand): Secret? {
|
if (!prov.currentUserCanSudo()) {
|
||||||
|
if (remoteHostSetSudoWithoutPasswordRequired) {
|
||||||
|
require(
|
||||||
|
password != null,
|
||||||
|
{ "User ${prov.whoami()} not able to sudo on remote machine without password and no password available for the user." })
|
||||||
|
prov.makeUserSudoerWithNoSudoPasswordRequired(password)
|
||||||
|
} else {
|
||||||
|
throw IllegalStateException("User ${prov.whoami()} not able to sudo on remote machine without password and option not set to enable user to sudo without password.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prov
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
internal fun retrievePassword(cliCommand: CliCommand): Secret? {
|
||||||
var password: Secret? = null
|
var password: Secret? = null
|
||||||
if (cliCommand.isValidRemote()) {
|
if (cliCommand.isValidRemote()) {
|
||||||
if (cliCommand.sshWithPasswordPrompt) {
|
if (cliCommand.sshWithPasswordPrompt) {
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package org.domaindrivenarchitecture.provs.core.cli
|
||||||
|
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockkStatic
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.domaindrivenarchitecture.provs.core.Prov
|
||||||
|
import org.domaindrivenarchitecture.provs.core.Secret
|
||||||
|
import org.domaindrivenarchitecture.provs.core.local
|
||||||
|
import org.domaindrivenarchitecture.provs.core.processors.PrintOnlyProcessor
|
||||||
|
import org.domaindrivenarchitecture.provs.core.remote
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
internal class CliCommandKtTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun createProvInstance_local() {
|
||||||
|
mockkStatic(::local)
|
||||||
|
|
||||||
|
// given
|
||||||
|
val cliCommand = CliCommand(true, null, null, false, null, false)
|
||||||
|
|
||||||
|
// when
|
||||||
|
createProvInstance(cliCommand)
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify { local() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun createProvInstance_remote_with_sshKey() {
|
||||||
|
mockkStatic(::remote)
|
||||||
|
every { remote(any(), any(), any(), any()) } returns Prov.newInstance(PrintOnlyProcessor())
|
||||||
|
|
||||||
|
// given
|
||||||
|
val cliCommand = CliCommand(false, "host123", "user123", false, null, true)
|
||||||
|
|
||||||
|
// when
|
||||||
|
createProvInstance(cliCommand)
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify { remote("host123", "user123", null, any()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun createProvInstance_remote_with_interactive_password_retrieval() {
|
||||||
|
mockkStatic(::remote)
|
||||||
|
every { remote(any(), any(), any(), any()) } returns Prov.newInstance(PrintOnlyProcessor())
|
||||||
|
|
||||||
|
mockkStatic(::retrievePassword)
|
||||||
|
every { retrievePassword(any()) } returns Secret("sec")
|
||||||
|
|
||||||
|
// given
|
||||||
|
val cliCommand = CliCommand(false, "host123", "user123", true, null, false)
|
||||||
|
|
||||||
|
// when
|
||||||
|
createProvInstance(cliCommand)
|
||||||
|
|
||||||
|
// then
|
||||||
|
verify { remote("host123", "user123", Secret("sec"), any()) }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue