Best Python code snippet using avocado_python
test_twmsbox_wmts_convert.py
Source:test_twmsbox_wmts_convert.py
1#!/usr/bin/env python32# Licensed under the Apache License, Version 2.0 (the "License");3# you may not use this file except in compliance with the License.4# You may obtain a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.13#14# Tests for twmsbox2wmts.py and wmts2twmsbox.py15#16import subprocess17import sys18import unittest2 as unittest19import xmlrunner20from optparse import OptionParser21from math import isclose22# Handles parsing the raw output of twmsbox2wmts.py and wmts2twmsbox.py23def parse_twms_wmts_output(output):24 lines = output.split('\n')25 result_dict = {}26 unexpected_lines = ""27 for line in lines[:-1]: # last line is whitespace28 if "EPSG" in line:29 result_dict["EPSG"] = line.split(':')[1]30 elif "Scale Denominator" in line:31 result_dict["Scale Denominator"] = line.split(': ')[1]32 elif "Top Left BBOX" in line:33 result_dict["Top Left BBOX"] = line.split(': ')[1]34 elif "Request BBOX" in line:35 result_dict["Request BBOX"] = line.split(': ')[1]36 elif "TILECOL" in line:37 result_dict["TILECOL"] = line.split('=')[1]38 elif "TILEROW" in line:39 result_dict["TILEROW"] = line.split('=')[1]40 elif "tilesize" in line:41 result_dict["tilesize"] = line.split(': ')[1]42 else: # parse error, something is there that shouldn't be43 unexpected_lines += line + '\n'44 return result_dict, unexpected_lines45 46# Handles comparing two strings representing bounding boxes47# For example: comparing "-81.0000000000,36.0000000000,-72.0000000000,45.0000000000"48# to "-81,36,-72,45", which is the same box49def compare_bbox_str(req_bbox, twmsbox):50 # convert both strings into numerical values for comparison51 req_bbox_lst = list(map(lambda x: float(x), req_bbox.split(',')))52 twmsbox_lst = list(map(lambda x: float(x), twmsbox.split(',')))53 if req_bbox_lst == twmsbox_lst:54 return True, ""55 else:56 fail_str = "Expected values: {0}\nActual values: {1}\n".format(','.join(map(str, twmsbox_lst)),57 ','.join(map(str, req_bbox_lst)))58 return False, fail_str59class TestTWMSboxWMTSConvert(unittest.TestCase):60 61 # Tests converting from a Tiled WMS box to WMTS tile using `twmsbox2wmts.py`.62 def test_twmsbox2wmts(self):63 twmsbox_input = "-81,36,-72,45"64 expected_wmts = """Using EPSG:432665Top Left BBOX: -180,81,-171,9066Request BBOX: -81,36,-72,4567Scale Denominator: 6988528.300358998368TILECOL=1169TILEROW=570"""71 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {0}".format(twmsbox_input)72 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")73 fail_str = "twmsbox2wmts.py output does not match expected.\n"74 fail_str += "The following is the expected output from twmsbox2wmts.py:\n{}".format(expected_wmts)75 fail_str += "\nThe following is the actual output from twmsbox2wmts.py:\n{}".format(wmts_output)76 77 self.assertTrue(wmts_output == expected_wmts, fail_str)78 79 # Tests converting from a Tiled WMS box to WMTS tile using `twmsbox2wmts.py` with a tilesize specified.80 def test_twmsbox2wmts_tilesize(self):81 twmsbox_input = "-81,36,-72,45"82 tilesize_input = "256"83 expected_wmts = """Using EPSG:432684Using tilesize: 25685Top Left BBOX: -180,81,-171,9086Request BBOX: -81,36,-72,4587Scale Denominator: 13977056.600717996788TILECOL=1189TILEROW=590"""91 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {0} -T {1}".format(twmsbox_input,92 tilesize_input)93 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")94 95 fail_str = "twmsbox2wmts.py output does not match expected.\n"96 fail_str += "The following is the expected output from twmsbox2wmts.py:\n{}".format(expected_wmts)97 fail_str += "\nThe following is the actual output from twmsbox2wmts.py:\n{}".format(wmts_output)98 99 self.assertTrue(wmts_output == expected_wmts, fail_str)100 # Tests converting from a WMTS tile to a Tiled WMS box using `wmts2twmsbox.py`.101 # Uses the Scale Denominator option for `wmts2twmsbox.py`102 def test_wmts2twmsbox_scale_denom(self):103 wmts_input = {104 "Scale Denominator": "6988528.300359",105 "TILECOL": "11",106 "TILEROW": "5"107 }108 expected_twmsbox = """Using EPSG:4326109Scale Denominator: 6988528.300359110TILECOL=11111TILEROW=5112Top Left BBOX: -180.0000000000,81.0000000000,-171.0000000000,90.0000000000113Request BBOX: -81.0000000000,36.0000000000,-72.0000000000,45.0000000000114"""115 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -s {0} -c {1} -r {2}".format(wmts_input['Scale Denominator'],116 wmts_input['TILECOL'],117 wmts_input['TILEROW'])118 twmsbox_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")119 fail_str = "wmts2twmsbox.py output does not match expected.\n"120 fail_str += "The following is the expected output from wmts2twmsbox.py:\n{}".format(expected_twmsbox)121 fail_str += "\nThe following is the actual output from wmts2twmsbox.py:\n{}".format(twmsbox_output)122 123 self.assertTrue(twmsbox_output == expected_twmsbox, fail_str)124 125 # Tests converting from a WMTS tile to a Tiled WMS box using `wmts2twmsbox.py`126 # with a specified tilesize.127 # Uses the Scale Denominator option for `wmts2twmsbox.py`.128 def test_wmts2twmsbox_scale_denom_tilesize(self):129 wmts_input = {130 "Scale Denominator": "6988528.300359",131 "TILECOL": "11",132 "TILEROW": "5",133 "tilesize": "256"134 }135 expected_twmsbox = """Using EPSG:4326136Using tilesize: 256137Scale Denominator: 6988528.300359138TILECOL=11139TILEROW=5140Top Left BBOX: -180.0000000000,85.5000000000,-175.5000000000,90.0000000000141Request BBOX: -130.5000000000,63.0000000000,-126.0000000000,67.5000000000142"""143 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -s {0} -c {1} -r {2} -T {3}".format(wmts_input['Scale Denominator'],144 wmts_input['TILECOL'],145 wmts_input['TILEROW'],146 wmts_input['tilesize'])147 twmsbox_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")148 149 fail_str = "wmts2twmsbox.py output does not match expected.\n"150 fail_str += "The following is the expected output from wmts2twmsbox.py:\n{}".format(expected_twmsbox)151 fail_str += "\nThe following is the actual output from wmts2twmsbox.py:\n{}".format(twmsbox_output)152 153 self.assertTrue(twmsbox_output == expected_twmsbox, fail_str)154 # Tests converting from a WMTS tile to a Tiled WMS box using `wmts2twmsbox.py`.155 # Uses the Top Left BBOX option for `wmts2twmsbox.py`156 def test_wmts2twmsbox_top_left_bbox(self):157 wmts_input = {158 "Top Left BBOX": "-180,81,-171,90",159 "TILECOL": "11",160 "TILEROW": "5"161 }162 expected_twmsbox = """Using EPSG:4326163Top Left BBOX: -180,81,-171,90164TILECOL=11165TILEROW=5166Scale Denominator: 6988528.300359167Request BBOX: -81.0000000000,36.0000000000,-72.0000000000,45.0000000000168"""169 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -t {0} -c {1} -r {2}".format(wmts_input['Top Left BBOX'],170 wmts_input['TILECOL'],171 wmts_input['TILEROW'])172 twmsbox_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")173 fail_str = "wmts2twmsbox.py output does not match expected.\n"174 fail_str += "The following is the expected output from wmts2twmsbox.py:\n{}".format(expected_twmsbox)175 fail_str += "\nThe following is the actual output from wmts2twmsbox.py:\n{}".format(twmsbox_output)176 177 self.assertTrue(twmsbox_output == expected_twmsbox, fail_str)178 # Tests converting from a Tiled WMS box to WMTS tile and back to a Tiled WMS box179 # using first `twmsbox2wmts.py` and then `wmts2twmsbox.py`.180 # Runs `wmts2twmsbox.py` with Scale Denominator, TILECOL, and TILEROW as input.181 def test_twmsbox2wmts2twmsbox_scale_denom(self):182 twmsbox_input = "-81,36,-72,45"183 fail_str = ""184 185 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {0}".format(twmsbox_input)186 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")187 # parse the result to use as input for wmts2twmsbox.py188 wmts_dict, unexpected_lines = parse_twms_wmts_output(wmts_output)189 if unexpected_lines != "":190 fail_str += "ERROR: Unexpected line(s) in twmsbox2wmts.py output:\n{}".format(unexpected_lines)191 self.fail(fail_str)192 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -e {0} -s {1} -c {2} -r {3}".format(wmts_dict['EPSG'],193 wmts_dict['Scale Denominator'],194 wmts_dict['TILECOL'],195 wmts_dict['TILEROW'])196 twms_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")197 twms_dict, unexpected_lines = parse_twms_wmts_output(twms_output)198 if unexpected_lines != "":199 fail_str += "ERROR: Unexpected line(s) in wmts2twmsbox.py output:\n{}".format(unexpected_lines)200 self.fail(fail_str)201 test_result, fail_str = compare_bbox_str(twms_dict["Request BBOX"], twmsbox_input)202 fail_str = "`wmts2twmsbox.py` did not return the correct twmsbox values.\n" + fail_str203 self.assertTrue(test_result, fail_str)204 # Tests converting from a Tiled WMS box to WMTS tile and back to a Tiled WMS box205 # using first `twmsbox2wmts.py` and then `wmts2twmsbox.py`.206 # Runs `wmts2twmsbox.py` with Top Left BBOX, TILECOL, and TILEROW as input.207 def test_twmsbox2wmts2twmsbox_top_left_bbox(self):208 twmsbox_input = "-81,36,-72,45"209 fail_str = ""210 211 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {0}".format(twmsbox_input)212 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")213 # parse the result to use as input for wmts2twmsbox.py214 wmts_dict, unexpected_lines = parse_twms_wmts_output(wmts_output)215 if unexpected_lines != "":216 fail_str += "ERROR: Unexpected line(s) in twmsbox2wmts.py output:\n{}".format(unexpected_lines)217 self.fail(fail_str)218 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -e {0} -t {1} -c {2} -r {3}".format(wmts_dict['EPSG'],219 wmts_dict['Top Left BBOX'],220 wmts_dict['TILECOL'],221 wmts_dict['TILEROW'])222 twms_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")223 twms_dict, unexpected_lines = parse_twms_wmts_output(twms_output)224 if unexpected_lines != "":225 fail_str += "ERROR: Unexpected line(s) in wmts2twmsbox.py output:\n{}".format(unexpected_lines)226 self.fail(fail_str)227 228 test_result, fail_str = compare_bbox_str(twms_dict["Request BBOX"], twmsbox_input)229 fail_str = "`wmts2twmsbox.py` did not return the correct twmsbox values.\n" + fail_str230 self.assertTrue(test_result, fail_str)231 232 # Tests converting from a Tiled WMS box to WMTS tile and back to a Tiled WMS box233 # with a specified tilesize using first `twmsbox2wmts.py` and then `wmts2twmsbox.py`.234 # Runs `wmts2twmsbox.py` with Top Left BBOX, TILECOL, and TILEROW as input.235 def test_twmsbox2wmts2twmsbox_tilesize_top_left_bbox(self):236 twmsbox_input = "-81,36,-72,45"237 tilesize_input = "256"238 fail_str = ""239 240 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {0} -T {1}".format(twmsbox_input, tilesize_input)241 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")242 # parse the result to use as input for wmts2twmsbox.py243 wmts_dict, unexpected_lines = parse_twms_wmts_output(wmts_output)244 if unexpected_lines != "":245 fail_str += "ERROR: Unexpected line(s) in twmsbox2wmts.py output:\n{}".format(unexpected_lines)246 self.fail(fail_str)247 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -e {0} -t {1} -c {2} -r {3} -T {4}".format(wmts_dict['EPSG'],248 wmts_dict['Top Left BBOX'],249 wmts_dict['TILECOL'],250 wmts_dict['TILEROW'],251 wmts_dict["tilesize"])252 twms_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")253 twms_dict, unexpected_lines = parse_twms_wmts_output(twms_output)254 255 if unexpected_lines != "":256 fail_str += "ERROR: Unexpected line(s) in wmts2twmsbox.py output:\n{}".format(unexpected_lines)257 self.fail(fail_str)258 259 test_result, fail_str = compare_bbox_str(twms_dict["Request BBOX"], twmsbox_input)260 fail_str = "`wmts2twmsbox.py` did not return the correct twmsbox values.\n" + fail_str261 self.assertTrue(test_result, fail_str)262 # Tests converting from a WMTS tile to Tiled WMS box and back to a WMTS box263 # using first `wmts2twmsbox.py` and then `twmsbox2wmts.py`.264 # Runs `wmts2twmsbox.py` with Scale Denominator, TILECOL, and TILEROW as input.265 def test_wmts2twmsbox2wmts_scale_denom(self):266 wmts_input = {267 "Scale Denominator": "6988528.300359",268 "TILECOL": "11",269 "TILEROW": "5"270 }271 fail_str = ""272 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -s {0} -c {1} -r {2}".format(wmts_input['Scale Denominator'],273 wmts_input['TILECOL'],274 wmts_input['TILEROW'])275 276 twms_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")277 # parse the result to use as input for twmsbox2wmts.py278 twms_dict, unexpected_lines = parse_twms_wmts_output(twms_output)279 if unexpected_lines != "":280 fail_str += "ERROR: Unexpected line(s) in wmts2twmsbox.py output:\n{}".format(unexpected_lines)281 self.fail(fail_str)282 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {}".format(twms_dict["Request BBOX"])283 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")284 wmts_dict, unexpected_lines = parse_twms_wmts_output(wmts_output)285 if unexpected_lines != "":286 fail_str += "ERROR: Unexpected line(s) in twmsbox2wmts.py output:\n{}".format(unexpected_lines)287 self.fail(fail_str)288 # check if the original input values were returned289 test_result = True290 # use isclose because the values may be rounded differently (6988528.300359 vs 6988528.3003589983)291 if not isclose(float(wmts_input["Scale Denominator"]),float(wmts_dict["Scale Denominator"])):292 test_result = False293 fail_str += "`twmsbox2wmts.py` returned {0} for Scale Denominator when it should have returned {1}\n".format(wmts_dict["Scale Denominator"],294 wmts_input["Scale Denominator"])295 if wmts_input["TILECOL"] != wmts_dict["TILECOL"]:296 test_result = False297 fail_str += "`twmsbox2wmts.py` returned {0} for TILECOL when it should have returned {1}\n".format(wmts_dict["TILECOL"],298 wmts_input["TILECOL"])299 if wmts_input["TILEROW"] != wmts_dict["TILEROW"]:300 test_result = False301 fail_str += "`twmsbox2wmts.py` returned {0} for TILEROW when it should have returned {1}\n".format(wmts_dict["TILEROW"],302 wmts_input["TILEROW"])303 self.assertTrue(test_result, fail_str)304 305 # Tests converting from a WMTS tile to Tiled WMS box and back to a WMTS box306 # with a specified tilesize using first `wmts2twmsbox.py` and then `twmsbox2wmts.py`.307 # Runs `wmts2twmsbox.py` with Scale Denominator, TILECOL, and TILEROW as input.308 def test_wmts2twmsbox2wmts_tilesize_scale_denom(self):309 wmts_input = {310 "Scale Denominator": "6988528.300359",311 "TILECOL": "11",312 "TILEROW": "5",313 "tilesize": "256"314 }315 fail_str = ""316 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -s {0} -c {1} -r {2} -T {3}".format(wmts_input['Scale Denominator'],317 wmts_input['TILECOL'],318 wmts_input['TILEROW'],319 wmts_input['tilesize'])320 321 twms_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")322 # parse the result to use as input for twmsbox2wmts.py323 twms_dict, unexpected_lines = parse_twms_wmts_output(twms_output)324 if unexpected_lines != "":325 fail_str += "ERROR: Unexpected line(s) in wmts2twmsbox.py output:\n{}".format(unexpected_lines)326 self.fail(fail_str)327 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {0} -T {1}".format(twms_dict["Request BBOX"],328 twms_dict["tilesize"])329 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")330 wmts_dict, unexpected_lines = parse_twms_wmts_output(wmts_output)331 if unexpected_lines != "":332 fail_str += "ERROR: Unexpected line(s) in twmsbox2wmts.py output:\n{}".format(unexpected_lines)333 self.fail(fail_str)334 # check if the original input values were returned335 test_result = True336 # use isclose because the values may be rounded differently (6988528.300359 vs 6988528.3003589983)337 if not isclose(float(wmts_input["Scale Denominator"]),float(wmts_dict["Scale Denominator"])):338 test_result = False339 fail_str += "`twmsbox2wmts.py` returned {0} for Scale Denominator when it should have returned {1}\n".format(wmts_dict["Scale Denominator"],340 wmts_input["Scale Denominator"])341 if wmts_input["TILECOL"] != wmts_dict["TILECOL"]:342 test_result = False343 fail_str += "`twmsbox2wmts.py` returned {0} for TILECOL when it should have returned {1}\n".format(wmts_dict["TILECOL"],344 wmts_input["TILECOL"])345 if wmts_input["TILEROW"] != wmts_dict["TILEROW"]:346 test_result = False347 fail_str += "`twmsbox2wmts.py` returned {0} for TILEROW when it should have returned {1}\n".format(wmts_dict["TILEROW"],348 wmts_input["TILEROW"])349 self.assertTrue(test_result, fail_str)350 # Tests converting from a WMTS tile to Tiled WMS box and back to a WMTS box351 # using first `wmts2twmsbox.py` and then `twmsbox2wmts.py`.352 # Runs `wmts2twmsbox.py` with Top Left BBOX, TILECOL, and TILEROW as input.353 def test_wmts2twmsbox2wmts_top_left_bbox(self):354 wmts_input = {355 "Top Left BBOX": "-180,81,-171,90",356 "TILECOL": "11",357 "TILEROW": "5"358 }359 fail_str = ""360 wmts_cmd = "python3 /home/oe2/onearth/src/scripts/wmts2twmsbox.py -t {0} -c {1} -r {2}".format(wmts_input['Top Left BBOX'],361 wmts_input['TILECOL'],362 wmts_input['TILEROW'])363 364 twms_output = subprocess.check_output(wmts_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")365 # parse the result to use as input for twmsbox2wmts.py366 twms_dict, unexpected_lines = parse_twms_wmts_output(twms_output)367 if unexpected_lines != "":368 fail_str += "ERROR: Unexpected line(s) in wmts2twmsbox.py output:\n{}".format(unexpected_lines)369 self.fail(fail_str)370 twms_cmd = "python3 /home/oe2/onearth/src/scripts/twmsbox2wmts.py -b {}".format(twms_dict["Request BBOX"])371 wmts_output = subprocess.check_output(twms_cmd, shell=True, stderr=subprocess.PIPE).decode("utf-8")372 wmts_dict, unexpected_lines = parse_twms_wmts_output(wmts_output)373 if unexpected_lines != "":374 fail_str += "ERROR: Unexpected line(s) in twmsbox2wmts.py output:\n{}".format(unexpected_lines)375 self.fail(fail_str)376 # check if the original input values were returned377 test_result, fail_str = compare_bbox_str(wmts_input["Top Left BBOX"], wmts_dict["Top Left BBOX"])378 fail_str = "`twmsbox2wmts.py` did not return the correct Top Left BBOX values.\n" + fail_str379 if wmts_input["TILECOL"] != wmts_dict["TILECOL"]:380 test_result = False381 fail_str += "`twmsbox2wmts.py` returned {0} for TILECOL when it should have returned {1}\n".format(wmts_dict["TILECOL"],382 wmts_input["TILECOL"])383 if wmts_input["TILEROW"] != wmts_dict["TILEROW"]:384 test_result = False385 fail_str += "`twmsbox2wmts.py` returned {0} for TILEROW when it should have returned {1}\n".format(wmts_dict["TILEROW"],386 wmts_input["TILEROW"])387 self.assertTrue(test_result, fail_str)388 389if __name__ == '__main__':390 # Parse options before running tests391 parser = OptionParser()392 parser.add_option(393 '-o',394 '--output',395 action='store',396 type='string',397 dest='outfile',398 default='test_twmsbox_wmts_convert_results.xml',399 help='Specify XML output file (default is test_twmsbox_wmts_convert_results.xml')400 parser.add_option(401 '-s',402 '--start_server',403 action='store_true',404 dest='start_server',405 help='Load test configuration into Apache and quit (for debugging)')406 (options, args) = parser.parse_args()407 # --start_server option runs the test Apache setup, then quits.408 if options.start_server:409 TestTWMSboxWMTSConvert.setUpClass()410 sys.exit(411 'Apache has been loaded with the test configuration. No tests run.'412 )413 # Have to delete the arguments as they confuse unittest414 del sys.argv[1:]415 with open(options.outfile, 'wb') as f:416 print('\nStoring test results in "{0}"'.format(options.outfile))...
test_validate_palette.py
Source:test_validate_palette.py
1#!/usr/bin/env python32# Licensed under the Apache License, Version 2.0 (the "License");3# you may not use this file except in compliance with the License.4# You may obtain a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.13#14# Tests for oe_validate_palette.py15#16import subprocess17import sys18import unittest2 as unittest19import xmlrunner20from optparse import OptionParser21import os22SCRIPT_PATH = "/usr/bin/oe_validate_palette.py"23# Handles parsing the raw output of oe_validate_palette.py in the event of a failure.24# Returns a dictionary of the relevant results25def parse_failure_output(output, returncode):26 lines = output.split('\n')27 result_dict = { "returncode": returncode }28 for line in lines:29 if "Matched palette entries" in line:30 result_dict["Matched palette entries"] = int(line.split(':')[1])31 elif "Mismatched palette entries" in line:32 result_dict["Mismatched palette entries"] = int(line.split(':')[1])33 elif "Missing palette entries" in line:34 result_dict["Missing palette entries"] = int(line.split(':')[1])35 elif "Extra palette entries" in line:36 result_dict["Extra palette entries"] = int(line.split(': ')[1])37 return result_dict38# Takes dictionaries containing the expected and actual output values for validation failure.39# Returns a string 40def report_discrepencies(expected_out, actual_out):41 report_str = ""42 for key in expected_out:43 if expected_out[key] != actual_out.get(key):44 report_str += "- Expected {0}: {1}\n Actual {0}: {2}\n".format(key,45 expected_out[key],46 actual_out.get(key, "NA (not listed in output)"))47 return report_str48# Runs oe_validate_palette.py for tests that intend for the validation to fail.49# Returns a string with the test failure details in the event that the validation doesn't fail correctly.50def run_failure_validation_test(cmd_lst, colormap_path, img_path, expected_out):51 try:52 subprocess.check_output(cmd_lst)53 fail_str = "oe_validate_palette.py incorrectly indicated that validation of {0} and {1} succeeded.".format(colormap_path, img_path)54 except subprocess.CalledProcessError as val_pal_except:55 fail_str_prefix = ("\noe_validate_palette.py correctly failed in validating {0} and {1},\n"56 "but encountered the following problems:\n").format(colormap_path, img_path)57 val_pal_output = val_pal_except.output.decode("utf-8")58 result_out = parse_failure_output(val_pal_output, val_pal_except.returncode)59 fail_str = report_discrepencies(expected_out, result_out)60 if fail_str != "":61 fail_str = fail_str_prefix + fail_str62 fail_str += "\nThe following is the complete output of oe_validate_palette.py:\n\n{}".format(val_pal_output)63 return fail_str64 65class TestValidatePalette(unittest.TestCase):66 67 # Tests oe_validate_palette.py using a colormap with the corresponding image that matches the colormap.68 # Passes if the validation is successful.69 def test_validate_correct_palette(self):70 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")71 img_path = os.path.join(os.getcwd(), "mrfgen_files/AIRS/AIRS_L2_SST_A_LL_v6_NRT_2019344.png")72 fail_str = ""73 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path]74 try:75 subprocess.check_output(cmd_lst)76 except subprocess.CalledProcessError as val_pal_except:77 fail_str += "oe_validate_palette.py failed to correctly validate {0} and {1}\n".format(colormap_path, img_path)78 fail_str += "oe_validate_palette.py returned {} and produced the following output:\n\n".format(val_pal_except.returncode)79 fail_str += val_pal_except.output.decode("utf-8")80 self.assertTrue(fail_str == "", fail_str)81 # Tests oe_validate_palette.py using a colormap with the corresponding image that matches the colormap.82 # Uses the `-n` (`--no_index`) option.83 # Passes if the validation is successful.84 def test_validate_correct_palette_no_index(self):85 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")86 img_path = os.path.join(os.getcwd(), "mrfgen_files/AIRS/AIRS_L2_SST_A_LL_v6_NRT_2019344.png")87 fail_str = ""88 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path, '-n']89 try:90 subprocess.check_output(cmd_lst)91 except subprocess.CalledProcessError as val_pal_except:92 fail_str += "oe_validate_palette.py failed to correctly validate {0} and {1}\n".format(colormap_path, img_path)93 fail_str += "oe_validate_palette.py returned {} and produced the following output:\n\n".format(val_pal_except.returncode)94 fail_str += val_pal_except.output.decode("utf-8")95 self.assertTrue(fail_str == "", fail_str)96 97 # Tests oe_validate_palette.py using a colormap with the corresponding image that matches the colormap.98 # Uses the `-x` (`--ignore_colors`) option.99 # Passes if the validation is successful.100 def test_validate_correct_palette_ignore_colors(self):101 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")102 img_path = os.path.join(os.getcwd(), "mrfgen_files/AIRS/AIRS_L2_SST_A_LL_v6_NRT_2019344.png")103 ignored_colors = "146,111,169,255|145,114,160,255"104 fail_str = ""105 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path, '-x', ignored_colors]106 try:107 subprocess.check_output(cmd_lst)108 except subprocess.CalledProcessError as val_pal_except:109 fail_str += "oe_validate_palette.py failed to correctly validate {0} and {1}\n".format(colormap_path, img_path)110 fail_str += "oe_validate_palette.py returned {} and produced the following output:\n\n".format(val_pal_except.returncode)111 fail_str += val_pal_except.output.decode("utf-8")112 self.assertTrue(fail_str == "", fail_str)113 # Tests oe_validate_palette.py using a colormap with the corresponding image that matches the colormap.114 # Uses the `-f` (`--fill_value`) option.115 # Passes if the validation is successful.116 def test_validate_correct_palette_fill_value(self):117 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")118 img_path = os.path.join(os.getcwd(), "mrfgen_files/AIRS/AIRS_L2_SST_A_LL_v6_NRT_2019344.png")119 fill_value = "146,111,169,255"120 fail_str = ""121 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path, '-f', fill_value]122 try:123 subprocess.check_output(cmd_lst)124 except subprocess.CalledProcessError as val_pal_except:125 fail_str += "oe_validate_palette.py failed to correctly validate {0} and {1}\n".format(colormap_path, img_path)126 fail_str += "oe_validate_palette.py returned {} and produced the following output:\n\n".format(val_pal_except.returncode)127 fail_str += val_pal_except.output.decode("utf-8")128 self.assertTrue(fail_str == "", fail_str)129 # Tests oe_validate_palette.py using a colormap with an image that doesn't match the colormap.130 # Passes if the validation fails and correct failure details are given.131 def test_validate_incorrect_palette(self):132 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")133 img_path = os.path.join(os.getcwd(), "mrfgen_files/MYR4ODLOLLDY/MYR4ODLOLLDY_global_2014277_10km.png")134 expected_out = {135 "returncode": 3,136 "Matched palette entries": 0,137 "Mismatched palette entries": 22,138 "Missing palette entries": 231,139 "Extra palette entries": 0140 }141 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path]142 fail_str = run_failure_validation_test(cmd_lst, colormap_path, img_path, expected_out)143 self.assertTrue(fail_str == "", fail_str)144 145 # Tests oe_validate_palette.py using a colormap with an image that doesn't match the colormap.146 # Uses the `-n` (`--no_index`) option.147 # Passes if the validation fails and correct failure details are given.148 def test_validate_incorrect_palette_no_index(self):149 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")150 img_path = os.path.join(os.getcwd(), "mrfgen_files/MYR4ODLOLLDY/MYR4ODLOLLDY_global_2014277_10km.png")151 expected_out = {152 "returncode": 3,153 "Matched palette entries": 0,154 "Missing palette entries": 253,155 "Extra palette entries": 21156 }157 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path, '-n']158 fail_str = run_failure_validation_test(cmd_lst, colormap_path, img_path, expected_out)159 self.assertTrue(fail_str == "", fail_str)160 161 # Tests oe_validate_palette.py using a colormap with an image that doesn't match the colormap.162 # Uses the `-x` (`--ignore_colors`) option.163 # Passes if the validation fails and correct failure details are given.164 def test_validate_incorrect_palette_ignore_colors(self):165 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")166 img_path = os.path.join(os.getcwd(), "mrfgen_files/MYR4ODLOLLDY/MYR4ODLOLLDY_global_2014277_10km.png")167 ignored_colors = "146,111,169,255|145,114,160,255|220,220,255,0"168 expected_out = {169 "returncode": 3,170 "Matched palette entries": 0,171 "Mismatched palette entries": 21,172 "Missing palette entries": 231,173 "Extra palette entries": 0174 }175 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path, '-x', ignored_colors]176 fail_str = run_failure_validation_test(cmd_lst, colormap_path, img_path, expected_out)177 self.assertTrue(fail_str == "", fail_str)178 # Tests oe_validate_palette.py using a colormap with an image that doesn't match the colormap.179 # Uses the `-f` (`--fill_value`) option.180 # Passes if the validation fails and correct failure details are given.181 def test_validate_incorrect_palette_fill_value(self):182 colormap_path = os.path.join(os.getcwd(), "mrfgen_files/colormaps/AIRS_Temperature.xml")183 img_path = os.path.join(os.getcwd(), "mrfgen_files/MYR4ODLOLLDY/MYR4ODLOLLDY_global_2014277_10km.png")184 fill_val = "145,114,160,255"185 expected_out = {186 "returncode": 3,187 "Matched palette entries": 0,188 "Mismatched palette entries": 253,189 "Missing palette entries": 0,190 "Extra palette entries": 3191 }192 cmd_lst = [SCRIPT_PATH, '-c', colormap_path, '-i', img_path, '-f', fill_val]193 fail_str = run_failure_validation_test(cmd_lst, colormap_path, img_path, expected_out)194 self.assertTrue(fail_str == "", fail_str)195 196if __name__ == '__main__':197 # Parse options before running tests198 parser = OptionParser()199 parser.add_option(200 '-o',201 '--output',202 action='store',203 type='string',204 dest='outfile',205 default='test_validate_palette_results.xml',206 help='Specify XML output file (default is test_validate_palette_results.xml')207 parser.add_option(208 '-s',209 '--start_server',210 action='store_true',211 dest='start_server',212 help='Load test configuration into Apache and quit (for debugging)')213 (options, args) = parser.parse_args()214 # --start_server option runs the test Apache setup, then quits.215 if options.start_server:216 TestValidatePalette.setUpClass()217 sys.exit(218 'Apache has been loaded with the test configuration. No tests run.'219 )220 # Have to delete the arguments as they confuse unittest221 del sys.argv[1:]222 with open(options.outfile, 'wb') as f:223 print('\nStoring test results in "{0}"'.format(options.outfile))...
prog_rpi.py
Source:prog_rpi.py
1# System imports2import RPi.GPIO as GPIO3import time4import sys5import os6from colorama import Fore7import serial8from serial.tools import list_ports9import usb.core10# Local imports11sys.path.append("rpi-lib")12from sp_io import *13from adc import *14from shift_reg import *15from sp_serial import *16from oled import *17GPIO.setwarnings(False)18GPIO.setmode(GPIO.BCM)19RST = 1720EXT = 2321dut_type = None22# Map between IO position on Sea-Picro and location in shift reg chain23IO_TO_SR_MAP = [15, 16, 17, 18, 19, 20, 21, 22, 7, 8, 14, 0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13]24# IO list of SP in same order as above25SP_PIN_MAP = ["D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D21", "D23", "D20", "D22", "D26", "D27", "D28", "D29", "D12", "D13", "D14", "D15", "D16"]26# USB strings27RPI_VID = 0x2e8a28RPI_PID = 0x000329CPY_VID = 0x6a6a30CPY_PID = 0x535031global fail_str32fail_str = ""33def flash_circuitpython():34 ''' Flashes circuitpython firmware. '''35 global fail_str36 test_fail = True37 found_rpi = False38 attempt_counter = 039 # Spinlock until RPi bootloader is found40 while found_rpi == False:41 print(f"{Fore.BLUE}Searching for RP2040 bootloader")42 dev = usb.core.find(find_all=1)43 for cfg in dev:44 if cfg.idVendor == RPI_VID and cfg.idProduct == RPI_PID:45 found_rpi = True46 time.sleep(1)47 attempt_counter = attempt_counter + 148 if attempt_counter > 5:49 print(f'{Fore.RED}TIMEOUT TRYING TO FIND BOOTLOADER!')50 fail_str = "CPY"51 test_fail = True52 break53 if found_rpi:54 print(f"{Fore.BLUE}Bootloader found, copying CircuitPython")55 oled_write("Flashing CPY")56 time.sleep(3)57 retval = os.system("cp circuitpy-sea-picro.uf2 /media/pi/RPI-RP2")58 if retval != 0:59 print(f'{Fore.RED}FAILED TO FLASH CIRCUITPYTHON!')60 fail_str = "CPY"61 test_fail = True62 else:63 test_fail = False64 return test_fail65def flash_firmware():66 ''' Flashes test firmware. '''67 global fail_str68 test_fail = True69 found_cpy = False70 attempt_counter = 071 # Spinlock until circuitpython is found72 while found_cpy == False:73 print(f"{Fore.BLUE}Searching for Circuitpython")74 dev = usb.core.find(find_all=1)75 for cfg in dev:76 if cfg.idVendor == CPY_VID and cfg.idProduct == CPY_PID:77 found_cpy = True78 time.sleep(1)79 80 attempt_counter = attempt_counter + 181 if attempt_counter > 15:82 print(f'{Fore.RED}TIMEOUT TRYING TO FIND CIRCUITPYTHON!')83 fail_str = "FW"84 test_fail = True85 break86 if found_cpy:87 print(f"{Fore.BLUE}CircuitPython found, copying test firmware")88 oled_write("Flashing Test FW")89 time.sleep(5)90 retval = os.system("cp -rf circuitpy-files/* /media/pi/CIRCUITPY")91 if retval != 0:92 print(f'{Fore.RED}FAILED TO FLASH TEST FIRMWARE!')93 fail_str = "FW"94 test_fail = True95 else:96 test_fail = False97 sp_reset()98 time.sleep(3)99 return test_fail100def test_ident():101 # Check we have the right /dev/ttyACM port102 global fail_str103 test_fail = True104 send_dut_string("ident")105 ret_str = get_dut_string()106 if ret_str == "ident = Sea-Picro!":107 print(f'{Fore.BLUE}IDENT passed')108 test_fail = False109 else:110 print(f'{Fore.RED}IDENT Failed, possibly wrong /dev/ttyACM port')111 fail_str = "IDENT"112 test_fail = True113 return test_fail114def test_vbus():115 # Check VBUS detection116 global fail_str117 test_fail = True118 send_dut_string("vbus_test")119 ret_str = get_dut_string()120 if ret_str == "vbus = high":121 print(f'{Fore.BLUE}VBUS detect passed')122 test_fail = False123 else:124 print(f'{Fore.RED}VBUS detect failed')125 fail_str = "VBUS"126 return test_fail127def test_led():128 # RGB test time129 global fail_str130 test_fail = True131 print(f'{Fore.YELLOW}Monitor LEDs please')132 send_dut_string("led_rgb")133 134 print(f'{Fore.YELLOW}Did LEDs cycle R/G/B? YES / NO {Fore.WHITE}')135 oled_write("LEDs R/G/B?")136 time.sleep(1) # delay long enough that the operator has seen all colours work137 while True:138 if (not io_read(SW_YES)):139 print(f'{Fore.BLUE}RGB LEDs passed')140 test_fail = False141 send_dut_string("led_off")142 time.sleep(0.1)143 break144 if (not io_read(SW_NO)):145 print(f'{Fore.RED}RGB LEDs failed')146 send_dut_string("led_off")147 time.sleep(0.1)148 fail_str = "RGB"149 break150 return test_fail151def test_cc():152 global fail_str153 cc_val = adc_read("CC")154 if cc_val < 3000:155 print(f'{Fore.BLUE}CC pin passed')156 return False157 else:158 print(f'{Fore.RED}CC pin failed')159 fail_str = "CC"160 return True161def test_io(model):162 global fail_str163 test_fail = False164 for pos in range(0, model):165 shift_out((1 << IO_TO_SR_MAP[pos]))166 send_dut_string("io_test")167 ret_str = get_dut_string()168 array = ret_str[1:-1].split(", ") # Convert to array of strings "True", "False"169 key_fail = False # Used to flag if a key passes it's test or not170 # Loop through array and check val = False unless index == pos171 for index in range(0,model):172 # Case where we expect a key to be pressed173 if index == pos:174 if array[index] == "True":175 pass176 else:177 print(f'{Fore.RED}{SP_PIN_MAP[pos]}: IO {SP_PIN_MAP[index]} was NOT pressed')178 key_fail = True179 test_fail = True180 # Case where we do not expect a key to be pressed181 elif index != pos:182 if array[index] == "False":183 pass184 else:185 print(f'{Fore.RED}{SP_PIN_MAP[pos]}: IO {SP_PIN_MAP[index]} WAS pressed')186 key_fail = True187 test_fail = True188 if not key_fail:189 print(f'{Fore.BLUE}{SP_PIN_MAP[pos]}: IO {SP_PIN_MAP[pos]} passed')190 if test_fail:191 fail_str = "IO"192 return test_fail193def test_dut(model):194 test_fail = False195 oled_write("Testing DUT")196 # Don't continue testing if we can't ident properly197 if test_ident():198 return True199 test_fail = test_fail | test_vbus()200 test_fail = test_fail | test_cc()201 test_fail = test_fail | test_io(model)202 test_fail = test_fail | test_led()203 return test_fail 204# Init all the things!205io_init()206adc_init()207shift_reg_init()208oled_init()209time.sleep(1)210retest = False211test_only = False212# Super loop time213while True:214 if not retest:215 print(f"{Fore.YELLOW}Press YES to begin test!")216 oled_write("Begin Test?", size=20)217 retest = False218 while True:219 # try:220 if (not io_read(SW_YES) or test_only):221 time.sleep(0.1) # Shitty debounce222 led_reset()223 print(f"{Fore.BLUE}Beginning test!")224 test_fail = False225 # Configure tester for EXT or RST226 if (not io_read(SW_EXT_RST)):227 print(f"{Fore.BLUE}Sea-Picro EXT selected")228 oled_write("Testing EXT")229 dut_type = EXT230 else:231 print(f"{Fore.BLUE}Sea-Picro RST selected")232 oled_write("Testing RST")233 dut_type = RST234 # Check if we need to flash cpy or not235 if (not io_read(SW_BOOTLD) and (test_fail == False) and not test_only):236 # Need to reset board to throw into bootloader237 # We also check the reset circuit is working correctly here238 test_fail = sp_bootloader()239 if test_fail == True:240 fail_str = "RESET"241 else:242 test_fail = flash_circuitpython()243 # Check if we need to flash firmare / test code244 if (not io_read(SW_FIRMWARE) and (test_fail == False) and not test_only):245 test_fail = flash_firmware()246 # Check if we need to test the DUT247 if (not io_read(SW_TEST) and (test_fail == False)):248 sp_reset() # Need to power cut here249 test_fail = serial_init()250 if test_fail == True:251 fail_str = "CDC2"252 # Need to power cut here253 else:254 test_fail = test_dut(dut_type)255 256 # If we are just doing the final test, check the reset circuit257 if (io_read(SW_BOOTLD) and io_read(SW_FIRMWARE) and (test_fail == False)) or (test_only and test_fail == False):258 test_fail = sp_bootloader()259 if test_fail == True:260 fail_str = "RESET"261 # Need to power cut here262 test_only = False263 # Advise user on how everything went264 if test_fail == False:265 print(f'{Fore.GREEN}ALL STEPS PASSED!!!')266 print(f"{Fore.YELLOW}Press YES for full test, NO for final test")267 oled_write_dual("Test Passed!", "Y: FULL N: FINAL", size2 = 12)268 retest = True269 led_pass()270 while True:271 if (not io_read(SW_YES)):272 break273 if (not io_read(SW_NO)):274 test_only = True275 break276 elif test_fail == True:277 print(f'{Fore.RED}TEST FAILED: {fail_str}')278 print(f"{Fore.YELLOW}Press YES for full test, NO for final test")279 oled_write_dual(f'FAIL: {fail_str}', "Y: FULL N: FINAL", size2 = 12)280 retest = True281 led_fail()282 while True:283 if (not io_read(SW_YES)):284 break285 if (not io_read(SW_NO)):286 test_only = True287 break288 break289 # except:290 # print(f"{Fore.RED}Something went wrong, let's try again")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!