]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_hostname.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "gtest/gtest.h"
16 #include "common/hostname.h"
17 #include "common/SubProcess.h"
19 #include <sys/types.h>
32 std::string
exec(const char* cmd
) {
33 std::array
<char, 128> buffer
;
35 std::shared_ptr
<FILE> pipe(popen(cmd
, "r"), pclose
);
36 if (!pipe
) throw std::runtime_error("popen() failed!");
37 while (!feof(pipe
.get())) {
38 if (fgets(buffer
.data(), 128, pipe
.get()) != NULL
)
39 result
+= buffer
.data();
42 return result
.substr(0, result
.size()-1);;
45 TEST(Hostname
, full
) {
46 std::string hn
= ceph_get_hostname();
47 if (const char *nn
= getenv("NODE_NAME")) {
48 // we are in a container
49 std::cout
<< "we are in a container on " << nn
<< ", reporting " << hn
53 ASSERT_EQ(hn
, exec("hostname")) ;
57 TEST(Hostname
, short) {
58 std::string shn
= ceph_get_short_hostname();
59 if (const char *nn
= getenv("NODE_NAME")) {
60 // we are in a container
61 std::cout
<< "we are in a container on " << nn
<< ", reporting short " << shn
62 << ", skipping test because env var may or may not be short form"
65 ASSERT_EQ(shn
, exec("hostname -s")) ;