Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / cls_hello / test_cls_hello.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) 2013 Inktank
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 <iostream>
16 #include <errno.h>
17
18 #include "include/rados/librados.hpp"
19 #include "include/encoding.h"
20 #include "test/librados/test.h"
21 #include "gtest/gtest.h"
22
23 using namespace librados;
24
25 TEST(ClsHello, SayHello) {
26   Rados cluster;
27   std::string pool_name = get_temp_pool_name();
28   ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
29   IoCtx ioctx;
30   cluster.ioctx_create(pool_name.c_str(), ioctx);
31
32   bufferlist in, out;
33   ASSERT_EQ(-ENOENT, ioctx.exec("myobject", "hello", "say_hello", in, out));
34   ASSERT_EQ(0, ioctx.write_full("myobject", in));
35   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "say_hello", in, out));
36   ASSERT_EQ(std::string("Hello, world!"), std::string(out.c_str(), out.length()));
37
38   out.clear();
39   in.append("Tester");
40   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "say_hello", in, out));
41   ASSERT_EQ(std::string("Hello, Tester!"), std::string(out.c_str(), out.length()));
42
43   out.clear();
44   in.clear();
45   char buf[4096];
46   memset(buf, 1, sizeof(buf));
47   in.append(buf, sizeof(buf));
48   ASSERT_EQ(-EINVAL, ioctx.exec("myobject", "hello", "say_hello", in, out));
49
50   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
51 }
52
53 TEST(ClsHello, RecordHello) {
54   Rados cluster;
55   std::string pool_name = get_temp_pool_name();
56   ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
57   IoCtx ioctx;
58   cluster.ioctx_create(pool_name.c_str(), ioctx);
59
60   bufferlist in, out;
61   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "record_hello", in, out));
62   ASSERT_EQ(-EEXIST, ioctx.exec("myobject", "hello", "record_hello", in, out));
63
64   in.append("Tester");
65   ASSERT_EQ(0, ioctx.exec("myobject2", "hello", "record_hello", in, out));
66   ASSERT_EQ(-EEXIST, ioctx.exec("myobject2", "hello", "record_hello", in, out));
67   ASSERT_EQ(0u, out.length());
68
69   in.clear();
70   out.clear();
71   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "replay", in, out));
72   ASSERT_EQ(std::string("Hello, world!"), std::string(out.c_str(), out.length()));
73   out.clear();
74   ASSERT_EQ(0, ioctx.exec("myobject2", "hello", "replay", in, out));
75   ASSERT_EQ(std::string("Hello, Tester!"), std::string(out.c_str(), out.length()));
76
77   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
78 }
79
80 TEST(ClsHello, WriteReturnData) {
81   Rados cluster;
82   std::string pool_name = get_temp_pool_name();
83   ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
84   IoCtx ioctx;
85   cluster.ioctx_create(pool_name.c_str(), ioctx);
86
87   bufferlist in, out;
88   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "writes_dont_return_data", in, out));
89   ASSERT_EQ(std::string(), std::string(out.c_str(), out.length()));
90
91   char buf[4096];
92   memset(buf, 1, sizeof(buf));
93   in.append(buf, sizeof(buf));
94   ASSERT_EQ(-EINVAL, ioctx.exec("myobject2", "hello", "writes_dont_return_data", in, out));
95   ASSERT_EQ(std::string("too much input data!"), std::string(out.c_str(), out.length()));
96   ASSERT_EQ(-ENOENT, ioctx.getxattr("myobject2", "foo", out));
97
98   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
99 }
100
101 TEST(ClsHello, Loud) {
102   Rados cluster;
103   std::string pool_name = get_temp_pool_name();
104   ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
105   IoCtx ioctx;
106   cluster.ioctx_create(pool_name.c_str(), ioctx);
107
108   bufferlist in, out;
109   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "record_hello", in, out));
110   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "replay", in, out));
111   ASSERT_EQ(std::string("Hello, world!"), std::string(out.c_str(), out.length()));
112
113   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "turn_it_to_11", in, out));
114   ASSERT_EQ(0, ioctx.exec("myobject", "hello", "replay", in, out));
115   ASSERT_EQ(std::string("HELLO, WORLD!"), std::string(out.c_str(), out.length()));
116
117   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
118 }
119
120 TEST(ClsHello, BadMethods) {
121   Rados cluster;
122   std::string pool_name = get_temp_pool_name();
123   ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
124   IoCtx ioctx;
125   cluster.ioctx_create(pool_name.c_str(), ioctx);
126
127   bufferlist in, out;
128
129   ASSERT_EQ(0, ioctx.write_full("myobject", in));
130   ASSERT_EQ(-EIO, ioctx.exec("myobject", "hello", "bad_reader", in, out));
131   ASSERT_EQ(-EIO, ioctx.exec("myobject", "hello", "bad_writer", in, out));
132
133   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
134 }
135
136 TEST(ClsHello, Filter) {
137   Rados cluster;
138   std::string pool_name = get_temp_pool_name();
139   ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
140   IoCtx ioctx;
141   cluster.ioctx_create(pool_name.c_str(), ioctx);
142
143   char buf[128];
144   memset(buf, 0xcc, sizeof(buf));
145   bufferlist obj_content;
146   obj_content.append(buf, sizeof(buf));
147
148   std::string target_str = "content";
149
150   // Write xattr bare, no ::encod'ing
151   bufferlist target_val;
152   target_val.append(target_str);
153   bufferlist nontarget_val;
154   nontarget_val.append("rhubarb");
155
156   ASSERT_EQ(0, ioctx.write("has_xattr", obj_content, obj_content.length(), 0));
157   ASSERT_EQ(0, ioctx.write("has_wrong_xattr", obj_content, obj_content.length(), 0));
158   ASSERT_EQ(0, ioctx.write("no_xattr", obj_content, obj_content.length(), 0));
159
160   ASSERT_EQ(0, ioctx.setxattr("has_xattr", "theattr", target_val));
161   ASSERT_EQ(0, ioctx.setxattr("has_wrong_xattr", "theattr", nontarget_val));
162
163   bufferlist filter_bl;
164   std::string filter_name = "hello.hello";
165   ::encode(filter_name, filter_bl);
166   ::encode("_theattr", filter_bl);
167   ::encode(target_str, filter_bl);
168
169   NObjectIterator iter(ioctx.nobjects_begin(filter_bl));
170   bool foundit = false;
171   int k = 0;
172   while (iter != ioctx.nobjects_end()) {
173     foundit = true;
174     // We should only see the object that matches the filter
175     ASSERT_EQ((*iter).get_oid(), "has_xattr");
176     // We should only see it once
177     ASSERT_EQ(k, 0);
178     ++iter;
179     ++k;
180   }
181   ASSERT_TRUE(foundit);
182
183   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
184 }
185