def __getattr__(self, item): if item in self.find_prefixes_shell(): #global prefix_ssh values_authentication = dict() try: values_authentication = { "shell_host": configs.config["env_info"][item + "_host"], "shell_port": configs.config["env_info"][item + "_port"], "shell_user": configs.config["env_info"][item + "_user"], "shell_type": item[: 3], # TODO: при добавалении новых типов shell изменить } if item + "_passwd" in configs.config["env_info"]: # есть пароль, используем пароль # "shell_passwd": configs.config["env_info"][item + "_passwd"], values_authentication.update({ "shell_passwd": configs.config["env_info"][item + "_passwd"] }) elif item + "_keyfile" in configs.config["env_info"]: # используем ключ, указываем путь до ключа values_authentication.update({ "shell_keyfile": configs.config["env_info"][item + "_keyfile"] }) else: raise AssertionError( "Not found password or keyfile for shell.") except Exception, tx: service_log.error("Not found mark in env.cfg: %s" % str(tx)) return ClassShellWork(**values_authentication)
def execute(self, req, fetch='all', cursor_view="dict"): """ Выполнить запрос. :param req: sql-запрос :param fetch: тип выборки строк запроса :return: результат операции """ what_cursor_view = lambda view, element: dict( element) if view == "dict" else element try: self.cursor_now = self.connect(cursor_view=cursor_view) self.cursor_now.execute(req) if fetch == "all": result = self.cursor_now.fetchall() service_log.put("Fetchall rows.") return [what_cursor_view(cursor_view, elem) for elem in result] elif fetch == "one": result = self.cursor_now.fetchone() service_log.put("Fetchone rows.") return what_cursor_view(cursor_view, result) elif fetch == "update" or fetch == "insert": service_log.put("No fetch rows. Operation by UPDATE.") self.conn.commit() service_log.put("Commit operation UPDATE.") return None except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def connect_plain_auth(self, user_name, password, port, key_space, contacts_point): """ Аутентификация cassandra через имя и пароль. :param user_name: имя пользователя :param password: пароль пользователя :return: ссылка на сессию """ try: service_log.put("Create obj PlainTextAuthProvider for cassandra.") auth_provider = PlainTextAuthProvider(username=user_name, password=password) service_log.put("Create cluster for cassandra.") self.cluster = Cluster(contact_points=contacts_point, port=port, auth_provider=auth_provider) service_log.put("Create connect with cassandra.") session = self.cluster.connect(keyspace=key_space) service_log.put("Create session for cluster cassandra.") return session except Exception, tx: msg_error = "Connect with casandra not success!" service_log.error(msg_error) service_log.error("%s" % str(tx)) raise msg_error
def add_users_strategy(link_db, mode='all'): """ :param driver: :param count: :param mode: :return: """ users_list = list() criteria = "account_status = 'ENABLED' AND id in (%s) AND phone like '7%s' AND length(phone)=11 AND " \ "display_name is not NULL" if mode == 'all': users_list = link_db.accounting.get_user_by_role('1,2,3,4', '100') value_list = [str(value["account_details_id"]) for value in users_list] string = ','.join(value_list) users_list = link_db.accounting.get_user_by_criteria_only(criteria % (string, '%')) elif mode == 'buyer': users_list = link_db.accounting.get_user_by_role('1', '2,3,4') value_list = [str(value["account_details_id"]) for value in users_list] string = ','.join(value_list) users_list = link_db.accounting.get_user_by_criteria_only(criteria % (string, '%')) elif mode == 'seller': users_list = link_db.accounting.get_user_by_role('2', '3,4') value_list = [str(value["account_details_id"]) for value in users_list] string = ','.join(value_list) users_list = link_db.accounting.get_user_by_criteria_only(criteria % (string, '%')) elif mode == 'disabled': users_list = link_db.accounting.get_accounts_by_criteria(criteria="account_status='DISABLED'") else: service_log.error("Undefined mode='%s'" % mode) return users_list
def execute(self, req, parameters=None): """ Выполнить запрос. :param req: сql-запрос :return: результат операции """ session = None try: session = self.connect_plain_auth(user_name=self.user, password=self.passwd, port=self.port, key_space=self.name, contacts_point=self.host) if parameters is not None: # вариант с предварительной привязкой параметров, для работы с BLOB prepare_params = session.prepare(req) bound_params = prepare_params.bind(parameters) result = session.execute(bound_params) else: result = session.execute(req) service_log.put("Response from cassandra.") if isinstance(result, list): if len(result) > 0: return [dict(index.__dict__) for index in result] else: return None else: return result except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def execute(self, req, fetch='all'): """ Выполнить запрос. :param req: sql-запрос :param fetch: тип выборки строк запроса :return: результат операции """ try: self.cursor_now = self.connect() self.cursor_now.execute(req) if fetch == "all": result = rows_to_dict_list(self.cursor_now) service_log.put("Fetchall rows.") return result elif fetch == "one": result = self.cursor_now.fetchone() service_log.put("Fetchone rows.") return result elif fetch == "update" or fetch == "insert": service_log.put("No fetch rows. Operation by UPDATE.") self.conn.commit() service_log.put("Commit operation UPDATE.") return None except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def __init__(self, cfg): """ Инициализация переменных различных сервисов. :param cfg: ссылка на конфигурацию """ self.cfg = cfg try: # Ключевые слова, которые не являются префиксами #__kwords = ("host", "port", "name", "login", "passwd") __kwords = () env_info = self.cfg["env_info"].keys() # определяем дополнительные функции для выборки префиксов сервисов num_pref = lambda a: a.rfind("_") if a.rfind("_") != -1 else False get_pref = lambda p: p[num_pref(p)+1:] get_name = lambda n: n[:num_pref(n)] no_kwords = lambda w, f: f(w) not in __kwords # Находим все префиксы сервисов self.array_var = set(get_pref(word) for word in env_info if num_pref(word) and no_kwords(word, get_pref)) self.array_prefix = set(get_name(word) for word in env_info if num_pref(word) and no_kwords(word, get_name)) except: msg_any_pref = "Unknown any prefix in env.cfg" service_log.error(msg_any_pref) raise AssertionError(msg_any_pref)
def __getattr__(self, item): """ Делаем привязку к сервису. :param item: наименование префикса :return: возвращаем значение переменной из env_info.cfg или вызываем Exception """ if item in self.array_all_prefix: global prefix_serv prefix_serv = item return GetVariableThrift(self.cfg) elif item in self.array_thrift_var: if item in self.array_thrift_var1: # проверка, что нужна переменная импорта return self.dict_thrift_var[prefix_serv + "_" + item] elif item in self.array_thrift_var2: # проверка, что нужна одна из переменных в thrift_variables return self.cfg["thrift_variables"][prefix_serv + "_" + item] elif item == "imports" and prefix_serv in self.array_user_prefix: # только импорты определённого воркера res = dict([name, path] for name, path in self.dict_thrift_var.iteritems() if name.find(prefix_serv) != -1) return res elif item == "workers" and prefix_serv is GetVariableThrift.main_prefix: # список названий воркеров return self.array_user_prefix elif item == "imports" and prefix_serv is GetVariableThrift.main_prefix: # список всех импортов для Thrift return self.dict_thrift_var else: msg_pref = "Unknown prefix=%s in env.cfg" % item service_log.error(msg_pref) raise AssertionError(msg_pref)
def execute(self, req, fetch="all"): """ Выполнить запрос. :param req: sql-запрос :param fetch: тип выборки строк запроса :return: результат операции """ try: self.cursor_now = self.connect() self.cursor_now.execute(req) if fetch == "all": result = rows_to_dict_list(self.cursor_now) service_log.put("Fetchall rows.") return result elif fetch == "one": result = self.cursor_now.fetchone() service_log.put("Fetchone rows.") return result elif fetch == "update" or fetch == "insert": service_log.put("No fetch rows. Operation by UPDATE.") self.conn.commit() service_log.put("Commit operation UPDATE.") return None except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def command_execution(self, cls, flag_output=True): """ Выполнить оперцию на удалённой консоли. :param client: ссылка на удалённую консоль :param cls: команда, которую необходимо запустить :param flag_output: если флаг False, то результат не возвращаем :return: Результат выполненной операции, либо ничего """ if self.passwd is not None: client = self.connect_by_user_password() elif self.keyfile is not None: client = self.connect_by_key_file() else: msg_error = "Not found password or keyfile for connect!" service_log.error(msg_error) raise AssertionError(msg_error) try: service_log.put("Execute: %s." % cls) if flag_output is True: stdin, stdout, stderr = client.exec_command("""%s""" % cls) data = dict(stdout=stdout.read(), stderr=stderr.read()) if data["stderr"] != '': service_log.put("Return. Output: %s" % str(stderr)) service_log.put("Execute success. Output: %s" % str(data)) raise AssertionError("Return error: %s" % str(stderr)) service_log.put("Execute success. Output: %s" % str(data)) return data else: client.exec_command("""%s""" % cls) service_log.put("Execute success.") return None except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def sudo_command_execution(self, cls, passwd=None): """ Make a remote command execution with sudo by ssh Return result :param cls: remote command to run """ client = self.connect() channel = None try: service_log.put("Open SSH chanel.") channel = client.get_transport().open_session() service_log.put("Open SSH chanel success.") channel.get_pty() channel.settimeout(5) service_log.put("Execute: %s." % cls) #self.channel.exec_command('sudo -i') channel.exec_command('sudo %s' % cls) if passwd is not None: channel.send(self.passwd+'\n') data = channel.recv(1024) service_log.put("Execute success. Output: %s" % str(data)) return data except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def get_response_by_WebServer(serv, count=1): """ Получаем данные от Веб-сервера. Метод вернёт управление по таймауту сервера или при получении всех данных :param serv: объект сервера :param count: количество принятых пакетов :return: информация о полученном пакете. """ full_answer = None run_time = int(configs.config["system_settings"]["sys_timeout_listen_port"]) start_timer = int(time.time()) while True: try: full_answer = serv.info_requests if len(full_answer) >= count or ('Disconnect' or 'Error') in full_answer[0].keys(): serv.stop_server = True #serv.setDaemon(True) # вырубить демон return serv.info_requests elif "Error" in full_answer[0]: raise AssertionError("Server return error: %s" % str(full_answer[0]["Error"])) elif full_answer[0]['Disconnect']: break except IndexError: pass except KeyError: pass # прерывание цикла if int(time.time()) - start_timer > run_time: service_log.error("Timeout!") break p_error = str(full_answer[0]["Disconnect"]) if len(full_answer) != 0 else "" raise AssertionError("Server return error: %s" % p_error)
def __getattr__(self, item): if item in self.find_prefixes_shell(): #global prefix_ssh values_authentication = dict() try: values_authentication = { "shell_host": configs.config["env_info"][item + "_host"], "shell_port": configs.config["env_info"][item + "_port"], "shell_user": configs.config["env_info"][item + "_user"], "shell_type": item[:3], # TODO: при добавалении новых типов shell изменить } if item + "_passwd" in configs.config["env_info"]: # есть пароль, используем пароль # "shell_passwd": configs.config["env_info"][item + "_passwd"], values_authentication.update({"shell_passwd": configs.config["env_info"][item + "_passwd"]}) elif item + "_keyfile" in configs.config["env_info"]: # используем ключ, указываем путь до ключа values_authentication.update({"shell_keyfile": configs.config["env_info"][item + "_keyfile"]}) else: raise AssertionError("Not found password or keyfile for shell.") except Exception, tx: service_log.error("Not found mark in env.cfg: %s" % str(tx)) return ClassShellWork(**values_authentication)
def find_prefixes_databases(): bases_val = [key for key in configs.config["env_info"].keys() if key[:2] == ClassDatabasesWork.prefix_selenium] prefixes = set([index[:index.find("_")] for index in bases_val]) if len(prefixes) != 0: return prefixes else: msg_error = "Not found prefix databases!" service_log.error(msg_error) raise AssertionError(msg_error)
def find_prefixes_shell(): bases_val = [key for key in configs.config["env_info"].keys() if key[:3] in ClassShellWork.prefix_shell] prefixes = set([index[:index.find("_")] for index in bases_val]) if len(prefixes) != 0: return prefixes else: msg_error = "Not found prefix for connect with shell!" service_log.error(msg_error) raise AssertionError(msg_error)
def generate_context_data(context_type): if context_type == "auth": return SessionMethods.generate_auth_data_context() elif context_type == "agent": return SessionMethods.generate_agent_data_context() else: msg = "Unknown context type %s!" % str(context_type) service_log.error(msg) raise AssertionError(msg)
def go_main(driver, phone=None, email=None, passwd=None, flag_auth=True, flag_api=True): """ Универсальный переход на главную страницу. :param driver: ссылка на драйвер :param phone: номер телефона :param email: электронная почта :param passwd: пароль :param flag_auth: флаг авторизации """ from support.utils.variables import EVariable env_base_url = EVariable.front_base.url.strip() # авторизоваться через API if flag_auth: service_log.put("To authorization via API") if flag_api: if email is None and phone is not None: HelpAuthMethods.set_authorization_by_phone(driver, phone, passwd) elif email is not None and phone is None: # TODO pass else: msg_error = "Not correct params." service_log.error(msg_error) assert AssertionError(msg_error) else: if email is None and phone is not None: HelpNavigateCheckMethods.get_page(driver, HelpNavigateCheckMethods.path_auth.PATH_AUTH) obj_phone, obj_password, obj_submit_button = HelpAuthCheckMethods.get_data_authorization(driver) # Вводим данные на авторизацию l = lambda e, p:e if p is None else p HelpAuthCheckMethods.send_phone(phone_object=obj_phone, phone_number=l(email, phone)) HelpAuthCheckMethods.send_password(password_object=obj_password, password_number=passwd) # Нажатие на кнопку авторизации HelpNavigateCheckMethods.element_click(driver, obj_submit_button, change_page_url=True) time.sleep(HelpNavigateCheckMethods.time_sleep) elif email is not None and phone is None: # TODO pass else: msg_error = "Not correct params." service_log.error(msg_error) assert AssertionError(msg_error) service_log.put("To authorization via API - success.") service_log.put("Get page: %s" % env_base_url) do_get_work = time.time() driver.get(env_base_url) work_get_time = HelpNavigateCheckMethods.work_time(do_get_work) service_log.put("Onload event time: [%s]" % work_get_time) HelpNavigateCheckMethods.progress(driver) work_load_time = HelpNavigateCheckMethods.work_time(do_get_work) service_log.put("Page received: %s" % env_base_url) service_log.put("Page received time: %s" % work_load_time) time.sleep(3)
def __init__(self, cfg): """ Инициализация переменных для сервиса thrift. :param cfg: ссылка на конфигурацию """ self.cfg = cfg try: # Ключевые слова, которые не являются префиксами p_kwords = self.cfg["prefix"]["prefix_kwords"] p_import = "thrift_import_workers" # Находим все префиксы сервисов (см. раздел prefix main.cfg) array_user_prefix = self.cfg["prefix"]["prefix_local"][1:-1].split(',') prefix_sys = self.cfg["prefix"]["prefix_sys"][1:-1].split(',') array_all_prefix = array_user_prefix[:] array_all_prefix.append(GetVariableThrift.main_prefix) env_keys = self.cfg["env_info"].keys() thrift_data = self.cfg["thrift_variables"] thrift_keys = thrift_data.keys() # определяем дополнительные функции для выборки префиксов сервисов listmerge = lambda ll: [el for lst in ll for el in lst] del_word = lambda p, k: k.replace(p, '').strip('_') get_prefix = lambda: (index for index in array_all_prefix) find_prefix = lambda i, p: i.find(p) != -1 # Получаем переменные из раздела thrift_variables в main.cfg if p_import in thrift_keys: # пути импорта файлов thrift self.dict_thrift_var = json.loads(thrift_data[p_import]) # получаем список список переменных для поля thrift_import_workers temp_lists1 = lambda v: [del_word(pref, v) for pref in array_user_prefix if find_prefix(v, pref)] temp_lists2 = [temp_lists1(v) for v in self.dict_thrift_var.keys()] self.array_thrift_var1 = listmerge(temp_lists2) # получаем список список переменных для остального раздела thrift_variables temp_lists3 = lambda k: [del_word(p, k) for p in get_prefix() if k != p_import and find_prefix(k, p)] temp_lists4 = [temp_lists3(k) for k in thrift_keys] self.array_thrift_var2 = listmerge(temp_lists4) self.array_thrift_var = self.array_thrift_var1 + self.array_thrift_var2 self.array_user_prefix = map(lambda a: a.split()[0], map(str, array_user_prefix)) self.array_all_prefix = array_all_prefix self.prefix_sys = prefix_sys except: msg_any_pref = "Unknown any prefix in main.cfg or env.cfg" service_log.error(msg_any_pref) raise AssertionError(msg_any_pref)
def check_err_msg(self, driver, xpath_err_msg): tx1 = None tx2 = None service_log.put("STEP: CHECK ERROR MESSAGE") try: service_log.put("...Start find abstract warning message.") driver.find_element_by_xpath(self.check_good.ERR_NEED_STOCK_ABSTRACT) service_log.put("...Success! Abstract warning message is found.") except Exception, tx1: service_log.error("...Abstract warning message is not found.") self.assertIsNone(tx1, "Warning message '%s' is not found." % xpath_err_msg)
def find_prefixes_databases(): bases_val = [ key for key in configs.config["env_info"].keys() if key[:2] == ClassDatabasesWork.prefix_selenium ] prefixes = set([index[:index.find("_")] for index in bases_val]) if len(prefixes) != 0: return prefixes else: msg_error = "Not found prefix databases!" service_log.error(msg_error) raise AssertionError(msg_error)
def NoneToInt(val): """ Если значение None, конвертим его в 0. :param val: значение :return: 0 или число """ if val is None: return 0 elif isinstance(val, int): return val else: service_log.error("Value is not int or None") assert AssertionError("Value is not int or None")
def find_prefixes_shell(): bases_val = [ key for key in configs.config["env_info"].keys() if key[:3] in ClassShellWork.prefix_shell ] prefixes = set([index[:index.find("_")] for index in bases_val]) if len(prefixes) != 0: return prefixes else: msg_error = "Not found prefix for connect with shell!" service_log.error(msg_error) raise AssertionError(msg_error)
def save_wares_data(data): """ Сохранить список данных товаров. :param data: данные товаров :return: True """ if isinstance(data, list): WarehouseMethods.SAVED_WARE = data service_log.put("Save the data in a single ware.") return True else: service_log.error("Data is not list.") assert AssertionError("Data is not list.")
def import_libs(self, name_prefix): try: # Производим динамический импорт сгенериррованных файлов для Thrift path = TVariables.thrift.imports[name_prefix + "_worker"] service_log.put("Get path by worker: %s" % path) get_lib = lambda path_lib: path_lib[path_lib.rfind(".")+1:] self.worker = __import__(path, fromlist=[get_lib(path)]) service_log.put("Import libs worker: path=%s, name=%s" % (path, get_lib(path))) return name_prefix except ImportError, tx: error_message = "Import lib for worker - %s" % tx service_log.error(error_message) raise AssertionError(error_message)
def modify(*args, **kwargs): try: service_log.put("Params for %s: %s, %s" % (func.func_name, str(args[1:]), str(kwargs))) link_func = unittest.TestCase.__dict__[func.func_name] return link_func(*args, **kwargs) except Exception, tx: limit_print_exc = 10 msg_line = "#" + ("-"*100) service_log.error(tx) service_log.put("\n%s\n%s\n%s" % (msg_line, traceback.format_exc(limit_print_exc), msg_line)) trace_stack_log = funcy.join(traceback.format_stack(limit=limit_print_exc)) service_log.put("Traceback stack:\n%s\n%s" % (str(trace_stack_log), msg_line)) raise AssertionError(tx)
def check_not_found(self, func, *args, **kwargs): """ Перехватываем исключение, если элемент не найден и говорим, что так и должно быть. :param func: результат поиска элемента :return: True """ try: func(*args, **kwargs) msg = "Find element!" service_log.error(msg) raise AssertionError("Error: %s" % msg) except NoSuchElementException, tx: service_log.put("Success! Element not found.") return True
def modify(*args, **kwargs): try: platform_name = CmdWork.get_platform() link_func = None if platform_name == "windows": link_func = CmdWindows.__dict__[func.func_name] elif platform_name == "linux": link_func = CmdLinux.__dict__[func.func_name] else: raise AssertionError("Not found name platform!") return link_func(*args, **kwargs) except Exception, tx: service_log.error(tx) raise AssertionError(tx)
def __getattr__(self, item): if item in self.find_prefixes_databases(): global prefix_db values_connections = { "dbhost": configs.config["env_info"][item + "_host"], "dbport": configs.config["env_info"][item + "_port"], "dbtype": configs.config["env_info"][item + "_type"], "dbname": configs.config["env_info"][item + "_name"] } # Могут быть необязательные поля if item + "_passwd" in configs.config["env_info"].keys(): values_connections.update( {"dbpasswd": configs.config["env_info"][item + "_passwd"]}) if item + "_login" in configs.config["env_info"]: values_connections.update( {"dbuser": configs.config["env_info"][item + "_login"]}) return ClassDatabasesWork(**values_connections) elif item in TVariables.thrift.workers: # карта сервисов и типов соответствия их с Базами данных sv = { "pgsql": { "accounting": [ClassAccountingSql, ClassPsyCopg2] }, "redis": { "session": [ClassSessionNoSql, ClassRedis], "messaging": [ClassMessagingNoSql, ClassRedis] }, "cassandra": { "warehouse": [ClassCassandraCql, ClassCassandraDriver], } } for name_type, service_value in sv.iteritems(): if self.dbtype == name_type: for name_service, links_service in service_value.iteritems( ): if item == name_service: return links_service[0](links_service[1]( **self.params_authentication).execute) else: msg_error = "Not found class for the work with worker databases." service_log.error(msg_error) raise AssertionError(msg_error) else: msg_error = "Not detected type database in env.cfg!" service_log.error(msg_error) raise AssertionError(msg_error)
def import_libs(self, name_prefix): try: # Производим динамический импорт сгенериррованных файлов для Thrift path = TVariables.thrift.imports[name_prefix + "_worker"] service_log.put("Get path by worker: %s" % path) get_lib = lambda path_lib: path_lib[path_lib.rfind(".") + 1:] self.worker = __import__(path, fromlist=[get_lib(path)]) service_log.put("Import libs worker: path=%s, name=%s" % (path, get_lib(path))) return name_prefix except ImportError, tx: error_message = "Import lib for worker - %s" % tx service_log.error(error_message) raise AssertionError(error_message)
def clear_connect_to_service(): """ Закрываем соединение. """ try: if 'thrift_transport' in globals(): globals()['thrift_transport'].close() del globals()['thrift_transport'] del globals()['prefix_serv'] del globals()['path_serv'] service_log.put("Transport - close.") else: service_log.put("Warning: The connection is already closed.") except Thrift.TException, tx: service_log.error(tx.message)
def connect_to_service(self, path_serv, ttype="framed"): """ Устанавливаем соединение с сервисом по протоколу Thrift. :param ttype: тип транспорта бинарного протокола :return: <type client> """ turl = configs.config["env_info"][prefix_serv + "_url"] tpath = configs.config["backend"][path_serv] thost = turl if path_serv == 'root' else turl + tpath tport = configs.config["env_info"][prefix_serv + "_port"] try: # Если соединение уже было установленно, используем его #if 'thrift_transport' not in globals(): # global thrift_transport thrift_transport = None socket = TSocket.TSocket(thost, tport) service_log.put("Make socket: %s" % socket) if ttype == "tframed": thrift_transport = TTransport.TFramedTransport(socket) service_log.put("Create TFramedTransport: %s" % thrift_transport) elif ttype == "tbuff": # Делаем буферизацию. Работа с сокетами очень медленная. thrift_transport = TTransport.TBufferedTransport(socket) service_log.put("Create TBufferedTransport: %s" % thrift_transport) service_log.put( "Buffering is critical. Raw sockets are very slow.") else: error_message = "Is not a valid binary protocol type" service_log.put(error_message) raise AssertionError(error_message) # Делаем врапер для протокола protocol = TBinaryProtocol.TBinaryProtocol(thrift_transport) service_log.put("Wrap in a protocol.") # Создаём клиента для работы с протоклом декодирования client = self.worker.Client(protocol) service_log.put("Create a client to use the protocol encoder.") # Коннектимся thrift_transport.open() service_log.put("Transport - open. Connect!") service_log.put("Control is returned to the method to call.") return client except Thrift.TException, tx: service_log.error(tx.message)
def connect_by_key_file(self): """ Коннектимся к удалённой консоли по ключу. :return: возвращаем ссылку на терминал """ try: client = paramiko.SSHClient() type_host_key = paramiko.AutoAddPolicy() client.set_missing_host_key_policy(type_host_key) # add ssh-key in list keys — file .ssh/known_hosts client.connect(hostname=self.host, username=self.user, port=int(self.port), key_filename=self.keyfile) service_log.put("Open SSH connections.") return client except Exception, tx: msg_error = "SSH not connections! /n %s" % tx service_log.error(msg_error) raise msg_error
def get_cookies(user_id): """ Получить cookies заданного пользователя в зависимости от тестового стенда. :param user_id: идентификатор пользователя :return: cookies заданного пользователя """ from support.utils.variables import EVariable cookies = None try: cookies = HelpAuthData.COOKIE_AUTH[EVariable.front_base.url.strip()][user_id] service_log.put("Get cookies for user Id: '%s' with cookies: '%s'" % (user_id, str(cookies))) except ValueError, tx: msg_error = "Not found cookies for user Id: '%s'" % user_id service_log.error(msg_error) service_log.error(str(tx)) raise AssertionError(msg_error)
def close_connect(self, client, channel=None): """ Close connection SSH if exist """ try: if self.channel: self.channel.close() self.channel = None if self.client: self.client.close() self.client = None except Exception, tx: service_log.error("SSH connection not success close.")
def get_password(source_passwd, type_passwd='CORRECT'): """ Вернуть пароль. :param source_passwd: исходный пароль :param type_passwd: тип возвращаемого пароля :return: строка с изменениями """ if type_passwd == 'CORRECT' or type_passwd == 'LARGE_CORRECT': return source_passwd elif type_passwd == 'INCORRECT_REGISTER': return source_passwd.lower() elif type_passwd == 'INCORRECT': return source_passwd + '123' else: msg_error = "Not fount type password: %s" % source_passwd service_log.error(msg_error) AssertionError(msg_error)
def __iterable_exec(item, conns): """ Динамически подгружаем ссылке на связанные с БД классы. :param item: название объекта :param conns: соеденения :return: список с результатом работы """ result = list() for conn in conns: local_code = None exec """local_code = conn.%s""" % item in local_code if local_code is not None: result.append(local_code) else: msg_error = "Not found attribute '%s' " % item service_log.error(msg_error) raise AssertionError(msg_error) return result
def connect_to_service(self, path_serv, ttype="framed"): """ Устанавливаем соединение с сервисом по протоколу Thrift. :param ttype: тип транспорта бинарного протокола :return: <type client> """ turl = configs.config["env_info"][prefix_serv + "_url"] tpath = configs.config["backend"][path_serv] thost = turl if path_serv == 'root' else turl + tpath tport = configs.config["env_info"][prefix_serv + "_port"] try: # Если соединение уже было установленно, используем его #if 'thrift_transport' not in globals(): # global thrift_transport thrift_transport = None socket = TSocket.TSocket(thost, tport) service_log.put("Make socket: %s" % socket) if ttype == "tframed": thrift_transport = TTransport.TFramedTransport(socket) service_log.put("Create TFramedTransport: %s" % thrift_transport) elif ttype == "tbuff": # Делаем буферизацию. Работа с сокетами очень медленная. thrift_transport = TTransport.TBufferedTransport(socket) service_log.put("Create TBufferedTransport: %s" % thrift_transport) service_log.put("Buffering is critical. Raw sockets are very slow.") else: error_message = "Is not a valid binary protocol type" service_log.put(error_message) raise AssertionError(error_message) # Делаем врапер для протокола protocol = TBinaryProtocol.TBinaryProtocol(thrift_transport) service_log.put("Wrap in a protocol.") # Создаём клиента для работы с протоклом декодирования client = self.worker.Client(protocol) service_log.put("Create a client to use the protocol encoder.") # Коннектимся thrift_transport.open() service_log.put("Transport - open. Connect!") service_log.put("Control is returned to the method to call.") return client except Thrift.TException, tx: service_log.error(tx.message)
def modify(*args, **kwargs): try: service_log.put("Params for %s: %s, %s" % (func.func_name, str(args[1:]), str(kwargs))) link_func = unittest.TestCase.__dict__[func.func_name] return link_func(*args, **kwargs) except Exception, tx: limit_print_exc = 10 msg_line = "#" + ("-" * 100) service_log.error(tx) service_log.put( "\n%s\n%s\n%s" % (msg_line, traceback.format_exc(limit_print_exc), msg_line)) trace_stack_log = funcy.join( traceback.format_stack(limit=limit_print_exc)) service_log.put("Traceback stack:\n%s\n%s" % (str(trace_stack_log), msg_line)) raise AssertionError(tx)
def __getattr__(self, item): if item in self.find_prefixes_connects("db"): # для БД global prefix_db values_authentication = { "dbhost": configs.config["env_info"][item + "_host"], "dbtype": configs.config["env_info"][item + "_type"], "dbname": configs.config["env_info"][item + "_name"] } # Могут быть необязательные поля if item + "_port" in configs.config["env_info"].keys(): values_authentication.update( {"dbport": configs.config["env_info"][item + "_port"]}) if item + "_passwd" in configs.config["env_info"].keys(): values_authentication.update( {"dbpasswd": configs.config["env_info"][item + "_passwd"]}) if item + "_login" in configs.config["env_info"]: values_authentication.update( {"dbuser": configs.config["env_info"][item + "_login"]}) return ClassDatabasesWork(**values_authentication) elif item in self.find_prefixes_connects("nut"): from support.utils.nutcracker import ClassNutcracker return ClassNutcracker(item) elif item in TVariables.thrift.workers: # Подключения по типам БД (связка файлов с методами для БД) for name_type, service_value in self.sv.iteritems(): if self.dbtype == name_type: for name_service, links_service in service_value.iteritems( ): if item == name_service: return links_service[0](links_service[1]( **self.params_authentication).execute) else: msg_error = "Not found class for the work with worker databases." service_log.error(msg_error) raise AssertionError(msg_error) else: msg_error = "Not detected type database in env.cfg!" service_log.error(msg_error) raise AssertionError(msg_error)
def __call__(self, *args, **kwargs): """ Подменяем вызов функции на её динамическое использование. :param args: аргументы передаваемые функции :param kwargs: аргументы передаваемые функции :return: """ call_m = self.__iterable_exec(self.name_exc_item, self.conns) result = [index(*args, **kwargs) for index in call_m] result_c = funcy.compact(result) if len(result_c) >= 2: msg_error = "Found several response!" service_log.error(msg_error) raise AssertionError(msg_error) else: service_log.put("Get response from nutcracker.") if len(result_c) == 0: return result_c else: return result_c[0]
def elem_WareContentDto_to_dict(instance, number_name=1): """ :param instance: :param number_name: :return: """ data = dict() if instance is not None: for index in instance.keys(): if number_name == 1: data.update({index: {"name": instance[index].name, "value": instance[index].value}}) elif number_name == 2: data.update({index: {"name": instance[index].name, "value": instance[index].values}}) else: msg = "Not found value number_name." service_log.error(msg) assert AssertionError(msg) return data
def execute2(self, req, named_params, output): """ Выполнить запрос. :param req: sql-запрос :param fetch: тип выборки строк запроса :return: результат операции """ try: self.cursor_now = self.connect() for param in named_params: if isinstance(named_params[param], type): named_params.update({param: self.cursor_now.var(named_params[param])}) self.cursor_now.execute(req, named_params) output = {out: named_params[out].getvalue() for out in output} service_log.put("Commit operation UPDATE.") return output except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def execute2(self, req, named_params, output): """ Выполнить запрос. :param req: sql-запрос :param fetch: тип выборки строк запроса :return: результат операции """ try: self.cursor_now = self.connect() for param in named_params: if isinstance(named_params[param], type): named_params.update( {param: self.cursor_now.var(named_params[param])}) self.cursor_now.execute(req, named_params) output = {out: named_params[out].getvalue() for out in output} service_log.put("Commit operation UPDATE.") return output except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def execute(self, req, parameters=None): """ Выполнить запрос. :param req: сql-запрос :return: результат операции """ try: result = None connection_elastic = self.connect(hosts=self.host) if parameters is not None: # result = connection_elastic.execute() # TODO: параметризированный запуск pass else: result = connection_elastic.search(index=self.name, body=req) service_log.put("Response from Elasticsearch.") return result except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def execute(self, req, fetch='all'): """ Выполнить запрос. :param req: nosql-запрос :param fetch: тип выборки строк запроса :return: результат операции """ try: self.cursor_now = self.connect() # Динамическое выполнение кода, параметр type - название функции в пуле коннекта if type(req) is dict: local_code = {"cursor_now": self.cursor_now} # Определяем количество параметров для передачи их функции if len(req) > 1: name_funct = req.pop("type") code_execution = """result = cursor_now.%s(**%s)""" % (name_funct, req) elif len(req) == 1: code_execution = """result = cursor_now.%s()""" % (req.pop("type")) else: msg = "Params for function is empty." service_log.error(msg) raise AssertionError(msg) service_log.put("Dynamic code execution: %s." % code_execution) exec code_execution in local_code service_log.put("Result dynamic code execution: %s." % local_code["result"]) return local_code["result"] else: msg = "Not found type operation for redis." service_log.error(msg) raise AssertionError(msg) except Exception, tx: service_log.error(str(tx)) raise AssertionError(str(tx))
def __getattr__(self, item): if item in TVariables.thrift.workers: # воркер входит в списк воркеров global prefix_serv prefix_serv = item return ClassThrift() elif item in configs.config['backend']: # path входит в список path для backend global path_serv path_serv = item return ClassThrift() elif item in ClassThrift.NAME_TYPE_TRANSPORT: # определяем тип транспорта для протокола prefix_serv = self.import_libs(prefix_serv) return self.connect_to_service(path_serv=path_serv, ttype=item) elif item == "close": # закрываем соединение return self.clear_connect_to_service else: msg_prefix = "Unknown prefix for thrift=%s" % item service_log.error(msg_prefix) raise AssertionError(msg_prefix)
def __getattr__(self, item): """ Делаем привязку к сервису. :param item: наименование префикса :return: возвращаем значение переменной из env_info.cfg или вызываем Exception """ if item in self.array_prefix: global prefix_serv prefix_serv = item return GetVariableService(self.cfg) elif item in self.array_var: # возвращаем значение переменной из env_info.cfg return self.cfg["env_info"][prefix_serv+"_"+item] elif item in "is_prod": # если это конфиг для прода, возвращаем True, иначе False return self.cfg["env"].endswith("_on_prod") elif item in "what_env": # возвращаем название схемы окружения return self.cfg["env"] else: msg_pref = "Unknown prefix=%s in env.cfg" % item service_log.error(msg_pref) raise AssertionError(msg_pref)
def get_response_by_WebServer(serv, count=1): """ Получаем данные от Веб-сервера. Метод вернёт управление по таймауту сервера или при получении всех данных :param serv: объект сервера :param count: количество принятых пакетов :return: информация о полученном пакете. """ full_answer = None run_time = int( configs.config["system_settings"]["sys_timeout_listen_port"]) start_timer = int(time.time()) while True: try: full_answer = serv.info_requests if len(full_answer) >= count or ('Disconnect' or 'Error') in full_answer[0].keys(): serv.stop_server = True #serv.setDaemon(True) # вырубить демон return serv.info_requests elif "Error" in full_answer[0]: raise AssertionError("Server return error: %s" % str(full_answer[0]["Error"])) elif full_answer[0]['Disconnect']: break except IndexError: pass except KeyError: pass # прерывание цикла if int(time.time()) - start_timer > run_time: service_log.error("Timeout!") break p_error = str( full_answer[0]["Disconnect"]) if len(full_answer) != 0 else "" raise AssertionError("Server return error: %s" % p_error)