From e583f80086d6afb960bbb44b85fe4b2dc73be549 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Wed, 29 Aug 2018 08:21:32 -0500 Subject: [PATCH] common/hostname: use NODE_NAME environment variable insetad, if set This is set by rook to the physical node. Signed-off-by: Sage Weil --- src/common/hostname.cc | 6 ++++++ src/test/common/test_hostname.cc | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/common/hostname.cc b/src/common/hostname.cc index 32436534ea70..879fc93969ee 100644 --- a/src/common/hostname.cc +++ b/src/common/hostname.cc @@ -18,6 +18,12 @@ std::string ceph_get_hostname() { + // are we in a container? if so we would prefer the *real* hostname. + const char *node_name = getenv("NODE_NAME"); + if (node_name) { + return node_name; + } + char buf[1024]; gethostname(buf, 1024); return std::string(buf); diff --git a/src/test/common/test_hostname.cc b/src/test/common/test_hostname.cc index 54f5e3e23343..4f9aeef4d29d 100644 --- a/src/test/common/test_hostname.cc +++ b/src/test/common/test_hostname.cc @@ -44,12 +44,24 @@ std::string exec(const char* cmd) { TEST(Hostname, full) { std::string hn = ceph_get_hostname(); - ASSERT_EQ(hn, exec("hostname")) ; - - + if (const char *nn = getenv("NODE_NAME")) { + // we are in a container + std::cout << "we are in a container on " << nn << ", reporting " << hn + << std::endl; + ASSERT_EQ(hn, nn); + } else { + ASSERT_EQ(hn, exec("hostname")) ; + } } TEST(Hostname, short) { std::string shn = ceph_get_short_hostname(); - ASSERT_EQ(shn, exec("hostname -s")) ; + if (const char *nn = getenv("NODE_NAME")) { + // we are in a container + std::cout << "we are in a container on " << nn << ", reporting short " << shn + << ", skipping test because env var may or may not be short form" + << std::endl; + } else { + ASSERT_EQ(shn, exec("hostname -s")) ; + } } -- 2.47.3