RackHD/on-core

View on GitHub
HWIMO-TEST

Summary

Maintainability
Test Coverage
#!/bin/bash -e
# Copyright 2015, EMC, Inc.

checkDependencies(){
    mongo_path=$( which mongod )
    if [ -z "$mongo_path" ]; then
        echo "[ERROR]: the unit test requires mongodb service installed"
        exit 1
    fi
    rabbitmq_path=$( which rabbitmq-server )
    if [ -z "$rabbitmq_path" ]; then
        echo "[ERROR]: the unit test requires rabbitmq service installed"
        exit 1
    fi

    # mongod works for version after 2.6
    # mongodb works for version before 2.6
    mongo_status=$(service mongodb status || service mongod status)
    export is_mongo_running=$(echo $mongo_status |grep running)
    export rabbitmq_status=$(sudo service rabbitmq-server status|grep pid)

}

cleanDatabase(){
    mongo pxe --eval "db.dropDatabase()"
    mongo monorail-test --eval "db.dropDatabase()"
}

handleDependServices(){
    action=$1
    if [ -z "$is_mongo_running" ]; then
        echo "[INFO]: $action mongodb service"
        sudo service mongodb $action || sudo service mongod $action
    fi

    if [ -z "$rabbitmq_status" ]; then
        echo "[INFO]: $action rabbitmq service"
        sudo service rabbitmq-server $action
    fi
}

prepareDependencies(){
    handleDependServices start
    sleep 2
    cleanDatabase
}

restoreDependencies(){
    cleanDatabase
    handleDependServices stop
}

build(){
    rm -rf node_modules
    npm install
}

unitTest(){
    # Checks the code against the jshint options
    ./node_modules/.bin/jshint -c .jshintrc --reporter=checkstyle lib index.js > checkstyle-result.xml || true
    ./node_modules/.bin/jshint -c .jshintrc lib index.js || true

    # Runs the mocha tests and reports the code coverage.
    ./node_modules/.bin/istanbul cover -x "**/spec/**" ./node_modules/.bin/_mocha -- $(find spec -name '*-spec.js') --timeout 10000 -R xunit-file --require spec/helper.js
    ./node_modules/.bin/istanbul report cobertura
}
checkDependencies
prepareDependencies
trap restoreDependencies SIGINT SIGTERM SIGKILL EXIT
build
unitTest