def ownership_test(params): """Save a domain to a file, check the ownership of the file after save and restore """ logger = params['logger'] guestname = params['guestname'] dynamic_ownership = params['dynamic_ownership'] use_nfs = params['use_nfs'] # set env logger.info("prepare the environment") ret = prepare_env(dynamic_ownership, use_nfs, logger) if ret: logger.error("failed to prepare the environment") return 1 conn = utils.get_conn() # save domain to the file logger.info("save domain %s to the file %s" % (guestname, SAVE_FILE)) logger.info("check the domain state") ret = check_domain_running(conn, guestname, logger) if ret: return 1 domobj = conn.lookupByName(guestname) try: domobj.save(SAVE_FILE) logger.info("Success save domain %s to %s" % (guestname, SAVE_FILE)) except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) return 1
def update_history(): cursor = None conn = None try: dic = get_tencent_data()[0] print(f"{time.asctime()}开始更新历史数据") conn, cursor = get_conn() sql = "insert into history values(%s,%s,%s,%s,%s,%s,%s,%s,%s)" sql_query = "select confirm from history where ds=%s" for k, v in dic.items(): if not cursor.execute(sql_query, k): cursor.execute(sql, [ k, v.get('confirm'), v.get('confirm_add'), v.get('suspect'), v.get('suspect_add'), v.get('heal'), v.get('heal_add'), v.get('dead'), v.get('dead_add') ]) conn.commit() print(f"{time.asctime()}历史数据更新完毕") except: traceback.print_exc() finally: close_conn(conn, cursor)
def insert_history(): cursor = None conn = None try: dic = get_tencent_data()[0] print(f"{time.asctime()}开始插入历史数据") conn, cursor = get_conn() sql = "insert into history values(%s,%s,%s,%s,%s,%s,%s,%s,%s)" for k, v in dic.items(): cursor.execute(sql, [ k, v.get('confirm'), v.get('confirm_add'), v.get('suspect'), v.get('suspect_add'), v.get('heal'), v.get('heal_add'), v.get('dead'), v.get('dead_add') ]) conn.commit() print(f"{time.asctime()}插入历史数据完成") except: traceback.print_exc() finally: close_conn(conn, cursor)
def sharemod_init(env_parser, logger): """ get connection object from libvirt module initialize sharemod for use by testcases """ uri = env_parser.get_value('variables', 'defaulturi') username = env_parser.get_value('variables', 'username') password = env_parser.get_value('variables', 'password') conn = utils.get_conn(uri, username, password) if not conn: return 1 # initialize conn object in sharedmod sharedmod.libvirtobj.clear() sharedmod.data.clear() sharedmod.libvirtobj['conn'] = conn return 0
def update_hotsearch(): cursor = None conn = None try: content = get_hot_search() print(f"{time.asctime()}开始更新热搜数据") conn, cursor = get_conn() sql = "insert into hotsearch (dt, content) values(%s, %s)" ts = time.strftime("%Y-%m-%d %X") for i in content: cursor.execute(sql, (ts, i)) conn.commit() print(f"{time.asctime()} 更新热搜数据结束") except: traceback.print_exc() finally: close_conn(conn, cursor)
def update_details(): cursor = None conn = None try: li = get_tencent_data()[1] # 0是历史数据字典, 1是详细数据字典 conn, cursor = get_conn() sql = "insert into details (update_time, province, city, confirm, confirm_now, confirm_add, heal, dead) values(%s,%s,%s,%s,%s,%s,%s,%s)" sql_query = "select %s=(select update_time from details order by id desc limit 1)" # 对比当前最大时间戳 cursor.execute(sql_query, li[0][0]) if not cursor.fetchone()[0]: print(f"{time.asctime()} 开始更新最新数据") for item in li: cursor.execute(sql, item) conn.commit() print(f"{time.asctime()} 更新数据已经完毕") else: print(f"{time.asctime()} 已经是最新的数据") except: traceback.print_exc() finally: close_conn(conn, cursor)
def domain_nfs_start_clean(params): """clean testing environment""" logger = params['logger'] guestname = params['guestname'] # Connect to local hypervisor connection URI conn = utils.get_conn() domobj = conn.lookupByName(guestname) if domobj.info()[0] != libvirt.VIR_DOMAIN_SHUTOFF: domobj.destroy() dom_xml = domobj.XMLDesc(0) disk_file = utils.get_disk_path(dom_xml) img_dir = os.path.dirname(disk_file) file_name = os.path.basename(disk_file) temp_file = "/tmp/%s" % file_name if os.path.ismount(img_dir): umount_cmd = "umount -f %s" % img_dir ret, out = utils.exec_cmd(umount_cmd, shell=True) if ret: logger.error("failed to umount %s" % img_dir) if os.path.exists(temp_file): os.remove(temp_file) clean_nfs_conf = "sed -i '$d' /etc/exports" utils.exec_cmd(clean_nfs_conf, shell=True) clean_qemu_conf = "sed -i '$d' %s" % QEMU_CONF utils.exec_cmd(clean_qemu_conf, shell=True) cmd = "service libvirtd restart" utils.exec_cmd(cmd, shell=True) return 0
except libvirtError, e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("Error: fail to get domain %s xml" % guestname) return 1 # set env logger.info("prepare the environment") ret = prepare_env(dynamic_ownership, virt_use_nfs, guestname, root_squash, disk_file, img_dir, logger) if ret: logger.error("failed to prepare the environment") return 1 # reconnect libvirt conn = utils.get_conn() domobj = conn.lookupByName(guestname) logger.info("begin to test start domain from nfs storage") logger.info("First, start the domain without chown the img file to qemu") logger.info("start domain %s" % guestname) if root_squash == "yes": if virt_use_nfs == "on": if dynamic_ownership == "enable": try: domobj.create() logger.error("Domain %s started, this is not expected" % guestname) return 1 except libvirtError, e:
except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) logger.error("Error: fail to get domain %s xml" % guestname) return 1 # set env logger.info("prepare the environment") ret = prepare_env(dynamic_ownership, virt_use_nfs, guestname, \ root_squash, disk_file, img_dir, logger) if ret: logger.error("failed to prepare the environment") return 1 # reconnect libvirt conn = utils.get_conn() domobj = conn.lookupByName(guestname) logger.info("begin to test start domain from nfs storage") logger.info("First, start the domain without chown the img file to qemu") logger.info("start domain %s" % guestname) if root_squash == "yes": if virt_use_nfs == "on": if dynamic_ownership == "enable": try: domobj.create() logger.error("Domain %s started, this is not expected" % \ guestname) return 1 except libvirtError, e:
def ownership_test(params): """Save a domain to a file, check the ownership of the file after save and restore """ logger = params['logger'] guestname = params['guestname'] dynamic_ownership = params['dynamic_ownership'] use_nfs = params['use_nfs'] # set env logger.info("prepare the environment") ret = prepare_env(dynamic_ownership, use_nfs, logger) if ret: logger.error("failed to prepare the environment") return 1 conn = utils.get_conn() # save domain to the file logger.info("save domain %s to the file %s" % (guestname, SAVE_FILE)) logger.info("check the domain state") ret = check_domain_running(conn, guestname, logger) if ret: return 1 domobj = conn.lookupByName(guestname) try: domobj.save(SAVE_FILE) logger.info("Success save domain %s to %s" % (guestname, SAVE_FILE)) except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) return 1 logger.info("check the ownership of %s after save" % SAVE_FILE) ret, uid, gid = ownership_get(logger) if use_nfs == 'enable': if uid == 107 and gid == 107: logger.info("As expected, the chown not change.") else: logger.error("The chown of %s is %s:%s, it's not as expected" % (SAVE_FILE, uid, gid)) return 1 elif use_nfs == 'disable': if dynamic_ownership == 'enable': if uid == 0 and gid == 0: logger.info("As expected, the chown changed to root:root") else: logger.error("The chown of %s is %s:%s, it's not as expected" % (SAVE_FILE, uid, gid)) return 1 elif dynamic_ownership == 'disable': if uid == 107 and gid == 107: logger.info("As expected, the chown not change.") else: logger.error("The chown of %s is %s:%s, it's not as expected" % (SAVE_FILE, uid, gid)) return 1 # restore domain from file logger.info("restore the domain from the file") try: conn.restore(SAVE_FILE) logger.info("Success restore domain %s from %s" % (guestname, SAVE_FILE)) except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("Error: fail to restore domain %s from %s" % (guestname, SAVE_FILE)) return 1 logger.info("check the ownership of %s after restore" % SAVE_FILE) ret, uid, gid = ownership_get(logger) if uid == 107 and gid == 107: logger.info("As expected, the chown not change.") else: logger.error("The chown of %s is %s:%s, not change back as expected" % (SAVE_FILE, uid, gid)) return 1 return 0