lintian doesn't like orphan packages with uploaders...
[debian/amanda] / packaging / common / mock_utils.sh
1 #!/bin/sh
2
3 ## Mock builtin utilities
4 # These should preempt builtin programs.
5 # They should print expected output on STDOUT, and write given arguments to
6 # mock_<name>_flags.  Be sure to overwrite this file, not append!  If they need
7 # to act differently depending on a condition, use temp files in $MOCKDIR, and
8 # delete when you're done.  (Solaris shell does not re-export environment
9 # variables correctly).
10
11 MOCKDIR=$TMPDIR/mocks; export MOCKDIR
12 [ -d $MOCKDIR ] || mkdir $MOCKDIR
13 PATH=${MOCKDIR}:$PATH; export PATH
14 # A list of flag files that must be present to avoid spurious output.
15 # Each mock util gets one whether it is used or not.
16 mock_flag_files=""
17
18 mk_mock_util() {
19     if [ ! "$IAmRoot" ]; then
20         # The repetitive stuff
21         cat << EOF > "$MOCKDIR/$1"
22 #!/bin/sh
23 EOF
24         /bin/chmod u+x $MOCKDIR/$1
25     else
26         touch $MOCKDIR/$1
27     fi
28     # Dynamic var names are ugly in shell, but useful.
29     eval mock_$1_flags=\$MOCKDIR/mock_\$1_flags
30     eval export mock_$1_flags
31     # Append to the list of flag files
32     mock_flag_files="$mock_flag_files mock_$1_flags"
33 }
34
35 ###############
36 # ps replacement
37 mk_mock_util "ps"
38 cat << EOF >> "${MOCKDIR}/ps"
39 echo "ps args: \${@}" > $mock_ps_flags
40 if [ -f "${MOCKDIR}/running" ]; then
41     ps_list="inetd xinetd launchd"
42 else
43     ps_list="foo bar baz"
44 fi
45
46 echo "faux process list: \$ps_list"
47 EOF
48
49 ###############
50 # chown replacement
51 mk_mock_util "chown"
52 cat << EOF >> "${MOCKDIR}/chown"
53 echo "chown args: \${@}" > $mock_chown_flags
54 EOF
55
56 ###############
57 # chmod replacement
58 mk_mock_util "chmod"
59 cat << EOF >> "${MOCKDIR}/chmod"
60 echo "chmod args: \${@}" > $mock_chmod_flags
61 EOF
62
63 ###############
64 # id replacemnt
65
66 # Set vars for output of -Gn
67 id_0="biff"
68 id_1="biff foo"
69 id_2="biff bar baz"
70 id_3="biff zip bop whir"
71
72 # Set defaults for uid and amanda_group, if running outside test_sh_libs
73 deb_uid=${deb_uid:=12345}
74 amanda_group=${amanda_group:=disk}
75
76 mk_mock_util "id"
77 cat << EOF >> "${MOCKDIR}/id"
78 echo "id args: \${@}" > $mock_id_flags
79 [ -n "\${1}" ] || { echo "Missing a username to id!"; exit 1; }
80
81 # We can only use id with no flags to have consistent results.  Solaris
82 # /usr/bin/id does not provide any standard flags. Since /usr/xpg4/bin is
83 # not part of a minimal install we can't depend on it. Any flags *at all*
84 # should raise an error in the tests.
85 # group file and /usr/bin/groups can be used (with some tweaks).
86 for f in "\$@"; do
87     case \$f in
88         # -- is ok, surprisingly.
89         --) : ;;
90         -?*) echo "id: no options are portable! '\$f'"
91              # Solaris exits with 2, others with 1. ugh.
92              exit 2
93         ;;
94         *) : ;;
95     esac
96 done
97
98 # Use id_group to control primary group name.
99 [ -f ${MOCKDIR}/id_group ] && group=\`cat ${MOCKDIR}/id_group\` || group=bar
100
101 # Solaris, Linux and OSX differ in the exact format of the output from id,
102 # so we provide sample output based on the contents of id_os.  We don't
103 # parse it, but its presence can't break things.
104 test_os=\`cat ${MOCKDIR}/id_os\`
105 case \${test_os} in
106     linux) sup_groups=" groups=999(\${group}),1000(foo)" ;;
107     solaris) sup_groups="" ;;
108     osx) sup_groups=" groups=999(\${group}), 1000(foo)" ;;
109 esac
110
111 if [ -f ${MOCKDIR}/id_exists ]; then
112     # Note: uid is set when the mock is created.
113     echo "uid=${deb_uid}(\${1}) gid=6(\${group})\${sup_groups}"
114 else
115     echo "id: \${1}: no such user" >&2
116     exit 1
117 fi
118 EOF
119
120 ###############
121 # groupadd replacement
122 mk_mock_util "groupadd"
123 cat << EOF >> "${MOCKDIR}/groupadd"
124 echo "groupadd args: \${@}" > $mock_groupadd_flags
125 # We check for return codes of 0 (group added) or 9 (group existed) to
126 # continue in the function that uses groupadd
127 groupadd_rc=\`cat ${MOCKDIR}/groupadd_rc\`
128 exit \${groupadd_rc}
129 EOF
130
131 ###############
132 # groups replacement
133 mk_mock_util "groups"
134 cat << EOF >> "${MOCKDIR}/groups"
135 echo "groups args: \${@}" > $mock_groups_flags
136 cat ${MOCKDIR}/groups_output
137 EOF
138
139 ###############
140 # usermod replacement
141 mk_mock_util "usermod"
142 cat << EOF >> "${MOCKDIR}/usermod"
143 echo "usermod args: \${@}" > $mock_usermod_flags
144 # Protect against passing a blank username.
145 [ "x\${1}" = "x" ] && exit 2 || exit 0
146 EOF
147
148 ###############
149 # svcs replacement
150 mk_mock_util "svcs"
151 cat << EOF >> "${MOCKDIR}/svcs"
152 echo "svcs args: \${@}" > $mock_svcs_flags
153 if [ -f "${MOCKDIR}/prexisting_service" ]; then
154     echo "online         Sep_09   svc:/network/amanda/tcp:default"
155 else
156     echo "Pattern '\${3}' doesn't match any instances"
157     exit 1
158 fi
159 EOF
160
161 ###############
162 # xinetd init script replacement
163 mk_mock_util xinetd
164 cat << EOF >> "${MOCKDIR}/xinetd"
165 echo "xinetd args: \${@}" > $mock_xinetd_flags
166 [ -f ${MOCKDIR}/success ] && exit 0
167 echo "xinetd did not \${1}"
168 exit 1
169 EOF
170
171 ###############
172 # inetd init script replacement
173 mk_mock_util inetd
174 cat << EOF >> "${MOCKDIR}/inetd"
175 echo "inetd args: \${@}" > $mock_inetd_flags
176 [ -f ${MOCKDIR}/success ] && exit 0
177 echo "inetd did not \${1}"
178 exit 1
179 EOF
180
181 ###############
182 # install replacement
183 mk_mock_util install
184 cat << EOF >> "${MOCKDIR}/install"
185 echo "install args: \${@}" > $mock_install_flags
186 [ -f ${MOCKDIR}/success ] && exit 0
187 echo "Some funky install error, yo!"
188 exit 1
189 EOF
190
191 # TODO: inetconv inetadm, svcadm
192
193 # Touch all the flag files
194 for file in $mock_flag_files; do
195     touch $MOCKDIR/$file
196 done
197