Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test alarm #160

Merged
merged 12 commits into from
Sep 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions config/test.alarm.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
############################ Alarm Config file#######################################

# Configuration for the alarm test script execution

CERT_PATH=../utils/server.crt
KEY_PATH=../utils/server.key

MONGO_HOST=localhost
REDIS_HOST=localhost

MONGO_PORT=27017
REDIS_PORT=6379
RUSH_PORT=5001

#Default values
REDIS_PATH="/usr/local/bin/redis-server"
MONGO_PATH="/usr/bin/mongod"

28 changes: 17 additions & 11 deletions lib/configBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ exports.consumer.casDir = '../utils/certs';

//Create Object container for further options. Do not touch!
exports.consumer.logger = {};
<<<<<<< HEAD
exports.consumer.logger.logLevel = 'warning';
=======


/*
Expand All @@ -215,6 +218,7 @@ exports.consumer.logger.logLevel = 'error';
InspectDepth.
Level of depth an object will be inspected for the log.
*/
>>>>>>> unstable
exports.consumer.logger.inspectDepth = 1;

/*
Expand Down Expand Up @@ -257,6 +261,9 @@ exports.consumer.logger.File = {

//Create Object container for further options. Do not touch!
exports.listener.logger = {};
<<<<<<< HEAD
exports.listener.logger.logLevel = 'warning';
=======


/*
Expand All @@ -270,6 +277,7 @@ exports.listener.logger.logLevel = 'error';
InspectDepth.
Level of depth an object will be inspected for the log.
*/
>>>>>>> unstable
exports.listener.logger.inspectDepth = 1;

/*
Expand Down Expand Up @@ -310,6 +318,15 @@ exports.listener.logger.File = {
=====================
*/

/*var gevLsnr = {};
gevLsnr.name = 'gevLsnr';
gevLsnr.mongoHost = gevlsnr_mongo;
gevLsnr.mongoPort = 27017;
gevLsnr.mongoDB = 'rush';
gevLsnr.collection = 'RushGeneric';
gevLsnr.filter = { state: 'error'};
gevLsnr.take = {id: 'id', topic: 'topic', body: 'task.body',
statusCode: 'result.statusCode'};*/

/* EvModules.
Arrays that contains the addons implemented for the listener or consumer. If you want
Expand All @@ -336,14 +353,3 @@ var gevlsnr_mongo = 'localhost';
if (process.env.RUSH_GEN_MONGO) {
gevlsnr_mongo = process.env.RUSH_GEN_MONGO;
}


//var gevLsnr = {};
//gevLsnr.name = 'gevLsnr';
//gevLsnr.mongoHost = gevlsnr_mongo;
//gevLsnr.mongoPort = 27017;
//gevLsnr.mongoDB = 'rush';
//gevLsnr.collection = 'RushGeneric';
//gevLsnr.filter = { state: 'error'};
//gevLsnr.take = {id: 'id', topic: 'topic', body: 'task.body',
// statusCode: 'result.statusCode'};
311 changes: 311 additions & 0 deletions scripts/test.alarms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
#!/bin/bash

LISTENER=../bin/listener

LOG=Rush_listener_$HOSTNAME.log

. ../config/test.alarm.cfg

RUSH_HOST=http://localhost:$RUSH_PORT

MANUAL=false

while getopts ":m" opt; do
case $opt in
m)
MANUAL=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done

TEST1=0
TEST2=0
TEST3=0
TEST4=0

red='\e[1;91m'
green='\e[1;32m'
endColor='\e[0m'

function stop_redis_wait {
echo -e "\n Now STOP Redis server at $REDIS_HOST:$REDIS_PORT"
while nc -z $REDIS_HOST $REDIS_PORT ##Wait until they are both up
do
sleep 1
done
}

function start_redis_wait {
echo -e "\n Now START Redis server at $REDIS_HOST:$REDIS_PORT"
until nc -z $REDIS_HOST $REDIS_PORT
do
sleep 1
done
}

function stop_mongo_wait {
echo -e "\n Now STOP Mongo server at $MONGO_HOST:$MONGO_PORT"
while nc -z $MONGO_HOST $MONGO_PORT ##Wait until they are both up
do
sleep 1
done
}

function start_mongo_wait {
echo -e "\n Now START Mongo server at $MONGO_HOST:$MONGO_PORT"
until nc -z $MONGO_HOST $MONGO_PORT
do
sleep 1
done
}


function kill_servers {
if nc -z $REDIS_HOST $REDIS_PORT; then
killall redis-server
fi
if nc -z $MONGO_HOST $MONGO_PORT; then
killall mongod
fi
while nc -z $REDIS_HOST $REDIS_PORT || nc -z $MONGO_HOST $MONGO_PORT;
do
sleep 1
done
}

function before {

rm -rf $LOG ##Remove log file

if $MANUAL ; then
start_redis_wait
start_mongo_wait
else
rm -rf mongo
mkdir mongo
$REDIS_PATH & ##Start redis-server
redis_pid=$!
$MONGO_PATH --dbpath ./mongo --quiet & #Start mongodb
mongo_pid=$!
until nc -z $MONGO_HOST $MONGO_PORT && nc -z $REDIS_HOST $REDIS_PORT ##Wait until they are both up
do
sleep 1
done
fi
}

function after {
if $MANUAL ; then
stop_redis_wait
stop_mongo_wait
else
kill $redis_pid #Stop redis
kill $mongo_pid #Stop mongo
rm -rf ./mongo #Remove db directories
while nc -z $REDIS_HOST $REDIS_PORT || nc -z $MONGO_HOST $MONGO_PORT;
do
sleep 1
done
fi
mv $LOG RUSH_LOG_`date -u +%Y-%m-%d-%H:%M`.log
}

function beforeEach {
sleep 1
}

function afterEach {
sleep 5
pid=$!
kill $pid
}

function cert_bak { #Certificates backup
mv -v $CERT_PATH $CERT_PATH.bak
mv -v $KEY_PATH $KEY_PATH.bak
}

function cert_recover { #Recover certificates
mv -v $CERT_PATH.bak $CERT_PATH
mv -v $KEY_PATH.bak $KEY_PATH
}

function no_certs_found { #Throw no certs found error
TEST1=1
cert_bak
beforeEach
$LISTENER &
afterEach
cert_recover
grep -q -e '| lvl=WARNING | op=LISTENER START UP | msg=Certs Not Found |' $LOG
if [ $? -ne 0 ]; then
TEST1=2
fi
}

function invalid_request {
TEST2=1
$LISTENER &
beforeEach
curl -v -H "X-Relayer-Host:nodejs.org" -H "X-relayer-persistence: INVALID_PERSISTENCE" $RUSH_HOST #nvalid persistence, should be logged
afterEach
grep -q -e '| lvl=WARNING | op=ASSIGN REQUEST | msg=Request Error |' $LOG
if [ $? -ne 0 ]; then
TEST2=2
fi
}

function redis_unavailable {
TEST3=1
if $MANUAL ; then
stop_redis_wait
else
kill $redis_pid ##Kill redis
fi

beforeEach
$LISTENER &
afterEach
grep -q -e '| lvl=ERROR | op=REDIS CONNECTION | msg=Redis Error |' $LOG
if [ $? -ne 0 ]; then
TEST3=2;
fi

if $MANUAL ; then
start_redis_wait
else
$REDIS_PATH & #Restart redis
redis_pid=$!
until nc -z $REDIS_HOST $REDIS_PORT ##Wait until it is up
do
sleep 1
done
fi
}

function mongo_unavailable {
TEST4=1
if $MANUAL ; then
stop_mongo_wait
else
kill $mongo_pid ##Kill redis
fi
beforeEach
$LISTENER &
afterEach
if $MANUAL ; then
start_mongo_wait
else
$MONGO_PATH --dbpath ./mongo --quiet & #Restart mongo
mongo_pid=$!
until nc -z $MONGO_HOST $MONGO_PORT #Wait until it is up
do
sleep 1
done
fi

grep -q -e '| lvl=WARNING | op=INIT EVENT LISTENER | msg=Could not connect with MongoDB |' $LOG
if [ $? -ne 0 ]; then
TEST4=2
fi
grep -q -e '| lvl=ERROR | op=ADD-ONS START UP | msg=Error subscribing event listener |' $LOG
if [ $? -ne 0 ]; then
TEST4=2
fi
}


function menu {
before

exec 2>&1
exec > >(tee ALARMS_LOG_`date -u +%Y-%m-%d-%H:%M`.log) # Save stdout to a file

case $option in
1)
no_certs_found
;;
2)
invalid_request
;;
3)
redis_unavailable
;;
4)
mongo_unavailable
;;
0)
no_certs_found
invalid_request
redis_unavailable
mongo_unavailable
;;
*) echo "Invalid input"
;;
esac
after
echo
echo TEST RESULTS:
echo ===========================
echo
if [ $TEST1 -eq 1 ]; then echo -e "${green}Scenario 1: ALARM Certs not found generated${endColor}"; fi
if [ $TEST1 -eq 2 ]; then echo -e "${red}Scenario 1: Should log Certs not found error${endColor}"; fi
if [ $TEST2 -eq 1 ]; then echo -e "${green}Scenario 2: ALARM Request Error generated${endColor}"; fi
if [ $TEST2 -eq 2 ]; then echo -e "${red}Scenario 2: Should log Request Error${endColor}"; fi
if [ $TEST3 -eq 1 ]; then echo -e "${green}Scenario 3: ALARM Redis Error generated${endColor}"; fi
if [ $TEST3 -eq 2 ]; then echo -e "${red}Scenario 3: Should log Redis Error${endColor}"; fi
if [ $TEST4 -eq 1 ]; then echo -e "${green}Scenario 4: ALARM MongoDB Error generated${endColor}"; fi
if [ $TEST4 -eq 2 ]; then echo -e "${red}Scenario 4: Should log Could not connect with MongoDB${endColor}"; fi
echo
}
echo "==================================================================="
echo " * ALARM TEST *"
echo "==================================================================="
echo ">> CONFIG << "
echo
echo "Configure Rush and Rush components in the config file:"
echo " - RUSH/config/test.alarm.cfg"
echo "Manual execution: "
echo " - For for manual steps execution launch the script using -m "
echo "==================================================================="
echo ">> OPTIONS << "
echo
echo -e "\tS: Stop redis-server and mongodb"
echo -e "\t1: Run Scenario 1. HTTPS certs not found"
echo -e "\t2: Run Scenario 2. Invalid request"
echo -e "\t3: Run Scenario 3. Redis server unavailable"
echo -e "\t4: Run Scenario 4. MongoDB unavailable"
echo -e "\t0: Run ALL tests"
echo
echo "==================================================================="
echo
echo -n "Choose one of the previous options: "
read option

if [ $option == "S" ]
then
kill_servers
exit 0
else
if nc -z $REDIS_HOST $REDIS_PORT; then
echo There is an active redis-server instance.
echo Stop it or run this script with flag -f
exit 1
fi

# if nc -z $MONGO_HOST $MONGO_PORT; then
# echo There is an active mongod instance.
# echo Stop it or run this script with flag -f
# exit 1
# fi
menu
fi

exit $return_value