common/hostname: use NODE_NAME environment variable insetad, if set

This is set by rook to the physical node.

Signed-off-by: Sage Weil <sage@redhat.com>
This commit is contained in:
Sage Weil 2018-08-29 08:21:32 -05:00
parent 0acb5cb454
commit e583f80086
2 changed files with 22 additions and 4 deletions

View File

@ -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);

View File

@ -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")) ;
}
}