aboutsummaryrefslogtreecommitdiff
path: root/tests/zfs-tests/tests/functional/cli_root/zfs_diff/zfs_diff_timestamp.ksh
blob: a4cedca49ce8311bd427bd69563b5aacd14bf955 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/ksh -p
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
#

. $STF_SUITE/include/libtest.shlib

#
# DESCRIPTION:
# 'zfs diff -t' should display inode change time correctly.
#
# STRATEGY:
# 1. Create a snapshot
# 2. Create some files with a random delay and snapshot the filesystem again
# 3. Verify 'zfs diff -t' correctly display timestamps
#

verify_runnable "both"

function cleanup
{
	for snap in $TESTSNAP1 $TESTSNAP2; do
		if snapexists "$snap"; then
			log_must zfs destroy "$snap"
		fi
	done
	find "$MNTPOINT" -type f -delete
	rm -f "$FILEDIFF"
}

#
# Creates $count files in $fspath. Waits a random delay between each file.
#
function create_random # <fspath> <count>
{
	fspath="$1"
	typeset -i count="$2"
	typeset -i i=0

	while (( i < count )); do
		log_must touch "$fspath/file$i"
		sleep $(random_int_between 1 3)
		(( i = i + 1 ))
	done
}

log_assert "'zfs diff -t' should display inode change time correctly."
log_onexit cleanup

DATASET="$TESTPOOL/$TESTFS"
TESTSNAP1="$DATASET@snap1"
TESTSNAP2="$DATASET@snap2"
MNTPOINT="$(get_prop mountpoint $DATASET)"
FILEDIFF="$TESTDIR/zfs-diff.txt"
FILENUM=5

# 1. Create a snapshot
log_must zfs snapshot "$TESTSNAP1"

# 2. Create some files with a random delay and snapshot the filesystem again
create_random "$MNTPOINT" $FILENUM
log_must zfs snapshot "$TESTSNAP2"

# 3. Verify 'zfs diff -t' correctly display timestamps
typeset -i count=0
log_must eval "zfs diff -t $TESTSNAP1 $TESTSNAP2 > $FILEDIFF"
awk '{print substr($1,1,index($1,".")-1)" "$NF}' < "$FILEDIFF" | while read line
do
	read ctime file <<< "$line"

	# If path from 'zfs diff' is not a file (could be xattr object) skip it
	if [[ ! -f "$file" ]]; then
		continue;
	fi

	if is_freebsd; then
		filetime="$(stat -f "%c" $file)"
	else
		filetime="$(stat -c '%Z' $file)"
	fi
	if [[ "$filetime" != "$ctime" ]]; then
		log_fail "Unexpected ctime for file $file ($filetime != $ctime)"
	else
		log_note "Correct ctime read on $file: $ctime"
	fi

	(( i = i + 1 ))
done
if [[ $i != $FILENUM ]]; then
	log_fail "Wrong number of files verified ($i != $FILENUM)"
fi

log_pass "'zfs diff -t' displays inode change time correctly."