From: Venky Shankar Date: Thu, 28 Jun 2018 03:31:27 +0000 (-0400) Subject: test: add tests for ceph_utime() and friends X-Git-Tag: v14.0.1~869^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ab49cde5a534b08aee5ae2bb603dacc5042522a0;p=ceph.git test: add tests for ceph_utime() and friends Signed-off-by: Venky Shankar Fixes: http://tracker.ceph.com/issues/24643 --- diff --git a/src/test/libcephfs/CMakeLists.txt b/src/test/libcephfs/CMakeLists.txt index 5d3f963484e2..022f48191be4 100644 --- a/src/test/libcephfs/CMakeLists.txt +++ b/src/test/libcephfs/CMakeLists.txt @@ -13,6 +13,7 @@ if(${WITH_CEPHFS}) set_target_properties(ceph_test_libcephfs PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS}) target_link_libraries(ceph_test_libcephfs + ceph-common cephfs ${UNITTEST_LIBS} ${EXTRALIBS} diff --git a/src/test/libcephfs/test.cc b/src/test/libcephfs/test.cc index f40be76c2fcc..6659cc0a82e6 100644 --- a/src/test/libcephfs/test.cc +++ b/src/test/libcephfs/test.cc @@ -26,6 +26,8 @@ #include #include +#include "common/Clock.h" + #ifdef __linux__ #include #endif @@ -2007,3 +2009,141 @@ TEST(LibCephFS, ShutdownRace) threads[i].join(); ASSERT_EQ(setrlimit(RLIMIT_NOFILE, &rold), 0); } + +static void get_current_time_utimbuf(struct utimbuf *utb) +{ + utime_t t = ceph_clock_now(); + utb->actime = t.sec(); + utb->modtime = t.sec(); +} + +static void get_current_time_timeval(struct timeval tv[2]) +{ + utime_t t = ceph_clock_now(); + t.copy_to_timeval(&tv[0]); + t.copy_to_timeval(&tv[1]); +} + +static void get_current_time_timespec(struct timespec ts[2]) +{ + utime_t t = ceph_clock_now(); + t.to_timespec(&ts[0]); + t.to_timespec(&ts[1]); +} + +TEST(LibCephFS, TestUtime) { + struct ceph_mount_info *cmount; + ASSERT_EQ(ceph_create(&cmount, NULL), 0); + ASSERT_EQ(ceph_conf_read_file(cmount, NULL), 0); + ASSERT_EQ(0, ceph_conf_parse_env(cmount, NULL)); + ASSERT_EQ(ceph_mount(cmount, NULL), 0); + + char test_file[256]; + sprintf(test_file, "test_utime_file_%d", getpid()); + int fd = ceph_open(cmount, test_file, O_CREAT, 0666); + ASSERT_GT(fd, 0); + + struct utimbuf utb; + struct ceph_statx stx; + + get_current_time_utimbuf(&utb); + + // ceph_utime() + EXPECT_EQ(0, ceph_utime(cmount, test_file, &utb)); + ASSERT_EQ(ceph_statx(cmount, test_file, &stx, + CEPH_STATX_MTIME|CEPH_STATX_ATIME, 0), 0); + ASSERT_EQ(utime_t(stx.stx_atime), utime_t(utb.actime, 0)); + ASSERT_EQ(utime_t(stx.stx_mtime), utime_t(utb.modtime, 0)); + + get_current_time_utimbuf(&utb); + + // ceph_futime() + EXPECT_EQ(0, ceph_futime(cmount, fd, &utb)); + ASSERT_EQ(ceph_statx(cmount, test_file, &stx, + CEPH_STATX_MTIME|CEPH_STATX_ATIME, 0), 0); + ASSERT_EQ(utime_t(stx.stx_atime), utime_t(utb.actime, 0)); + ASSERT_EQ(utime_t(stx.stx_mtime), utime_t(utb.modtime, 0)); + + ceph_close(cmount, fd); + ceph_shutdown(cmount); +} + +TEST(LibCephFS, TestUtimes) { + struct ceph_mount_info *cmount; + ASSERT_EQ(ceph_create(&cmount, NULL), 0); + ASSERT_EQ(ceph_conf_read_file(cmount, NULL), 0); + ASSERT_EQ(0, ceph_conf_parse_env(cmount, NULL)); + ASSERT_EQ(ceph_mount(cmount, NULL), 0); + + char test_file[256]; + char test_symlink[256]; + + sprintf(test_file, "test_utimes_file_%d", getpid()); + sprintf(test_symlink, "test_utimes_symlink_%d", getpid()); + int fd = ceph_open(cmount, test_file, O_CREAT, 0666); + ASSERT_GT(fd, 0); + + ASSERT_EQ(ceph_symlink(cmount, test_file, test_symlink), 0); + + struct timeval times[2]; + struct ceph_statx stx; + + get_current_time_timeval(times); + + // ceph_utimes() on symlink, validate target file time + EXPECT_EQ(0, ceph_utimes(cmount, test_symlink, times)); + ASSERT_EQ(ceph_statx(cmount, test_symlink, &stx, + CEPH_STATX_MTIME|CEPH_STATX_ATIME, 0), 0); + ASSERT_EQ(utime_t(stx.stx_atime), utime_t(times[0])); + ASSERT_EQ(utime_t(stx.stx_mtime), utime_t(times[1])); + + get_current_time_timeval(times); + + // ceph_lutimes() on symlink, validate symlink time + EXPECT_EQ(0, ceph_lutimes(cmount, test_symlink, times)); + ASSERT_EQ(ceph_statx(cmount, test_symlink, &stx, + CEPH_STATX_MTIME|CEPH_STATX_ATIME, AT_SYMLINK_NOFOLLOW), 0); + ASSERT_EQ(utime_t(stx.stx_atime), utime_t(times[0])); + ASSERT_EQ(utime_t(stx.stx_mtime), utime_t(times[1])); + + get_current_time_timeval(times); + + // ceph_futimes() + EXPECT_EQ(0, ceph_futimes(cmount, fd, times)); + ASSERT_EQ(ceph_statx(cmount, test_file, &stx, + CEPH_STATX_MTIME|CEPH_STATX_ATIME, 0), 0); + ASSERT_EQ(utime_t(stx.stx_atime), utime_t(times[0])); + ASSERT_EQ(utime_t(stx.stx_mtime), utime_t(times[1])); + + ceph_close(cmount, fd); + ceph_shutdown(cmount); +} + +TEST(LibCephFS, TestFutimens) { + struct ceph_mount_info *cmount; + ASSERT_EQ(ceph_create(&cmount, NULL), 0); + ASSERT_EQ(ceph_conf_read_file(cmount, NULL), 0); + ASSERT_EQ(0, ceph_conf_parse_env(cmount, NULL)); + ASSERT_EQ(ceph_mount(cmount, NULL), 0); + + char test_file[256]; + + sprintf(test_file, "test_futimens_file_%d", getpid()); + int fd = ceph_open(cmount, test_file, O_CREAT, 0666); + ASSERT_GT(fd, 0); + + struct timespec times[2]; + struct ceph_statx stx; + + get_current_time_timespec(times); + + // ceph_futimens() + EXPECT_EQ(0, ceph_futimens(cmount, fd, times)); + ASSERT_EQ(ceph_statx(cmount, test_file, &stx, + CEPH_STATX_MTIME|CEPH_STATX_ATIME, 0), 0); + ASSERT_EQ(utime_t(stx.stx_atime), utime_t(times[0])); + ASSERT_EQ(utime_t(stx.stx_mtime), utime_t(times[1])); + + ceph_close(cmount, fd); + ceph_shutdown(cmount); +}