Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / test_mutate.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) 2011 New Dream Network
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 /*
16  * Test Ioctx::operate
17  */
18
19 #include "common/ceph_argparse.h"
20 #include "common/debug.h"
21 #include "common/config.h"
22 #include "global/global_init.h"
23 #include "include/rados/librados.hpp"
24 #include "include/types.h"
25
26 #include <errno.h>
27 #include <iostream>
28 #include <string>
29
30 using std::cerr;
31 using std::string;
32
33 using namespace librados;
34
35 static void usage(void)
36 {
37   cerr << "--oid           set object id to 'operate' on" << std::endl;
38   cerr << "--pool          set pool to 'operate' on" << std::endl;
39 }
40
41 int main(int argc, const char **argv)
42 {
43   int ret = 0;
44   vector<const char*> args;
45   argv_to_vec(argc, argv, args);
46   env_to_vec(args);
47   auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
48                          CODE_ENVIRONMENT_UTILITY, 0);
49   common_init_finish(g_ceph_context);
50
51   string val;
52   string oid("ceph_test_object");
53   string pool_name("test_pool");
54   for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
55     if (ceph_argparse_double_dash(args, i)) {
56       break;
57     }
58     else if (ceph_argparse_witharg(args, i, &val, "--oid", "-o", (char*)NULL)) {
59       oid = val;
60     }
61     else if (ceph_argparse_witharg(args, i, &val, "--pool", "-p", (char*)NULL)) {
62       pool_name = val;
63     }
64     else {
65       cerr << "unknown command line option: " << *i << std::endl;
66       cerr << std::endl;
67       usage();
68       return 2;
69     }
70   }
71
72   Rados rados;
73   if (rados.init_with_context(g_ceph_context) < 0) {
74      cerr << "couldn't initialize rados!" << std::endl;
75      return 1;
76   }
77   if (rados.conf_read_file(NULL) < 0) {
78      cerr << "failed to read rados configuration file!" << std::endl;
79      return 1;
80   }
81   if (rados.connect() < 0) {
82      cerr << "couldn't connect to cluster!" << std::endl;
83      return 1;
84   }
85
86   librados::ObjectWriteOperation o;
87   IoCtx ioctx;
88   if (rados.pool_lookup(pool_name.c_str()) <= 0) {
89     ret = rados.pool_create(pool_name.c_str());
90     if (ret) {
91        cerr << "failed to create pool named '" << pool_name
92             << "': error " << ret << std::endl;
93        return 1;
94     }
95   }
96   ret = rados.ioctx_create(pool_name.c_str(), ioctx);
97   if (ret) {
98      cerr << "failed to create ioctx for pool '" << pool_name
99           << "': error " << ret << std::endl;
100      return 1;
101   }
102   ioctx.application_enable("rados", true);
103
104   librados::ObjectWriteOperation op;
105   op.create(true);
106   ret = ioctx.operate(oid, &op);
107   if (ret) {
108      cerr << "ioctx.operate failed: ret = " << ret << std::endl;
109      return 1;
110   }
111
112   return 0;
113 }