a0989c2bc79c1f08ee6c54ba9fd5bf8f4fe2fb8c
[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 mock_flag_files=""
15
16 mk_mock_util() {
17     if [ ! "$IAmRoot" ]; then
18         # The repetitive stuff
19         cat << EOF > "$MOCKDIR/$1"
20 #!/bin/sh
21 EOF
22         /bin/chmod u+x $MOCKDIR/$1
23     else
24         touch $MOCKDIR/$1
25     fi
26     # Dynamic var names are ugly in shell, but useful.
27     eval mock_$1_flags=\$MOCKDIR/mock_\$1_flags
28     eval export mock_$1_flags
29     # Append to the list of flag files
30     mock_flag_files="$mock_flag_files mock_$1_flags"
31 }
32
33 ###############
34 # ps replacement
35 mk_mock_util "ps"
36 cat << EOF >> "${MOCKDIR}/ps"
37 echo "ps args: \${@}" > $mock_ps_flags
38 if [ -f "${MOCKDIR}/running" ]; then
39     ps_list="inetd xinetd launchd"
40 else
41     ps_list="foo bar baz"
42 fi
43
44 echo "faux process list: \$ps_list"
45 EOF
46
47 ###############
48 # chown replacement
49 mk_mock_util "chown"
50 cat << EOF >> "${MOCKDIR}/chown"
51 echo "chown args: \${@}" > $mock_chown_flags
52 EOF
53
54 ###############
55 # chmod replacement
56 mk_mock_util "chmod"
57 cat << EOF >> "${MOCKDIR}/chmod"
58 echo "chmod args: \${@}" > $mock_chmod_flags
59 EOF
60
61 ###############
62 # id replacemnt
63
64 # Set defaults for deb_uid and amanda_group, if running outside test_sh_libs
65 deb_uid=${deb_uid:=12345}
66 amanda_group=${amanda_group:=disk}
67
68 mk_mock_util "id"
69 cat << EOF >> "${MOCKDIR}/id"
70 echo "id args: \${1}" > $mock_id_flags
71 [ -n "\${1}" ] || { echo "Missing a username to id!"; exit 1; }
72
73 # We have to return the most basic form of id to be portable
74 # group file is used for supplemental groups.
75 if [ -f "${MOCKDIR}/is_member" ]; then
76     echo "uid=${deb_uid}(\${1}) gid=6(${amanda_group})"
77 else
78     echo "uid=123(\${1}) gid=123(foobar)"
79 fi
80 EOF
81
82 ###############
83 # svcs replacement
84 mk_mock_util "svcs"
85 cat << EOF >> "${MOCKDIR}/svcs"
86 echo "svcs args: \${@}" > $mock_svcs_flags
87 if [ -f "${MOCKDIR}/prexisting_service" ]; then
88     echo "online         Sep_09   svc:/network/amanda/tcp:default"
89 else
90     echo "Pattern '\${3}' doesn't match any instances"
91     exit 1
92 fi
93 EOF
94
95 ###############
96 # xinetd init script replacement
97 mk_mock_util xinetd
98 cat << EOF >> "${MOCKDIR}/xinetd"
99 echo "xinetd args: \${1}" > $mock_xinetd_flags
100 [ -f ${MOCKDIR}/success ] && exit 0
101 echo "xinetd did not \${1}"
102 exit 1
103 EOF
104
105 ###############
106 # inetd init script replacement
107 mk_mock_util inetd
108 cat << EOF >> "${MOCKDIR}/inetd"
109 echo "inetd args: \${1}" > $mock_inetd_flags
110 [ -f ${MOCKDIR}/success ] && exit 0
111 echo "inetd did not \${1}"
112 exit 1
113 EOF
114
115 ###############
116 # install replacement
117 mk_mock_util install
118 cat << EOF >> "${MOCKDIR}/install"
119 echo "install args: \$@" > $mock_install_flags
120 [ -f ${MOCKDIR}/success ] && exit 0
121 echo "Some funky install error, yo!"
122 exit 1
123 EOF
124
125 # TODO: inetconv inetadm, svcadm
126
127 # Touch all the flag files
128 for file in $mock_flag_files; do
129     touch $MOCKDIR/$file
130 done
131