summaryrefslogtreecommitdiff
path: root/src/test/admin_socket.cc
blob: 17cbeefbe85af16d01af715cdd0f5262502bfdb0 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */
#include <stdint.h>
#include <string.h>
#include <string>
#include <sys/un.h>

#include "common/Mutex.h"
#include "common/admin_socket.h"
#include "common/admin_socket_client.h"

#include "unit.h"

class AdminSocketTest
{
public:
  AdminSocketTest(AdminSocket *asokc)
    : m_asokc(asokc)
  {
  }
  bool init(const std::string &uri) {
    return m_asokc->init(uri);
  }
  bool shutdown() {
    m_asokc->shutdown();
    return true;
  }
  AdminSocket *m_asokc;
};

TEST(AdminSocket, Teardown) {
  std::auto_ptr<AdminSocket>
      asokc(new AdminSocket(g_ceph_context));
  AdminSocketTest asoct(asokc.get());
  ASSERT_EQ(true, asoct.shutdown());
}

TEST(AdminSocket, TeardownSetup) {
  std::auto_ptr<AdminSocket>
      asokc(new AdminSocket(g_ceph_context));
  AdminSocketTest asoct(asokc.get());
  ASSERT_EQ(true, asoct.shutdown());
  ASSERT_EQ(true, asoct.init(get_rand_socket_path()));
  ASSERT_EQ(true, asoct.shutdown());
}

TEST(AdminSocket, SendNoOp) {
  std::auto_ptr<AdminSocket>
      asokc(new AdminSocket(g_ceph_context));
  AdminSocketTest asoct(asokc.get());
  ASSERT_EQ(true, asoct.shutdown());
  ASSERT_EQ(true, asoct.init(get_rand_socket_path()));
  AdminSocketClient client(get_rand_socket_path());
  string version;
  ASSERT_EQ("", client.do_request("0", &version));
  ASSERT_EQ(CEPH_ADMIN_SOCK_VERSION, version);
  ASSERT_EQ(true, asoct.shutdown());
}

class MyTest : public AdminSocketHook {
  bool call(std::string command, std::string args, bufferlist& result) {
    result.append(command);
    result.append("|");
    result.append(args);
    return true;
  }
};

TEST(AdminSocket, RegisterCommand) {
  std::auto_ptr<AdminSocket>
      asokc(new AdminSocket(g_ceph_context));
  AdminSocketTest asoct(asokc.get());
  ASSERT_EQ(true, asoct.shutdown());
  ASSERT_EQ(true, asoct.init(get_rand_socket_path()));
  AdminSocketClient client(get_rand_socket_path());
  ASSERT_EQ(0, asoct.m_asokc->register_command("test", new MyTest(), ""));
  string result;
  ASSERT_EQ("", client.do_request("test", &result));
  ASSERT_EQ("test|", result);
  ASSERT_EQ(true, asoct.shutdown());
}

class MyTest2 : public AdminSocketHook {
  bool call(std::string command, std::string args, bufferlist& result) {
    result.append(command);
    result.append("|");
    result.append(args);
    return true;
  }
};

TEST(AdminSocket, RegisterCommandPrefixes) {
  std::auto_ptr<AdminSocket>
      asokc(new AdminSocket(g_ceph_context));
  AdminSocketTest asoct(asokc.get());
  ASSERT_EQ(true, asoct.shutdown());
  ASSERT_EQ(true, asoct.init(get_rand_socket_path()));
  AdminSocketClient client(get_rand_socket_path());
  ASSERT_EQ(0, asoct.m_asokc->register_command("test", new MyTest(), ""));
  ASSERT_EQ(0, asoct.m_asokc->register_command("test command", new MyTest2(), ""));
  string result;
  ASSERT_EQ("", client.do_request("test", &result));
  ASSERT_EQ("test|", result);
  ASSERT_EQ("", client.do_request("test command", &result));
  ASSERT_EQ("test command|", result);
  ASSERT_EQ("", client.do_request("test command post", &result));
  ASSERT_EQ("test command|post", result);
  ASSERT_EQ("", client.do_request("test command  post", &result));
  ASSERT_EQ("test command| post", result);
  ASSERT_EQ("", client.do_request("test this thing", &result));
  ASSERT_EQ("test|this thing", result);

  ASSERT_EQ("", client.do_request("test  command post", &result));
  ASSERT_EQ("test| command post", result);
  ASSERT_EQ("", client.do_request("test  this thing", &result));
  ASSERT_EQ("test| this thing", result);
  ASSERT_EQ(true, asoct.shutdown());
}