summaryrefslogtreecommitdiffstats
path: root/src/test/librados/service_cxx.cc
blob: 40869f0f8d9ff5436e08d85afa0f489312ee4a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <algorithm>
#include <thread>
#include <errno.h>
#include "gtest/gtest.h"

#include "include/rados/librados.hpp"
#include "include/stringify.h"
#include "common/config_proxy.h"
#include "test/librados/test_cxx.h"
#include "test/librados/testcase_cxx.h"
#include "test/unit.cc"

using namespace librados;

TEST(LibRadosServicePP, RegisterEarly) {
  Rados cluster;
  cluster.init("admin");
  ASSERT_EQ(0, cluster.conf_read_file(NULL));
  cluster.conf_parse_env(NULL);
  string name = string("pid") + stringify(getpid());
  ASSERT_EQ(0, cluster.service_daemon_register(
	      "laundry", name, {{"foo", "bar"}, {"this", "that"}}));
  ASSERT_EQ(-EEXIST, cluster.service_daemon_register(
	      "laundry", name, {{"foo", "bar"}, {"this", "that"}}));
  ASSERT_EQ(0, cluster.connect());
  sleep(5);
  cluster.shutdown();
}

TEST(LibRadosServicePP, RegisterLate) {
  Rados cluster;
  cluster.init("admin");
  ASSERT_EQ(0, cluster.conf_read_file(NULL));
  cluster.conf_parse_env(NULL);
  ASSERT_EQ("", connect_cluster_pp(cluster));
  string name = string("pid") + stringify(getpid());
  ASSERT_EQ(0, cluster.service_daemon_register(
	      "laundry", name, {{"foo", "bar"}, {"this", "that"}}));
}

TEST(LibRadosServicePP, Status) {
  Rados cluster;
  cluster.init("admin");
  ASSERT_EQ(0, cluster.conf_read_file(NULL));
  cluster.conf_parse_env(NULL);
  string name = string("pid") + stringify(getpid());
  ASSERT_EQ(-ENOTCONN, cluster.service_daemon_update_status(
	      {{"testing", "starting"}}));
  ASSERT_EQ(0, cluster.connect());
  ASSERT_EQ(0, cluster.service_daemon_register(
	      "laundry", name, {{"foo", "bar"}, {"this", "that"}}));
  for (int i=0; i<20; ++i) {
    ASSERT_EQ(0, cluster.service_daemon_update_status({
	  {"testing", "running"},
	  {"count", stringify(i)}
	}));
    sleep(1);
  }
  cluster.shutdown();
}

TEST(LibRadosServicePP, Close) {
  int tries = 20;
  string name = string("close-test-pid") + stringify(getpid());
  int i;
  for (i = 0; i < tries; ++i) {
    cout << "attempt " << i << " of " << tries << std::endl;
    {
      Rados cluster;
      cluster.init("admin");
      ASSERT_EQ(0, cluster.conf_read_file(NULL));
      cluster.conf_parse_env(NULL);
      ASSERT_EQ(0, cluster.connect());
      ASSERT_EQ(0, cluster.service_daemon_register(
		  "laundry", name, {{"foo", "bar"}, {"this", "that"}}));
      sleep(3); // let it register
      cluster.shutdown();
    }
    // mgr updates servicemap every tick
    //sleep(g_conf().get_val<int64_t>("mgr_tick_period"));
    std::this_thread::sleep_for(g_conf().get_val<std::chrono::seconds>(
				  "mgr_tick_period"));
    // make sure we are deregistered
    {
      Rados cluster;
      cluster.init("admin");
      ASSERT_EQ(0, cluster.conf_read_file(NULL));
      cluster.conf_parse_env(NULL);
      ASSERT_EQ(0, cluster.connect());
      bufferlist inbl, outbl;
      ASSERT_EQ(0, cluster.mon_command("{\"prefix\": \"service dump\"}",
				       inbl, &outbl, NULL));
      string s = outbl.to_str();
      cluster.shutdown();

      if (s.find(name) != string::npos) {
	cout << " failed to deregister:\n" << s << std::endl;
      } else {
	break;
      }
    }
  }
  ASSERT_LT(i, tries);
}