Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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   ASSERT_EQ(hn, exec("hostname")) ;
48
49         
50 }
51
52 TEST(Hostname, short) {
53   std::string shn = ceph_get_short_hostname();
54   ASSERT_EQ(shn, exec("hostname -s")) ;
55 }