]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_hostname.cc
update sources to 12.2.10
[ceph.git] / 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
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15 #include "gtest/gtest.h"
16 #include "common/hostname.h"
17 #include "common/SubProcess.h"
18 #include "stdio.h"
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22
23 #include "unistd.h"
24
25 #include <array>
26 #include <iostream>
27 #include <stdexcept>
28 #include <stdio.h>
29 #include <string>
30 #include <memory>
31
32 std::string exec(const char* cmd) {
33 std::array<char, 128> buffer;
34 std::string result;
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();
40 }
41 // remove \n
42 return result.substr(0, result.size()-1);;
43 }
44
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
50 << std::endl;
51 ASSERT_EQ(hn, nn);
52 } else {
53 ASSERT_EQ(hn, exec("hostname")) ;
54 }
55 }
56
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"
63 << std::endl;
64 } else {
65 ASSERT_EQ(shn, exec("hostname -s")) ;
66 }
67 }