From e2c5632db8c78b9aaea00d33d6f8ee909e645bad Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 16 Oct 2016 13:57:27 +0200 Subject: [PATCH] tests: deduplicate UtimesNano testing code --- tests/matrix/matrix_test.go | 86 ++++++++++++++----------------------- 1 file changed, 32 insertions(+), 54 deletions(-) diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go index 1c94cb6..8aa5c9c 100644 --- a/tests/matrix/matrix_test.go +++ b/tests/matrix/matrix_test.go @@ -631,41 +631,6 @@ func TestLchown(t *testing.T) { } } -// Set nanoseconds by path, normal file -func TestUtimesNano(t *testing.T) { - path := test_helpers.DefaultPlainDir + "/utimesnano" - err := ioutil.WriteFile(path, []byte("foobar"), 0600) - if err != nil { - t.Fatal(err) - } - ts := make([]syscall.Timespec, 2) - // atime - ts[0].Sec = 1 - ts[0].Nsec = 2 - // mtime - ts[1].Sec = 3 - ts[1].Nsec = 4 - err = syscall.UtimesNano(path, ts) - if err != nil { - t.Fatal(err) - } - var st syscall.Stat_t - err = syscall.Stat(path, &st) - if err != nil { - t.Fatal(err) - } - if st.Atim != ts[0] { - if st.Atim.Nsec == 0 { - // TODO remove this once the pull request is merged - t.Skip("Known limitation, https://github.com/hanwen/go-fuse/pull/131") - } - t.Errorf("Wrong atime: %v, want: %v", st.Atim, ts[0]) - } - if st.Mtim != ts[1] { - t.Errorf("Wrong mtime: %v, want: %v", st.Mtim, ts[1]) - } -} - // Set nanoseconds by path, symlink func TestUtimesNanoSymlink(t *testing.T) { path := test_helpers.DefaultPlainDir + "/utimesnano_symlink" @@ -684,28 +649,20 @@ func TestUtimesNanoSymlink(t *testing.T) { } } -// Set nanoseconds by fd -func TestUtimesNanoFd(t *testing.T) { - path := test_helpers.DefaultPlainDir + "/utimesnanofd" - f, err := os.Create(path) +// doTestUtimesNano verifies that setting nanosecond-precision times on "path" +// works correctly. Pass "/proc/self/fd/N" to test a file descriptor. +func doTestUtimesNano(t *testing.T, path string) { + ts := make([]syscall.Timespec, 2) + // atime + ts[0].Sec = 1 + ts[0].Nsec = 2 + // mtime + ts[1].Sec = 3 + ts[1].Nsec = 4 + err := syscall.UtimesNano(path, ts) if err != nil { t.Fatal(err) } - - ts := make([]syscall.Timespec, 2) - // atime - ts[0].Sec = 5 - ts[0].Nsec = 6 - // mtime - ts[1].Sec = 7 - ts[1].Nsec = 8 - - procPath := fmt.Sprintf("/proc/self/fd/%d", f.Fd()) - err = syscall.UtimesNano(procPath, ts) - if err != nil { - t.Fatalf("%s: %v", procPath, err) - } - var st syscall.Stat_t err = syscall.Stat(path, &st) if err != nil { @@ -722,3 +679,24 @@ func TestUtimesNanoFd(t *testing.T) { t.Errorf("Wrong mtime: %v, want: %v", st.Mtim, ts[1]) } } + +// Set nanoseconds by path, normal file +func TestUtimesNano(t *testing.T) { + path := test_helpers.DefaultPlainDir + "/utimesnano" + err := ioutil.WriteFile(path, []byte("foobar"), 0600) + if err != nil { + t.Fatal(err) + } + doTestUtimesNano(t, path) +} + +// Set nanoseconds by fd +func TestUtimesNanoFd(t *testing.T) { + path := test_helpers.DefaultPlainDir + "/utimesnanofd" + f, err := os.Create(path) + if err != nil { + t.Fatal(err) + } + procPath := fmt.Sprintf("/proc/self/fd/%d", f.Fd()) + doTestUtimesNano(t, procPath) +}