1#!/bin/ksh -p
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18
19#
20# DESCRIPTION:
21# 'zfs diff' should display changes correctly.
22#
23# STRATEGY:
24# 1. Create a filesystem with both files and directories, then snapshot it
25# 2. Generate different types of changes and verify 'zfs diff' displays them
26#
27
28verify_runnable "both"
29
30function cleanup
31{
32	log_must zfs destroy -r "$DATASET"
33	rm -f "$FILEDIFF"
34}
35
36#
37# Verify object $path has $change type
38# Valid types are:
39# * - (The path has been removed)
40# * + (The path has been created)
41# * M (The path has been modified)
42# * R (The path has been renamed)
43#
44function verify_object_change # <path> <change>
45{
46	path="$1"
47	change="$2"
48
49	log_must eval "zfs diff -F $TESTSNAP1 $TESTSNAP2 > $FILEDIFF"
50	diffchg="$(nawk -v path="$path" '$NF == path { print $1 }' < $FILEDIFF)"
51	if [[ "$diffchg" != "$change" ]]; then
52		log_fail "Unexpected change for $path ('$diffchg' != '$change')"
53	else
54		log_note "Object $path change is displayed correctly: '$change'"
55	fi
56}
57
58log_assert "'zfs diff' should display changes correctly."
59log_onexit cleanup
60
61DATASET="$TESTPOOL/$TESTFS/fs"
62TESTSNAP1="$DATASET@snap1"
63TESTSNAP2="$DATASET@snap2"
64FILEDIFF="$TESTDIR/zfs-diff.txt"
65
66# 1. Create a filesystem with both files and directories, then snapshot it
67log_must zfs create $DATASET
68MNTPOINT="$(get_prop mountpoint $DATASET)"
69log_must touch "$MNTPOINT/fremoved"
70log_must touch "$MNTPOINT/frenamed"
71log_must touch "$MNTPOINT/fmodified"
72log_must mkdir "$MNTPOINT/dremoved"
73log_must mkdir "$MNTPOINT/drenamed"
74log_must mkdir "$MNTPOINT/dmodified"
75log_must zfs snapshot "$TESTSNAP1"
76
77# 2. Generate different types of changes and verify 'zfs diff' displays them
78log_must rm -f "$MNTPOINT/fremoved"
79log_must mv "$MNTPOINT/frenamed" "$MNTPOINT/frenamed.new"
80log_must touch "$MNTPOINT/fmodified"
81log_must rmdir "$MNTPOINT/dremoved"
82log_must mv "$MNTPOINT/drenamed" "$MNTPOINT/drenamed.new"
83log_must touch "$MNTPOINT/dmodified/file"
84log_must touch "$MNTPOINT/fcreated"
85log_must mkdir "$MNTPOINT/dcreated"
86log_must zfs snapshot "$TESTSNAP2"
87verify_object_change "$MNTPOINT/fremoved" "-"
88verify_object_change "$MNTPOINT/frenamed.new" "R"
89verify_object_change "$MNTPOINT/fmodified" "M"
90verify_object_change "$MNTPOINT/fcreated" "+"
91verify_object_change "$MNTPOINT/dremoved" "-"
92verify_object_change "$MNTPOINT/drenamed.new" "R"
93verify_object_change "$MNTPOINT/dmodified" "M"
94verify_object_change "$MNTPOINT/dcreated" "+"
95
96log_pass "'zfs diff' displays changes correctly."
97