Turn the CLEANTEST smokes into full blown CPAN-based installs with testing
[dbsrgits/DBIx-Class.git] / maint / travis-ci_prepare_env
1 #!/bin/bash
2
3 # * The way .travis.yml is fed to the command controller is idiotic - it
4 # makes using multiline `bash -c` statements impossible. Therefore to
5 # aid readability (our travis logic is rather complex), the bulk of
6 # functionality is moved to a script. More about the problem here:
7 # https://github.com/travis-ci/travis-ci/issues/497
8
9 set -e
10
11 echo_err() { echo "$@" 1>&2 ; }
12
13 tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
14
15 run_or_err() {
16   echo_err -n "$(tstamp) $1 ... "
17
18   LASTOUT=$( bash -c "$2" 2>&1)
19   LASTEXIT=$?
20   if [[ "$LASTEXIT" != "0" ]] ; then
21     echo_err -e "FAILED !!!\nCommand executed:\n$2\nSTDOUT+STDERR:\n$LASTOUT"
22     return $LASTEXIT
23   else
24     echo_err "done."
25   fi
26 }
27
28 if [[ "$TRAVIS" != "true" ]] ; then
29   echo_err "Running this script makes no sense outside of travis-ci"
30   exit 1
31 fi
32
33 #
34 # Current dir is the root of the DBIC checkout (make sure to
35 # come back there if moving around before end of this script)
36 #
37 # envvars available for us:
38 #
39 # NUMTHREADS = { number}
40 #   dynamically determined amount of threads we want to run on this
41 #   smoker concurrently
42 #
43 # CLEANTEST = [ true | false ]
44 #   controls whether we simulate a "user-side" install experience
45 #   that is - no author deps and no M::I installation
46 #
47 # BREWVER = { tripple dotted perl version, e.g. 5.8.3 }
48 #   brew a custom perl version such and such
49 #
50 # BREWOPTS = { string to be fed unquoted to perlbrew, e.g. -Duseithreads }
51 #   build options for perlbrew
52 #
53
54 echo_err "$(tstamp) Smoke preconfiguration starting $(date)"
55
56 TRAVIS_CPAN_MIRROR=$(echo "$PERL_CPANM_OPT" | grep -oP -- '--mirror\s+\S+' | head -n 1 | cut -d ' ' -f 2)
57 if ! [[ "$TRAVIS_CPAN_MIRROR" =~ "http://" ]] ; then
58   echo_err "Unable to extract primary cpan mirror from PERL_CPANM_OPT - something is wrong"
59   echo_err "PERL_CPANM_OPT: $PERL_CPANM_OPT"
60   exit 1
61 fi
62
63 export PERL_MM_USE_DEFAULT=1 PERL_MM_NONINTERACTIVE=1 PERL_AUTOINSTALL_PREFER_CPAN=1 PERLBREW_CPAN_MIRROR="$TRAVIS_CPAN_MIRROR"
64
65 # Fixup CPANM_OPT to behave more like a traditional cpan client
66 export PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--skip-satisfied//' )"
67
68 if [[ -n "$BREWVER" ]] ; then
69   # if this is not master and not a smoke/ branch - cancel all testing
70   if [[ "$TRAVIS_BRANCH" =~ "topic/" ]]; then
71     export SHORT_CIRCUIT_SMOKE=1
72     echo_err "$(tstamp) non-smoke branch and custom perl compilation requested - bailing out"
73     sleep 20  # give the console time to attach, otherwise it hangs
74     return  # this is like an `exit 0` in sourcing
75   fi
76
77   run_or_err "Compiling/installing Perl $BREWVER (without testing, may take up to 5 minutes)" \
78     "perlbrew install --as $BREWVER --notest $BREWOPTS -j $NUMTHREADS perl-$BREWVER"
79
80   perlbrew use $BREWVER
81 fi
82
83 # configure CPAN.pm - older versions go into an endless loop
84 # when trying to autoconf themselves
85 CPAN_CFG_SCRIPT="
86   require CPAN;
87   require CPAN::FirstTime;
88   *CPAN::FirstTime::conf_sites = sub {};
89   CPAN::Config->load;
90   \$CPAN::Config->{urllist} = [qw{ $TRAVIS_CPAN_MIRROR }];
91   \$CPAN::Config->{halt_on_failure} = 1;
92   CPAN::Config->commit;
93 "
94 run_or_err "Configuring CPAN.pm" "perl -e '$CPAN_CFG_SCRIPT'"
95
96 extract_prereqs() {
97   # hack-hack-hack
98   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
99     || LASTEXIT=$?
100
101   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
102   ERR=$(grep -v " is up to date." <<< "${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}")
103
104   [[ -n "$LASTEXIT" ]] || LASTEXIT=0
105   if [[ "$LASTEXIT" != "0" ]] || [[ -n "$ERR" ]] ; then
106     echo_err "$(echo -e "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:\n$ERR\n$OUT")"
107     exit 1
108   fi
109
110   # throw away non-children (what was in $@), throw away ascii art, convert to modnames
111   perl -p -e 's/^\s*[^\\].+//; s/^[^A-Za-z]+//; s/\-[^\-]+$/ /; s/\-/::/g' <<< "$OUT"
112 }
113
114 parallel_installdeps_notest() {
115   if [[ -z "$@" ]] ; then return; fi
116
117   # flatten list into one string
118   MODLIST=$(echo "$@")
119
120   # The reason we do things so "non-interactively" is that xargs -P will have the
121   # latest cpanm instance overwrite the buildlog. There seems to be no way to
122   # specify a custom buildlog, hence we just collect the verbose output
123   # and display it in case of failure
124   run_or_err "Installing (without testing) $MODLIST" \
125     "echo $MODLIST | xargs -n 1 -P $NUMTHREADS cpanm --verbose --no-interactive --notest --no-man-pages"
126 }
127
128 if [[ "$CLEANTEST" = "true" ]]; then
129   # get the last inc/ off cpan - we will get rid of MI
130   # soon enough, but till then this will do
131   # the point is to have a *really* clean perl (the ones
132   # we build are guaranteed to be clean, without side
133   # effects from travis preinstalls)
134
135   # trick cpanm into executing true as shell - we just need the find+unpack
136   run_or_err "Downloading DBIC inc/ from CPAN" \
137     "SHELL=/bin/true cpanm --look DBIx::Class"
138
139   mv ~/.cpanm/latest-build/DBIx-Class-*/inc .
140 else
141   # we will be running all dbic tests - preinstall lots of stuff, run basic tests
142   # using SQLT and set up whatever databases necessary
143
144   # do the preinstall in several passes to minimize amount of cross-deps installing
145   # multiple times, and to avoid module re-architecture breaking another install
146   # (e.g. once Carp is upgraded there's no more Carp::Heavy)
147   #
148   parallel_installdeps_notest ExtUtils::MakeMaker
149   parallel_installdeps_notest Module::Build Carp
150   parallel_installdeps_notest Module::Runtime ExtUtils::Depends File::Spec
151   parallel_installdeps_notest Test::Exception Data::Dumper LWP
152   parallel_installdeps_notest Test::Fatal Test::Warn bareword::filehandles
153   parallel_installdeps_notest namespace::clean Class::XSAccessor MRO::Compat
154   parallel_installdeps_notest DBD::SQLite Moo Class::Accessor::Grouped
155   parallel_installdeps_notest Module::Install DateTime::Format::Strptime
156   parallel_installdeps_notest JSON::DWIW JSON JSON::XS Test::Pod::Coverage Test::EOL
157   parallel_installdeps_notest MooseX::Types JSON::Any Class::DBI
158
159   export DBICTEST_SQLT_DEPLOY=1
160
161 ### apt-get invocation - faster to grab everything at once
162   #
163   # FIXME these debconf lines should automate the firebird config but do not :(((
164   sudo bash -c 'echo -e "firebird2.5-super\tshared/firebird/enabled\tboolean\ttrue" | debconf-set-selections'
165   sudo bash -c 'echo -e "firebird2.5-super\tshared/firebird/sysdba_password/new_password\tpassword\t123" | debconf-set-selections'
166
167   APT_PACKAGES="memcached firebird2.5-super firebird2.5-dev expect"
168   run_or_err "Installing packages ($APT_PACKAGES)" "sudo apt-get install -y $APT_PACKAGES"
169
170 ### memcached
171   export DBICTEST_MEMCACHED=127.0.0.1:11211
172
173 ### mysql
174   run_or_err "Creating MySQL TestDB" "mysql -e 'create database dbic_test;'"
175   export DBICTEST_MYSQL_DSN='dbi:mysql:database=dbic_test;host=127.0.0.1'
176   export DBICTEST_MYSQL_USER=root
177
178 ### pg
179   run_or_err "Creating PostgreSQL TestDB" "psql -c 'create database dbic_test;' -U postgres"
180   export DBICTEST_PG_DSN='dbi:Pg:database=dbic_test;host=127.0.0.1'
181   export DBICTEST_PG_USER=postgres
182
183 ### firebird
184   # poor man's deb config
185   EXPECT_FB_SCRIPT='
186     spawn dpkg-reconfigure --frontend=text firebird2.5-super
187     expect "Enable Firebird server?"
188     send "\177\177\177\177yes\r"
189     expect "Password for SYSDBA"
190     send "123\r"
191     sleep 1
192     wait
193     sleep 1
194   '
195   run_or_err "Re-configuring Firebird" "
196     sync
197     DEBIAN_FRONTEND=text sudo expect -c '$EXPECT_FB_SCRIPT'
198     sleep 1
199     sync
200     # restart the server for good measure
201     sudo /etc/init.d/firebird2.5-super stop || true
202     sleep 1
203     sync
204     sudo /etc/init.d/firebird2.5-super start
205     sleep 1
206     sync
207   "
208
209   # creating testdb
210   # FIXME - this step still fails from time to time >:(((
211   # has to do with the FB reconfiguration I suppose
212   # for now if it fails - simply skip FB testing
213   if run_or_err "Creating Firebird TestDB" \
214     "echo \"CREATE DATABASE '/var/lib/firebird/2.5/data/dbic_test.fdb';\" | sudo isql-fb -u sysdba -p 123"
215   then
216     # the official version is full of 5.10-isms, but works perfectly fine on 5.8
217     # pull in our patched copy
218     run_or_err "Fetching patched DBD::Firebird" \
219       "git clone https://github.com/dbsrgits/perl-dbd-firebird-5.8.git ~/dbd-firebird"
220
221     # the official version is very much outdated and does not compile on 5.14+
222     # use this rather updated source tree (needs to go to PAUSE):
223     # https://github.com/pilcrow/perl-dbd-interbase
224     run_or_err "Fetching patched DBD::InterBase" \
225       "git clone https://github.com/dbsrgits/perl-dbd-interbase ~/dbd-interbase"
226
227     parallel_installdeps_notest ~/dbd-interbase/ ~/dbd-firebird/
228
229     export DBICTEST_FIREBIRD_DSN=dbi:Firebird:dbname=/var/lib/firebird/2.5/data/dbic_test.fdb
230     export DBICTEST_FIREBIRD_USER=SYSDBA
231     export DBICTEST_FIREBIRD_PASS=123
232
233     export DBICTEST_FIREBIRD_INTERBASE_DSN=dbi:InterBase:dbname=/var/lib/firebird/2.5/data/dbic_test.fdb
234     export DBICTEST_FIREBIRD_INTERBASE_USER=SYSDBA
235     export DBICTEST_FIREBIRD_INTERBASE_PASS=123
236   fi
237
238 ### oracle
239   # FIXME: todo
240   #DBICTEST_ORA_DSN=dbi:Oracle:host=localhost;sid=XE
241   #DBICTEST_ORA_USER=dbic_test
242   #DBICTEST_ORA_PASS=123
243   #DBICTEST_ORA_EXTRAUSER_DSN=dbi:Oracle:host=localhost;sid=XE
244   #DBICTEST_ORA_EXTRAUSER_USER=dbic_test_extra
245   #DBICTEST_ORA_EXTRAUSER_PASS=123
246   #ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/client
247 fi
248
249 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
250
251 # install the rest
252 if [[ "$CLEANTEST" = "true" ]]; then
253   # older perls do not have a CPAN which understands configure_requires
254   # properly and what is worse a `cpan Foo` run exits with 0 even if some
255   # modules failed to install
256   # The first CPAN which is somewhat sane is around 1.94_56 (perl 5.12)
257   # The problem is that the first sane version also brings a *lot* of
258   # deps with it, notably things like YAML and HTTP::Tiny
259   # The goal of CLEANTEST is to have as little extra stuff installed as
260   # possible, mainly to catch "but X is perl core" mistakes
261   # So instead we still use our stock (possibly old) CPAN, and add some
262   # handholding
263
264   CPAN_is_sane || \
265     run_or_err "Pre-installing ExtUtils::MakeMaker and Module::Build" \
266       "cpan ExtUtils::MakeMaker Module::Build"
267
268   if ! perl -MModule::Build -e 1 &> /dev/null ; then
269     echo_err -e "Module::Build installation failed\n$LASTOUT"
270     exit 1
271   fi
272
273   # DBI has by far the longest test runtime - run less tests
274   # FIXME horrible horrible hack, need to implement in DBI itself
275   run_or_err "Downloading latest DBI distdir from CPAN" \
276     "SHELL=/bin/true cpanm --look DBI"
277   cd ~/.cpanm/latest-build/DBI-*/
278   perl -p -i -e 's/(create_.+?_tests) => 1/$1 => 0/' Makefile.PL
279   run_or_err "Pre-installing DBI, but running less tests" "perl Makefile.PL && make && make test && make install"
280   cd - &>/dev/null
281
282   # generate the makefile which will have different deps depending on
283   # the runmode and envvars set above
284   run_or_err "Configure on current branch" "perl Makefile.PL"
285   HARD_DEPS="$(echo $(make listdeps))"
286
287   # this is a fucked CPAN - won't understand configure_requires of
288   # various pieces we may run into
289   CPAN_is_sane || HARD_DEPS="ExtUtils::Depends B::Hooks::OP::Check $HARD_DEPS"
290
291 ##### TEMPORARY WORKAROUNDS
292
293   # not sure what's going on here yet
294   perl -M5.008008 -e 1 &> /dev/null || \
295     parallel_installdeps_notest multidimensional bareword::filehandles
296
297   # work around Params::Validate not having a Makefile.PL so really old
298   # toolchains can not figure out what the prereqs are ;(
299   # Need to do more research before filing a bug requesting Makefile inclusion
300   perl -M5.008008 -e 1 &> /dev/null || \
301     HARD_DEPS="$(extract_prereqs Params::Validate) $HARD_DEPS"
302
303 ##### END TEMPORARY WORKAROUNDS
304
305   run_or_err "Installing/testing dependencies (may take up to 10 minutes): $HARD_DEPS" "cpan $HARD_DEPS"
306
307   # this is a fucked CPAN - save the log as we may need it
308   CPAN_is_sane || INSTALLDEPS_OUT="$LASTOUT"
309
310 else
311   # generate the makefile which will have different deps depending on
312   # the runmode and envvars set above
313   run_or_err "Configure on current branch" "perl Makefile.PL"
314
315   # listalldeps is deliberate - will upgrade everything it can find
316   parallel_installdeps_notest $(make listalldeps)
317 fi
318
319 echo_err "$(tstamp) Module configuration finished"
320 # this will display list of available versions
321 perl Makefile.PL
322
323 # make sure we got everything we need
324 if [[ -n "$(make listdeps)" ]] ; then
325   echo_err "$(tstamp) Not all deps installed - something went wrong :("
326   sleep 1 # without this the echo below confuses the console listener >.<
327   CPAN_is_sane || echo_err -e "Outdated CPAN.pm used - full logs follows\n$INSTALLDEPS_OUT\n\nSearch for 'NOT OK' in the text above\n\nDeps still missing:"
328   sleep 3 # without this the above echo confuses the console listener >.<
329   make listdeps
330   exit 1
331 fi
332
333 run_or_err "Prepare blib" "make pure_all"
334
335 # announce what are we running
336 echo_err "
337 ========================= CONFIGURATION COMPLETE ===========================
338 $(tstamp) Configuration phase seems to have taken $(date -ud "@$SECONDS" '+%H:%M:%S') (@$SECONDS)
339
340 = CPUinfo
341 $(perl -0777 -p -e 's/.+\n\n(?!\z)//s' < /proc/cpuinfo)
342
343 = Meminfo
344 $(free -m -t)
345
346 = Environment
347 $(env | grep -P 'TEST|TRAVIS|PERL|DBIC' | LC_ALL=C sort | cat -v)
348
349 = Perl in use
350 $(perl -V)
351 ============================================================================
352
353 $(tstamp) Starting tests using $NUMTHREADS concurrent processes"