~mpontillo/maas/bcm_wedge40_demo_curtin_userdata_preseed

1
2
3
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
#!/bin/bash
# Copyright 2015 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# Author: Lee Trager <lee.trager@canonical.com>

MAAS_HOME="$(cd "$(dirname $0)/.." && pwd)"
TESTING_PATH=${TESTING_PATH:-$MAAS_HOME}
OUTPUT_DIR=${OUTPUT_DIR:-$MAAS_HOME/test-dir}
TEST_CMD=${TEST_CMD:-time make test}

source $MAAS_HOME/templates/script.sh

function run()
{
    if [ ! -d "$TESTING_PATH" ]; then
        echo "ERROR: $TESTING_PATH does not exist!"
        usage
    fi
    cd $TESTING_PATH
    if [ ! -d $OUTPUT_DIR ]; then
        mkdir -p $OUTPUT_DIR
        TEST_RUN=1
    else
        TEST_RUN=$(ls $OUTPUT_DIR | cut -d '-' -f2 | sort -n | tail -n1)
        TEST_RUN=$(($TEST_RUN + 1))
    fi
    while true; do
        run_out="$OUTPUT_DIR/test-${TEST_RUN}"
        echo "Test run: $((TEST_RUN++))"
        # Start with a clean environment incase an intermittent error corrupts
        # the database
        make clean ||:
        make ||:
        $TEST_CMD 2>&1 | tee -a $run_out
        if [ $# -eq 1 ] && [ "$TEST_RUN" -gt "$1" ]; then
            return
        fi
    done        
}

function stats()
{
    if [ ! -d "$OUTPUT_DIR" ]; then
        echo "Error: $OUTPUT_DIR does not exist!"
        usage
    fi

    echo "Tests run: $(ls $OUTPUT_DIR | wc -l)"
    for i in $OUTPUT_DIR/*; do
	 cat $i | awk '/FAIL$/ {
            for(i=1;i<=NF-2;i++) {
                printf("%s ", $i);
            }
            printf("\n");
            count++;
        }
        END {
            if(count == 0) print("No failures");
        }'
    done | sort | uniq -c | sort -r -n -k 1
}

function usage()
{
    cat <<EOF
usage: $0 <option> (run|stats|watch)
  -p    The path to the directory to run testing in, default is "$TESTING_PATH"
  -o    The output directory for test results, default is "$OUTPUT_DIR"
  -c    The command to run the tests, default is "$TEST_CMD"

run <runs>  Start running make test in a loop logging to the output directory.
            You can optionally specify the number of runs you want.
stats       Show stats on the run tests
watch       Run stats through the watch command
EOF
    exit 1
}

while getopts "p:o:c:h" option
do
    case $option in
        p) export TESTING_PATH=$OPTARG ;;
        o) export OUTPUT_DIR=$OPTARG ;;
        c) export TEST_CMD=$OPTARG ;;
        *) usage ;;
    esac
done

shift $(($OPTIND - 1))

if [ -z "$OUTPUT_DIR" ]; then
    echo "ERROR: Please specify the output directory!"
    usage
fi

if [ $# -lt 1 ]; then
    echo "Error: Please specify a command"
    usage
elif [ "$1" == "run" ]; then
    shift 1
    run $@
elif [ "$1" == "stats" ]; then
    stats
elif [ "$1" == "watch" ]; then
    watch $0 stats
else
    usage
fi