e71eafe563452262770da2854375520da13b4d8e
[openstack-build/cinder-build.git] / tests / runtests.sh
1 #! /bin/bash
2
3 set -o xtrace
4
5 EC=0
6 MYSQL_PASSWORD="cinder"
7
8 update_ec(){
9   local ec=$1
10   [ "$ec" -gt "$EC" ] && EC=$ec
11 }
12
13
14 function prepare_vm(){
15   if [[ $(cat /etc/*-release | head -n 1 | awk '{print $1}') =~ Ubuntu ]]
16   then
17     echo "mysql-server mysql-server/root_password select $MYSQL_PASSWORD" |
18       debconf-set-selections
19     echo "mysql-server mysql-server/root_password_again select $MYSQL_PASSWORD" |
20       debconf-set-selections
21     apt-get install -y mysql-server python-mysqldb rabbitmq-server curl screen nmap
22     CONF_FILE='/usr/share/cinder-common/cinder.conf'
23   else
24     yum install -y mysql-server mysql-wsrep-client-5.6 MySQL-python nmap-ncat
25     CONF_FILE='/usr/share/cinder/cinder-dist.conf'
26     # workaround for `sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError)
27     # (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")`
28     rm -f /etc/my.cnf
29     local current_password=$(cat /root/.mysql_secret | head -1 | awk -F': ' '{print $2}')
30     service mysql start
31     mysqladmin -u root -p"$current_password" password "$MYSQL_PASSWORD"
32   fi
33   mysql -uroot -p"$MYSQL_PASSWORD" << MYSQL_SCRIPT
34 CREATE DATABASE cinder;
35 CREATE USER 'cinder'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';
36 GRANT ALL PRIVILEGES ON *.* TO 'cinder'@'localhost';
37 MYSQL_SCRIPT
38   cinder-manage --config-file $CONF_FILE db sync
39 }
40
41
42 function test_cinder_service(){
43   local service=$1
44   local port=$2
45
46   echo "Starting $1..."
47   service "$service" start
48
49   sleep 5
50   echo | ncat -w 5 127.0.0.1 "$port"
51   local ec=$?
52   update_ec "$ec"
53
54   if [ "$ec" -eq 0 ]
55   then
56     echo "Cinder $1 successfully started"
57   else
58     echo "$service failed to start!"
59     tail -n 40 /var/log/messages
60   fi
61 }
62
63
64 function test_cinder_configs(){
65   local files=("/etc/cinder/cinder.conf"
66                "/etc/cinder/api-paste.ini"
67                "/etc/cinder/policy.json")
68   for f in "${files[@]}"
69   do
70     [ -f "$f" ] ; update_ec "$?"
71   done
72 }
73
74
75 case $1 in
76   python-cinder)
77     local imports=("cinder"
78                    "cinder.volume.drivers.lvm"
79                    "cinder.volume.drivers.rbd")
80     for x in "${imports[@]}"
81     do
82       python -c "import $x" ; update_ec "$?"
83     done
84   ;;
85   openstack-cinder)
86     prepare_vm
87     test_cinder_configs
88     ls /usr/bin/cinder* ; update_ec "$?"
89     test_cinder_service openstack-cinder-api 8776 ; update_ec "$?"
90     [ -f "/usr/bin/cinder-volume" ] ; update_ec "$?"
91     [ -f "/usr/bin/cinder-scheduler" ] ; update_ec "$?"
92   ;;
93   cinder-common)
94     test_cinder_configs
95     [ -f "/usr/bin/cinder-manage" ] ; update_ec "$?"
96   ;;
97   cinder-scheduler)
98     [ -f "/usr/bin/cinder-scheduler" ] ; update_ec "$?"
99   ;;
100   cinder-api)
101     prepare_vm
102     [ -f "/usr/bin/cinder-api" ] ; update_ec "$?"
103     test_cinder_service cinder-api 8776 ; update_ec "$?"
104   ;;
105   cinder-volume)
106     [ -f "/usr/bin/cinder-volume" ] ; update_ec "$?"
107   ;;
108   cinder-backup)
109     [ -f "/usr/bin/cinder-backup" ] ; update_ec "$?"
110   ;;
111   cinder-doc|openstack-cinder-doc)
112     echo "Skipping test for $1"
113     EC=0
114   ;;
115   *)
116     echo "Test not defined for $1"
117     EC=1
118   ;;
119 esac
120
121 exit "$EC"