fix flaky findIpForHostname function

This commit is contained in:
ansgarz 2022-06-14 19:22:04 +02:00
parent f0636067ef
commit e2317b323e
2 changed files with 54 additions and 12 deletions

View file

@ -7,10 +7,7 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.creat
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.normalizePath
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
import java.util.regex.Pattern
/**
@ -64,15 +61,10 @@ fun Prov.downloadFromURL(
* 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()
val ipText = cmd("dig +short $hostname").out?.trim() ?: ""
// check if ipText is valid
return try {
val ip = InetAddress.getByName(ipText ?: "")
return if (ip is Inet4Address || ip is Inet6Address) ipText else null
} catch (exception: UnknownHostException) {
null
}
return if (isIp4(ipText) || isIp6(ipText)) ipText else null
}
/**
@ -81,3 +73,24 @@ fun Prov.findIpForHostname(hostname: String): String? {
fun Prov.getIpForHostname(hostname: String): String {
return findIpForHostname(hostname) ?: throw RuntimeException("Could not resolve ip for: $hostname")
}
internal fun isIp4(ip: String): Boolean {
val IPV4_PATTERN = "^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!\$)|\$)){4}\$"
val pattern = Pattern.compile(IPV4_PATTERN)
val matcher = pattern.matcher(ip)
return matcher.matches()
}
internal fun isIp6(ip: String): Boolean {
val IPV6_PATTERN = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}" +
"|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}" +
"|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)" +
"|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]" +
"|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
val pattern = Pattern.compile(IPV6_PATTERN)
val matcher = pattern.matcher(ip)
return matcher.matches()
}

View file

@ -8,6 +8,7 @@ import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.domaindrivenarchitecture.provs.test.tags.NonCi
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
internal class WebKtTest {
@ -112,4 +113,32 @@ internal class WebKtTest {
// then
assertEquals("Could not resolve ip for: hostwhichisnotexisting", exception.message)
}
@Test
fun isIp4_recognizes_Ip_correctly() {
// when
val res1 = isIp4("123.123.123.123")
val res2 = isIp4("123.abc.123.123")
val res3 = isIp4("")
// then
assertTrue(res1)
assertFalse(res2)
assertFalse(res3)
}
@Test
fun isIp6_recognizes_Ip_correctly() {
// when
val res1 = isIp6("2001:db8:3333:4444:5555:6666:7777:8888")
val res2 = isIp6("2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF")
val res3 = isIp6("123.123.123.123")
val res4 = isIp6("")
// then
assertTrue(res1)
assertTrue(res2)
assertFalse(res3)
assertFalse(res4)
}
}