Skip to content

Commit c45eff6

Browse files
committed
Refactor: Improve uninstall function and tests
1 parent 7af530a commit c45eff6

File tree

2 files changed

+122
-88
lines changed

2 files changed

+122
-88
lines changed

test/ufw-docker.test.sh

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ source "$working_dir"/bach/bach.sh
2424

2525
builtin source <(@sed -n -e '/^# UFW-DOCKER GLOBAL VARIABLES START #$/,/^# UFW-DOCKER GLOBAL VARIABLES END #$/{' -e '/^PATH=/d' -e 'p' -e '}' "$working_dir/../ufw-docker")
2626
UFW_DOCKER_AGENT_IMAGE=chaifeng/ufw-docker-agent:090502-legacy
27+
28+
@mock man-page === @stdout "MAN PAGE FOR UFW-DOCKER"
2729
}
2830

2931
function ufw-docker() {
@@ -861,7 +863,6 @@ EOF
861863
}
862864

863865
test-man-command() {
864-
@mock man-page === @stdout "MAN PAGE FOR UFW-DOCKER"
865866
@capture man -l -
866867

867868
ufw-docker man
@@ -875,7 +876,6 @@ test-install-command-with-system() {
875876
@mock ufw-docker--check-install_ipv6 === @true
876877
@allow-real dirname /usr/local/bin/ufw-docker
877878
@allow-real dirname /usr/local/man/man8/ufw-docker.8
878-
@mock man-page === @stdout "MAN PAGE FOR UFW-DOCKER"
879879
@capture tee /usr/local/man/man8/ufw-docker.8
880880

881881
load-ufw-docker-function ufw-docker--install
@@ -906,37 +906,99 @@ test-check-command-with-system-assert() {
906906
err "Installing man page to '/usr/local/man/man8/ufw-docker.8'"
907907
}
908908

909-
test-uninstall() {
910-
after_rules="/dev/null"
911-
after6_rules="/dev/null"
912-
test_file="after.rules-ufw-docker~2015-03-07-141100~"
913-
test6_file="after6.rules-ufw-docker~2015-03-07-141100~"
914-
@mock get_restore_file === @stdout $test_file
915-
@mock get_restore6_file === @stdout $test6_file
916-
@mock command -v ip6tables === true
917-
@mock dirname $man_location === @stdout /dev/null
918-
@mock dirname $bin_location === @stdout /dev/null
919-
load-ufw-docker-function ufw-docker--uninstall
920-
921-
ufw-docker--uninstall
922-
}
923-
test-uninstall-assert() {
924-
after_rules="/dev/null"
925-
after6_rules="/dev/null"
926-
test_file="after.rules-ufw-docker~2015-03-07-141100~"
927-
test6_file="after6.rules-ufw-docker~2015-03-07-141100~"
928-
929-
cp $test_file $after_rules
930-
cp $test6_file $after6_rules
931-
shopt -s nullglob
932-
dirname $after_rules
933-
cd
934-
rm -- after.rules-ufw-docker~*-*-*-*~
935-
cd -
936-
dirname $after6_rules
937-
cd
938-
rm -- after6.rules-ufw-docker~*-*-*-*~
939-
cd -
940-
shopt -u nullglob
941-
rm -f /usr/local/man/man8/ufw-docker.8 /usr/local/bin/ufw-docker
909+
setup-ufw-docker--uninstall() {
910+
@mock date '+%Y-%m-%d-%H%M' === @stdout 2009-02-14-0731
911+
912+
@mocktrue grep -F 'UFW DOCKER' /etc/ufw/after.rules
913+
@mocktrue grep -F 'UFW DOCKER' /etc/ufw/after6.rules
914+
915+
@mocktrue docker service inspect ufw-docker-agent
916+
917+
@mocktrue [ -f /usr/local/bin/ufw-docker ]
918+
@mocktrue [ -f /usr/local/man/man8/ufw-docker.8 ]
919+
920+
@mocktrue type systemctl
921+
}
922+
test-ufw-docker--uninstall() {
923+
setup-ufw-docker--uninstall
924+
925+
load-ufw-docker-function ufw-docker--uninstall
926+
927+
ufw-docker--uninstall
928+
}
929+
test-ufw-docker--uninstall-assert() {
930+
cp -v /etc/ufw/after.rules /etc/ufw/after.rules~2009-02-14-0731
931+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' /etc/ufw/after.rules
932+
diff /etc/ufw/after.rules~2009-02-14-0731 /etc/ufw/after.rules
933+
934+
cp -v /etc/ufw/after6.rules /etc/ufw/after6.rules~2009-02-14-0731
935+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' /etc/ufw/after6.rules
936+
diff /etc/ufw/after6.rules~2009-02-14-0731 /etc/ufw/after6.rules
937+
938+
docker service rm ufw-docker-agent
939+
940+
rm -v /usr/local/bin/ufw-docker
941+
rm -v /usr/local/man/man8/ufw-docker.8
942+
}
943+
944+
test-ufw-docker--uninstall-missing-rules() {
945+
setup-ufw-docker--uninstall
946+
@mockfalse grep -F 'UFW DOCKER' /etc/ufw/after.rules
947+
@mockfalse grep -F 'UFW DOCKER' /etc/ufw/after6.rules
948+
949+
load-ufw-docker-function ufw-docker--uninstall
950+
951+
ufw-docker--uninstall
952+
}
953+
test-ufw-docker--uninstall-missing-rules-assert() {
954+
# Expect no cp or sed calls for after.rules/after6.rules
955+
docker service rm ufw-docker-agent
956+
957+
rm -v /usr/local/bin/ufw-docker
958+
rm -v /usr/local/man/man8/ufw-docker.8
959+
}
960+
961+
test-ufw-docker--uninstall-no-service() {
962+
setup-ufw-docker--uninstall
963+
@mockfalse docker service inspect ufw-docker-agent # Service not found
964+
965+
load-ufw-docker-function ufw-docker--uninstall
966+
967+
ufw-docker--uninstall
968+
}
969+
test-ufw-docker--uninstall-no-service-assert() {
970+
cp -v /etc/ufw/after.rules /etc/ufw/after.rules~2009-02-14-0731
971+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' /etc/ufw/after.rules
972+
diff /etc/ufw/after.rules~2009-02-14-0731 /etc/ufw/after.rules
973+
974+
cp -v /etc/ufw/after6.rules /etc/ufw/after6.rules~2009-02-14-0731
975+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' /etc/ufw/after6.rules
976+
diff /etc/ufw/after6.rules~2009-02-14-0731 /etc/ufw/after6.rules
977+
978+
# Expect no docker service rm call
979+
rm -v /usr/local/bin/ufw-docker
980+
rm -v /usr/local/man/man8/ufw-docker.8
981+
}
982+
983+
test-ufw-docker--uninstall-missing-files() {
984+
setup-ufw-docker--uninstall
985+
@mockfalse [ -f /usr/local/bin/ufw-docker ] # Binary missing
986+
@mockfalse [ -f /usr/local/man/man8/ufw-docker.8 ] # Man page missing
987+
988+
load-ufw-docker-function ufw-docker--uninstall
989+
990+
ufw-docker--uninstall
991+
}
992+
test-ufw-docker--uninstall-missing-files-assert() {
993+
cp -v /etc/ufw/after.rules /etc/ufw/after.rules~2009-02-14-0731
994+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' /etc/ufw/after.rules
995+
diff /etc/ufw/after.rules~2009-02-14-0731 /etc/ufw/after.rules
996+
997+
cp -v /etc/ufw/after6.rules /etc/ufw/after6.rules~2009-02-14-0731
998+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' /etc/ufw/after6.rules
999+
diff /etc/ufw/after6.rules~2009-02-14-0731 /etc/ufw/after6.rules
1000+
1001+
docker service rm ufw-docker-agent
1002+
1003+
# Expect no rm calls for missing files
9421004
}

ufw-docker

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ function ufw-docker--install() {
578578

579579
err "\\nInstalling man page to '${man_location}'"
580580
mkdir -p "$(dirname "${man_location}")"
581-
man-page | tee "${man_location}"
581+
man-page | tee "${man_location}" >/dev/null
582582
mandb -q
583583
fi
584584

@@ -608,63 +608,35 @@ function get_restore6_file() {
608608
}
609609

610610
function ufw-docker--uninstall() {
611-
# restore ufw rules
612-
local restore_failed=false
613-
local failed=false
614-
615-
restore_file=$(get_restore_file || true)
616-
if [ -z "${restore_file}" ]; then
617-
restore_failed=true
618-
else
619-
err "restoring '${after_rules}'"
620-
cp ${restore_file} ${after_rules}
611+
local changed=false
612+
local backup_suffix="$(date '+%Y-%m-%d-%H%M')"
613+
if grep -F 'UFW DOCKER' "$after_rules" >/dev/null; then
614+
cp -v "$after_rules" "${after_rules}~${backup_suffix}"
615+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' "$after_rules"
616+
! diff "${after_rules}~${backup_suffix}" "${after_rules}"
617+
changed=true
621618
fi
622-
623-
if command -v ip6tables >/dev/null 2>&1; then
624-
restore6_file=$(get_restore6_file || true)
625-
if [ -z "${restore6_file}" ]; then
626-
restore_failed=true
627-
else
628-
err "restoring '${after6_rules}'"
629-
cp ${restore6_file} ${after6_rules}
630-
fi
619+
if grep -F 'UFW DOCKER' "$after6_rules" >/dev/null; then
620+
cp -v "$after6_rules" "${after6_rules}~${backup_suffix}"
621+
sed -i -e '/^# BEGIN UFW AND DOCKER/,/^# END UFW AND DOCKER/d' "$after6_rules"
622+
! diff "${after6_rules}~${backup_suffix}" "${after6_rules}"
623+
changed=true
631624
fi
632625

633-
if "$restore_failed"; then
634-
err "restoring of some files failed, cleanup '${after_rules}' and '${after6_rules}' yourself."
635-
failed=true
636-
else
637-
# delete backup files
638-
err "deleting backup files"
639-
640-
shopt -s nullglob # allow '*' expansion
641-
cd $(dirname ${after_rules})
642-
local files=( $file_name )
643-
#printf '%s\n' "${files[@]}"
644-
rm -- ${files[@]}
645-
cd - >/dev/null
646-
647-
cd $(dirname ${after6_rules})
648-
local files6=( $file6_name )
649-
#printf '%s\n' "${files6[@]}"
650-
rm -- ${files6[@]}
651-
cd - >/dev/null
652-
shopt -u nullglob
626+
if docker service inspect "$ufw_docker_agent" >/dev/null; then
627+
docker service rm "$ufw_docker_agent"
653628
fi
654629

655-
# delete files
656-
if [ -n "$(dirname ${man_location})" ] && [ -n "$(dirname ${bin_location})" ]; then
657-
err "deleting: $man_location, $bin_location"
658-
rm -f ${man_location} ${bin_location}
659-
else
660-
err "'\$man_location' or '\$bin_location' are not valid, will not remove any files."
661-
failed=true
662-
fi
663-
664-
if ! "$failed"; then
665-
err "\e[1mUninstall successful!\e[0m"
666-
else
667-
exit 1
630+
if [ -f "$bin_location" ]; then rm -v "$bin_location"; fi
631+
if [ -f "$man_location" ]; then rm -v "$man_location"; fi
632+
633+
if "$changed"; then
634+
err "\\nPlease restart UFW service manually by using the following command:"
635+
if type systemctl &>/dev/null; then
636+
err " sudo systemctl restart ufw"
637+
else
638+
err " sudo service ufw restart"
639+
fi
668640
fi
669641
}
670642

0 commit comments

Comments
 (0)