-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
executable file
·187 lines (151 loc) · 3.72 KB
/
utils.sh
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/sh
# function to mount bkpfs
function mount_bkpfs {
maxversions=$1
mount_path=$2
writefrequency=$3
# create the mount point if doesn't exist
if [ ! -d "$mount_path" ]; then
mkdir -p "$mount_path"test
fi
# mount bkpfs
mount -t bkpfs -o maxver="$maxversions" -o writefrequency="$writefrequency" "$mount_path"test "$mount_path"
retval=$?
if [ $retval -ne 0 ]; then
echo $retval
else
echo 0
fi
}
# unmount bkpfs
function unmount_bkpfs {
mount_point=$1
umount "$mount_point"
retval=$?
if [ $retval -ne 0 ]; then
echo $retval
else
echo 0
fi
}
# create the backup files
function create {
number_of_backups=$1
filename=$2
mount_path=$3
index=1
content=""
# the idea is to concat all the inputs without a newline to make it easier for testing
while [ $index -le $number_of_backups ]; do
if [ -f "$mount_path$filename" ]; then
content=$(cat "$mount_path$filename")
fi
content="$content$index"
echo $content > "$mount_path$filename"
index=`expr $index + 1`
done;
}
# remove any remnants of the test case we just ran
function cleanup {
index=1
number_of_backups=$1
filename=$2
mount_path=$3
while [ $index -le $number_of_backups ]; do
backup_filename="$filename.bckp.""$index"
if [ -f "$mount_path$backup_filename" ]; then
rm -f "$mount_path$backup_filename";
fi
index=`expr $index + 1`
done;
if [ -f "$mount_path$filename" ]; then
rm -f "$mount_path$filename"
fi
}
# get the output of listing which matches the output generated by the user program
function get_versions_list {
local versions=("$@")
local expected=""
for i in ${!versions[@]}; do
line=$(printf "Backup version %d : %s.bckp.%d" $((${i}+1)) "$filename" ${versions[$i]})
expected="$expected""$line"
if [ $(($i + 1)) -lt ${#versions[@]} ]; then
expected="$expected"" "
fi
done
echo "$expected"
}
# intialize the test
function init_test {
mount_path=$1
maxversions=$2
write_frequency=$3
filename=$4
number_of_backups=$5
status=-1
# clean any previous run and build the user program
make clean
make
retval=$?
if [ $retval -ne 0 ]; then
exit $retval
fi
if [ -z $maxversions ]; then
maxversions=5
fi
if [ -z $filename ]; then
filename=test_file.txt
fi
if [ -z $mount_path ]; then
mount_path=/mnt/bkpfs/
fi
if [ -z $writefrequency ]; then
writefrequency=1
fi
if [ -z $number_of_backups ]; then
number_of_backups=$maxversions
fi
#cleanup any old master file or backups related to it
cleanup $number_of_backups $filename $mount_path
# unmount bkpfs if already mounted
if mountpoint -q $mount_path
then
status=$(unmount_bkpfs "$mount_path")
if [ $status -ne 0 ]; then
exit -1
fi
fi
# now finally mount the file system
status=$(mount_bkpfs $maxversions $mount_path $writefrequency)
if [ $status -ne 0 ]; then
exit -1
fi
#Now create the backup files
create $number_of_backups $filename $mount_path
}
# teardown procedure; cleanup master and backup files, build files and finally unmount the file system
function teardown_test {
filename=$1
mount_path=$2
number_of_backups=$3
status=-1
if [ -z $number_of_backups ]; then
number_of_backups=5
fi
if [ -z $filename ]; then
filename=test_file.txt
fi
if [ -z $mount_path ]; then
mount_path=/mnt/bkpfs/
fi
cleanup $number_of_backups $filename $mount_path
make clean
# unmount bkpfs if already mounted
if mountpoint -q $mount_path
then
status=$(unmount_bkpfs "$mount_path")
if [ $status -ne 0 ]; then
exit -1
fi
fi
}