#!/bin/bash
#----------------------
# Testing cinder-volume
#----------------------
set -e
apt-get install -y cinder-volume cinder-backup 2>&1 > /dev/null
DAEMONS=('cinder-volume' 'cinder-backup')
failure=false

for daemon in "${DAEMONS[@]}"; do
    systemctl restart $daemon
done

for daemon in "${DAEMONS[@]}"; do
    TIMEOUT=50
    while [ "$TIMEOUT" -gt 0 ]; do
        if pidof -x $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"
        failure=true
    else
        echo "${daemon} IS RUNNING"
    fi
done

apt-get remove -y cinder-volume cinder-backup 2>&1 > /dev/null

if [ "$failure" = true ]; then
    exit 1
fi
