5
5
import time
6
6
import sys
7
7
import docker
8
+ import requests
9
+ import anybadge
8
10
from selenium import webdriver
9
11
from selenium .common .exceptions import ErrorInResponseException ,TimeoutException
10
12
from jinja2 import Template
@@ -66,6 +68,10 @@ def convert_env(vars):
66
68
global dockerenv
67
69
global region
68
70
global bucket
71
+ global screenshot
72
+ global port
73
+ global ssl
74
+ global testdelay
69
75
try :
70
76
webauth = os .environ ["WEB_AUTH" ]
71
77
except KeyError :
@@ -86,24 +92,33 @@ def convert_env(vars):
86
92
bucket = os .environ ["DO_BUCKET" ]
87
93
except KeyError :
88
94
bucket = 'lsio-ci'
89
-
95
+ try :
96
+ screenshot = os .environ ["WEB_SCREENSHOT" ]
97
+ except KeyError :
98
+ screenshot = 'false'
99
+ try :
100
+ port = os .environ ["PORT" ]
101
+ except KeyError :
102
+ port = '80'
103
+ try :
104
+ ssl = os .environ ["SSL" ]
105
+ except KeyError :
106
+ ssl = 'false'
107
+ try :
108
+ testdelay = os .environ ["DELAY_START" ]
109
+ except KeyError :
110
+ testdelay = '5'
90
111
91
112
# Make sure all needed env variables are set
92
113
def check_env ():
93
114
try :
94
115
global image
95
- global testdelay
96
116
global tags
97
117
global meta_tag
98
- global port
99
- global ssl
100
118
global base
101
119
global spaces_key
102
120
global spaces_secret
103
121
image = os .environ ["IMAGE" ]
104
- testdelay = os .environ ["DELAY_START" ]
105
- port = os .environ ["PORT" ]
106
- ssl = os .environ ["SSL" ]
107
122
base = os .environ ["BASE" ]
108
123
spaces_key = os .environ ["ACCESS_KEY" ]
109
124
spaces_secret = os .environ ["SECRET_KEY" ]
@@ -130,9 +145,13 @@ def create_dir():
130
145
# Take a screenshot using the webdriver
131
146
def take_screenshot (endpoint ,container_tag ):
132
147
try :
148
+ requests .get (endpoint , timeout = 3 )
133
149
driver .get (endpoint )
134
150
driver .get_screenshot_as_file (outdir + container_tag + '.png' )
135
151
report_tests .append (['Screenshot ' + container_tag ,'PASS' ])
152
+ except (requests .Timeout , requests .ConnectionError , KeyError ) as e :
153
+ report_tests .append (['Screenshot ' + container_tag ,'FAIL CONNECTION ERROR' ])
154
+ mark_fail ()
136
155
except ErrorInResponseException as error :
137
156
report_tests .append (['Screenshot ' + container_tag ,'FAIL SERVER ERROR' ])
138
157
mark_fail ()
@@ -167,16 +186,17 @@ def container_test(tag):
167
186
elif logsfound == False :
168
187
report_tests .append (['Startup ' + tag ,'FAIL INIT NOT FINISHED' ])
169
188
mark_fail ()
170
- # Sleep for the user specified amount of time
171
- time .sleep (int (testdelay ))
172
- # Take a screenshot
173
- if ssl == 'true' :
174
- proto = 'https://'
175
- else :
176
- proto = 'http://'
177
- container .reload ()
178
- ip = container .attrs ["NetworkSettings" ]["Networks" ]["bridge" ]["IPAddress" ]
179
- take_screenshot (proto + webauth + '@' + ip + ':' + port + webpath ,tag )
189
+ if screenshot == 'true' :
190
+ # Sleep for the user specified amount of time
191
+ time .sleep (int (testdelay ))
192
+ # Take a screenshot
193
+ if ssl == 'true' :
194
+ proto = 'https://'
195
+ else :
196
+ proto = 'http://'
197
+ container .reload ()
198
+ ip = container .attrs ["NetworkSettings" ]["Networks" ]["bridge" ]["IPAddress" ]
199
+ take_screenshot (proto + webauth + '@' + ip + ':' + port + webpath ,tag )
180
200
# Dump package information
181
201
if base == 'alpine' :
182
202
command = 'apk info -v'
@@ -220,13 +240,23 @@ def report_render():
220
240
meta_tag = meta_tag ,
221
241
image = image ,
222
242
bucket = bucket ,
223
- region = region )
243
+ region = region ,
244
+ screenshot = screenshot )
224
245
with open (outdir + 'report.md' , 'w' ) as f :
225
246
f .write (markdown )
226
247
248
+ # Render the markdown file for upload
249
+ def badge_render ():
250
+ try :
251
+ badge = anybadge .Badge ('CI' , report_status , thresholds = {'PASS' : 'green' , 'FAIL' : 'red' })
252
+ badge .write_badge (outdir + 'badge.svg' )
253
+ except Exception as error :
254
+ print (error )
255
+
227
256
# Upload report to DO Spaces
228
257
def report_upload ():
229
258
destination_dir = image + '/' + meta_tag + '/'
259
+ latest_dir = image + '/latest/'
230
260
spaces = session .client (
231
261
's3' ,
232
262
region_name = region ,
@@ -241,16 +271,33 @@ def report_upload():
241
271
bucket ,
242
272
destination_dir + 'index.html' ,
243
273
ExtraArgs = {'ContentType' : "text/html" , 'ACL' : "public-read" })
274
+ spaces .upload_file (
275
+ index_file ,
276
+ bucket ,
277
+ latest_dir + 'index.html' ,
278
+ ExtraArgs = {'ContentType' : "text/html" , 'ACL' : "public-read" })
244
279
except Exception as error :
245
280
core_fail ('Upload Error ' + str (error ))
246
281
# Loop for all others
247
282
for filename in os .listdir (outdir ):
283
+ # Set content types for files
284
+ if filename .lower ().endswith ('.svg' ):
285
+ CT = 'image/svg+xml'
286
+ elif filename .lower ().endswith ('.png' ):
287
+ CT = 'image/png'
288
+ elif filename .lower ().endswith ('.md' ):
289
+ CT = 'text/markdown'
248
290
try :
249
291
spaces .upload_file (
250
292
outdir + filename ,
251
293
bucket ,
252
294
destination_dir + filename ,
253
- ExtraArgs = {'ACL' : "public-read" })
295
+ ExtraArgs = {'ContentType' : CT ,'ACL' : "public-read" })
296
+ spaces .upload_file (
297
+ outdir + filename ,
298
+ bucket ,
299
+ latest_dir + filename ,
300
+ ExtraArgs = {'ContentType' : CT ,'ACL' : "public-read" })
254
301
except Exception as error :
255
302
core_fail ('Upload Error ' + str (error ))
256
303
@@ -266,6 +313,7 @@ def report_upload():
266
313
# Quit selenium webdriver
267
314
driver .quit ()
268
315
report_render ()
316
+ badge_render ()
269
317
report_upload ()
270
318
# Exit based on test results
271
319
if report_status == 'pass' :
0 commit comments