summaryrefslogtreecommitdiff
path: root/tests/crawl-test.sh
blob: a595446b5bb5c093b33bcac52dd6c050b98fdc07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
#

function warning {
  echo "** $*" >&2
  return 0
}

function die {
  warning "$*"
  exit 1
}

function debug {
  if test -n "$verbose"; then
    echo "$*" >&2
  fi
}

function info {
  printf "[$*] ..\t" >&2
}

function result {
  echo "$*" >&2
}

function cleanup {
  debug "cleaning function called"
  if test -n "$tmpdir"; then
    if test -d "$tmpdir"; then
      if test -z "$nopurge"; then
        debug "cleaning up $tmpdir"
        rm -rf "$tmpdir"
      fi
    fi
  fi
  if test -n "$crawlpid"; then
    debug "killing $crawlpid"
    kill -9 "$crawlpid"
    crawlpid=
  fi
}

function usage {
  cat << EOF
usage: $0
EOF
}

function assert_equals {
  info "$1"
  if test ! "$2" == "$3"; then
    result "expected '$2', got '$3'"
    exit 1
  else
    result "OK ($2)"
  fi
}

function start-crawl {
  # parse args
  pos=1
  while test "$#" -ge "$pos" ; do
    case "${!pos}" in
    --debug)
      verbose=1
      ;;
    --no-purge|--summary|--print-files)
      ;;
    --errors|--files|--found|--not-found|--directory)
      pos=$[${pos}+1]
      test "$#" -ge "$pos" || warning "missing argument" || return 1
      ;;
    httrack)
      pos=$[${pos}+1]
      break;
      ;;
    *)
      warning "unrecognized option ${!pos}"
      return 1
      ;;
    esac
    pos=$[${pos}+1]
  done
  debug "remaining args: ${@:${pos}}"

  # ut/ won't exceed 2 minutes
  moreargs="--quiet --max-time=120 --timeout=30 --connection-per-second=5"

  # proxy environment ?
  if test -n "$http_proxy"; then
    moreargs="$moreargs --proxy $http_proxy"
  fi

  test -n "$tmpdir" || ! warning "no tmpdir" || return 1
  tmp="${tmpdir}/crawl"
  rm -rf "$tmp"
  mkdir "$tmp" || ! warning "could not create $tmp" || return 1

  which httrack >/dev/null || ! warning "could not find httrack" || return 1
  ver=$(httrack -O /dev/null --version | sed -e 's/HTTrack version //')
  test -n "$ver" || ! warning "could not run httrack" || return 1

  # start crawl
  log="${tmp}/log"
  debug starting httrack -O "${tmp}" ${moreargs} ${@:${pos}}
  info "running httrack ${@:${pos}}"
  httrack -O "${tmp}" --user-agent="httrack $ver ut ($(uname -omrs))" ${moreargs} ${@:${pos}} >"${log}" 2>&1 &
  crawlpid="$!"
  debug "started cralwer on pid $crawlpid"
  wait "$crawlpid"
  result="$?"
  crawlpid=
  test "$result" -eq 0 || ! result "error code $result" || return 1
  result "OK"
  grep -iE "^[0-9\:]*[[:space:]]Error:" "${tmp}/hts-log.txt" >&2

  # now audit
  while test "$#" -gt 0; do
    case "$1" in
    --no-purge)
      nopurge=1
      ;;
    --summary)
      grep -E "^HTTrack Website Copier/[^ ]* mirror complete in " "${tmp}/hts-log.txt"
      ;;
    --print-files)
      find "${tmp}" -mindepth 1 -type f
      ;;
    --errors)
      shift
      assert_equals "checking errors" "$1" "$(grep -iEc "^[0-9\:]*[[:space:]]Error:" "${tmp}/hts-log.txt")"
      ;;
    --found)
      shift
      info "checking for $1"
      if test -f "${tmp}/$1" ; then
        result "OK"
      else
        result "not found"
        exit 1
      fi
      ;;
    --not-found)
      shift
      info "checking for $1"
      if test -f "${tmp}/$1" ; then
        result "OK"
      else
        result "not found"
        exit 1
      fi
      ;;
    --directory)
      shift
      info "checking for $1"
      if test -d "${tmp}/$1" ; then
        result "OK"
      else
        result "not found"
        exit 1
      fi
      ;;
    --files)
      shift
      nFiles=$(grep -E "^HTTrack Website Copier/[^ ]* mirror complete in " "${tmp}/hts-log.txt" \
        | sed -e 's/.*[[:space:]]\([^ ]*\)[[:space:]]files written.*/\1/g')
      assert_equals "checking files" "$1" "$nFiles"
      ;;
    httrack)
      break;
      ;;
    esac
    shift
  done

  # cleanup
  if test -z "$nopurge"; then
    rm -rf "$tmp"
  else
    tmpdir=
  fi
}

# check args
test "$#" -le 0 && usage && exit 0

# <http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html>
tmptopdir=${TMPDIR:-/tmp}
test -d "$tmptopdir" || mkdir -p "${tmptopdir}" || die "can not find a temporary directory ; please set TMPDIR"

# final cleanup
tmpdir=
crawlpid=
nopurge=
verbose=
trap "cleanup" 0 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25

# working directory
tmpdir="${tmptopdir}/httrack_ut.$$"
mkdir "${tmpdir}"

# rock'in
start-crawl "${@}"

# that's all, folks!