~andrewjbeach/juju-ci-tools/get-juju-dict

122 by Aaron Bentley
Add script to spawn and ssh to ec2 instances.
1
#!/bin/bash
127 by Aaron Bentley
Admit this script runs on jenkins.
2
set -eux
503.1.1 by Aaron Bentley
Provide SCRIPTS var if not supplied.
3
: ${SCRIPTS=$(readlink -f $(dirname $0))}
258 by Curtis Hovey
Run unittests for all archs and series.
4
233.1.10 by Aaron Bentley
Manual provider almost working.
5
export INSTANCE_TYPE=$1
6
export AMI_IMAGE=$2
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
7
shift; shift
8
1351 by Curtis Hovey
Addedd daily streams to get_amy.py get_ami.py tests/test_get_ami.py
9
JOB_NAME=${JOB_NAME:-$USER-run-unit-tests}
10
export JOB_NAME
11
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
12
FIXUP_SOURCES=""
13
IS_LOCAL="false"
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
14
RETRY="false"
1459.1.1 by Curtis Hovey
Require --local or --revision-build with run-unit-tests.
15
REVISION_BUILD="none"
618.1.2 by Curtis Hovey
Remove supoprt for launchpad.net branches
16
USE_PPA=""
1225.1.1 by Curtis Hovey
Added rule to pass the install-deps at the command line for unit-tests.
17
INSTALL_DEPS=""
578.1.1 by Martin Packman
Add flag to run-unit-tests script to include Andrew's tmpfs fix
18
USE_TMPFS="false"
962.1.1 by Martin Packman
Add option to run with race detection to run-unit-tests script
19
GOTESTCMD="go test"
874.1.1 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
20
LOG_FILE_PATH="/usr/share/distro-info/ubuntu.csv"
885 by Curtis Hovey
Rety tagging instances instead of failing.
21
LOG_TXT="17.04,Angsty Antelope,angsty,2016-10-23,2017-04-30,2018-01-29"
874.1.1 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
22
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
23
while [[ "${1-}" != "" ]]; do
24
    case $1 in
25
        --force-archive)
947.1.2 by Curtis Hovey
Remove extra sed.
26
            FIXUP_SOURCES="sudo sed s,//.*ec2.archive.ubuntu.com,//archive.ubuntu.com, /etc/apt/sources.list -i"
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
27
            ;;
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
28
        --retry)
29
            RETRY="true"
30
            ;;
577 by Curtis Hovey
Add a --safe option to run-unit-tests which limits the number the number of procs to
31
        --safe)
1520.1.2 by Curtis Hovey
Make timeouts consistent.
32
            GOTESTCMD="$GOTESTCMD -p 2 -test.timeout=2400s"
577 by Curtis Hovey
Add a --safe option to run-unit-tests which limits the number the number of procs to
33
            ;;
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
34
        --local)
35
            IS_LOCAL="true"
36
            shift
37
            tarfile=$1
38
            ;;
618.1.2 by Curtis Hovey
Remove supoprt for launchpad.net branches
39
        --revision-build)
40
            shift
41
            REVISION_BUILD=$1
42
            ;;
43
        --use-ppa)
44
            shift
45
            USE_PPA=$1
46
            if [[ -z $(echo $USE_PPA | grep 'ppa:.*/.*') ]]; then
47
                echo "$USE_PPA is not a ppa."
48
                exit 1
49
            fi
50
            ;;
1225.1.1 by Curtis Hovey
Added rule to pass the install-deps at the command line for unit-tests.
51
        --install-deps)
52
            shift
53
            INSTALL_DEPS=$1
54
            ;;
578.1.1 by Martin Packman
Add flag to run-unit-tests script to include Andrew's tmpfs fix
55
        --use-tmpfs)
56
            USE_TMPFS="true"
57
            ;;
962.1.1 by Martin Packman
Add option to run with race detection to run-unit-tests script
58
        --race)
1520.1.1 by Curtis Hovey
Increase unit-test timeout to 2400. Remove --robust.
59
            GOTESTCMD="$GOTESTCMD -race -test.timeout=2400s"
962.1.1 by Martin Packman
Add option to run with race detection to run-unit-tests script
60
            ;;
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
61
    esac
62
    shift
63
done
64
618.1.2 by Curtis Hovey
Remove supoprt for launchpad.net branches
65
1459.1.1 by Curtis Hovey
Require --local or --revision-build with run-unit-tests.
66
if [[ $IS_LOCAL == "false" && $REVISION_BUILD == "none" ]]; then
67
    echo "You must use either the --local or --revision-build option."
68
    exit 1
69
fi
70
71
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
72
if [[ $IS_LOCAL == "false" ]]; then
772.2.8 by Curtis Hovey
Make it clear only one tarball is expected to be found.
73
    # Download the buildvars and tartfile and lean the tarfile name.
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
74
    tarfile=$(
1391.1.2 by Curtis Hovey
Use JUJU_HOME.
75
        $SCRIPTS/s3ci.py get --config $JUJU_HOME/juju-qa.s3cfg \
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
76
        $REVISION_BUILD build-revision '.*.tar.gz' ./)
1391.1.2 by Curtis Hovey
Use JUJU_HOME.
77
    $SCRIPTS/s3ci.py get-summary --config $JUJU_HOME/juju-qa.s3cfg \
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
78
        $REVISION_BUILD $AMI_IMAGE
503.1.3 by Aaron Bentley
Support --force-archive to ensure main archive is used.
79
fi
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
80
tarfile_path=$(readlink -f $tarfile)
874.1.1 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
81
tarfile_basename=$(basename $tarfile)
262 by Curtis Hovey
simplify the logic in the embedded script.
82
juju_version=$(basename $tarfile .tar.gz)
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
83
84
85
if [[ $INSTANCE_TYPE == "lxc" ]]; then
86
    instance_id="juju-ci-unit-tester"
980.2.1 by Martin Packman
Try destroying an existing lxc container before creating one for unit tests
87
    sudo lxc-stop -n $instance_id || true
88
    sudo lxc-destroy -n $instance_id || true
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
89
    echo "Creating LXC $instance_id"
90
    if [[ $AMI_IMAGE =~ .*template ]]; then
91
        sudo lxc-clone -o $AMI_IMAGE -n $instance_id
92
    else
93
        sudo lxc-create -n $instance_id -t ubuntu -- \
94
            -r $AMI_IMAGE -S $HOME/.ssh/id_rsa.pub
95
    fi
96
    sudo lxc-start -d -n $instance_id
97
    instance_name="-"
98
    while [[ "$instance_name" == "-" ]]; do
99
        instance_name=$(sudo lxc-ls --fancy |
100
            grep $instance_id |
885 by Curtis Hovey
Rety tagging instances instead of failing.
101
            tr -s ' ' |
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
102
            cut -d ' ' -f 3)
103
        sleep 2
104
    done
105
    sudoers_file="/var/lib/lxc/$instance_id/rootfs/etc/sudoers.d/91-ubuntu"
106
    echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' | sudo tee -a $sudoers_file
627 by Curtis Hovey
Force perms on sudoers for precise.
107
    sudo chmod 0440 $sudoers_file
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
108
    echo "$instance_id is at $instance_name"
695.2.1 by Martin Packman
Bring in remaining elements from run-unit-tests-new-archs to run-unit-tests to unify the scripts
109
elif [[ $INSTANCE_TYPE == "host" ]]; then
110
    instance_name=$AMI_IMAGE
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
111
else
112
    instance_id=$($SCRIPTS/ec2-run-instance-get-id)
885 by Curtis Hovey
Rety tagging instances instead of failing.
113
    for attempt in $(seq 5); do
114
        if $SCRIPTS/ec2-tag-job-instances $instance_id; then
115
            break
116
        elif [ "$attempt" == "5" ]; then
117
            exit 1
118
        fi
119
        sleep 15
120
    done
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
121
    set +x
122
    echo Starting instance $instance_id
123
    instance_name=$($SCRIPTS/ec2-get-name $instance_id)
124
    echo Instance has name $instance_name
125
    sleep 30
126
    $SCRIPTS/wait-for-port $instance_name 22
127
fi
128
258 by Curtis Hovey
Run unittests for all archs and series.
129
283 by Curtis Hovey
Make the embedded script do less work.
130
# Define the common vars before the embedded scripts; do not make them evalaute
131
# what is already know in the outer script.
284 by Curtis Hovey
Restore set -x+e to ensure instances shutdown after failed tests.
132
set -x
133
set +e
283 by Curtis Hovey
Make the embedded script do less work.
134
scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" \
521.1.1 by Curtis Hovey
Add --local to pass the local tarball.
135
    $tarfile_path ubuntu@$instance_name:~/
260 by Curtis Hovey
Hack to force make check to work. This test can be restored to a simple call to
136
283 by Curtis Hovey
Make the embedded script do less work.
137
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" \
138
    ubuntu@$instance_name  <<EOT
261 by Curtis Hovey
Separate hack to support archs and series from the original script to run the tests
139
set -eux
1378 by Curtis Hovey
Set TMPDIR when /tmp is a symlink.
140
if  [[ -L /tmp ]]; then
1379 by Curtis Hovey
No early evaulation.
141
    export TMPDIR=\$(readlink -f /tmp)
1377 by Curtis Hovey
Always source .bashrc when it exists to get an env adapted to the host.
142
fi
695.2.1 by Martin Packman
Bring in remaining elements from run-unit-tests-new-archs to run-unit-tests to unify the scripts
143
if [[ ! -f ~/.ssh/id_rsa ]]; then
144
    ssh-keygen -t rsa -b 2048 -N "" -f ~/.ssh/id_rsa
145
fi
578.1.1 by Martin Packman
Add flag to run-unit-tests script to include Andrew's tmpfs fix
146
if [[ "$USE_TMPFS" == "true" ]]; then
147
    sudo mkdir -m 777 /mnt/tmp
796 by Curtis Hovey
Increase the size of tmpfs to 3G.
148
    sudo mount -t tmpfs -o size=3G /mnt/tmp /mnt/tmp
578.1.1 by Martin Packman
Add flag to run-unit-tests script to include Andrew's tmpfs fix
149
    export TMPDIR=/mnt/tmp
150
fi
1469.1.1 by Curtis Hovey
Clean up GOPATH before untaring.
151
export GOPATH=\$HOME/$juju_version
1472 by Curtis Hovey
Escape vars in run-unit-tests.
152
if [[ -d \$GOPATH ]]; then
1469.1.1 by Curtis Hovey
Clean up GOPATH before untaring.
153
    echo "Cleaning up files unpacked into the GOPATH by a previous test."
1472 by Curtis Hovey
Escape vars in run-unit-tests.
154
    rm -r \$GOPATH
1469.1.1 by Curtis Hovey
Clean up GOPATH before untaring.
155
fi
874.1.1 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
156
tar -xzf $tarfile_basename
922 by Curtis Hovey
Use the local goroot if it is present.
157
if [[ -d \$HOME/goroot ]]; then
158
    export GOROOT=\$HOME/goroot
159
fi
618.1.2 by Curtis Hovey
Remove supoprt for launchpad.net branches
160
cd \$GOPATH/src/github.com/juju/juju
698 by Martin Packman
Fix run-unit-tests with host to not check ec2 sources
161
if [[ "$INSTANCE_TYPE" != "lxc" && "$INSTANCE_TYPE" != "host" ]]; then
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
162
    for attempt in \$(seq 10); do
163
        if grep ec2.archive.ubuntu.com /etc/apt/sources.list > /dev/null; then
164
            break
165
        elif [ "\$attempt" == "10" ]; then
166
            exit 1
167
        fi
168
      sleep 10
169
    done
170
    $FIXUP_SOURCES
171
fi
1351 by Curtis Hovey
Addedd daily streams to get_amy.py get_ami.py tests/test_get_ami.py
172
export PATH=/usr/lib/go-1.6/bin:$PATH
1347 by Curtis Hovey
Ensure unattended upgrades work, xenial want to prompt.
173
export DEBIAN_FRONTEND=noninteractive
263 by Curtis Hovey
Remove arch-series compatabilities in hopes to see amd64+precise passes.
174
sudo apt-get update
1350 by Curtis Hovey
Use force-confnew to get dist-upgrades to just-work.
175
sudo apt-get upgrade -y --force-yes \
176
    -o Dpkg::Options::="--force-confdef" \
177
    -o Dpkg::Options::="--force-confold" \
178
    -o Dpkg::Options::="--force-confnew"
1352 by Curtis Hovey
Added gcc to list of packages to always install for unit-tests.
179
sudo apt-get install -y make gcc software-properties-common distro-info-data
1231 by Curtis Hovey
Install ppas after python-software-properties is installed.
180
if [[ "$USE_PPA" != "" ]]; then
181
    sudo apt-add-repository -y $USE_PPA;
182
    sudo apt-get update
183
fi
1225.1.1 by Curtis Hovey
Added rule to pass the install-deps at the command line for unit-tests.
184
if [[ "$INSTALL_DEPS" == "" ]]; then
185
    echo "Installing default deps."
186
    make install-dependencies
187
else
188
    echo "Installing deps from command line."
189
    sudo apt-get install -y $INSTALL_DEPS
190
fi
191
695.2.1 by Martin Packman
Bring in remaining elements from run-unit-tests-new-archs to run-unit-tests to unify the scripts
192
# TODO(gz): Remove bzr whoami?
124 by Aaron Bentley
Add actual test-running and tarfile detection.
193
bzr whoami 'J. Random Hacker <jrandom@example.org>'
562 by Curtis Hovey
Print the compiler version before working.
194
go version || gccgo -v
282 by Curtis Hovey
Always build before check to catch explicitly catch compilation errors.
195
make build
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
196
export JUJU_NOTEST_MONGOJS=1
874.1.2 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
197
198
if ! grep 'Angsty Antelope' $LOG_FILE_PATH ; then
199
  echo "$LOG_TXT" | sudo tee -a $LOG_FILE_PATH
874.1.1 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
200
fi
1365 by Curtis Hovey
Delay Xenial LTS birthday by one month.
201
# Delay Xenial LTS birthday by one month.
202
sudo sed -i -e 's,2016-04-21,2016-05-21,' $LOG_FILE_PATH
874.1.1 by Seman Said
Appends bogus data to ubuntu.csv and fixes the issue with path to Juju tar file (the unit test script fails if the path to Juju tar file is other than where the unit test script is executing)
203
1091.1.1 by Curtis Hovey
Clean up dirty /tmp before a test. The cleanup after the test will raise an error if it fails.
204
if [[ $INSTANCE_TYPE == "host" ]]; then
205
    echo "Cleaning up /tmp if it is dirty."
206
    sudo killall -SIGABRT /usr/bin/mongod || true
207
    sudo killall -SIGABRT /usr/lib/juju/bin/mongod || true
1525.1.2 by Curtis Hovey
Clean up /tmp to free disk space.
208
    rm -rf /tmp/adduser-* || true
209
    rm -rf /tmp/check* || true
210
    rm -rf /tmp/go-* || true
211
    rm -rf /tmp/gocheck* || true
212
    rm -rf /tmp/gui* || true
213
    rm -rf /tmp/juju-* || true
214
    rm -rf /tmp/pprof* || true
215
    rm -rf /tmp/test-* || true
1091.1.1 by Curtis Hovey
Clean up dirty /tmp before a test. The cleanup after the test will raise an error if it fails.
216
fi
217
1397 by Curtis Hovey
Always attempt to setup lxd in unit tests.
218
make setup-lxd || true
219
1091.4.1 by James Tunnicliffe
Merged upstream
220
go test -i ./...
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
221
if [[ "$RETRY" == "true" ]]; then
962.1.1 by Martin Packman
Add option to run with race detection to run-unit-tests script
222
    $GOTESTCMD ./... || $GOTESTCMD ./...
223
elif [[  "$GOTESTCMD" != "go test"  ]]; then
224
    $GOTESTCMD ./...
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
225
else
570 by Curtis Hovey
Re-enable testing.
226
    make check
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
227
fi
124 by Aaron Bentley
Add actual test-running and tarfile detection.
228
EOT
229
EXIT_STATUS=$?
258 by Curtis Hovey
Run unittests for all archs and series.
230
135 by Curtis Hovey
Allow errors to be caught and the instance terminated before returning.
231
set -e
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
232
if [[ $INSTANCE_TYPE == "lxc" ]]; then
233
    echo "Deleting lxc"
234
    sudo lxc-stop -n $instance_id
235
    sudo lxc-destroy -n $instance_id
695.2.1 by Martin Packman
Bring in remaining elements from run-unit-tests-new-archs to run-unit-tests to unify the scripts
236
elif [[ $INSTANCE_TYPE == "host" ]]; then
237
    ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" \
238
        ubuntu@$instance_name  <<EOT
239
    set -eux
240
    echo "Cleaning up /tmp"
700 by Martin Packman
Pass run-unit-test if no mongod needs killing
241
    sudo killall -SIGABRT /usr/bin/mongod || true
956 by Curtis Hovey
Also ABRT mongod runing from /usr/lib/juju/bin/.
242
    sudo killall -SIGABRT /usr/lib/juju/bin/mongod || true
1525.1.2 by Curtis Hovey
Clean up /tmp to free disk space.
243
    rm -rf /home/ubuntu/$juju_version || true
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
244
    rm -rf /tmp/adduser-* || true
1525.1.2 by Curtis Hovey
Clean up /tmp to free disk space.
245
    rm -rf /tmp/check* || true
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
246
    rm -rf /tmp/go-* || true
1525.1.2 by Curtis Hovey
Clean up /tmp to free disk space.
247
    rm -rf /tmp/gocheck* || true
248
    rm -rf /tmp/gui* || true
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
249
    rm -rf /tmp/juju-* || true
1525.1.2 by Curtis Hovey
Clean up /tmp to free disk space.
250
    rm -rf /tmp/pprof* || true
1391.1.1 by Curtis Hovey
Use s3ci.py because it honours proxies.
251
    rm -rf /tmp/test-* || true
695.2.1 by Martin Packman
Bring in remaining elements from run-unit-tests-new-archs to run-unit-tests to unify the scripts
252
EOT
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
253
else
781 by Curtis Hovey
Do not let EC2 issues damn unit-test results.
254
    set +e
255
    # EC2 failures cannot change the EXIT_STATUS.
569 by Curtis Hovey
Added --retry to retry failed suites with -p 2.
256
    $SCRIPTS/ec2-terminate-job-instances
257
fi
124 by Aaron Bentley
Add actual test-running and tarfile detection.
258
exit $EXIT_STATUS