Added a utility function to get back non-loopback address on the host. Review: https://reviews.apache.org/r/57628/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/b61cab7f Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/b61cab7f Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/b61cab7f Branch: refs/heads/master Commit: b61cab7ff496401ec84b63865093eeb3c75b6444 Parents: 02b9cd0 Author: Avinash sridharan <[email protected]> Authored: Sun Mar 19 09:09:12 2017 -0700 Committer: Jie Yu <[email protected]> Committed: Sun Mar 19 09:09:12 2017 -0700 ---------------------------------------------------------------------- src/tests/utils.cpp | 28 ++++++++++++++++++++++++++++ src/tests/utils.hpp | 4 ++++ 2 files changed, 32 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/b61cab7f/src/tests/utils.cpp ---------------------------------------------------------------------- diff --git a/src/tests/utils.cpp b/src/tests/utils.cpp index bc5c8ab..3190daa 100644 --- a/src/tests/utils.cpp +++ b/src/tests/utils.cpp @@ -35,6 +35,7 @@ namespace http = process::http; namespace inet = process::network::inet; +using std::set; using std::string; using process::Future; @@ -183,6 +184,33 @@ string getWebUIDir() return path; } + +Try<net::IPNetwork> getNonLoopbackIP() +{ + Try<set<string>> links = net::links(); + if (links.isError()) { + return Error( + "Unable to retrieve interfaces on this host: " + links.error()); + } + + foreach (const string& link, links.get()) { + Result<net::IPNetwork> hostIPNetwork = + net::IPNetwork::fromLinkDevice(link, AF_INET); + + if (hostIPNetwork.isError()) { + return Error( + "Unable to find a non-loopback address: " + hostIPNetwork.error()); + } + + if (hostIPNetwork.isSome() && + (hostIPNetwork.get() != net::IPNetwork::LOOPBACK_V4())) { + return hostIPNetwork.get(); + } + } + + return Error("No non-loopback addresses available on this host"); +} + } // namespace tests { } // namespace internal { } // namespace mesos { http://git-wip-us.apache.org/repos/asf/mesos/blob/b61cab7f/src/tests/utils.hpp ---------------------------------------------------------------------- diff --git a/src/tests/utils.hpp b/src/tests/utils.hpp index cdbbc1c..a2ae43c 100644 --- a/src/tests/utils.hpp +++ b/src/tests/utils.hpp @@ -21,6 +21,7 @@ #include <string> +#include <stout/ip.hpp> #include <stout/json.hpp> #include <stout/try.hpp> @@ -81,6 +82,9 @@ std::string getWebUIDir(); // caller can bind to it. Try<uint16_t> getFreePort(); +// Get a non-loopback IP available on this host. +Try<net::IPNetwork> getNonLoopbackIP(); + } // namespace tests { } // namespace internal { } // namespace mesos {
