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).
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.
19 if [ ! "$IAmRoot" ]; then
20 # The repetitive stuff
21 cat << EOF > "$MOCKDIR/$1"
24 /bin/chmod u+x $MOCKDIR/$1
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"
38 cat << EOF >> "${MOCKDIR}/ps"
39 echo "ps args: \${@}" > $mock_ps_flags
40 if [ -f "${MOCKDIR}/running" ]; then
41 ps_list="inetd xinetd launchd"
46 echo "faux process list: \$ps_list"
52 cat << EOF >> "${MOCKDIR}/chown"
53 echo "chown args: \${@}" > $mock_chown_flags
59 cat << EOF >> "${MOCKDIR}/chmod"
60 echo "chmod args: \${@}" > $mock_chmod_flags
66 # Set vars for output of -Gn
70 id_3="biff zip bop whir"
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}
77 cat << EOF >> "${MOCKDIR}/id"
78 echo "id args: \${@}" > $mock_id_flags
79 [ -n "\${1}" ] || { echo "Missing a username to id!"; exit 1; }
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).
88 # -- is ok, surprisingly.
90 -?*) echo "id: no options are portable! '\$f'"
91 # Solaris exits with 2, others with 1. ugh.
98 # Use id_group to control primary group name.
99 [ -f ${MOCKDIR}/id_group ] && group=\`cat ${MOCKDIR}/id_group\` || group=bar
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\`
106 linux) sup_groups=" groups=999(\${group}),1000(foo)" ;;
107 solaris) sup_groups="" ;;
108 osx) sup_groups=" groups=999(\${group}), 1000(foo)" ;;
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}"
115 echo "id: \${1}: no such user" >&2
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\`
133 mk_mock_util "groups"
134 cat << EOF >> "${MOCKDIR}/groups"
135 echo "groups args: \${@}" > $mock_groups_flags
136 cat ${MOCKDIR}/groups_output
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
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"
156 echo "Pattern '\${3}' doesn't match any instances"
162 # xinetd init script replacement
164 cat << EOF >> "${MOCKDIR}/xinetd"
165 echo "xinetd args: \${@}" > $mock_xinetd_flags
166 [ -f ${MOCKDIR}/success ] && exit 0
167 echo "xinetd did not \${1}"
172 # inetd init script replacement
174 cat << EOF >> "${MOCKDIR}/inetd"
175 echo "inetd args: \${@}" > $mock_inetd_flags
176 [ -f ${MOCKDIR}/success ] && exit 0
177 echo "inetd did not \${1}"
182 # install replacement
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!"
191 # TODO: inetconv inetadm, svcadm
193 # Touch all the flag files
194 for file in $mock_flag_files; do