add findIpForHostname
This commit is contained in:
parent
7157dc57dd
commit
744a4b2a86
2 changed files with 76 additions and 7 deletions
|
@ -2,11 +2,15 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.web.base
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.Prov
|
import org.domaindrivenarchitecture.provs.framework.core.Prov
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
|
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
|
||||||
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.normalizePath
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.normalizePath
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
|
||||||
|
import java.net.Inet4Address
|
||||||
|
import java.net.Inet6Address
|
||||||
|
import java.net.InetAddress
|
||||||
|
import java.net.UnknownHostException
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,3 +58,26 @@ fun Prov.downloadFromURL(
|
||||||
ProvResult(true, out = "No sha256sum check requested.")
|
ProvResult(true, out = "No sha256sum check requested.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the ip for the given hostname if found else null
|
||||||
|
*/
|
||||||
|
fun Prov.findIpForHostname(hostname: String): String? {
|
||||||
|
val ipText = cmd("dig +short $hostname").out?.trim()
|
||||||
|
|
||||||
|
// check if ipText is valid
|
||||||
|
return try {
|
||||||
|
val ip: Any = InetAddress.getByName(ipText ?: "")
|
||||||
|
return if (ip is Inet4Address || ip is Inet6Address) ipText else null
|
||||||
|
} catch (exception: UnknownHostException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the ip for the given hostname if found else throws a RuntimeExeption
|
||||||
|
*/
|
||||||
|
fun Prov.getIpForHostname(hostname: String): String {
|
||||||
|
return findIpForHostname(hostname) ?: throw RuntimeException("Could not resolve ip for: $hostname")
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package org.domaindrivenarchitecture.provs.framework.ubuntu.web.base
|
package org.domaindrivenarchitecture.provs.framework.ubuntu.web.base
|
||||||
|
|
||||||
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContent
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContent
|
||||||
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
|
||||||
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
||||||
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
|
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
|
||||||
import org.junit.jupiter.api.Assertions.*
|
import org.junit.jupiter.api.Assertions.*
|
||||||
|
@ -56,18 +57,59 @@ internal class WebKtTest {
|
||||||
@Test
|
@Test
|
||||||
fun downloadFromURL_local_file_with_incorrect_checksum() {
|
fun downloadFromURL_local_file_with_incorrect_checksum() {
|
||||||
// given
|
// given
|
||||||
val a = defaultTestContainer()
|
val prov = defaultTestContainer()
|
||||||
val srcFile = "file3.txt"
|
val srcFile = "file3.txt"
|
||||||
val targetFile = "file3b.txt"
|
val targetFile = "file3b.txt"
|
||||||
a.createFile("/tmp/" + srcFile, "hello")
|
prov.createFile("/tmp/" + srcFile, "hello")
|
||||||
|
|
||||||
// when
|
// when
|
||||||
val res = a.downloadFromURL("file:///tmp/" + srcFile, targetFile, "tmp", sha256sum = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824WRONG", overwrite = true)
|
val res = prov.downloadFromURL("file:///tmp/" + srcFile, targetFile, "tmp", sha256sum = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824WRONG", overwrite = true)
|
||||||
|
|
||||||
// then
|
// then
|
||||||
val res2 = a.checkFile("tmp/$targetFile")
|
val res2 = prov.checkFile("tmp/$targetFile")
|
||||||
|
|
||||||
assertFalse(res.success)
|
assertFalse(res.success)
|
||||||
assertFalse(res2)
|
assertFalse(res2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ContainerTest
|
||||||
|
fun findIpForHostname_finds_ip() {
|
||||||
|
// given
|
||||||
|
val prov = defaultTestContainer()
|
||||||
|
prov.aptInstall("dnsutils")
|
||||||
|
|
||||||
|
// when
|
||||||
|
val ip = prov.findIpForHostname("localhost")
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals("127.0.0.1", ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ContainerTest
|
||||||
|
fun findIpForHostname_returns_null_if_ip_not_found() {
|
||||||
|
// given
|
||||||
|
val prov = defaultTestContainer()
|
||||||
|
prov.aptInstall("dnsutils")
|
||||||
|
|
||||||
|
// when
|
||||||
|
val ip = prov.findIpForHostname("hostwhichisnotexisting")
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(null, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getIpForHostname_throws_exception_if_ip_not_found() {
|
||||||
|
// given
|
||||||
|
val prov = defaultTestContainer()
|
||||||
|
prov.aptInstall("dnsutils")
|
||||||
|
|
||||||
|
val exception = org.junit.jupiter.api.assertThrows<RuntimeException> {
|
||||||
|
// when
|
||||||
|
prov.getIpForHostname("hostwhichisnotexisting")
|
||||||
|
}
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals("Could not resolve ip for: hostwhichisnotexisting", exception.message)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue