Skip to content

Instantly share code, notes, and snippets.

@phemmer
Last active December 29, 2015 07:17
Show Gist options
  • Select an option

  • Save phemmer/506366ce84f2db0cdc02 to your computer and use it in GitHub Desktop.

Select an option

Save phemmer/506366ce84f2db0cdc02 to your computer and use it in GitHub Desktop.
#!/bin/bash
docker=docker
cd $(mktemp -d)
cat > entrypoint.sh <<'EOF'
#!/bin/sh
cp /etc/resolv.conf /etc/resolv.conf.orig
echo "nameserver 127.0.0.1" > /etc/resolv.conf
echo "search example.com" >> /etc/resolv.conf
exec dnsmasq -d --user=root --resolv-file=/etc/resolv.conf.orig "$@"
EOF
chmod a+x entrypoint.sh
cat > Dockerfile <<'EOF'
FROM fedora
RUN dnf install -y dnsmasq bind-utils
ADD entrypoint.sh /entrypoint
ENTRYPOINT ["/entrypoint"]
EOF
$docker build -t dnstest/dnsmasq .
dnsmasq1_cid=$( $docker run -d --add-host bar.example.com:4.5.6.7 dnstest/dnsmasq --host-record=foo.example.com,1.2.3.4 )
dnsmasq1_ip=$( $docker inspect --format='{{.NetworkSettings.IPAddress}}' $dnsmasq1_cid )
echo "dnsmasq1: $dnsmasq1_cid"
dnsmasq2_cid=$( $docker run -d --dns=$dnsmasq1_ip --privileged dnstest/dnsmasq )
dnsmasq2_ip=$( $docker inspect --format='{{.NetworkSettings.IPAddress}}' $dnsmasq2_cid )
echo "dnsmasq2: $dnsmasq2_cid"
$docker exec $dnsmasq2_cid sh -c 'iptables -F; iptables -t nat -F; iptables -t mangle -F'
digs=(
${dnsmasq1_cid},localhost,a,foo.example.com,1.2.3.4
${dnsmasq1_cid},localhost,a,bar.example.com,4.5.6.7
${dnsmasq1_cid},localhost,any,foo.example.com,1.2.3.4
${dnsmasq1_cid},localhost,any,bar.example.com,4.5.6.7
${dnsmasq1_cid},${dnsmasq1_ip},a,foo.example.com,1.2.3.4
${dnsmasq1_cid},${dnsmasq1_ip},a,bar.example.com,4.5.6.7
${dnsmasq1_cid},${dnsmasq1_ip},any,foo.example.com,1.2.3.4
${dnsmasq1_cid},${dnsmasq1_ip},any,bar.example.com,4.5.6.7
${dnsmasq1_cid},${dnsmasq2_ip},a,foo.example.com,1.2.3.4
${dnsmasq1_cid},${dnsmasq2_ip},a,bar.example.com,4.5.6.7
${dnsmasq1_cid},${dnsmasq2_ip},any,foo.example.com,1.2.3.4
${dnsmasq1_cid},${dnsmasq2_ip},any,bar.example.com,4.5.6.7
${dnsmasq2_cid},localhost,a,foo.example.com,1.2.3.4
${dnsmasq2_cid},localhost,a,bar.example.com,4.5.6.7
${dnsmasq2_cid},localhost,any,foo.example.com,1.2.3.4
${dnsmasq2_cid},localhost,any,bar.example.com,4.5.6.7
${dnsmasq2_cid},${dnsmasq1_ip},a,foo.example.com,1.2.3.4
${dnsmasq2_cid},${dnsmasq1_ip},a,bar.example.com,4.5.6.7
${dnsmasq2_cid},${dnsmasq1_ip},any,foo.example.com,1.2.3.4
${dnsmasq2_cid},${dnsmasq1_ip},any,bar.example.com,4.5.6.7
${dnsmasq2_cid},${dnsmasq2_ip},a,foo.example.com,1.2.3.4
${dnsmasq2_cid},${dnsmasq2_ip},a,bar.example.com,4.5.6.7
${dnsmasq2_cid},${dnsmasq2_ip},any,foo.example.com,1.2.3.4
${dnsmasq2_cid},${dnsmasq2_ip},any,bar.example.com,4.5.6.7
)
for lookup in "${digs[@]}"; do
IFS=, read cid host type record result <<<"$lookup"
cmd=( dig +short @$host $type $record )
output="$( $docker exec $cid "${cmd[@]}" )"
if [[ "$output" != "$result" ]]; then
echo "${cid:0:12} --" "${cmd[@]}" "-- fail (expected=$result actual=$output)" >&2
else
echo "${cid:0:12} --" "${cmd[@]}" "-- pass"
fi
done
hosts=(
${dnsmasq1_cid},ahosts,foo.example.com,1.2.3.4
${dnsmasq1_cid},ahostsv4,foo.example.com,1.2.3.4
${dnsmasq1_cid},ahostsv6,foo.example.com,::ffff:1.2.3.4
${dnsmasq1_cid},ahosts,bar.example.com,4.5.6.7
${dnsmasq1_cid},ahostsv4,bar.example.com,4.5.6.7
${dnsmasq1_cid},ahostsv6,bar.example.com,::ffff:4.5.6.7
${dnsmasq2_cid},ahosts,foo.example.com,1.2.3.4
${dnsmasq2_cid},ahostsv4,foo.example.com,1.2.3.4
${dnsmasq2_cid},ahostsv6,foo.example.com,::ffff:1.2.3.4
${dnsmasq2_cid},ahosts,bar.example.com,4.5.6.7
${dnsmasq2_cid},ahostsv4,bar.example.com,4.5.6.7
${dnsmasq2_cid},ahostsv6,bar.example.com,::ffff:4.5.6.7
${dnsmasq1_cid},ahosts,foo,1.2.3.4
${dnsmasq1_cid},ahostsv4,foo,1.2.3.4
${dnsmasq1_cid},ahostsv6,foo,::ffff:1.2.3.4
${dnsmasq1_cid},ahosts,bar,4.5.6.7
${dnsmasq1_cid},ahostsv4,bar,4.5.6.7
${dnsmasq1_cid},ahostsv6,bar,::ffff:4.5.6.7
${dnsmasq2_cid},ahosts,foo,1.2.3.4
${dnsmasq2_cid},ahostsv4,foo,1.2.3.4
${dnsmasq2_cid},ahostsv6,foo,::ffff:1.2.3.4
${dnsmasq2_cid},ahosts,bar,4.5.6.7
${dnsmasq2_cid},ahostsv4,bar,4.5.6.7
${dnsmasq2_cid},ahostsv6,bar,::ffff:4.5.6.7
)
for host in "${hosts[@]}"; do
IFS=, read cid type record result <<<"$host"
cmd=( getent $type $record )
cmdstr="$( printf '%q ' "${cmd[@]}" )"
output="$( $docker exec $cid sh -c "$cmdstr | awk '\$2 == \"STREAM\" { print \$1 }'" )"
if [[ "$output" != "$result" ]]; then
echo "${cid:0:12} --" "${cmd[@]}" "-- fail (expected=$result actual=$output)" >&2
else
echo "${cid:0:12} --" "${cmd[@]}" "-- pass"
fi
done
$docker rm -fv $dnsmasq2_cid
$docker rm -fv $dnsmasq1_cid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment