]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test/libcephfs: Add a test for validating caps
authorSam Lang <sam.lang@inktank.com>
Sat, 1 Dec 2012 22:55:47 +0000 (16:55 -0600)
committerSam Lang <sam.lang@inktank.com>
Fri, 7 Dec 2012 20:18:35 +0000 (10:18 -1000)
Signed-off-by: Sam Lang <sam.lang@inktank.com>
src/Makefile.am
src/test/libcephfs/caps.cc [new file with mode: 0644]

index 2102e065b03faa4bceb520a1d4d7e17ea84a1e4e..4632fdc879b5c012a77fb58d9b6c2fc0fad996ba 100644 (file)
@@ -919,7 +919,7 @@ test_rados_api_misc_LDADD =  librados.la ${UNITTEST_STATIC_LDADD}
 test_rados_api_misc_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS}
 bin_DEBUGPROGRAMS += test_rados_api_misc
 
-test_libcephfs_SOURCES = test/libcephfs/test.cc test/libcephfs/readdir_r_cb.cc
+test_libcephfs_SOURCES = test/libcephfs/test.cc test/libcephfs/readdir_r_cb.cc test/libcephfs/caps.cc
 test_libcephfs_LDFLAGS = $(PTHREAD_CFLAGS) ${AM_LDFLAGS}
 test_libcephfs_LDADD =  ${UNITTEST_STATIC_LDADD} libcephfs.la
 test_libcephfs_CXXFLAGS = $(AM_CXXFLAGS) ${UNITTEST_CXXFLAGS}
diff --git a/src/test/libcephfs/caps.cc b/src/test/libcephfs/caps.cc
new file mode 100644 (file)
index 0000000..5a421ea
--- /dev/null
@@ -0,0 +1,139 @@
+// -*- 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.
+ *
+ */
+
+#include "gtest/gtest.h"
+#include "include/cephfs/libcephfs.h"
+#include <linux/types.h>
+#include <inttypes.h>
+#include "include/ceph_fs.h"
+#include <errno.h>
+#include <sys/fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <sys/xattr.h>
+#include <signal.h>
+
+void do_sigusr1(int s) {}
+
+// wait_and_suspend() forks the process, waits for the
+// child to signal SIGUSR1, suspends the child with SIGSTOP
+// sleeps for s seconds, and then unsuspends the child,
+// waits for the child to exit, and then returns the exit code
+// of the child
+static int _wait_and_suspend(int s) {
+
+  int fpid = fork();
+  if (fpid != 0) {
+    // wait for child to signal
+    signal(SIGUSR1, &do_sigusr1);
+    sigset_t set;
+    sigaddset(&set, SIGUSR1);
+    int sig;
+    sigwait(&set, &sig);
+
+    // fork and suspend child, sleep for 20 secs, and resume
+    kill(fpid, SIGSTOP);
+    sleep(s);
+    kill(fpid, SIGCONT);
+    int status;
+    wait(&status);
+    if (WIFEXITED(status))
+      return WEXITSTATUS(status);
+    return 1;
+  }
+  return -1;
+}
+
+// signal_for_suspend sends the parent the SIGUSR1 signal
+// and sleeps for 1 second so that it can be suspended at the
+// point of the call
+static void _signal_for_suspend() {
+  kill(getppid(), SIGUSR1);
+}
+
+TEST(Caps, ReadZero) {
+
+  int w = _wait_and_suspend(20);
+  if (w >= 0) {
+    ASSERT_EQ(0, w);
+    return;
+  }
+
+  pid_t mypid = getpid();
+  struct ceph_mount_info *cmount;
+  ASSERT_EQ(0, ceph_create(&cmount, NULL));
+  ASSERT_EQ(0, ceph_conf_read_file(cmount, NULL));
+  ASSERT_EQ(0, ceph_mount(cmount, "/"));
+
+  ASSERT_EQ(0, ceph_conf_set(cmount, "client_cache_size", "10"));
+
+  int i = 0;
+  for(; i < 30; ++i) {
+
+    char c_path[1024];
+    sprintf(c_path, "/caps_rzfile_%d_%d", mypid, i);
+    int fd = ceph_open(cmount, c_path, O_CREAT|O_TRUNC|O_WRONLY, 0644);
+    ASSERT_LT(0, fd);
+
+    int expect = CEPH_CAP_FILE_EXCL | CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER;
+    int caps = ceph_debug_get_fd_caps(cmount, fd);
+
+    ASSERT_EQ(expect, caps & expect);
+    ASSERT_EQ(0, ceph_close(cmount, fd));
+
+    caps = ceph_debug_get_file_caps(cmount, c_path);
+    ASSERT_EQ(expect, caps & expect);
+
+    char cw_path[1024];
+    sprintf(cw_path, "/caps_wzfile_%d_%d", mypid, i);
+    int wfd = ceph_open(cmount, cw_path, O_CREAT|O_TRUNC|O_WRONLY, 0644);
+    ASSERT_LT(0, wfd);
+
+    char wbuf[4096];
+    ASSERT_EQ(4096, ceph_write(cmount, wfd, wbuf, 4096, 0));
+
+    ASSERT_EQ(0, ceph_close(cmount, wfd));
+
+    struct stat st;
+    ASSERT_EQ(0, ceph_stat(cmount, c_path, &st));
+
+    caps = ceph_debug_get_file_caps(cmount, c_path);
+    ASSERT_EQ(expect, caps & expect);
+  }
+
+  _signal_for_suspend();
+
+  for(i = 0; i < 30; ++i) {
+
+    char c_path[1024];
+    sprintf(c_path, "/caps_rzfile_%d_%d", mypid, i);
+
+    int fd = ceph_open(cmount, c_path, O_RDONLY, 0);
+    ASSERT_LT(0, fd);
+    char buf[256];
+
+    int expect = CEPH_CAP_FILE_RD | CEPH_STAT_CAP_SIZE | CEPH_CAP_FILE_CACHE;
+    int caps = ceph_debug_get_fd_caps(cmount, fd);
+    ASSERT_EQ(expect, caps & expect);
+    ASSERT_EQ(0, ceph_read(cmount, fd, buf, 256, 0));
+
+    caps = ceph_debug_get_fd_caps(cmount, fd);
+    ASSERT_EQ(expect, caps & expect);
+    ASSERT_EQ(0, ceph_close(cmount, fd));
+
+  }
+  ceph_shutdown(cmount);
+}