def _compose_arg(self, variables): """ Search for the method in generated _pb2. Resolve input type by the method's input. """ schema = fill_template(self.schema, variables) service = fill_template(self.service, variables) method = fill_template(self.method, variables) # search for the service mod = module_utils.load_external_actions( join(variables['RESOURCES_DIR'], file_utils.get_filename(schema) + '_pb2.py')) services = { str(k).lower(): v for k, v in mod.DESCRIPTOR.services_by_name.items() } service = services.get(service) if not service: raise Exception('Unable to find service {} in {}'.format( service, file_utils.get_filename(schema) + '_pb2')) # find service's method methods = dict([(f.name.lower(), f) for f in service.methods]) method = methods.get(method) if not method: raise Exception('No method {} in service {}'.format( method, service)) # find method's input type input_type = method.input_type classes = module_utils.get_all_classes(mod) data = try_get_objects(fill_template_str(self.data, variables)) return method.name, classes[input_type.name](**data)
def run_tests(self) -> bool: try: self._compose.up() variables = {} if self.inventory is not None: variables = read_source_file(self.inventory) variables['INVENTORY'] = get_filename(self.inventory) variables = try_get_object(fill_template_str( variables, {})) # fill env vars variables['CURRENT_DIR'] = self.path test_files = get_files(self.tests_path) results = [] for file in test_files: self.all_includes = [] try: variables['TEST_NAME'] = file test = self.prepare_test(file, variables) test.run() results.append(True) info('Test ' + file + ' passed.') except Exception as e: warning('Test ' + file + ' failed: ' + str(e)) results.append(False) return all(results) finally: self._compose.down()
def read_inventory(self) -> dict: if self.inventory is not None: inv_vars = read_source_file(self.inventory) inv_vars['INVENTORY'] = get_filename(self.inventory) inv_vars['INVENTORY_FILE'] = self.inventory return inv_vars return {}
def __prepare_file(file: dict, variables: dict): resources = variables['RESOURCES_DIR'] [file_key] = [k for k in file.keys() if k != 'type'] filepath = file[file_key] file_type = file.get('type', 'text/plain') filename = file_utils.get_filename(filepath) file = fill_template_str( file_utils.read_file(os.path.join(resources, filepath)), variables) return file_key, (filename, file, file_type)
def _compile_kotlin(file, variables, libraries=None): resource_dir = variables['RESOURCES_DIR'] filename = file_utils.get_filename(file) return_code, stdout, stderr = run_cmd( 'kotlinc {}.kt -include-runtime -d {}.jar'.format(filename, filename), variables, cwd=resource_dir, shell=True) # compile everything if return_code != 0: raise Exception("Can't compile {}. Out: {}, Err: {}".format( file, stdout, stderr)) return filename + '.jar'
def _compile_java(file, variables, libraries=None): resource_dir = variables['RESOURCES_DIR'] classpath = _form_classpath(libraries) return_code, stdout, stderr = run_cmd( 'javac {} -d . *.java'.format(classpath), variables, cwd=resource_dir, shell=True) # compile everything if return_code != 0: raise Exception("Can't compile {}. Out: {}, Err: {}".format( file, stdout, stderr)) class_file = file_utils.find_resource(resource_dir, file_utils.get_filename(file), '.class') if len(class_file) > 1: debug('Found more than 1 resource for {}: {}. Use last.'.format( file_utils.get_filename(file), class_file)) class_file = class_file[-1] else: class_file = class_file[0] module = file_utils.cut_part_path(resource_dir, class_file).replace('/', '.') return module.split('.class')[0]
def _open_channel(self, channel, variables): """ Search for stub in generated module _pb2_grpc. Instantiate it with channel and return. """ schema = fill_template(self.schema, variables) mod = module_utils.load_external_actions( join(variables['RESOURCES_DIR'], file_utils.get_filename(schema) + '_pb2_grpc.py')) classes = module_utils.get_all_classes(mod) classes = {k.lower(): v for k, v in classes.items()} stub = classes.get(fill_template(self.service, variables) + 'stub') if not stub: raise Exception( 'Can\'t find stub in generated code. Something went wrong') return stub(channel)
def run_tests(self) -> bool: try: self._compose.up() if self.system_vars: debug('Use system variables: ' + str(list(self.system_vars.keys()))) variables = self.system_vars else: variables = {} if self.inventory is not None: inv_vars = read_source_file(self.inventory) inv_vars['INVENTORY'] = get_filename(self.inventory) variables = try_get_object( fill_template_str(inv_vars, variables)) # fill env vars variables['CURRENT_DIR'] = self.path variables[ 'RESOURCES_DIR'] = self.resources or self.path + '/resources' test_files = get_files(self.tests_path) results = [] for file in test_files: self.all_includes = [] try: variables['TEST_NAME'] = file test = self.prepare_test(file, variables) logger.log_storage.test_start(file) test.run() results.append(True) info('Test ' + file + ' passed.') logger.log_storage.test_end(file, True) except Exception as e: warning('Test ' + file + ' failed: ' + str(e)) results.append(False) logger.log_storage.test_end(file, False, str(e)) return all(results) finally: logger.log_storage.write_report(self.path) self._compose.down()