-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbug761254.py
260 lines (249 loc) · 10.7 KB
/
bug761254.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
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
#!/usr/bin/env python
# encoding: utf-8
# Copyright 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Regression test for Image Factory #bug761254
# Created by koca ([email protected])
# Date: 02/12/2011
# Issue: imagefactory only allows one image to be built
# return values:
# 0 - OK: everything OK
# 1 - Fail: setupTest wasn't OK
# 2 - Fail: bodyTest wasn't OK
# 3 - Fail: cleanTest wasn't OK
# 4 - Fail: any other error (reserved value)
#necessary libraries
import os
import sys
import time
import subprocess
import re
import shutil
import oauth2 as oauth
import httplib2
import json
from syck import *
configuration = load(file("configuration.yaml", 'r').read())
#constants
SUCCESS=0
FAILED=1
RET_SETUPTEST=1
RET_BODYTEST=2
RET_CLEANTEST=3
RET_UNEXPECTED_ERROR=4
ROOTID=0
TIMEOUT=360
MINUTE=60
#setup variables, constants
CrazyCommand=["aeolus-image build --target rhevm --template templates/bug761254.tdl --environment default",\
"aeolus-image build --target ec2 --template templates/bug761254.tdl --environment default",\
"aeolus-image build --target vsphere --template templates/bug761254.tdl --environment default"]
LogFileIF=configuration["LogFileIF"]
LogFileIWH=configuration["LogFileIWH"]
consumer = oauth.Consumer(key='key', secret='secret')
sig_method = oauth.SignatureMethod_HMAC_SHA1()
params = {'oauth_version':"0.4.4",
'oauth_nonce':oauth.generate_nonce(),
'oauth_timestamp':oauth.generate_timestamp(),
'oauth_signature_method':sig_method.name,
'oauth_consumer_key':consumer.key}
url_https="https://localhost:8075/imagefactory/builders/"
VSPHEREbugFile=configuration["VSPHEREbugFile"]
VSPHEREconfigureFile=configuration["VSPHEREconfigureFile"]
VSPHEREBackupFile=configuration["VSPHEREBackupFile"]
RHEVMbugFile=configuration["RHEVMbugFile"]
RHEVMconfigureFile=configuration["RHEVMconfigureFile"]
RHEVMBackupFile=configuration["RHEVMBackupFile"]
def setupTest():
print "=============================================="
print "Setup of the regression test based on bug761254"
print "See the test case https://tcms.engineering.redhat.com/case/122800/?from_plan=4953"
print "Checking if you have enough permission..."
if os.geteuid() != ROOTID:
print "You must have root permissions to run this script, I'm sorry buddy"
return False #exit the test
#run the cleanup configuration
print "Cleanup configuration...."
if os.system("aeolus-cleanup") != SUCCESS:
print "Some error raised in aeolus-cleanup !"
return False
#first backup old rhvm file
print "Backup old rhevm configuration file"
if os.path.isfile(RHEVMconfigureFile):
shutil.copyfile(RHEVMconfigureFile, RHEVMBackupFile)
#then copy the conf. file
print "Copy rhevm configuration file to /etc/aeolus-configure/nodes/rhevm_configure"
if os.path.isfile(RHEVMbugFile):
shutil.copyfile(RHEVMbugFile, RHEVMconfigureFile)
else:
print RHEVMbugFile + " didn't find!"
return False
#first backup old vsphere file
print "Backup old vsphere configuration file"
if os.path.isfile(VSPHEREconfigureFile):
shutil.copyfile(VSPHEREconfigureFile, VSPHEREBackupFile)
#then copy the conf. file
print "Copy rhevm configuration file to /etc/aeolus-configure/nodes/vsphere_configure"
if os.path.isfile(VSPHEREbugFile):
shutil.copyfile(VSPHEREbugFile, VSPHEREconfigureFile)
else:
print VSPHEREbugFile + " didn't find!"
return False
print "running aeolus-configure -p ec2,vsphere,rhevm"
if os.system("aeolus-configure -p ec2,vsphere,rhevm") != SUCCESS:
print "Some error raised in aeolus-configure !"
return False
print "Clearing log file for Image Factory"
os.system("> " + LogFileIF)
print "Clearing log file for Image Warehouse"
os.system("> " + LogFileIWH)
return True
#this functions suppose to be as a help function to do not write one code multiple times
def helpTest(imageTest):
url = url_https + imageTest
req = oauth.Request(method='GET', url=url, parameters=params)
sig = sig_method.sign(req, consumer, None)
req['oauth_signature'] = sig
r, c = httplib2.Http().request(url, 'GET', None, headers=req.to_header())
response = 'Response headers: %s\nContent: %s' % (r,c)
print response
return c
#body - the core of the test
def bodyTest():
print "=============================================="
print "test being started"
target_image = list()
for command in CrazyCommand:
try:
print command
retcode = os.popen(command).read()
print "output is :"
print retcode
tempvar = re.search(r'.*\n.*\n([a-zA-Z0-9\-]*).*',retcode,re.I)
if tempvar == None:
print "An unknown error occurred. I'm not able to get target image ID. Check the log file out:"
print "======================================================"
outputtmp = os.popen("cat " + LogFileIF).read()
print outputtmp
print "See the template templates/bug761254.tdl"
print "======================================================"
outputtmp = os.popen("cat templates/bug761254.tdl").read()
print outputtmp
return False
else:
target_image.append(tempvar.group(1))
except subprocess.CalledProcessError, e:
print >>sys.stderr, "Execution failed:", e
return False
time.sleep(5) #sleep for 5 seconds
print "wait until build process is done"
#setup counter to do not wait longer then 1 hour
Counter=0
for timage in target_image:
print "Let\'s check this image: " + timage
data = json.loads(helpTest(timage))
print "Data Status: " + data['status']
while data['status'] == "BUILDING":
Counter = Counter + 1
#wait a minute
time.sleep(MINUTE)
data = json.loads(helpTest(timage))
print "Data Status: " + data['status']
if Counter > TIMEOUT:
print "Error: timeout over "+str(TIMEOUT)+" minutes !"
return False
#check if there is no error in logs
print "Checking if there is any error in log of image factory"
if os.system("grep -i \"FAILED\\|Error\"" + LogFileIF) == SUCCESS:
print "Found FAILED or error message in log file:"
outputtmp = os.popen("grep -i \"FAILED\\|Error\" " + LogFileIF).read()
print outputtmp
print "See the output from log file " + LogFileIF + ":"
print "======================================================"
outputtmp = os.popen("cat " + LogFileIF).read()
print outputtmp
print "See the output from log file " + LogFileIWH + ":"
print "======================================================"
outputtmp = os.popen("cat " + LogFileIWH).read()
print outputtmp
print "See the template templates/bug761254.tdl"
print "======================================================"
outputtmp = os.popen("cat templates/bug761254.tdl").read()
print outputtmp
return False
#check if status is either complete or building
for timage in target_image:
print "Let\'s check this image: " + timage
data = json.loads(helpTest(timage))
print "Data Status for image "+timage+": " + data['status']
while data['status'] == "BUILDING":
Counter = Counter + 1
data = json.loads(helpTest(timage))
print "Data Status for image "+timage+": " + data['status']
#wait a minute
time.sleep(MINUTE)
data = json.loads(helpTest(timage))
print "Data Status for image "+timage+": " + data['status']
if Counter > TIMEOUT:
print "Error: timeout over "+str(TIMEOUT)+" minutes !"
return False
if data['status'] != "COMPLETED":
print "Build "+timage+" is not completed for some reason! It looks it stuck in the NEW status."
print "Perhaps you can find something in the log file " + LogFileIF + ":"
print "======================================================"
outputtmp = os.popen("cat " + LogFileIF).read()
print outputtmp
print "See the output from log file " + LogFileIWH + " too:"
print "======================================================"
outputtmp = os.popen("cat " + LogFileIWH).read()
print outputtmp
print "See the template templates/bug761254.tdl"
print "======================================================"
outputtmp = os.popen("cat templates/bug761254.tdl").read()
print outputtmp
return False
return True
#cleanup after test
def cleanTest():
print "=============================================="
print "Cleaning the mess after test"
if os.path.isfile(RHEVMBackupFile):
#copy file back rhevm
shutil.copyfile(RHEVMBackupFile, RHEVMconfigureFile)
if os.path.isfile(VSPHEREBackupFile):
#copy file back VSPHERE
shutil.copyfile(VSPHEREBackupFile, VSPHEREconfigureFile)
#future TODO: maybe delete all iso's and images beneath directories /var/lib/imagefactory/images/ and /var/lib/oz/isos/
return True
#execute the tests and return value (can be saved as a draft for future tests)
if setupTest():
if bodyTest():
if cleanTest():
print "=============================================="
print "Test PASSED entirely !"
sys.exit(SUCCESS)
else:
print "=============================================="
print "Although Test was successful, cleaning after test wasn't successful !"
sys.exit(RET_CLEANTEST)
else:
print "=============================================="
print "Test Failed !"
cleanTest()
sys.exit(RET_BODYTEST)
else:
print "=============================================="
print "Test setup wasn't successful ! Test didn't even proceed !"
cleanTest()
sys.exit(RET_SETUPTEST)