-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathec2_test_run.py
228 lines (199 loc) · 9.99 KB
/
ec2_test_run.py
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
#!/usr/bin/env python
'''
github : https://github.com/liangxiao1/mini_utils
This tool is using for running avocado-cloud ec2 test in paralle with multi users
for more efficent.
instance_yaml file should be like below, more instances supported.
instance_types: !mux
i3.large:
instance_type: i3.large
cpu: 2
memory: 15.25
disks: 2
net_perf : 10
ipv6: True
'''
import logging
import os
import sys
import argparse
import pexpect
import signal
import shutil
import errno
import tempfile
avocado_cloud_dir = '/home/ec2/avocado-cloud'
def sig_handler(signum, frame):
logging.info('Got signal %s, exit!', signum)
sys.exit(0)
def setup_dir():
log.info("Setup dir %s" % args.result_dir)
#user_list = map(lambda user: 'cloud'+str(user), range(0, 100))
if not os.path.exists(args.result_dir):
log.info("%s not found" % args.result_dir)
sys.exit(errno.ENOENT)
if not os.path.exists(avocado_cloud_dir):
log.info("%s please put avocado-cloud to /home/ec2 directory")
sys.exit(errno.ENOENT)
try:
log.info('Copy ec2 config files to %s' % args.result_dir)
ec2_env_yaml = '%s/ec2_env_conf.yaml' % args.result_dir
ec2_testcase_yaml = '%s/ec2_testcases.yaml' % args.result_dir
ec2_test_yaml = '%s/ec2_test.yaml' % args.result_dir
ec2_instance_yaml = '%s/ec2_instance_types.yaml' % args.result_dir
shutil.copy(avocado_cloud_dir +
'/config/ec2_env_conf.yaml', ec2_env_yaml)
shutil.copy(avocado_cloud_dir +
'/config/ec2_testcases.yaml', ec2_testcase_yaml)
shutil.copy(avocado_cloud_dir +
'/config/ec2_test.yaml', ec2_test_yaml)
shutil.copy(avocado_cloud_dir +
'/config/ec2_instance_types.yaml', ec2_instance_yaml)
except Exception as err:
log.error("Copy exception hit!\n %s" % err)
sys.exit(errno.ENOENT)
def setup_avocado():
if args.ami_id is None or args.region is None or args.subnet_id is None or args.security_group_ids is None or args.instance_yaml is None:
log.error(
"ami_id,region,subnet_id,security_group_ids, instance_yaml is not allowed empty")
sys.exit(1)
instance_yaml = args.instance_yaml
if not os.path.exists(instance_yaml):
log.error("No %s found!" % instance_yaml)
sys.exit(1)
instance_yaml_dest = '%s/ec2_instance_types.yaml' % args.result_dir
if os.path.exists(instance_yaml_dest):
os.unlink(instance_yaml_dest)
log.info('Copy %s to %s' % (instance_yaml, instance_yaml_dest))
shutil.copy(instance_yaml, instance_yaml_dest)
tmp_yaml = "/%s/t.yaml" % args.result_dir
ec2_env_yaml = '%s/ec2_env_conf.yaml' % args.result_dir
if os.path.exists(tmp_yaml):
os.unlink(tmp_yaml)
with open(ec2_env_yaml, 'r') as fh:
has_additionalinfo = False
has_profile_name = False
for line in fh.readlines():
if line.startswith('ami_id :'):
line = 'ami_id : %s\n' % args.ami_id
if line.startswith('region : '):
line = 'region : %s\n' % args.region
if line.startswith('ssh_user : '):
line = 'ssh_user : %s\n' % args.ssh_user
if line.startswith('availability_zone : '):
line = 'availability_zone : %s\n' % args.zone
if line.startswith('subnet_id_ipv6 : '):
line = 'subnet_id_ipv6 : %s\n' % args.subnet_id
if line.startswith('subnet_id_ipv4 : '):
line = 'subnet_id_ipv4 : %s\n' % args.subnet_id
if line.startswith('security_group_ids : '):
line = 'security_group_ids : %s\n' % args.security_group_ids
if line.startswith('ssh_key_name : '):
line = 'ssh_key_name : %s\n' % args.key_name
if line.startswith('ec2_tagname : '):
line = 'ec2_tagname : virtqe_auto_cloud\n'
if line.startswith('ltp_url : ') and args.ltp_url is not None:
line = 'ltp_url: %s\n' % args.ltp_url
if line.startswith('code_cover : '):
line = 'code_cover : %s\n' % args.is_gcov
if line.startswith('profile_name : '):
line = 'profile_name : %s\n' % args.profile_name
has_profile_name = True
if line.startswith('additionalinfo : ') and args.additionalinfo is not None:
line = 'additionalinfo : %s\n' % args.additionalinfo
has_additionalinfo = True
elif line.startswith('additionalinfo : ') and args.additionalinfo is None:
line = '#additionalinfo : %s\n' % args.additionalinfo
with open(tmp_yaml, 'a') as fd:
fd.writelines(line)
if args.additionalinfo is not None and not has_additionalinfo:
line = 'additionalinfo : %s\n' % args.additionalinfo
with open(tmp_yaml, 'a') as fd:
fd.writelines(line)
if args.profile_name is not None and not has_profile_name:
line = 'profile_name : %s\n' % args.profile_name
with open(tmp_yaml, 'a') as fd:
fd.writelines(line)
if os.path.exists(ec2_env_yaml):
os.unlink(ec2_env_yaml)
log.info('Copy %s to %s' % (tmp_yaml, ec2_env_yaml))
shutil.copy(tmp_yaml, ec2_env_yaml)
def run_avocado():
log.info("Start to run avocado-cloud......")
avocado_dir = '/home/ec2/avocado-cloud'
os.chdir(avocado_dir)
if args.timeout is None:
timeout = 28800
else:
timeout = args.timeout
log.info("Wait timeout was set to %s" % timeout)
if 'acceptance' in args.casetag:
cmd = 'avocado run -m %s/ec2_test.yaml --filter-by-tags %s %s/tests/aws/ \
--execution-order=tests-per-variant --job-results-dir %s' % (args.result_dir,
args.casetag, avocado_cloud_dir, args.result_dir)
else:
casetags = ''.join(
map(lambda s: ' --filter-by-tags '+s, args.casetag.split(',')))
cmd = 'avocado run -m %s/ec2_test.yaml %s --filter-by-tags test_cleanupall %s/tests/aws/ \
--execution-order=tests-per-variant --job-results-dir %s' % (args.result_dir,
casetags, avocado_cloud_dir, args.result_dir)
log.info("Run cmd: %s" % cmd)
ret, output = pexpect.run(cmd, timeout=int(timeout), withexitstatus=True)
if ret != 0:
log.error('Error got, ret%s' % ret)
log.info(output)
parser = argparse.ArgumentParser(
description="This tool is using for running avocado-cloud ec2 test in paralle.\
eg. python ec2_test_run.py --instance_yaml /tmp/t.yaml --ami-id ami-xxxx --key_name xxxx \
--security_group_ids sg-xxxx --subnet_id subnet-xxxx --region us-west-2")
parser.add_argument('--instance_yaml', dest='instance_yaml', action='store', default=None, required=False,
help='instance types yaml file')
parser.add_argument('-d', dest='is_debug', action='store_true',
help='run in debug mode', required=False)
parser.add_argument('--clean', dest='is_clean', action='store_true',
help='caution: clean up all exists users /home/cloudN before test', required=False)
parser.add_argument('--ami-id', dest='ami_id', default=None, action='store',
help='image id', required=False)
parser.add_argument('--key_name', dest='key_name', default=None, action='store',
help='key to create instance', required=False)
parser.add_argument('--ssh_user', dest='ssh_user', default='ec2-user', action='store',
help='user for ssh login, default is ec2-user', required=False)
parser.add_argument('--security_group_ids', dest='security_group_ids', default=None, action='store',
help='securitt group id', required=False)
parser.add_argument('--subnet_id', dest='subnet_id', default=None, action='store',
help='subnet id', required=False)
parser.add_argument('--region', dest='region', default=None, action='store',
help='region to run ', required=False)
parser.add_argument('--zone', dest='zone', default=None, action='store',
help='zone to run ', required=False)
parser.add_argument('--timeout', dest='timeout', default=None, action='store',
help='bare metal can set to 8hrs each, others can be 7200 each, default it 28800s', required=False)
parser.add_argument('--casetag', dest='casetag', default='acceptance', action='store',
help='cases filter tag, default is acceptance, more tags can be seperated by ","', required=False)
parser.add_argument('--result_dir', dest='result_dir', default=None, action='store',
help='where to save the result', required=True)
parser.add_argument('--ltp_url', dest='ltp_url', default=None, action='store',
help='ltp rpm url', required=False)
parser.add_argument('-g', dest='is_gcov', action='store_true',
help='optional,enable collect code coverage report, image should have gcov version kernel installed', required=False)
parser.add_argument('--additionalinfo', dest='additionalinfo', default=None, action='store',
help='for preview access instance', required=False)
parser.add_argument('--profile', dest='profile_name', default='default', action='store',
help='which crediential profile to use', required=False)
args = parser.parse_args()
log = logging.getLogger(__name__)
FORMAT = "%(levelname)s:FUNC-%(funcName)s:%(message)s"
if args.is_debug:
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
else:
logging.basicConfig(level=logging.INFO, format=FORMAT)
def main():
signal.signal(signal.SIGHUP, sig_handler)
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGQUIT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
setup_dir()
setup_avocado()
run_avocado()
if __name__ == '__main__':
main()