Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / system / st_rados_list_objects.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 #include "cross_process_sem.h"
16 #include "include/rados/librados.h"
17 #include "st_rados_list_objects.h"
18 #include "systest_runnable.h"
19 #include "systest_settings.h"
20
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sstream>
26 #include <string>
27
28 using std::ostringstream;
29
30 StRadosListObjects::
31 StRadosListObjects(int argc, const char **argv,
32                    const std::string &pool_name,
33                    bool accept_list_errors,
34                    int midway_cnt,
35                    CrossProcessSem *pool_setup_sem,
36                    CrossProcessSem *midway_sem_wait,
37                    CrossProcessSem *midway_sem_post)
38   : SysTestRunnable(argc, argv),
39     m_pool_name(pool_name),
40     m_accept_list_errors(accept_list_errors),
41     m_midway_cnt(midway_cnt),
42     m_pool_setup_sem(pool_setup_sem),
43     m_midway_sem_wait(midway_sem_wait),
44     m_midway_sem_post(midway_sem_post)
45 {
46 }
47
48 StRadosListObjects::
49 ~StRadosListObjects()
50 {
51 }
52
53 int StRadosListObjects::
54 run()
55 {
56   int retval = 0;
57   rados_t cl;
58   RETURN1_IF_NONZERO(rados_create(&cl, NULL));
59   rados_conf_parse_argv(cl, m_argc, m_argv);
60   RETURN1_IF_NONZERO(rados_conf_read_file(cl, NULL));
61   rados_conf_parse_env(cl, NULL);
62   RETURN1_IF_NONZERO(rados_connect(cl));
63   m_pool_setup_sem->wait();
64   m_pool_setup_sem->post();
65
66   rados_ioctx_t io_ctx;
67   rados_pool_create(cl, m_pool_name.c_str());
68   RETURN1_IF_NONZERO(rados_ioctx_create(cl, m_pool_name.c_str(), &io_ctx));
69
70   int saw = 0;
71   const char *obj_name;
72   rados_list_ctx_t h;
73   printf("%s: listing objects.\n", get_id_str());
74   RETURN1_IF_NONZERO(rados_nobjects_list_open(io_ctx, &h));
75   while (true) {
76     int ret = rados_nobjects_list_next(h, &obj_name, NULL, NULL);
77     if (ret == -ENOENT) {
78       break;
79     }
80     else if (ret != 0) {
81       if (m_accept_list_errors && (!m_midway_sem_post || saw > m_midway_cnt))
82         break;
83       printf("%s: rados_objects_list_next error: %d\n", get_id_str(), ret);
84       retval = ret;
85       goto out;
86     }
87     if ((saw % 25) == 0) {
88       printf("%s: listed object %d...\n", get_id_str(), saw);
89     }
90     ++saw;
91     if (saw == m_midway_cnt) {
92       if (m_midway_sem_wait)
93         m_midway_sem_wait->wait();
94       if (m_midway_sem_post)
95         m_midway_sem_post->post();
96     }
97   }
98
99   printf("%s: saw %d objects\n", get_id_str(), saw);
100
101 out:
102   rados_nobjects_list_close(h);
103   rados_ioctx_destroy(io_ctx);
104   rados_shutdown(cl);
105
106   return retval;
107 }