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