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,11 +93,25 @@ 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!!
|
} else {
|
||||||
|
throw IllegalArgumentException("Error: neither a valid localHost nor a valid remoteHost was specified! Use option -h for help.")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println("Invalid command line options.\nPlease use option -h for help.")
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createProvInstanceRemote(
|
||||||
|
host: String,
|
||||||
|
remoteUser: String,
|
||||||
|
sshWithKey: Boolean,
|
||||||
|
password: Secret?,
|
||||||
|
remoteHostSetSudoWithoutPasswordRequired: Boolean
|
||||||
|
): Prov {
|
||||||
val prov =
|
val prov =
|
||||||
if (cliCommand.sshWithKey) {
|
if (sshWithKey) {
|
||||||
remote(host, remoteUser)
|
remote(host, remoteUser)
|
||||||
} else {
|
} else {
|
||||||
require(
|
require(
|
||||||
|
@ -117,17 +131,10 @@ internal fun createProvInstance(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return prov
|
return prov
|
||||||
} else {
|
|
||||||
throw IllegalArgumentException("Error: neither a valid localHost nor a valid remoteHost was specified! Use option -h for help.")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println("Invalid command line options.\nPlease use option -h for help.")
|
|
||||||
exitProcess(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun retrievePassword(cliCommand: CliCommand): Secret? {
|
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