Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / libcephfs / caps.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 #include "include/int_types.h"
15
16 #include "gtest/gtest.h"
17 #include "include/ceph_fs.h"
18 #include "include/cephfs/libcephfs.h"
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <sys/xattr.h>
26 #include <signal.h>
27
28 TEST(Caps, ReadZero) {
29
30   int mypid = getpid();
31   struct ceph_mount_info *cmount;
32   ASSERT_EQ(0, ceph_create(&cmount, NULL));
33   ASSERT_EQ(0, ceph_conf_read_file(cmount, NULL));
34   ASSERT_EQ(0, ceph_mount(cmount, "/"));
35
36   int i = 0;
37   for(; i < 30; ++i) {
38
39     char c_path[1024];
40     sprintf(c_path, "/caps_rzfile_%d_%d", mypid, i);
41     int fd = ceph_open(cmount, c_path, O_CREAT|O_TRUNC|O_WRONLY, 0644);
42     ASSERT_LT(0, fd);
43
44     int expect = CEPH_CAP_FILE_EXCL | CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER;
45     int caps = ceph_debug_get_fd_caps(cmount, fd);
46
47     ASSERT_EQ(expect, caps & expect);
48     ASSERT_EQ(0, ceph_close(cmount, fd));
49
50     caps = ceph_debug_get_file_caps(cmount, c_path);
51     ASSERT_EQ(expect, caps & expect);
52
53     char cw_path[1024];
54     sprintf(cw_path, "/caps_wzfile_%d_%d", mypid, i);
55     int wfd = ceph_open(cmount, cw_path, O_CREAT|O_TRUNC|O_WRONLY, 0644);
56     ASSERT_LT(0, wfd);
57
58     char wbuf[4096];
59     ASSERT_EQ(4096, ceph_write(cmount, wfd, wbuf, 4096, 0));
60
61     ASSERT_EQ(0, ceph_close(cmount, wfd));
62
63     struct ceph_statx stx;
64     ASSERT_EQ(0, ceph_statx(cmount, c_path, &stx, CEPH_STATX_MTIME, 0));
65
66     caps = ceph_debug_get_file_caps(cmount, c_path);
67     ASSERT_EQ(expect, caps & expect);
68   }
69
70   ASSERT_EQ(0, ceph_conf_set(cmount, "client_debug_inject_tick_delay", "20"));
71
72   for(i = 0; i < 30; ++i) {
73
74     char c_path[1024];
75     sprintf(c_path, "/caps_rzfile_%d_%d", mypid, i);
76
77     int fd = ceph_open(cmount, c_path, O_RDONLY, 0);
78     ASSERT_LT(0, fd);
79     char buf[256];
80
81     int expect = CEPH_CAP_FILE_RD | CEPH_STAT_CAP_SIZE | CEPH_CAP_FILE_CACHE;
82     int caps = ceph_debug_get_fd_caps(cmount, fd);
83     ASSERT_EQ(expect, caps & expect);
84     ASSERT_EQ(0, ceph_read(cmount, fd, buf, 256, 0));
85
86     caps = ceph_debug_get_fd_caps(cmount, fd);
87     ASSERT_EQ(expect, caps & expect);
88     ASSERT_EQ(0, ceph_close(cmount, fd));
89
90   }
91   ceph_shutdown(cmount);
92 }