diff options
-rw-r--r-- | setup-apkrepos.in | 24 | ||||
-rwxr-xr-x | tests/setup_alpine_test | 1 | ||||
-rwxr-xr-x | tests/setup_apkrepos_test | 20 |
3 files changed, 38 insertions, 7 deletions
diff --git a/setup-apkrepos.in b/setup-apkrepos.in index 5bd5417..aecbd7f 100644 --- a/setup-apkrepos.in +++ b/setup-apkrepos.in @@ -5,7 +5,7 @@ PREFIX=@PREFIX@ : ${LIBDIR=$PREFIX/lib} . "$LIBDIR/libalpine.sh" -MIRRORS_URL=https://mirrors.alpinelinux.org/mirrors.txt +: ${MIRRORS_URL:=https://mirrors.alpinelinux.org/mirrors.txt} if [ "$ROOT" != "/" ]; then apk_root_opt="--root $ROOT" @@ -33,7 +33,12 @@ ask_setup_method() { add_random_mirror() { local i=0 - local random_mirror_index="$(( $RANDOM % $(get_mirror_count) ))" + local count=$(get_mirror_count) + if [ ${count:-0} -eq 0 ]; then + echo "Warning! no mirror found" >&2 + return 1 + fi + local random_mirror_index="$(( $RANDOM % $count ))" printf %s "Picking random mirror... " for mirror in $MIRRORS; do @@ -70,7 +75,7 @@ add_fastest_mirror() { echo "Finding fastest mirror... " local fastest="$(find_fastest_mirror)" if [ -z "$fastest" ]; then - echo "Warning! No mirror found" + echo "Warning! No mirror found" >&2 return 1 fi add_mirror "$fastest" @@ -207,15 +212,22 @@ fi if $add_first; then set -- $MIRRORS - add_mirror "$1" && changed=true + if [ $# -eq 0 ]; then + echo "Warning! No mirror found" >&2 + exit 1 + fi + add_mirror "$1" || exit + changed=true fi if $add_random; then - add_random_mirror && changed=true + add_random_mirror || exit + changed=true fi if $add_fastest; then - add_fastest_mirror && changed=true + add_fastest_mirror || exit + changed=true fi if $add_first || $add_random || $add_fastest; then diff --git a/tests/setup_alpine_test b/tests/setup_alpine_test index 82e64b6..d7fbd87 100755 --- a/tests/setup_alpine_test +++ b/tests/setup_alpine_test @@ -8,6 +8,7 @@ init_tests \ setup_alpine_kvm_clock \ setup_alpine_restart_network +export WGETCONTENT="https://mirror.example.com" setup_alpine_usage_body() { test_usage setup-alpine } diff --git a/tests/setup_apkrepos_test b/tests/setup_apkrepos_test index f70b400..1998dbe 100755 --- a/tests/setup_apkrepos_test +++ b/tests/setup_apkrepos_test @@ -7,7 +7,8 @@ init_tests \ setup_apkrepos_https \ setup_apkrepos_random \ setup_apkrepos_first \ - setup_apkrepos_fastest + setup_apkrepos_fastest \ + setup_apkrepos_network_failure setup_apkrepos_usage_body() { test_usage setup-apkrepos @@ -55,3 +56,20 @@ setup_apkrepos_fastest_body() { -o match:"Added mirror a[0-9].example.com" \ setup-apkrepos -f } + +setup_apkrepos_network_failure_body() { + init_env + MIRRORS_URL=https://example.com/fail \ + atf_check -s not-exit:0 \ + -o match:"Finding" \ + -e match:"Warning! No mirror found" \ + setup-apkrepos -f + MIRRORS_URL=https://example.com/fail \ + atf_check -s not-exit:0 \ + -e match:"Warning" \ + setup-apkrepos -r + MIRRORS_URL=https://example.com/fail \ + atf_check -s not-exit:0 \ + -e match:"Warning! No mirror found" \ + setup-apkrepos -1 +} |