From 744a4b2a863a14776ac95b8bd852695f1c3c95f9 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Tue, 7 Jun 2022 16:58:17 +0200 Subject: [PATCH] add findIpForHostname --- .../provs/framework/ubuntu/web/base/Web.kt | 31 ++++++++++- .../framework/ubuntu/web/base/WebKtTest.kt | 52 +++++++++++++++++-- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt index 0ae428b..d7cf432 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt @@ -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.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.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.install.base.aptInstall +import java.net.Inet4Address +import java.net.Inet6Address +import java.net.InetAddress +import java.net.UnknownHostException /** @@ -53,4 +57,27 @@ fun Prov.downloadFromURL( } else { ProvResult(true, out = "No sha256sum check requested.") } -} \ No newline at end of file +} + + +/** + * 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") +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt index e323ccd..d6f02a6 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt @@ -1,8 +1,9 @@ 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.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.tags.ContainerTest import org.junit.jupiter.api.Assertions.* @@ -56,18 +57,59 @@ internal class WebKtTest { @Test fun downloadFromURL_local_file_with_incorrect_checksum() { // given - val a = defaultTestContainer() + val prov = defaultTestContainer() val srcFile = "file3.txt" val targetFile = "file3b.txt" - a.createFile("/tmp/" + srcFile, "hello") + prov.createFile("/tmp/" + srcFile, "hello") // 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 - val res2 = a.checkFile("tmp/$targetFile") + val res2 = prov.checkFile("tmp/$targetFile") assertFalse(res.success) 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 { + // when + prov.getIpForHostname("hostwhichisnotexisting") + } + + // then + assertEquals("Could not resolve ip for: hostwhichisnotexisting", exception.message) + } } \ No newline at end of file