5 # Print a message and exit.
8 echo "$1" >&2 ; exit ${2:-1}
11 not_implemented_exit_code=1
18 A fake CTDB stub that prints items depending on the variables
19 FAKE_CTDB_PNN (default 0) depending on command-line options.
26 echo "${prog}: command \"$1\" not implemented in stub" >&2
27 exit $not_implemented_exit_code
30 # Don't set $POSIXLY_CORRECT here.
31 _temp=$(getopt -n "$prog" -o "Xvhn:" -l help -- "$@") || \
37 machine_readable=false
44 -X) machine_readable=true ; shift ;;
45 -v) verbose=true ; shift ;;
46 -n) nodespec="$2" ; shift 2 ;;
48 -h|--help|*) usage ;; # * shouldn't happen, so this is reasonable.
56 # Make sure tickles file exists.
57 tickles_file="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb/tickles"
58 mkdir -p $(dirname "$tickles_file")
69 echo "|source ip|port|destination ip|port|"
70 while read _src _dst ; do
71 if [ -z "$_ip" -o "$_ip" = "${_dst%:*}" ] ; then
72 if [ -z "$_port" -o "$_port" = "${_dst##*:}" ] ; then
73 echo "|${_src%:*}|${_src##*:}|${_dst%:*}|${_dst##*:}|"
86 if [ -n "$_dst" ] ; then
87 echo "${_src} ${_dst}" >>"$tickles_file"
100 if [ -n "$_dst" ] ; then
101 _t=$(grep -F -v "${_src} $${_dst}" "$tickles_file")
103 _t=$(cat "$tickles_file")
104 while read _src _dst ; do
105 _t=$(echo "$_t" | grep -F -v "${_src} ${_dst}")
108 echo "$_t" >"$tickles_file"
113 if [ "$nodespec" = "all" ] ; then
114 nodes="$(seq 0 $((FAKE_CTDB_NUMNODES - 1)) )"
115 elif [ -n "$nodespec" ] ; then
116 nodes="$(echo $nodespec | sed -e 's@,@ @g')"
122 # For testing backward compatibility...
123 for i in $CTDB_NOT_IMPLEMENTED ; do
124 if [ "$i" = "$1" ] ; then
132 echo "${FAKE_CTDB_PNN:-0}"
135 ######################################################################
137 FAKE_CTDB_NODE_STATE="$FAKE_CTDB_STATE/node-state"
138 FAKE_CTDB_NODES_DISABLED="$FAKE_CTDB_NODE_STATE/0x4"
140 ######################################################################
142 # NOTE: all nodes share public addresses file
144 FAKE_CTDB_IP_LAYOUT="$FAKE_CTDB_STATE/ip-layout"
148 touch "$FAKE_CTDB_IP_LAYOUT"
153 _pa="${CTDB_BASE}/public_addresses"
155 if [ ! -s "$FAKE_CTDB_IP_LAYOUT" ] ; then
156 sed -n -e 's@^\([^#][^/]*\)/.*@\1 -1@p' \
157 "$_pa" >"$FAKE_CTDB_IP_LAYOUT"
160 _t="${FAKE_CTDB_IP_LAYOUT}.new"
163 for _i in $(seq 0 $((FAKE_CTDB_NUMNODES - 1)) ) ; do
164 if ls "$FAKE_CTDB_STATE/node-state/"*"/$_i" >/dev/null 2>&1 ; then
165 # Have non-zero flags
167 for _j in "$FAKE_CTDB_STATE/node-state/"*"/$_i" ; do
168 _tf="${_j%/*}" # dirname
169 _f="${_tf##*/}" # basename
170 _this=$(( $_this | $_f ))
175 _flags="${_flags}${_flags:+,}${_this}"
177 CTDB_TEST_LOGLEVEL=NOTICE \
178 "ctdb_takeover_tests" \
179 "ipalloc" "$_flags" <"$FAKE_CTDB_IP_LAYOUT" |
181 mv "$_t" "$FAKE_CTDB_IP_LAYOUT"
182 ) <"$FAKE_CTDB_IP_LAYOUT"
187 # If nobody has done any IP-fu then generate a layout.
188 [ -f "$FAKE_CTDB_IP_LAYOUT" ] || ip_reallocate
192 if $machine_readable ; then
194 echo "|Public IP|Node|ActiveInterface|AvailableInterfaces|ConfiguredInterfaces|"
196 echo "|Public IP|Node|"
199 echo "Public IPs on node ${_mypnn}"
202 # Join public addresses file with $FAKE_CTDB_IP_LAYOUT, and
203 # process output line by line...
204 _pa="${CTDB_BASE}/public_addresses"
205 sed -e 's@/@ @' "$_pa" | sort | join - "$FAKE_CTDB_IP_LAYOUT" |
206 while read _ip _bit _ifaces _pnn ; do
208 # If more than 1 interface, assume all addresses are on the 1st.
209 _first_iface="${_ifaces%%,*}"
210 # Only show interface if address is on this node.
212 if [ "$_pnn" = "$_mypnn" ]; then
213 _my_iface="$_first_iface"
215 if $machine_readable ; then
216 echo "|${_ip}|${_pnn}|${_my_iface}|${_first_iface}|${_ifaces}|"
218 echo "${_ip} node[${_pnn}] active[${_my_iface}] available[${_first_iface}] configured[[${_ifaces}]"
221 if $machine_readable ; then
222 echo "|${_ip}|${_pnn}|"
224 echo "${_ip} ${_pnn}"
235 ip_reallocate # should be harmless and ensures we have good state
240 _t="${FAKE_CTDB_IP_LAYOUT}.new"
242 while read _i _pnn ; do
243 if [ "$_ip" = "$_i" ] ; then
249 mv "$_t" "$FAKE_CTDB_IP_LAYOUT"
250 ) <"$FAKE_CTDB_IP_LAYOUT"
253 ######################################################################
259 for _i in $nodes ; do
260 rm -f "${FAKE_CTDB_NODES_DISABLED}/${_i}"
270 for _i in $nodes ; do
271 mkdir -p "$FAKE_CTDB_NODES_DISABLED"
272 touch "${FAKE_CTDB_NODES_DISABLED}/${_i}"
278 ######################################################################
282 echo "CTDB says BYE!"
285 ######################################################################
287 # This is only used by the NAT and LVS gateway code at the moment, so
288 # use a hack. Assume that $CTDB_NATGW_NODES or $CTDB_LVS_NODES
289 # contains all nodes in the cluster (which is what current tests
290 # assume). Use the PNN to find the address from this file. The NAT
291 # gateway code only used the address, so just mark the node healthy.
294 echo '|Node|IP|Disconnected|Banned|Disabled|Unhealthy|Stopped|Inactive|PartiallyOnline|ThisNode|'
295 _line=$(( $FAKE_CTDB_PNN + 1 ))
296 _ip=$(sed -e "${_line}p" "${CTDB_NATGW_NODES:-${CTDB_LVS_NODES}}")
297 echo "|${FAKE_CTDB_PNN}|${_ip}|0|0|0|0|0|0|0|Y|"
300 ######################################################################
306 for _i in $FAKE_CTDB_TUNABLES_OK ; do
307 if [ "$_var" = "$_i" ] ; then
312 for _i in $FAKE_CTDB_TUNABLES_OBSOLETE ; do
313 if [ "$_var" = "$_i" ] ; then
314 echo "Setting obsolete tunable variable '${_var}'"
319 echo "Unable to set tunable variable '${_var}'"
323 ######################################################################
327 _t_dir="$EVENTSCRIPTS_TESTS_VAR_DIR/fake-ctdb/fake-tdb/$1"
333 echo "$2" >"${_t_dir}/$1"
361 _t_get "$2" >"$3" 2>/dev/null
368 while IFS="" read _line ; do
369 _k=$(echo "$_line" | sed -n -e 's@^"\([^"]*\)" "[^"]*"$@\1@p')
370 _v=$(echo "$_line" | sed -e 's@^"[^"]*" "\([^"]*\)"$@\1@')
371 [ -n "$_k" ] || die "ctdb ptrans: bad line \"${line}\""
372 if [ -n "$_v" ] ; then
384 # This will break on keys with spaces but we don't have any of
387 for _i in "${_t_dir}/"* ; do
388 [ -r "$_i" ] || continue
389 _k="${_i##*/}" # basename
391 _kn=$(echo -n "$_k" | wc -c)
392 _vn=$(echo -n "$_v" | wc -c)
394 key(${_kn}) = "${_k}"
397 data(${_vn}) = "${_v}"
400 _count=$(($_count + 1))
403 echo "Dumped ${_count} records"
406 ######################################################################
410 _f="${CTDB_BASE}/public_addresses"
412 if [ ! -f "$_f" ] ; then
413 die "Public addresses file \"${_f}\" not found"
417 echo "|Name|LinkStatus|References|"
418 while read _ip _iface ; do
423 # For now assume _iface contains only 1.
424 if [ -f "{FAKE_CTDB_IFACES_DOWN}/${_iface}" ] ; then
427 # Nobody looks at references
428 echo "|${_iface}|${_status}|0|"
439 # Existence of file means CTDB thinks interface is down.
440 _f="${FAKE_CTDB_IFACES_DOWN}/${_iface}"
445 *) die "ctdb setifacelink: unsupported interface status ${_state}" ;;
449 ######################################################################
455 for _i in $FAKE_TCP_LISTEN ; do
456 if [ "$_port" = "${_i##*:}" ] ; then
470 ######################################################################
477 # This could inadvertently run an external function instead of a local
478 # function. However, this can only happen if testing a script
479 # containing a new ctdb command that is not implemented, so this is
480 # unlikely to do harm.
481 if type "$func" >/dev/null 2>&1 ; then
484 not_implemented "$cmd"