summaryrefslogtreecommitdiff
path: root/src/test/test_mutate.cc
blob: a9dd891233e83cb4b55ac9cdf598161afe787131 (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
// -*- 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.
 *
 */

/*
 * Test Ioctx::operate
 */

#include "common/ceph_argparse.h"
#include "common/debug.h"
#include "common/config.h"
#include "global/global_context.h"
#include "global/global_init.h"
#include "include/rados/librados.hpp"
#include "include/types.h"

#include <errno.h>
#include <iostream>
#include <string>

using std::cerr;
using std::string;

using namespace librados;

static void usage(void)
{
  cerr << "--oid           set object id to 'operate' on" << std::endl;
  cerr << "--pool          set pool to 'operate' on" << std::endl;
}

int main(int argc, const char **argv)
{
  int ret = 0;
  vector<const char*> args;
  argv_to_vec(argc, argv, args);
  env_to_vec(args);
  global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0);
  common_init_finish(g_ceph_context);

  string val;
  string oid("test_object");
  string pool_name("test_pool");
  for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
    if (ceph_argparse_double_dash(args, i)) {
      break;
    }
    else if (ceph_argparse_witharg(args, i, &val, "--oid", "-o", (char*)NULL)) {
      oid = val;
    }
    else if (ceph_argparse_witharg(args, i, &val, "--pool", "-p", (char*)NULL)) {
      pool_name = val;
    }
    else {
      cerr << "unknown command line option: " << *i << std::endl;
      cerr << std::endl;
      usage();
      return 2;
    }
  }

  Rados rados;
  if (rados.init_with_context(g_ceph_context) < 0) {
     cerr << "couldn't initialize rados!" << std::endl;
     return 1;
  }
  if (rados.conf_read_file(NULL) < 0) {
     cerr << "failed to read rados configuration file!" << std::endl;
     return 1;
  }
  if (rados.connect() < 0) {
     cerr << "couldn't connect to cluster!" << std::endl;
     return 1;
  }

  librados::ObjectWriteOperation o;
  IoCtx ioctx;
  if (rados.pool_lookup(pool_name.c_str()) <= 0) {
    ret = rados.pool_create(pool_name.c_str());
    if (ret) {
       cerr << "failed to create pool named '" << pool_name
	    << "': error " << ret << std::endl;
       return 1;
    }
  }
  ret = rados.ioctx_create(pool_name.c_str(), ioctx);
  if (ret) {
     cerr << "failed to create ioctx for pool '" << pool_name
	  << "': error " << ret << std::endl;
     return 1;
  }
  librados::ObjectWriteOperation op;
  op.create(true);
  ret = ioctx.operate(oid, &op);
  if (ret) {
     cerr << "ioctx.operate failed: ret = " << ret << std::endl;
     return 1;
  }

  return 0;
}