~ev/ubuntu-ci-services-itself/charms-in-config-manager

« back to all changes in this revision

Viewing changes to tests/run

[r=Andy Doan] Pythonise the integration test runner.  from Evan Dandrea

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh -e
2
 
#
3
 
# A runner for the Amulet tests.
4
 
#
5
 
# You need:
6
 
# - apt-get install sshuttle python-nova juju-core
7
 
# - A novarc file sourced.
8
 
# - The following in /etc/sudoers.d/sshuttle:
9
 
#
10
 
# your_username ALL = (root) NOPASSWD: /usr/bin/python /usr/lib/sshuttle/main.py *
11
 
 
12
 
TESTDIR="${TESTDIR-$(dirname $0)}"
13
 
TESTS="${TESTS-$(ls $TESTDIR)}"
14
 
 
15
 
SSH_OPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
16
 
AMULET=$(readlink -f $(mktemp -d ci-amulet.XXXX))
17
 
DEPLOYER=$(readlink -f $(mktemp -d ci-deployer.XXXX))
18
 
export PYTHONPATH=$AMULET:$DEPLOYER
19
 
export PATH=$DEPLOYER:$PATH
20
 
 
21
 
if [ -z "$OS_USERNAME" ]; then
22
 
    echo "Please source a novarc before running this harness." 1>&2
23
 
    exit 1
24
 
fi
25
 
 
26
 
sudoersfile=/etc/sudoers.d/sshuttle
27
 
if [ ! -f $sudoersfile ]; then
28
 
    echo "Please run: " 1>&2
29
 
    echo "echo \"$USER ALL = (root) NOPASSWD: /usr/bin/python /usr/lib/sshuttle/main.py *\" > $sudoersfile" 1>&2
30
 
    exit 1
31
 
fi
32
 
 
33
 
cleanup()
34
 
{
35
 
    echo "Cleaning up environment."
36
 
 
37
 
    rm -rf $AMULET $DEPLOYER
38
 
 
39
 
    # Kill sshuttle if it's running (and it should be).
40
 
    if [ -e sshuttle.pid ]; then
41
 
        kill $(cat sshuttle.pid)
42
 
    fi
43
 
 
44
 
    if [ -n "$(euca-describe-instances)" ]; then
45
 
        juju destroy-environment -y
46
 
    fi
47
 
 
48
 
    max=20
49
 
    while [ -n "$(euca-describe-instances)" ]; do
50
 
        sleep 5
51
 
        max=$(($max-1))
52
 
        if [ $max = 0 ]; then
53
 
            echo "Timed out waiting for nodes to disappear." 1>&2
54
 
            exit 1
55
 
        fi
56
 
    done
57
 
}
58
 
 
59
 
 
60
 
bootstrap()
61
 
{
62
 
    echo "Bootstrapping juju."
63
 
 
64
 
    juju bootstrap --constraints="mem=1024M"
65
 
 
66
 
    # we'll be waiting a while any way, so pull down amulet if needed
67
 
    if [ ! -d $AMULET ] ; then
68
 
        echo "downloading amulet"
69
 
        bzr co --lightweight lp:amulet $AMULET
70
 
    fi
71
 
    if [ ! -d $DEPLOYER ] ; then
72
 
        # the "to" stanza only works in the latest version of deployer not
73
 
        # yet packages and the juju-gui test requires this
74
 
        echo "downloading deployer"
75
 
        bzr co --lightweight lp:juju-deployer $DEPLOYER
76
 
        cat > $DEPLOYER/juju-deployer <<EOF
77
 
#!/bin/sh
78
 
PYTHONPATH=$PYTHONPATH
79
 
exec python $DEPLOYER/deployer/cli.py \$*
80
 
EOF
81
 
        chmod +x $DEPLOYER/juju-deployer
82
 
    fi
83
 
}
84
 
 
85
 
get_bootstrap_ip()
86
 
{
87
 
    max=20
88
 
    while [ $max != 0 ]; do
89
 
        ip="$(nova list --name machine-0 --fields networks | grep = | sed 's,.*=\(.*\) .*,\1,')"
90
 
        [ -n "$ip" ] && break
91
 
 
92
 
        # No IP? Try again.
93
 
        sleep 5
94
 
        max=$((max-1))
95
 
    done
96
 
 
97
 
    if [ -z "$ip" ]; then
98
 
        echo "Could not find IP for bootstrap node." 1>&2
99
 
        exit 1
100
 
    fi
101
 
    echo "$ip"
102
 
}
103
 
 
104
 
wait_for_node()
105
 
{
106
 
    # 5 second intervals to wait
107
 
    max=60
108
 
    ip="$1"
109
 
    while :; do
110
 
        echo "Trying $ip ($max)"
111
 
        # Attempt to ssh into the machine an immediately exit if successful. We
112
 
        # have a connection, break out of the loop.
113
 
        ssh $SSH_OPTS ubuntu@$ip exit 2>/dev/null && break
114
 
 
115
 
        sleep 20
116
 
        max=$((max-1))
117
 
 
118
 
        if [ $max = 0 ]; then
119
 
            echo "Timed out waiting for bootstrap node."
120
 
            nova list
121
 
            exit 1
122
 
        fi
123
 
    done
124
 
}
125
 
 
126
 
tunnel()
127
 
{
128
 
    echo "Tunneling traffic through bootstrap node."
129
 
 
130
 
    ip="$1"
131
 
    sshuttle -D -r ubuntu@$ip 10.55.0.0/16 -e "ssh $SSH_OPTS"
132
 
}
133
 
 
134
 
for test in $TESTS; do
135
 
    trap 'cleanup' TERM QUIT INT EXIT
136
 
 
137
 
    # Assume tests are of the form tests/$test/test.py.
138
 
    [ -d "$TESTDIR/$test" ] || continue
139
 
    echo ========================================
140
 
    echo Testing $test ...
141
 
    echo ========================================
142
 
 
143
 
    cleanup
144
 
    bootstrap
145
 
 
146
 
    ip="$(get_bootstrap_ip)"
147
 
    wait_for_node $ip
148
 
    tunnel $ip
149
 
 
150
 
    juju status
151
 
    $TESTDIR/$test/test.py
152
 
 
153
 
done