#!/bin/bash
#---------------------
# Testing gnocchi-daemons
#---------------------
set -e
DAEMONS=('apache2' 'gnocchi-statsd' 'gnocchi-metricd')

ret=0

mysql -u root << EOF
CREATE DATABASE gnocchi;
GRANT ALL PRIVILEGES ON gnocchi.* TO 'gnocchi'@'localhost' \
  IDENTIFIED BY 'changeme';
GRANT ALL PRIVILEGES ON gnocchi.* TO 'gnocchi'@'%' \
  IDENTIFIED BY 'changeme';
EOF

crudini --set /etc/gnocchi/gnocchi.conf indexer url "mysql://gnocchi:changeme@localhost/gnocchi"
crudini --set /etc/gnocchi/gnocchi.conf statsd resource_id "`uuidgen`"

gnocchi-upgrade

for daemon in "${DAEMONS[@]}"; do
    systemctl restart $daemon
    TIMEOUT=50
    while [ "$TIMEOUT" -gt 0 ]; do
        if systemctl is-active $daemon > /dev/null; then
            echo "OK"
            break
        fi
        TIMEOUT=$((TIMEOUT - 1))
        sleep 0.1
    done

    if [ "$TIMEOUT" -le 0 ]; then
        echo "ERROR: ${daemon} IS NOT RUNNING"
        ret=1
    fi
done

exit $ret
