Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / libcephfs / readdir_r_cb.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 "gtest/gtest.h"
16 #include "include/cephfs/libcephfs.h"
17 #include <errno.h>
18 #include <fcntl.h>
19
20 TEST(LibCephFS, ReaddirRCB) {
21   struct ceph_mount_info *cmount;
22   ASSERT_EQ(0, ceph_create(&cmount, NULL));
23   ASSERT_EQ(0, ceph_conf_read_file(cmount, NULL));
24   ASSERT_EQ(0, ceph_mount(cmount, "/"));
25
26   char c_dir[256];
27   sprintf(c_dir, "/readdir_r_cb_tests_%d", getpid());
28   struct ceph_dir_result *dirp;
29   ASSERT_EQ(0, ceph_mkdirs(cmount, c_dir, 0777));
30   ASSERT_LE(0, ceph_opendir(cmount, c_dir, &dirp));
31
32   // dir is empty, check that it only contains . and ..
33   int buflen = 100;
34   char *buf = new char[buflen];
35   // . is 2, .. is 3 (for null terminators)
36   ASSERT_EQ(5, ceph_getdnames(cmount, dirp, buf, buflen));
37   char c_file[256];
38   sprintf(c_file, "/readdir_r_cb_tests_%d/foo", getpid());
39   int fd = ceph_open(cmount, c_file, O_CREAT, 0777);
40   ASSERT_LT(0, fd);
41   ASSERT_EQ(0, ceph_close(cmount, fd));
42
43   // check correctness with one entry
44   ASSERT_LE(0, ceph_closedir(cmount, dirp));
45   ASSERT_LE(0, ceph_opendir(cmount, c_dir, &dirp));
46   ASSERT_EQ(9, ceph_getdnames(cmount, dirp, buf, buflen)); // ., .., foo
47
48   // check correctness if buffer is too small
49   ASSERT_LE(0, ceph_closedir(cmount, dirp));
50   ASSERT_GE(0, ceph_opendir(cmount, c_dir, &dirp));
51   ASSERT_EQ(-ERANGE, ceph_getdnames(cmount, dirp, buf, 1));
52
53   //check correctness if it needs to split listing
54   ASSERT_LE(0, ceph_closedir(cmount, dirp));
55   ASSERT_LE(0, ceph_opendir(cmount, c_dir, &dirp));
56   ASSERT_EQ(5, ceph_getdnames(cmount, dirp, buf, 6));
57   ASSERT_EQ(4, ceph_getdnames(cmount, dirp, buf, 6));
58
59   // free cmount after finishing testing
60   ASSERT_LE(0, ceph_closedir(cmount, dirp));
61   ASSERT_EQ(0, ceph_unmount(cmount));
62   ASSERT_EQ(0, ceph_release(cmount));
63 }