Imported Upstream version 1.11.0
[platform/upstream/augeas.git] / tests / test-save-mode.sh
1 #!/bin/sh
2
3 # Test manipulating the save flags in /augeas/save
4
5 root=$abs_top_builddir/build/test-save-mode
6 hosts=$root/etc/hosts
7 augopts="--nostdinc -r $root -I $abs_top_srcdir/lenses"
8
9 run_augtool() {
10     exp=$1
11     shift
12     augtool $augopts "$@" > /dev/null
13     status=$?
14     if [ "x$exp" = ok -a $status -ne 0 ] ; then
15         echo "augtool failed"
16         exit 1
17     elif [ "x$exp" = fail -a $status -eq 0 ] ; then
18         echo "augtool succeeded but should have failed"
19         exit 1
20     fi
21 }
22
23 assert_ipaddr() {
24     exp="/files/etc/hosts/1/ipaddr = $1"
25     act=$(augtool $augopts get /files/etc/hosts/1/ipaddr)
26
27     if [ "$act" != "$exp" ] ; then
28         printf "Expected: %s\n" "$exp"
29         printf "Actual  : %s\n" "$act"
30         exit 1
31     fi
32 }
33
34 assert_file_exists() {
35     if [ ! -f "$1" ] ; then
36         echo "File $1 does not exist, but should"
37         exit 1
38     fi
39 }
40
41 assert_file_exists_not() {
42     if [ -f "$1" ] ; then
43         echo "File $1 exists, but should not"
44         exit 1
45     fi
46 }
47
48 setup() {
49 #    echo $*
50     rm -rf $root
51     mkdir -p $(dirname $hosts)
52     cat > $hosts <<EOF
53 127.0.0.1 localhost
54 EOF
55 }
56
57 setup "No /augeas/save"
58 run_augtool fail <<EOF
59 set /files/etc/hosts/1/ipaddr 127.0.0.2
60 rm /augeas/save
61 save
62 EOF
63 assert_ipaddr 127.0.0.1
64
65 setup "Invalid /augeas/save"
66 run_augtool fail <<EOF
67 set /files/etc/hosts/1/ipaddr 127.0.0.2
68 set /augeas/save "not a valid flag"
69 save
70 EOF
71 assert_ipaddr 127.0.0.1
72
73 setup "noop"
74 run_augtool fail <<EOF
75 set /files/etc/hosts/1/ipaddr 127.0.0.2
76 set /augeas/save noop
77 save
78 EOF
79 assert_ipaddr 127.0.0.1
80 assert_file_exists_not $hosts.augnew
81 assert_file_exists_not $hosts.augsave
82
83 setup "newfile"
84 run_augtool ok <<EOF
85 set /files/etc/hosts/1/ipaddr 127.0.0.2
86 set /augeas/save newfile
87 save
88 EOF
89 assert_ipaddr 127.0.0.1
90 assert_file_exists $hosts.augnew
91 assert_file_exists_not $hosts.augsave
92
93 setup "overwrite"
94 run_augtool ok <<EOF
95 set /files/etc/hosts/1/ipaddr 127.0.0.2
96 set /augeas/save overwrite
97 save
98 EOF
99 assert_ipaddr 127.0.0.2
100 assert_file_exists_not $hosts.augnew
101 assert_file_exists_not $hosts.augsave
102
103 setup "backup"
104 run_augtool ok <<EOF
105 set /files/etc/hosts/1/ipaddr 127.0.0.2
106 set /augeas/save backup
107 save
108 EOF
109 assert_ipaddr 127.0.0.2
110 assert_file_exists_not $hosts.augnew
111 assert_file_exists $hosts.augsave
112
113
114 augopts="${augopts} --autosave"
115
116 setup "autosave"
117 run_augtool ok <<EOF
118 set /files/etc/hosts/1/ipaddr 127.0.0.2
119 EOF
120 assert_ipaddr 127.0.0.2
121 assert_file_exists_not $hosts.augnew
122 assert_file_exists_not $hosts.augsave
123
124 setup "autosave command line"
125 run_augtool ok set /files/etc/hosts/1/ipaddr 127.0.0.2
126 assert_ipaddr 127.0.0.2
127 assert_file_exists_not $hosts.augnew
128 assert_file_exists_not $hosts.augsave