Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / workunits / fs / misc / chmod.sh
1 #!/bin/sh -x
2
3 set -e
4
5 check_perms() {
6
7         file=$1
8         r=$(ls -la ${file})
9         if test $? != 0; then
10                 echo "ERROR: File listing/stat failed"
11                 exit 1
12         fi
13
14         perms=$2
15         if test "${perms}" != $(echo ${r} | awk '{print $1}') && \
16            test "${perms}." != $(echo ${r} | awk '{print $1}') && \
17            test "${perms}+" != $(echo ${r} | awk '{print $1}'); then
18                 echo "ERROR: Permissions should be ${perms}"
19                 exit 1
20         fi
21 }
22
23 file=test_chmod.$$
24
25 echo "foo" > ${file}
26 if test $? != 0; then
27         echo "ERROR: Failed to create file ${file}"
28         exit 1
29 fi
30
31 chmod 400 ${file}
32 if test $? != 0; then
33         echo "ERROR: Failed to change mode of ${file}"
34         exit 1
35 fi
36
37 check_perms ${file} "-r--------"
38
39 set +e
40 echo "bar" >> ${file}
41 if test $? = 0; then
42         echo "ERROR: Write to read-only file should Fail"
43         exit 1
44 fi
45
46 set -e
47 chmod 600 ${file}
48 echo "bar" >> ${file}
49 if test $? != 0; then
50         echo "ERROR: Write to writeable file failed"
51         exit 1
52 fi
53
54 check_perms ${file} "-rw-------"
55
56 echo "foo" >> ${file}
57 if test $? != 0; then
58         echo "ERROR: Failed to write to file"
59         exit 1
60 fi