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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#!/bin/bash
show_help() {
echo "usage: fs-state mock-dir [dirname]"
echo " fs-state restore-dir [dirname]"
echo " fs-state mock-file [filename]"
echo " fs-state restore-file [filename]"
echo " fs-state start-monitor"
echo " fs-state check-monitor"
}
_mock_dir() {
local dir="$1"
if [ ! -d "$dir" ]; then
echo "fs-state: created mock directory: $dir"
mkdir -p "$dir"
touch "$dir.fake"
fi
}
mock_dir() {
local dir="$1"
if [ -d "$dir" ]; then
mv "$dir" "$dir.back"
fi
_mock_dir "$dir"
}
restore_dir() {
local dir="$1"
if [ -e "$dir.fake" ]; then
echo "fs-state: restoring mocked directory: $dir"
rm -rf "$dir"
rm -f "$dir.fake"
else
echo "fs-state: cannot restore non-mocked directory: $dir" >&2
exit 1
fi
if [ -d "$dir.back" ]; then
mv "$dir.back" "$dir"
fi
}
_mock_file() {
local file="$1"
if [ ! -e "$file" ]; then
echo "fs-state: creating file: $file"
echo "The file is a mock file used by fs-state." > "$file"
echo "The .fake file is a marker file used by fs-state." > "$file.fake"
fi
}
mock_file() {
local file="$1"
if [ -e "$file" ]; then
echo "fs-state: backing up original file: $file"
mv "$file" "$file.back"
fi
# ensure the parent dir is available
if [ ! -d "$(dirname "$file")" ]; then
mkdir -p "$(dirname "$file")"
fi
_mock_file "$file"
}
restore_file() {
local file="$1"
if [ -e "$file.fake" ]; then
echo "fs-state: restoring mocked file: $file"
rm -f "$file"
rm -f "$file.fake"
else
echo "fs-state: cannot restore non-mocked file: $file" >&2
exit 1
fi
if [ -e "$file.back" ]; then
mv "$file.back" "$file"
fi
}
save_current_tree(){
local dir=$1
rm -rf "$dir"
mkdir "$dir"
# .snap files created in tests/lib/snaps are excluded as they are not deleted to be reused
# runtime-state dir contains cursor and run log which are created during the first test
find /etc ! -name "*~" > "$dir"/etc
find /boot ! -name "*~" > "$dir"/boot
find /home ! -name "*~" | grep -v -e "$PROJECT_PATH/tests/lib/snaps/.*/.*.snap" -e "$PWD/.*.snap" > "$dir"/home
find /root ! -name "*~" > "$dir"/root
}
start_monitor() {
if os.query is-classic; then
save_current_tree /tmp/fs-initial
fi
}
skip_monitor() {
local path=$1
echo "$path" >> /tmp/fs-skip
}
clean_skipped() {
# shellcheck disable=SC2002
cat /tmp/fs-skip | while read -r file; do
for filename in /tmp/fs-initial/*; do
sed -i "/$file/d" "$filename"
done
for filename in /tmp/fs-final/*; do
sed -i "/$file/d" "$filename"
done
done
}
check_monitor() {
save_current_tree /tmp/fs-final
clean_skipped
rm -f /tmp/fs-diff
for f in /tmp/fs-initial/*; do
diff /tmp/fs-initial/"$f" /tmp/fs-final/"$f" >> /tmp/fs-diff
done
if [ ! -e /tmp/fs-diff ]; then
echo "the /tmp/fs-diff file does not exist"
exit 1
fi
if [ -n "$(cat /tmp/fs-diff)" ]; then
echo "the diff file is not empty:"
cat /tmp/fs-diff
exit 1
fi
}
main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi
action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help|'')
show_help
exit 0
;;
*)
action=$(echo "$1" | tr '-' '_')
shift
break
;;
esac
done
"$action" "$@"
}
main "$@"
|