Bump to 1.14.1
[platform/upstream/augeas.git] / tests / test-preserve.sh
1 #!/bin/sh
2
3 # Check that saving preserves mode and ownership; for this test to make
4 # much sense (if any) the user running it should have at least one
5 # supplementary group
6
7 root=$abs_top_builddir/build/preserve
8 hosts=$root/etc/hosts
9
10 init_dirs() {
11 rm -rf $root
12 mkdir -p $(dirname $hosts)
13 }
14
15 stat_inode() {
16 ls -il $1 | awk '{ print $1 }'
17 }
18
19 AUGTOOL="augtool --nostdinc -r $root -I $abs_top_srcdir/lenses"
20 init_dirs
21
22 printf '127.0.0.1\tlocalhost\n' > $hosts
23
24 chmod 0600 $hosts
25 group=$(groups | tr ' ' '\n' | tail -1)
26 chgrp $group $hosts
27
28 [ -x /usr/bin/chcon ] && selinux=yes || selinux=no
29 [ x$SKIP_TEST_PRESERVE_SELINUX = x1 ] && selinux=no
30 if [ $selinux = yes ] ; then
31   /usr/bin/chcon -t etc_t $hosts > /dev/null 2>/dev/null || selinux=no
32 fi
33
34 $AUGTOOL >/dev/null <<EOF
35 set /files/etc/hosts/1/alias alias.example.com
36 save
37 EOF
38 if [ $? != 0 ] ; then
39     echo "augtool failed on existing file"
40     exit 1
41 fi
42
43 act_group=$(ls -l $hosts | sed -e 's/  */ /g' | cut -d ' ' -f 4)
44 act_mode=$(ls -l $hosts | cut -b 1-10)
45 if [ $selinux = yes ] ; then
46   act_con=$(stat --format=%C $hosts | cut -d ':' -f 3)
47 fi
48 if [ "x$group" != "x$act_group" ] ; then
49     echo "Expected group $group but got $act_group"
50     exit 1
51 fi
52
53 if [ x-rw------- != "x$act_mode" ] ; then
54     echo "Expected mode 0600 but got $act_mode"
55     exit 1
56 fi
57
58 if [ $selinux = yes -a xetc_t != "x$act_con" ] ; then
59     echo "Expected SELinux type etc_t but got $act_con"
60     exit 1
61 fi
62
63 # Check that we create new files without error and with permissions implied
64 # from the umask
65 init_dirs
66
67 oldumask=$(umask)
68 umask 0002
69 $AUGTOOL > /dev/null <<EOF
70 set /files/etc/hosts/1/ipaddr 127.0.0.1
71 set /files/etc/hosts/1/canonical host.example.com
72 save
73 EOF
74 if [ $? != 0 ] ; then
75     echo "augtool failed on new file"
76     exit 1
77 fi
78 if [ ! -e $hosts ]; then
79     echo "augtool didn't create new /etc/hosts file"
80     exit 1
81 fi
82 act_mode=$(ls -l $hosts | cut -b 1-10)
83 if [ x-rw-rw-r-- != "x$act_mode" ] ; then
84     echo "Expected mode 0664 due to $(umask) umask but got $act_mode"
85     exit 1
86 fi
87 umask $oldumask
88
89 # Check that we create new files without error when backups are requested
90 init_dirs
91
92 $AUGTOOL -b > /dev/null <<EOF
93 set /files/etc/hosts/1/ipaddr 127.0.0.1
94 set /files/etc/hosts/1/canonical host.example.com
95 save
96 EOF
97 if [ $? != 0 ] ; then
98     echo "augtool -b failed on new file"
99     exit 1
100 fi
101
102 # Check that we preserve a backup file on request
103 printf '127.0.0.1\tlocalhost\n' > $hosts
104 exp_inode=$(stat_inode $hosts)
105
106 $AUGTOOL -b > /dev/null <<EOF
107 set /files/etc/hosts/1/alias alias.example.com
108 print /augeas/save
109 save
110 EOF
111
112 if [ ! -f $hosts.augsave ] ; then
113   echo "Backup file was not created"
114   exit 1
115 fi
116
117 act_inode=$(stat_inode $hosts.augsave)
118 if [ "x$act_inode" != "x$exp_inode" ] ; then
119   echo "Backup file's inode changed"
120   exit 1
121 fi
122
123 act_inode=$(stat_inode $hosts)
124 if [ "x$act_inode" = "x$exp_inode" ] ; then
125   echo "Same inode for backup file and main file"
126   exit 1
127 fi