Add OracleXE 10.2-based testing
[dbsrgits/DBIx-Class.git] / maint / travis-ci_scripts / common.bash
1 #!/bin/bash
2
3 set -e
4
5 TEST_STDERR_LOG=/tmp/dbictest.stderr
6
7 echo_err() { echo "$@" 1>&2 ; }
8
9 if [[ "$TRAVIS" != "true" ]] ; then
10   echo_err "Running this script makes no sense outside of travis-ci"
11   exit 1
12 fi
13
14 tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
15
16 run_or_err() {
17   echo_err -n "$(tstamp) $1 ... "
18
19   LASTEXIT=0
20   START_TIME=$SECONDS
21   # the tee is a handy debugging tool when stumpage is exceedingly strong
22   #LASTOUT=$( bash -c "$2" 2>&1 | tee /dev/stderr) || LASTEXIT=$?
23   LASTOUT=$( bash -c "$2" 2>&1 ) || LASTEXIT=$?
24   DELTA_TIME=$(( $SECONDS - $START_TIME ))
25
26   if [[ "$LASTEXIT" != "0" ]] ; then
27     echo_err "FAILED !!! (after ${DELTA_TIME}s)"
28     echo_err "Command executed:"
29     echo_err "$2"
30     echo_err "STDOUT+STDERR:"
31     echo_err "$LASTOUT"
32
33     return $LASTEXIT
34   else
35     echo_err "done (took ${DELTA_TIME}s)"
36   fi
37 }
38
39 apt_install() {
40   # flatten
41   pkgs="$@"
42
43   # Need to do this at every step, the sources list may very well have changed
44   run_or_err "Updating APT available package list" "sudo apt-get update"
45
46   run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated  --no-install-recommends -y $pkgs"
47 }
48
49 extract_prereqs() {
50   # once --verbose is set, --no-verbose can't disable it
51   # do this by hand
52   local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
53
54   # hack-hack-hack
55   LASTEXIT=0
56   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
57     || LASTEXIT=$?
58
59   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
60   ERR=$(grep -v " is up to date." <<< "${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}")
61
62   if [[ "$LASTEXIT" != "0" ]] ; then
63     echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
64     echo_err "$ERR"
65     echo_err "$OUT"
66     exit 1
67   fi
68
69   # throw away warnings, ascii art, convert to modnames
70   PQ=$(perl -p -e 's/^\!.*//; s/^[^a-z]+//i; s/\-[^\-]+$/ /; s/\-/::/g' <<< "$OUT")
71
72   # throw away what was in $@
73   for m in "$@" ; do
74     PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
75   done
76
77   # RV
78   echo "$PQ"
79 }
80
81 parallel_installdeps_notest() {
82   if [[ -z "$@" ]] ; then return; fi
83
84   # one module spec per line
85   MODLIST="$(printf '%s\n' "$@")"
86
87   # The reason we do things so "non-interactively" is that xargs -P will have the
88   # latest cpanm instance overwrite the buildlog. There seems to be no way to
89   # specify a custom buildlog, hence we just collect the verbose output
90   # and display it in case of "worker" failure
91   #
92   # Explanation of inline args:
93   #
94   # [09:38] <T> you need a $0
95   # [09:38] <G> hence the _
96   # [09:38] <G> bash -c '...' _
97   # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
98   # [09:39] <G> or --, yes
99   # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
100   #
101   run_or_err "Installing (without testing) $(echo $MODLIST)" \
102     "echo \\
103 \"$MODLIST\" \\
104       | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
105         'OUT=\$(cpanm --notest --no-man-pages \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
106         'giant space monkey penises'
107     "
108 }
109
110 installdeps() {
111   if [[ -z "$@" ]] ; then return; fi
112
113   echo_err "$(tstamp) Processing dependencies: $@"
114
115   local -x HARNESS_OPTIONS
116
117   HARNESS_OPTIONS="j$NUMTHREADS"
118
119   echo_err -n "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ... "
120
121   LASTEXIT=0
122   START_TIME=$SECONDS
123   LASTOUT=$( cpan_inst "$@" ) || LASTEXIT=$?
124   DELTA_TIME=$(( $SECONDS - $START_TIME ))
125
126   if [[ "$LASTEXIT" = "0" ]] ; then
127     echo_err "done (took ${DELTA_TIME}s)"
128   else
129     local errlog="after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel installfail" <<< "$LASTOUT")"
130     echo_err -n "failed ($errlog) retrying with sequential testing ... "
131     POSTMORTEM="$POSTMORTEM$(
132       echo
133       echo "Depinstall under $HARNESS_OPTIONS parallel testing failed $errlog"
134       echo "============================================================="
135       echo "Attempted installation of: $@"
136       echo "============================================================="
137     )"
138
139     HARNESS_OPTIONS=""
140     LASTEXIT=0
141     START_TIME=$SECONDS
142     LASTOUT=$( cpan_inst "$@" ) || LASTEXIT=$?
143     DELTA_TIME=$(( $SECONDS - $START_TIME ))
144
145     if [[ "$LASTEXIT" = "0" ]] ; then
146       echo_err "done (took ${DELTA_TIME}s)"
147     else
148       echo_err "FAILED !!! (after ${DELTA_TIME}s)"
149       echo_err "STDOUT+STDERR:"
150       echo_err "$LASTOUT"
151       exit 1
152     fi
153   fi
154
155   INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
156 }
157
158 cpan_inst() {
159   /usr/bin/timeout --kill-after=9.5m --signal=TERM 9m cpan "$@" 2>&1
160
161   # older perls do not have a CPAN which can exit with error on failed install
162   for m in "$@"; do
163     if ! perl -e '
164
165 eval ( q{require } . (
166   $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
167     ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
168     : $ARGV[0]
169 ) ) or ( print $@ and exit 1)' "$m" 2> /dev/null ; then
170
171       echo -e "$m installation seems to have failed"
172       return 1
173     fi
174   done
175 }
176
177 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
178
179 CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }