55import time
66import sys
77import docker
8+ import requests
9+ import anybadge
810from selenium import webdriver
911from selenium .common .exceptions import ErrorInResponseException ,TimeoutException
1012from jinja2 import Template
@@ -66,6 +68,10 @@ def convert_env(vars):
6668global dockerenv
6769global region
6870global bucket
71+ global screenshot
72+ global port
73+ global ssl
74+ global testdelay
6975try :
7076 webauth = os .environ ["WEB_AUTH" ]
7177except KeyError :
@@ -86,24 +92,33 @@ def convert_env(vars):
8692 bucket = os .environ ["DO_BUCKET" ]
8793except KeyError :
8894 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'
90111
91112# Make sure all needed env variables are set
92113def check_env ():
93114 try :
94115 global image
95- global testdelay
96116 global tags
97117 global meta_tag
98- global port
99- global ssl
100118 global base
101119 global spaces_key
102120 global spaces_secret
103121 image = os .environ ["IMAGE" ]
104- testdelay = os .environ ["DELAY_START" ]
105- port = os .environ ["PORT" ]
106- ssl = os .environ ["SSL" ]
107122 base = os .environ ["BASE" ]
108123 spaces_key = os .environ ["ACCESS_KEY" ]
109124 spaces_secret = os .environ ["SECRET_KEY" ]
@@ -130,9 +145,13 @@ def create_dir():
130145# Take a screenshot using the webdriver
131146def take_screenshot (endpoint ,container_tag ):
132147 try :
148+ requests .get (endpoint , timeout = 3 )
133149 driver .get (endpoint )
134150 driver .get_screenshot_as_file (outdir + container_tag + '.png' )
135151 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 ()
136155 except ErrorInResponseException as error :
137156 report_tests .append (['Screenshot ' + container_tag ,'FAIL SERVER ERROR' ])
138157 mark_fail ()
@@ -167,16 +186,17 @@ def container_test(tag):
167186 elif logsfound == False :
168187 report_tests .append (['Startup ' + tag ,'FAIL INIT NOT FINISHED' ])
169188 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 )
180200 # Dump package information
181201 if base == 'alpine' :
182202 command = 'apk info -v'
@@ -220,13 +240,23 @@ def report_render():
220240 meta_tag = meta_tag ,
221241 image = image ,
222242 bucket = bucket ,
223- region = region )
243+ region = region ,
244+ screenshot = screenshot )
224245 with open (outdir + 'report.md' , 'w' ) as f :
225246 f .write (markdown )
226247
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+
227256# Upload report to DO Spaces
228257def report_upload ():
229258 destination_dir = image + '/' + meta_tag + '/'
259+ latest_dir = image + '/latest/'
230260 spaces = session .client (
231261 's3' ,
232262 region_name = region ,
@@ -241,16 +271,33 @@ def report_upload():
241271 bucket ,
242272 destination_dir + 'index.html' ,
243273 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" })
244279 except Exception as error :
245280 core_fail ('Upload Error ' + str (error ))
246281 # Loop for all others
247282 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'
248290 try :
249291 spaces .upload_file (
250292 outdir + filename ,
251293 bucket ,
252294 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" })
254301 except Exception as error :
255302 core_fail ('Upload Error ' + str (error ))
256303
@@ -266,6 +313,7 @@ def report_upload():
266313# Quit selenium webdriver
267314driver .quit ()
268315report_render ()
316+ badge_render ()
269317report_upload ()
270318# Exit based on test results
271319if report_status == 'pass' :
0 commit comments