change test utils to run from the source tree, #37340
[freeside.git] / FS-Test / bin / freeside-test-start
1 #!/bin/bash
2
3 VERSION='3.8.1'
4
5 set -ae
6
7 # get the parent directory of bin/(this script)
8 testroot=$( dirname ${BASH_SOURCE[0]} )
9 testroot=$( cd $testroot; cd ..; pwd )
10
11 echo "Stopping services."
12 sudo service freeside stop
13 sudo service apache2 stop
14
15 newname=freeside_`date +%Y%m%d`
16
17 # get company_name from existing DB, strip whitespace
18 # (if there is no existing DB, continue anyway)
19 testdbmarker=$(sudo -u freeside \
20   psql -tc 'select value from conf where name='\'company_name''\' | \
21   sed "s/^ //" \
22   || true
23 )
24
25 createdb=YES
26
27 if psql -l |grep -q '^ *freeside '; then
28   if [ "$testdbmarker" = "Freeside Test $VERSION" ]; then
29     createdb=NO
30     echo "Freeside test database found."
31   else
32     echo "Renaming database to $newname."
33     psql postgres -c "ALTER DATABASE freeside RENAME TO $newname"
34   fi
35 fi
36
37 if [ $createdb = YES ]; then
38   echo "Creating new database from stock schema."
39   createdb --owner=freeside freeside
40   sudo -u freeside psql freeside -q -f $testroot/share/test.sql > /dev/null
41 fi
42
43 if [ -f /usr/local/etc/freeside/htpasswd ]; then
44   newhtpasswd=htpasswd_`date +%Y%m%d`
45   echo "Renaming htpasswd to $newhtpasswd."
46   sudo mv /usr/local/etc/freeside/htpasswd /usr/local/etc/freeside/$newhtpasswd
47 fi
48
49 echo "Installing new htpasswd."
50 sudo cp $testroot/share/htpasswd /usr/local/etc/freeside/htpasswd
51
52 newtime=$(sudo -u freeside \
53   psql -tc 'SELECT to_timestamp(max(_date) + 86400) FROM cust_bill' \
54 )
55 # no longer needed since we don't run LaTeX in the test script
56 #echo "Setting clock to $newtime."
57 #sudo date -s "$newtime"
58
59 #echo "Applying upgrades."
60 #sudo -u freeside freeside-upgrade test
61 # shouldn't happen automatically
62
63 # start Apache on a future date when all of the invoice dates are in the past
64 echo "Restarting Apache."
65 sudo faketime "$newtime" apache2ctl start
66
67 echo "Done."