forked from TritonDataCenter/centos-image-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests.sh
executable file
·274 lines (216 loc) · 6.46 KB
/
run-tests.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env bash
if [[ -n "$TRACE" ]]; then
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -euo pipefail
IFS=$'\n\t'
DATE=`date +%H%M%S`
usage() {
cat <<EOF
Setup test instances and run tests for given image.
Note: This script assumes you have the triton cli command tool setup with a
profile for your test environment.
This script will:
- Create a test instance of the given image with user-script, user-data,
a public IP and private IP.
- Create a custom image of the test instance.
- Create a test instance from the custom image.
- Run tests on the above instances.
Usage:
$0 -i <IMAGE> -p <PROFILE> -n <PROPER_NAME>
Example:
$0 -i dd31507e-031e-11e6-be8a-8f2707b5b3ee -p testing-dc -n "CentOS 7.1"
Options:
-i The image you want to test. Can be UUID or image name.
-p The profile you wish to use. This assumes you have the triton
CLI tool setup with your desired profile.
-n The name of the image. This is the proper name found in the
motd and /etc/product files. Should be in quotes.
-h Show this message
EOF
exit 1
}
IMAGE=
PROFILE=
PROPER_NAME=
PACKAGE=
IMAGENAME=
VERSION=
UUID=
TAG="test-instance=true"
UUID=
SCRIPT=$PWD/userscript.sh
METADATAFILE="user-data=$PWD/user-data"
while getopts “hi:p:n:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
i)
IMAGE=$OPTARG
;;
p)
PROFILE=$OPTARG
;;
n)
PROPER_NAME=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $IMAGE ]] || [[ -z $PROFILE ]] || [[ -z $PROPER_NAME ]]; then
usage
exit 1
fi
get_image_details() {
echo ""
echo "Getting image details:"
IMAGEDETAILS=$(triton -p ${PROFILE} image get $1 | json -a name version id)
IMAGENAME=$(echo $IMAGEDETAILS | cut -d ' ' -f 1)
VERSION=$(echo $IMAGEDETAILS | cut -d ' ' -f 2)
UUID=$(echo $IMAGEDETAILS | cut -d ' ' -f 3)
echo " $IMAGEDETAILS"
echo ""
}
choose_package() {
PACKAGE=$(triton -p ${PROFILE} package list -H memory=768 -o id | head -1)
echo "Using package:"
echo " $PACKAGE"
echo ""
}
get_networks() {
echo "Getting networks:"
PUBLIC_NETWORK=$(triton -p ${PROFILE} network list -j | json -ag id -c 'this.public === true' | head -1)
PRIVATE_NETWORK=$(triton -p ${PROFILE} network list -j | json -ag id -c 'this.public === false' -c 'this.fabric !== true' | head -1)
# Trying using a fabric network instead
if [[ -z "$PRIVATE_NETWORK" ]]; then
PRIVATE_NETWORK=$(triton -p ${PROFILE} network list -j | json -ag id -c 'this.public === false' | head -1)
fi
echo " Public: $PUBLIC_NETWORK"
echo " Private: $PRIVATE_NETWORK"
echo ""
}
cat <<USERSCRIPT >userscript.sh
#!/bin/sh
echo "testing user-script" >> /var/tmp/test
hostname $IMAGENAME
USERSCRIPT
cat <<USERDATA >user-data
This is user-data!
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
USERDATA
create_instance() {
local IMAGE=$1
local INST_NAME=$2
echo "Provisioning:"
triton -p ${PROFILE} instance create -w -n $INST_NAME -N $PUBLIC_NETWORK -N $PRIVATE_NETWORK -t $TAG --script=$SCRIPT -M $METADATAFILE $IMAGE $PACKAGE
}
wait_for_IP() {
local INST_NAME=$1
echo "Checking Public IP:"
while [[ true ]]; do
echo -n "."
ping -c1 $(triton -p ${PROFILE} instance ip $INST_NAME) > /dev/null && break;
done
echo ""
echo "IP is now live."
}
wait_for_ssh() {
local INST_NAME=$1
local COUNT=0
echo "Checking ssh on $INST_NAME"
# Time out after a minute
while [[ "$COUNT" -lt 60 ]]; do
echo -n "."
ssh -q root@$(triton -p ${PROFILE} instance ip $INST_NAME) exit > /dev/null && break;
sleep 1
COUNT=$((COUNT+1))
done
if [[ "$COUNT" -ge 60 ]]; then
echo "ssh timed out after ~60 seconds"
exit 1
fi
}
test_image() {
local INST_NAME=$1
cat <<PROPYML >properties.yml
$INST_NAME:
:roles:
- centos
:name: $PROPER_NAME
:version: $VERSION
:doc_url: https://docs.joyent.com/images/linux/centos
PROPYML
export TARGET_HOST_NAME=$(triton -p ${PROFILE} instance ip $INST_NAME)
export TARGET_USER_NAME=root
rake serverspec
echo "###########################"
echo "All CentOS tests PASSED."
echo "###########################"
echo ""
}
create_custom_image() {
local INSTANCE=$1
local CUSTOM_IMAGE=$IMAGENAME-CUST
# This is here to workaround a bug where the instance is not stopped properly
echo "Stopping $INSTANCE"
triton -p ${PROFILE} instance stop -w $INSTANCE
echo "Creating custom image of $INSTANCE"
triton -p ${PROFILE} image create -w -t $TAG $INSTANCE $CUSTOM_IMAGE $VERSION
echo "Restarting $INSTANCE"
triton -p ${PROFILE} instance start -w $INSTANCE
}
delete_instance() {
local INST_NAME=$1
echo "Deleting test instance $INST_NAME"
triton -p ${PROFILE} instance delete $INST_NAME
}
delete_image() {
local CUSTOM_IMAGE=$1
triton -p ${PROFILE} image delete -f $CUSTOM_IMAGE
}
cleanup() {
echo "Cleaning up."
rm -rf userscript.sh
rm -rf user-data
unset TARGET_HOST_NAME
unset TARGET_USER_NAME
echo "Done."
echo ""
}
get_image_details $IMAGE
choose_package
get_networks
INSTACE_NAME=${IMAGENAME}-${VERSION}-${DATE}
create_instance $IMAGE $INSTACE_NAME
wait_for_IP $INSTACE_NAME
wait_for_ssh $INSTACE_NAME
test_image $INSTACE_NAME
create_custom_image $INSTACE_NAME
get_image_details $IMAGENAME-CUST
choose_package
get_networks
CUSTOM_INSTANCE_NAME=${IMAGENAME}-${VERSION}-${DATE}
create_instance $IMAGENAME $CUSTOM_INSTANCE_NAME
wait_for_IP $CUSTOM_INSTANCE_NAME
wait_for_ssh $CUSTOM_INSTANCE_NAME
test_image $CUSTOM_INSTANCE_NAME
echo "Deleting instance and custom image"
ssh-keygen -q -R $(triton -p ${PROFILE} inst ip $INSTACE_NAME)
delete_instance $INSTACE_NAME
ssh-keygen -q -R $(triton -p ${PROFILE} inst ip $CUSTOM_INSTANCE_NAME)
delete_instance $CUSTOM_INSTANCE_NAME
delete_image $IMAGENAME
cleanup
exit 0