Skip to content

Commit 159d64f

Browse files
committed
Adjust unit tests
1 parent ec8c4bc commit 159d64f

File tree

5 files changed

+103
-80
lines changed

5 files changed

+103
-80
lines changed

script/config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
D_MOD="drwxr-xr-x"
2+
F_MOD="-rw-r--r--"
3+
S_MOD="lrwxrwxrwx"
4+
SIMPLEFS_MAX_BLOCKS_PER_EXTENT=8
5+
SIMPLEFS_BLOCK_SIZE=4096
6+
SIMPLEFS_MAX_EXTENTS=$(( ($SIMPLEFS_BLOCK_SIZE - 4) / 16 ))
7+
MAXFILESIZE=$(( $SIMPLEFS_MAX_EXTENTS * $SIMPLEFS_MAX_BLOCKS_PER_EXTENT * $SIMPLEFS_BLOCK_SIZE ))
8+
# MAXFILES=1173 # max files per dir
9+
MAXFILES=30600 # max files per dir
10+
MOUNT_TEST=100

script/test.sh

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
#!/usr/bin/env bash
22

3+
. script/test_func.sh
34
. script/test_large_file.sh
5+
. script/test_remount.sh
6+
. script/config
47

58
SIMPLEFS_MOD=simplefs.ko
69
IMAGE=$1
710
IMAGESIZE=$2
811
MKFS=$3
912

10-
D_MOD="drwxr-xr-x"
11-
F_MOD="-rw-r--r--"
12-
S_MOD="lrwxrwxrwx"
13-
MAXFILESIZE=11173888 # SIMPLEFS_MAX_EXTENTS * SIMPLEFS_MAX_BLOCKS_PER_EXTENT * SIMPLEFS_BLOCK_SIZE
14-
MAXFILES=40920 # max files per dir
15-
MOUNT_TEST=100
16-
17-
test_op() {
18-
local op=$1
19-
echo
20-
echo -n "Testing cmd: $op..."
21-
sudo sh -c "$op" >/dev/null && echo "Success"
22-
}
23-
24-
check_exist() {
25-
local mode=$1
26-
local nlink=$2
27-
local name=$3
28-
echo
29-
echo -n "Check if exist: $mode $nlink $name..."
30-
sudo ls -lR | grep -e "$mode $nlink".*$name >/dev/null && echo "Success" || \
31-
echo "Failed"
32-
}
33-
3413
if [ "$EUID" -eq 0 ]
3514
then echo "Don't run this script as root"
3615
exit
@@ -46,62 +25,32 @@ echo && \
4625
sudo insmod $SIMPLEFS_MOD && \
4726
dd if=/dev/zero of=$IMAGE bs=1M count=$IMAGESIZE status=none && \
4827
./$MKFS $IMAGE && \
28+
4929
sudo mount -t simplefs -o loop $IMAGE test && \
5030
pushd test >/dev/null
5131

52-
# create 40920 files
53-
for ((i=0; i<=$MAXFILES; i++))
54-
do
55-
test_op "touch $i.txt" # expected to fail with more than 40920 files
56-
done
57-
filecnts=$(ls | wc -w)
58-
test $filecnts -eq $MAXFILES || echo "Failed, it should be $MAXFILES files"
59-
find . -name '[0-9]*.txt' | xargs -n 2000 sudo rm
60-
sync
61-
62-
# create 100 files with filenames inside
63-
for ((i=1; i<=$MOUNT_TEST; i++))
64-
do
65-
echo file_$i | sudo tee file_$i.txt >/dev/null && echo "file_$i.txt created."
66-
done
67-
sync
32+
# test serial to write
33+
test_create_max_nr_files
34+
# test remount file exist or not
35+
test_remount_file_exist
6836

69-
# unmount and remount the filesystem
70-
echo "Unmounting filesystem..."
7137
popd >/dev/null || { echo "popd failed"; exit 1; }
72-
sudo umount test || { echo "umount failed"; exit 1; }
73-
sleep 1
74-
echo "Remounting filesystem..."
75-
sudo mount -t simplefs -o loop $IMAGE test || { echo "mount failed"; exit 1; }
76-
echo "Remount succeeds."
77-
pushd test >/dev/null || { echo "pushd failed"; exit 1; }
7838

79-
# check if files exist and content is correct after remounting
80-
for ((i=1; i<=$MOUNT_TEST; i++))
81-
do
82-
if [[ -f "file_$i.txt" ]]; then
83-
content=$(cat "file_$i.txt" | tr -d '\000')
84-
if [[ "$content" == "file_$i" ]]; then
85-
echo "Success: file_$i.txt content is correct."
86-
else
87-
echo "Failed: file_$i.txt content is incorrect."
88-
exit 1
89-
fi
90-
else
91-
echo "Failed: file_$i.txt does not exist."
92-
exit 1
93-
fi
94-
done
95-
find . -name 'file_[0-9]*.txt' | xargs sudo rm || { echo "Failed to delete files"; exit 1; }
39+
# Get ready to count free block
40+
sudo touch test/test.txt
41+
sudo rm test/* -rf
9642
sync
9743

98-
popd >/dev/null || { echo "popd failed"; exit 1; }
99-
ls test -laR
100-
rm test/* -rf
10144
nr_free_blk=$(($(dd if=$IMAGE bs=1 skip=28 count=4 2>/dev/null | hexdump -v -e '1/4 "0x%08x\n"')))
10245
echo "$nr_free_blk"
10346
pushd test >/dev/null || { echo "pushd failed"; exit 1; }
10447

48+
# write a file larger than max size
49+
test_too_large_file
50+
51+
# Write the a file larger than BLOCK_SIZE
52+
test_file_size_larger_than_block_size
53+
10554
# mkdir
10655
test_op 'mkdir dir'
10756
test_op 'mkdir dir' # expected to fail
@@ -128,13 +77,6 @@ test_op 'ln -s dir len_of_name_of_the_link_is_29'
12877
test_op 'echo abc > file'
12978
test $(cat file) = "abc" || echo "Failed to write"
13079

131-
# file too large
132-
test_too_large_file
133-
134-
# Write the file size larger than BLOCK_SIZE
135-
# test serial to write
136-
test_file_size_larger_than_block_size
137-
13880
# test remove symbolic link
13981
test_op 'ln -s file symlink_fake'
14082
test_op 'rm -f symlink_fake'

script/test_func.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
test_op() {
2+
local op=$1
3+
echo
4+
echo -n "Testing cmd: $op..."
5+
sudo sh -c "$op" >/dev/null && echo "Success"
6+
}
7+
8+
check_exist() {
9+
local mode=$1
10+
local nlink=$2
11+
local name=$3
12+
echo
13+
echo -n "Check if exist: $mode $nlink $name..."
14+
sudo ls -lR | grep -e "$mode $nlink".*$name >/dev/null && echo "Success" || \
15+
echo "Failed"
16+
}

script/test_large_file.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
# file too large
22
test_too_large_file() {
3-
test_op 'dd if=/dev/zero of=file bs=1M count=12 status=none'
4-
filesize=$(sudo ls -lR | grep -e "$F_MOD 2".*file | awk '{print $5}')
3+
TESTLG_FILE_SZ=$(( $MAXFILESIZE / 1024 / 1024 + 1 ))
4+
test_op "dd if=/dev/zero of=exceed_max_sz_file bs=1M count=$TESTLG_FILE_SZ status=none"
5+
filesize=$(sudo ls -lR | grep -e "$F_MOD 1".*file | awk '{print $5}')
56
test $filesize -le $MAXFILESIZE || echo "Failed, file size over the limit"
7+
test_op 'rm exceed_max_sz_file'
68
}
79

810
# Write the file size larger than BLOCK_SIZE
911
# test serial to write
1012
test_file_size_larger_than_block_size() {
11-
test_op 'yes 123456789 | head -n 1600 | tr -d "\n" > file.txt'
12-
count=$(awk '{count += gsub(/123456789/, "")} END {print count}' "file.txt")
13+
test_op 'yes 123456789 | head -n 1600 | tr -d "\n" > exceed_blk.txt'
14+
count=$(awk '{count += gsub(/123456789/, "")} END {print count}' "exceed_blk.txt")
1315
echo "test $count"
1416
test "$count" -eq 1600 || echo "Failed, file size not matching"
1517
# test block to write
16-
test_op 'cat file.txt > checkfile.txt'
18+
test_op 'cat exceed_blk.txt > checkfile.txt'
1719
count=$(awk '{count += gsub(/123456789/, "")} END {print count}' "checkfile.txt")
1820
echo "test $count"
1921
test "$count" -eq 1600 || echo "Failed, file size not matching"
22+
test_op 'rm exceed_blk.txt checkfile.txt'
2023
}

script/test_remount.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# test create max nr files
2+
test_create_max_nr_files() {
3+
echo "max size $MAXFILESIZE"
4+
# create 40920 files
5+
for ((i=0; i<=$MAXFILES; i++))
6+
do
7+
test_op "touch $i.txt" # expected to fail with more than 40920 files
8+
done
9+
sync
10+
filecnts=$(ls | wc -w)
11+
test $filecnts -eq $MAXFILES || echo "Failed($filecnts), it should be $MAXFILES files"
12+
find . -name '[0-9]*.txt' | xargs -n 2000 sudo rm
13+
sync
14+
}
15+
16+
# create 100 files with filenames inside
17+
test_remount_file_exist() {
18+
for ((i=1; i<=$MOUNT_TEST; i++))
19+
do
20+
echo file_$i | sudo tee file_$i.txt >/dev/null && echo "file_$i.txt created."
21+
done
22+
sync
23+
24+
# unmount and remount the filesystem
25+
echo "Unmounting filesystem..."
26+
popd >/dev/null || { echo "popd failed"; exit 1; }
27+
sudo umount test || { echo "umount failed"; exit 1; }
28+
sleep 1
29+
echo "Remounting filesystem..."
30+
sudo mount -t simplefs -o loop $IMAGE test || { echo "mount failed"; exit 1; }
31+
echo "Remount succeeds."
32+
pushd test >/dev/null || { echo "pushd failed"; exit 1; }
33+
34+
# check if files exist and content is correct after remounting
35+
for ((i=1; i<=$MOUNT_TEST; i++))
36+
do
37+
if [[ -f "file_$i.txt" ]]; then
38+
content=$(cat "file_$i.txt" | tr -d '\000')
39+
if [[ "$content" == "file_$i" ]]; then
40+
echo "Success: file_$i.txt content is correct."
41+
else
42+
echo "Failed: file_$i.txt content is incorrect."
43+
exit 1
44+
fi
45+
else
46+
echo "Failed: file_$i.txt does not exist."
47+
exit 1
48+
fi
49+
done
50+
find . -name 'file_[0-9]*.txt' | xargs sudo rm || { echo "Failed to delete files"; exit 1; }
51+
sync
52+
}

0 commit comments

Comments
 (0)