#!/bin/bash

cvmfs_test_name="DNS Unreachable"
cvmfs_test_suites="quick"

do_faulty_mount() {
  cvmfs_mount $repo \
              "CVMFS_DNS_SERVER=127.0.0.2" \
              "CVMFS_TIMEOUT=3" \
              "CVMFS_TIMEOUT_DIRECT=3" \
              "CVMFS_MAX_RETRIES=1" \
              "CVMFS_HTTP_PROXY=DIRECT"
}

cvmfs_run_test() {
  logfile=$1

  local repo="atlas.cern.ch"

  echo "trying to mount $repo first"
  cvmfs_mount $repo || return 1

  echo "check if the repo is listable"
  ls /cvmfs/$repo || return 2

  echo "unmounting and cleaning"
  cvmfs_clean || return 3

  echo "trying to mount again with unreachable DNS"
  local milliseconds
  cvmfs_disable_config_repository
  # should fail
  if milliseconds=$(stop_watch do_faulty_mount); then
    # no grep -q: early exit SIGPIPEs cat_syslog under pipefail
    if cat_syslog | grep "libcurl lacks c-ares support" >/dev/null; then
      echo "WARNING: libcurl ignores CVMFS_DNS_SERVER, skipping"
      cvmfs_umount $repo
      cvmfs_enable_config_repository
      CVMFS_GENERAL_WARNING_FLAG=1
      return 0
    fi
    return 4
  fi
  cvmfs_enable_config_repository
  echo "timeout was $milliseconds seconds"

  # Unreachable name server should fail immediately
  local expected_timout_max=2000
  if [ $milliseconds -gt $expected_timout_max ]; then
    echo "timeout was too long: $milliseconds (expected at most $expected_timout_max)"
    CVMFS_TIME_WARNING_FLAG=1
  fi

  echo "check if repo is _not_ listable"
  ls /cvmfs/$repo && return 4

  return 0
}
