From c35a82f369ac958131056c7159513300efec6990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Tue, 2 Feb 2021 10:00:47 +0100 Subject: [PATCH 1/5] pull request branch From a8b701da1bafd3841912b044679ee973d3e9e5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Tue, 2 Feb 2021 10:07:11 +0100 Subject: [PATCH 2/5] support all output and verbose option --- check_selenium_docker.py | 64 +++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/check_selenium_docker.py b/check_selenium_docker.py index efccffc..a4c1444 100755 --- a/check_selenium_docker.py +++ b/check_selenium_docker.py @@ -23,21 +23,29 @@ # Parse commandline arguments parser = argparse.ArgumentParser() parser.add_argument("--timeout", type=int, default=300, help="results waiting timeout in sec, default 300") +parser.add_argument('--verbose', '-v', action='count', default=0, + help="show failed test names and failure messages (-vv)") parser.add_argument("path", type=str, help="path to selenium test") args = parser.parse_args() path = args.path timeout = abs(args.timeout) +verbose = args.verbose os.chdir(path) -# Find side file and parse it to get the project name -side = glob.glob(path + '/sides/*.side') -side_file = open(side[0],'r') -side_json = json.loads(side_file.read()) -side_file.close() -result = path + '/out/' + side_json['name'] + '.json' +# Count projects for results observe +projects = [] +for side in glob.glob(path + '/sides/*.side'): + side_file = open(side,'r') + side_json = json.loads(side_file.read()) + side_file.close() + if side_json['name'] in projects: + print("Error: duplicated project names! Check input side files.") + sys.exit(3) + projects.append(side_json['name']) +projectsNb = len(projects) -# Remove old result json file if it exists -if os.path.isfile(result): +# Remove old result json files +for result in glob.glob(path + '/out/*.json'): os.remove(result) # Start selenium docker container @@ -48,27 +56,40 @@ # Wait for result file to be written waitedfor = 0 -while not os.path.isfile(result) and waitedfor <= timeout: +while len(glob.glob(path + '/out/*.json')) < projectsNb and waitedfor <= timeout: time.sleep(1) waitedfor += 1 +# Stop and remove container +container.stop() + # If no result was received after timeout exit with status unknown if waitedfor >= timeout: - container.stop() print("UNKNOWN: Test timed out. Investigate issues.") sys.exit(3) -# Open and read result file -file = open(result,'r') -json_input = json.loads(file.read()) -file.close() - -# Stop and remove container -container.stop() +# Parse result files +times = { 'startTime': 0, 'endTime': 0 } +json_input = { 'numPassedTests': 0, 'numFailedTests': 0, 'numTotalTests': 0 } +failed = {} +for result in glob.glob(path + '/out/*.json'): + file = open(result,'r') + jsonResult = json.loads(file.read()) + file.close() + if times['startTime'] == 0 or times['startTime'] > jsonResult['startTime']: + times['startTime'] = jsonResult['startTime'] + for par in json_input.keys(): + json_input[par] += jsonResult[par] + for testResult in jsonResult['testResults']: + if times['endTime'] < testResult['endTime']: + times['endTime'] = testResult['endTime'] + for aResult in testResult['assertionResults']: + if aResult['status'] == 'failed': + failed[aResult['fullName']] = aResult['failureMessages'] # Calculate execution time -exec_time = 0 if len(json_input['testResults']) == 0 else \ - int(str(json_input['testResults'][0]['endTime'])[:-3]) - int(str(json_input['startTime'])[:-3]) +exec_time = 0 if times['endTime'] <= times['startTime'] else \ + int(str(times['endTime'] - times['startTime'])[:-3]) # Exit logic with performance data if json_input['numFailedTests'] == 0: @@ -79,9 +100,12 @@ elif json_input['numFailedTests'] > 0: print("CRITICAL: Failed " +str(json_input['numFailedTests']) + " of " + str(json_input['numTotalTests']) + - " tests. Error message: " + str(json_input['testResults'][0]['message']) + + " tests" + ('.' if verbose == 0 else ": " + ', '.join(failed.keys()) + '.' ) + " | 'passed'=" + str(json_input['numPassedTests']) + ";;;; 'failed'=" + str(json_input['numFailedTests']) + ";;;; " + "'exec_time'=" + str(exec_time) + "s;;;;") + if verbose > 1: + for testName in failed: + print('TEST: ' + testName + '\n\n' + '\n\n'.join(failed[testName])) sys.exit(2) else: From 7cbd55eeeb6c87997186a2ad7b7baed9e0aeceb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Tue, 2 Feb 2021 10:09:33 +0100 Subject: [PATCH 3/5] fix help typo --- check_selenium_docker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_selenium_docker.py b/check_selenium_docker.py index a4c1444..e6f15f2 100755 --- a/check_selenium_docker.py +++ b/check_selenium_docker.py @@ -24,7 +24,7 @@ parser = argparse.ArgumentParser() parser.add_argument("--timeout", type=int, default=300, help="results waiting timeout in sec, default 300") parser.add_argument('--verbose', '-v', action='count', default=0, - help="show failed test names and failure messages (-vv)") + help="show failed tests names and failure messages (-vv)") parser.add_argument("path", type=str, help="path to selenium test") args = parser.parse_args() path = args.path From ca658a3f1a21086aa7110c95d92401e1ca99cdf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Wed, 3 Feb 2021 11:34:03 +0100 Subject: [PATCH 4/5] improve help info --- check_selenium_docker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/check_selenium_docker.py b/check_selenium_docker.py index e6f15f2..85d5bcb 100755 --- a/check_selenium_docker.py +++ b/check_selenium_docker.py @@ -22,9 +22,9 @@ # Parse commandline arguments parser = argparse.ArgumentParser() +parser.add_argument('-v', '--verbose', action='count', default=0, + help="show failed test names and failure messages (-vv)") parser.add_argument("--timeout", type=int, default=300, help="results waiting timeout in sec, default 300") -parser.add_argument('--verbose', '-v', action='count', default=0, - help="show failed tests names and failure messages (-vv)") parser.add_argument("path", type=str, help="path to selenium test") args = parser.parse_args() path = args.path From c5373fbec41d5751615007fc1b81e2845d5e6fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Tue, 16 Feb 2021 09:26:10 +0100 Subject: [PATCH 5/5] browser option, check for valid input side files --- check_selenium_docker.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/check_selenium_docker.py b/check_selenium_docker.py index 85d5bcb..4f9030c 100755 --- a/check_selenium_docker.py +++ b/check_selenium_docker.py @@ -25,12 +25,17 @@ parser.add_argument('-v', '--verbose', action='count', default=0, help="show failed test names and failure messages (-vv)") parser.add_argument("--timeout", type=int, default=300, help="results waiting timeout in sec, default 300") +parser.add_argument("--browser", type=str, default="chrome", help="container version to use, default 'chrome'") parser.add_argument("path", type=str, help="path to selenium test") args = parser.parse_args() path = args.path +browser = args.browser timeout = abs(args.timeout) verbose = args.verbose os.chdir(path) +if browser not in ['chrome', 'firefox', 'opera']: + print("Error: not allowed browser!") + sys.exit(3) # Count projects for results observe projects = [] @@ -43,6 +48,9 @@ sys.exit(3) projects.append(side_json['name']) projectsNb = len(projects) +if projectsNb == 0: + print("Error: no valid input side files found!") + sys.exit(3) # Remove old result json files for result in glob.glob(path + '/out/*.json'): @@ -50,7 +58,8 @@ # Start selenium docker container client = docker.from_env() -container = client.containers.run("opsdis/selenium-chrome-node-with-side-runner", auto_remove=True, shm_size="2G", +container = client.containers.run("opsdis/selenium-" + browser + "-node-with-side-runner", + auto_remove=True, shm_size="2G", volumes={ path + '/out': {'bind': '/selenium-side-runner/out', 'mode': 'rw'}, path + '/sides': {'bind': '/sides', 'mode': 'rw'}}, detach=True)