tests.sh 4.19 KB
Newer Older
1
#!/bin/sh
2 3
set -e

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
runAstakosTests () {
    if [ -z "$astakos_tests" ]; then return; fi

    export SYNNEFO_EXCLUDE_PACKAGES="snf-cyclades-app"
    runTest $astakos_tests
}

runCycladesTests () {
    if [ -z "$cyclades_tests" ]; then return; fi

    export SYNNEFO_EXCLUDE_PACKAGES="snf-pithos-app snf-astakos-app"
    runTest $cyclades_tests
}

runPithosTests () {
    if [ -z "$pithos_tests" ]; then return; fi

    export SYNNEFO_EXCLUDE_PACKAGES="snf-cyclades-app"
    runTest $pithos_tests
}

runAstakosclientTests () {
    if [ -z "$astakosclient_tests" ]; then return; fi

    if [ ! $COVERAGE_EXISTS ]; then
        echo "Warning: Coverage not found. Astakosclient tests will not run."
        return
    fi

    for test in $astakosclient_tests; do
        runCoverage $test
    done
}
37

38
runTest () {
39
    TEST="$SNF_MANAGE test $* --traceback --noinput --settings=synnefo.settings.test"
40

41 42 43 44 45 46 47 48 49 50 51
    if [ $COVERAGE_EXISTS ]; then
        runCoverage "$TEST"
    else
        # Stop here, if we are on dry run
        if [ $DRY_RUN ]; then
            echo "$TEST"
            return
        fi

        eval $TEST
    fi
52 53 54
}

runCoverage () {
55 56 57 58
    # Stop here, if we are on dry run
    if [ $DRY_RUN ]; then
        echo "coverage run $1"
        return
59
    fi
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    coverage run $1
    coverage report --include=snf-*
}

usage(){
    echo "$1: Wrong input."
    echo "    Usage: $0 [--dry-run] component[.test]"
    exit
}

# Append a string to a given variable.
#
# Arguments: $1: the variable name
#            $2: the string
# Note, the variable must be passed by name, so we need to resort to a bit
# complicated parameter expansions
append () {
    eval $(echo "$1=\"\$${1}\"\" \"\"$2\"")
}

# Check if a string contains a substring
#
# Arguments: $1: The string
#            $2: The substring
contains () {
    case "$1" in
        *$2*) return 0;;  # True
        *) return 1;;     # False
    esac
}

# Get a list of tests for a given component.
#
# Arguments: $1: a component to extract tests from or a single component test
# Returns:   $(astakos/cyclades/pithos/ac)_tests,
#            a list with apps to be tested for each component
extract_tests () {
    # Check all components:
        # If the given component matches one of the components:
            # If total match, return all the tests of the component.
        # Else, if its form matches "component.test", extract only the
        # test.
    # Anything else is considered wrong input

    for c in $ALL_COMPONENTS; do
        if contains $1 $c; then
            if [ "$1" = "$c" ]; then
                append "${c}_tests" "$(eval "echo \$"${c}"_all_tests")"
                return
            elif contains $1 "$c."; then
                append "${c}_tests" $(echo $1 | sed -e 's/[a-z]*\.//g')
                return
            fi
        fi
    done

    usage $1
118 119 120 121
}

export SYNNEFO_SETTINGS_DIR=/tmp/snf-test-settings

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
astakos_all_tests="im quotaholder_app oa2"
cyclades_all_tests="api db logic plankton quotas vmapi helpdesk userdata"
pithos_all_tests="api"
astakosclient_all_tests="nosetests astakosclient"
ALL_COMPONENTS="astakos cyclades pithos astakosclient"

astakos_tests=""
cyclades_tests=""
pithos_tests=""
astakosclient_tests=""

if [ "$1" = "--dry-run" ]; then
    DRY_RUN=0
    shift
fi
137 138 139

TEST_COMPONENTS="$@"
if [ -z "$TEST_COMPONENTS" ]; then
140
    TEST_COMPONENTS=$ALL_COMPONENTS
141
fi
142

143 144 145 146 147 148 149 150
# Check if coverage and snf-manage exist
if command -v coverage >/dev/null 2>&1; then
    COVERAGE_EXISTS=0
fi
SNF_MANAGE=$(which snf-manage) ||
    { echo "Cannot find snf-manage in $PATH" 1>&2; exit 1; }

# Extract tests from a component
151
for component in $TEST_COMPONENTS; do
152
    extract_tests $component
153
done
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

echo "|===============|============================"
echo "| Component     | Tests"
echo "|---------------|----------------------------"
echo "| Astakos       | $astakos_tests"
echo "| Cyclades      | $cyclades_tests"
echo "| Pithos        | $pithos_tests"
echo "| Astakosclient | $astakosclient_tests"
echo "|===============|============================"
echo ""

if [ ! $COVERAGE_EXISTS ]; then
    echo "WARNING: Cannot find coverage in path, skipping coverage tests" >&2
    echo ""
fi

# For each component, run the specified tests.
runAstakosTests
runCycladesTests
runPithosTests
runAstakosclientTests