diff options
Diffstat (limited to 'profiles/robot/src/main/resources/script')
-rw-r--r-- | profiles/robot/src/main/resources/script/discover-robot-testcases.py | 121 | ||||
-rw-r--r-- | profiles/robot/src/main/resources/script/run-robot-testcase.py | 84 |
2 files changed, 205 insertions, 0 deletions
diff --git a/profiles/robot/src/main/resources/script/discover-robot-testcases.py b/profiles/robot/src/main/resources/script/discover-robot-testcases.py new file mode 100644 index 00000000..06c460a3 --- /dev/null +++ b/profiles/robot/src/main/resources/script/discover-robot-testcases.py @@ -0,0 +1,121 @@ +#!/usr/bin/python +# Copyright 2020 Simran Singhal. +# +# 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. + +import os +import argparse +from argparse import RawTextHelpFormatter +from robot.parsing.model import TestData +import yaml +from yaml.representer import SafeRepresenter +from yaml import Dumper +from collections import OrderedDict +import re + +class LiteralString(str): + pass + +class LineBreakDumper(Dumper): + def write_line_break(self, data=None): + super().write_line_break(data) + if len(self.indents) == 1: + super().write_line_break() + +def create_testcase_yaml(testcase_name, description, testsuite_name, test_suite_folder_path): + + def change_style(style, representer): + def new_representer(dumper, data): + scalar = representer(dumper, data) + scalar.style = style + return scalar + return new_representer + + represent_literal_str = change_style('|', SafeRepresenter.represent_str) + yaml.add_representer(LiteralString, represent_literal_str) + + def ordered_dict_presenter(dumper, data): + return dumper.represent_dict(data.items()) + yaml.add_representer(OrderedDict, ordered_dict_presenter) + + open_cli_schema_version = 1.0 + name = re.sub(re.compile('(?:\-){2,}'), '-', testcase_name.replace(' ', '-').lower()) + description = LiteralString(description.replace(r'\n', '\n') + '\n') + + product = "etsi-mano" + service = test_suite_folder_path.split('/')[0] + info = OrderedDict(product = product, service = service) + + test_suite_path = '${api-tests-folder-path}/api-tests/' + test_suite_folder_path + '/' + testsuite_name + '.robot' + command = ['python3 $s{env:OPEN_CLI_HOME}/script/run-robot-testcase.py --variables-file-path ${variables-file-path} --test-suite ' + + test_suite_path + ' --testcase ' + testcase_name] + success_codes = [0] + working_directory = '.' + output = '$stdout' + robot = OrderedDict(command=command, success_codes=success_codes, working_directory=working_directory, output=output) + + data = OrderedDict(open_cli_schema_version=open_cli_schema_version, name=name, description=description, + info=info, robot=robot) + + yaml_path = os.getenv('OPEN_CLI_HOME') + '/open-cli-schema/robot/' + test_suite_folder_path + '/' + testsuite_name + os.makedirs(yaml_path, exist_ok=True) + + with open(yaml_path + '/' + name + '.yaml', 'w') as file: + yaml.dump(data, file, Dumper=LineBreakDumper, default_flow_style=False) + +def discover_testcases(api_tests_folder_path): + + for root, dirs, files in os.walk(api_tests_folder_path): + for file in files: + + if file.endswith(".robot"): + path_to_test_suite = os.path.join(root, file) + try: + + suite = TestData(parent=None, source=path_to_test_suite) + for testcase in suite.testcase_table: + test_suite_folder_path = root[len(api_tests_folder_path):] + create_testcase_yaml(testcase.name, testcase.doc.value, suite.name, test_suite_folder_path) + + except Exception as e: + pass + +def main(): + text = 'This command helps to discover all robot testcases\n' \ + 'These python modules are need to be installed for running the tests\n' \ + 'robotframework==3.1\n' \ + 'RESTinstance==1.0.0rc4\n' \ + 'robotframework-dependencylibrary==1.0.0.post1\n' \ + 'robotframework-jsonlibrary==0.3\n' \ + 'robotframework-jsonschemalibrary==1.0\n' \ + 'robotframework-mockserver==0.0.4\n' + + parser = argparse.ArgumentParser(description=text, formatter_class=RawTextHelpFormatter) + parser.add_argument('--api-tests-folder-path', action='store', dest='api_tests_folder_path', + help='Location to api-tests folder', required=True) + + args = parser.parse_args() + api_tests_folder_path = args.api_tests_folder_path + '/api-tests/' + + if os.path.exists(api_tests_folder_path): + + if not os.path.isdir(api_tests_folder_path): + raise Exception('Given api-tests folder location is not a directory\n') + + else: + raise Exception('Given api-tests folder location does not exist\n') + + discover_testcases(api_tests_folder_path) + +if __name__ == '__main__': + main() diff --git a/profiles/robot/src/main/resources/script/run-robot-testcase.py b/profiles/robot/src/main/resources/script/run-robot-testcase.py new file mode 100644 index 00000000..9fe0f607 --- /dev/null +++ b/profiles/robot/src/main/resources/script/run-robot-testcase.py @@ -0,0 +1,84 @@ +#!/usr/bin/python +# Copyright 2020 Simran Singhal. +# +# 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. + +import os +import argparse +import uuid +from robot import run +from argparse import RawTextHelpFormatter + +def execute_robot_run(request_id, test_suite, testcase, variables_file_path): + output_dir = os.getenv('OPEN_CLI_HOME') + '/data/executions/' + request_id + '/logs' + + with open('run.txt', 'w') as output: + if(variables_file_path): + run(test_suite, report=None, log=None, test=testcase, variablefile=variables_file_path, stdout=output, stderr=output, outputdir=output_dir) + else: + run(test_suite, report=None, log=None, test=testcase, stdout=output, stderr=output, outputdir=output_dir) + + if os.path.exists('run.txt'): + with open('run.txt', 'r') as log: + print(log.read()) + os.remove('run.txt') + +def main(): + text = 'This command helps to run a robot testcase\n' \ + 'These python modules are need to be installed for running the tests\n' \ + 'robotframework==3.1\n' \ + 'RESTinstance==1.0.0rc4\n' \ + 'robotframework-dependencylibrary==1.0.0.post1\n' \ + 'robotframework-jsonlibrary==0.3\n' \ + 'robotframework-jsonschemalibrary==1.0\n' \ + 'robotframework-mockserver==0.0.4\n' + + parser = argparse.ArgumentParser(description=text, formatter_class=RawTextHelpFormatter) + parser.add_argument('--request-id', action='store', dest='request_id', + help='Request Id to track the progress of running this script', + default=os.environ.get('OPEN_CLI_REQUEST_ID')) + parser.add_argument('--test-suite', action='store', dest='test_suite', + help='Location to test suite file', required=True) + parser.add_argument('--testcase', action='store', dest='testcase', + help='Name of the testcase', required=True, nargs='+') + parser.add_argument('--variables-file-path', action='store', dest='variables_file_path', + help='Location to variable file', nargs='?', const='') + + args = parser.parse_args() + + request_id = args.request_id + test_suite = args.test_suite + testcase = ' '.join(args.testcase) + variables_file_path = '' + + if not request_id: + request_id = str(uuid.uuid4()) + + if args.variables_file_path: + variables_file_path = args.variables_file_path + + if os.path.exists(variables_file_path): + + if not os.path.isfile(variables_file_path): + raise Exception('Given variable file path is not a file\n') + + else: + raise Exception('Given variable file path does not exist\n') + + if not os.path.exists(test_suite): + raise Exception('Given api-tests folder location does not exist\n') + + execute_robot_run(request_id,test_suite, testcase, variables_file_path) + +if __name__ == '__main__': + main() |