text: """
  Copyright (c) 2022 Huawei Technologies Co.,Ltd.
  
  openGauss is licensed under Mulan PSL v2.
  You can use this software according to the terms and conditions of the Mulan PSL v2.
  You may obtain a copy of Mulan PSL v2 at:
  
            http://license.coscl.org.cn/MulanPSL2
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  See the Mulan PSL v2 for more details.
  """
  """
  Case Type   : openGauss-tools-backup
  Case Name   :  创建非约束触发器后，使用jdbcpgbackup -m dump导出指定数据库
  Description :
          1.创建数据库
          2.指定数据库下建表，建用户，触发器函数
          3.创建触发器并执行INSERT触发事件
          4.修改信任方式为sha256
          5.导出指定数据库
          6.删表后执行导入
          7.查看导入表
          8.清理环境
  Expect      :
          1.创建成功
          2.创建成功
          3.创建成功
          4.修改成功
          5.导出成功
          6.导入成功
          7.导入的表数据正确
          8.清理环境完成
  """
  import os
  import unittest
  
  from testcase.utils.CommonSH import CommonSH
  from testcase.utils.Constant import Constant
  from testcase.utils.Logger import Logger
  from yat.test import Node
  from yat.test import macro
  
  
  class ToolsBackup(unittest.TestCase):
      def setUp(self):
          self.log = Logger()
          self.pri_sh = CommonSH('PrimaryDbUser')
          self.log.info(
              '---Opengauss_Function_JdbcGsBackup_Case0038start---')
          self.constant = Constant()
          self.DB_ENV_PATH = macro.DB_ENV_PATH
          self.Primary_Node = Node('PrimaryDbUser')
          self.Root_Node = Node('PrimaryRoot')
          self.package = os.path.join(
              os.path.dirname(macro.DB_INSTANCE_PATH), 'package_zh')
          self.db_name = "gs_db03"
          self.user = "us_03"
          self.tb_name = "t_03"
          self.tb_name_1 = "t_03_bak"
          self.log.info('---备份pg_hba.conf文件---')
          cmd = f"cp {os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')}  " \
                f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf_t_bak')}"
          self.log.info(cmd)
          msg = self.Primary_Node.sh(cmd).result()
          self.log.info(msg)
  
      def test_tools_backup(self):
          self.log.info('---创建工具所在目录---')
          mkdir_cmd = f'''if [ ! -d "{self.package}" ]
                          then
                              mkdir -p {self.package}
                          fi'''
          self.log.info(mkdir_cmd)
          result = self.Primary_Node.sh(mkdir_cmd).result()
          self.log.info(result)
          self.assertEqual(result, '')
          self.log.info('---获取openGauss-tools-backup工具包---')
          sql_cmd = f'''wget -P {self.package} {macro.PACKAGE_URL}; '''
          self.log.info(sql_cmd)
          result = self.Primary_Node.sh(sql_cmd).result()
          self.log.info(result)
          self.assertIn(f"‘{self.package}/openGauss-tools-backup.tar.gz’ saved"
                        , result)
          self.log.info('---解压工具包---')
          sql_cmd = f'''cd {self.package};
              tar -zxvf openGauss-tools-backup.tar.gz; '''
          self.log.info(sql_cmd)
          result = self.Primary_Node.sh(sql_cmd).result()
          self.log.info(result)
  
          self.log.info('-----创建数据库----')
          sql_cmd = self.pri_sh.execut_db_sql(f'''drop database if exists \
              {self.db_name};
              create database {self.db_name};''')
          self.log.info(sql_cmd)
          self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_cmd)
          self.log.info('---在指定数据库下创建测试用户和表---')
          sql_cmd = f'''drop user if exists {self.user};
              create user {self.user} with sysadmin \
              password '{macro.COMMON_PASSWD}';
              drop table if exists {self.tb_name} cascade;
              drop table if exists {self.tb_name_1} cascade;
              create table {self.tb_name}(id1 int, id2 int, id3 int);
              create table  {self.tb_name_1}(id1 int, id2 int, id3 int);'''
          self.log.info(sql_cmd)
          sql_result = self.pri_sh.execut_db_sql(sql=sql_cmd,
                                                 dbname=f'{self.db_name}')
          self.log.info(sql_result)
          self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_result)
          self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_result)
          self.log.info('---创建触发器函数---')
          sql_cmd = f'''create or replace function tri_insert_func01() returns \
          trigger as
              \$\$
              declare
              begin
                  insert into {self.tb_name_1} values(new.id1, new.id2, \
                  new.id3);
                  return new;
              end
              \$\$ language plpgsql;
              create trigger insert_jdbc01 before insert on {self.tb_name} \
              for each row execute procedure tri_insert_func01();
              insert into {self.tb_name} values(100,200,300);
              insert into {self.tb_name} values(100,200,300);
              insert into {self.tb_name} values(100,200,300);'''
          sql_result = self.pri_sh.execut_db_sql(sql=sql_cmd,
                                                 dbname=f'{self.db_name}')
          self.log.info(sql_result)
          self.assertIn(self.constant.TRIGGER_CREATE_SUCCESS_MSG, sql_result)
  
          self.log.info('---修改信任方式为sha256---')
          cmd = f"grep -nr '127.0.0.1/32' " \
                f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')}"
          self.log.info(cmd)
          line = self.Primary_Node.sh(
              cmd).result().splitlines()[0].split(':')[0]
          self.log.info(line)
          cmd = f'sed -i "{str(int(line)+1)} ihost   all     all ' \
                f'{self.Primary_Node.db_host}/32   sha256" ' \
                f'{os.path.join(macro.DB_INSTANCE_PATH, "pg_hba.conf")}; ' \
                f'cat {os.path.join(macro.DB_INSTANCE_PATH, "pg_hba.conf")};'
          self.log.info(cmd)
          result = self.Primary_Node.sh(cmd).result()
          self.log.info(result)
          restart_msg = self.pri_sh.restart_db_cluster()
          self.log.info(restart_msg)
          status = self.pri_sh.get_db_cluster_status()
          self.assertTrue("Degraded" in status or "Normal" in status)
  
          self.log.info('---导出---')
          sql_cmd = f'''cd {self.package}/openGauss-tools-backup;\
              java -jar openGauss-tools-backup.jar \
              -m dump \
              -d {self.db_name} \
              -h {self.Primary_Node.db_host} \
              -p {self.Primary_Node.db_port} \
              -U {self.user} \
              -P {macro.COMMON_PASSWD} \
              -s public '''
          self.log.info(sql_cmd)
          msg = self.Primary_Node.sh(sql_cmd).result()
          self.log.info(msg)
          self.assertIn(self.constant.jdbcgsbackup_success, msg)
          self.assertNotIn(self.constant.jdbcgsbackup_failed[0] and
                           self.constant.jdbcgsbackup_failed[1] and
                           self.constant.jdbcgsbackup_failed[2], msg)
  
          self.log.info('---删表后执行导入---')
          sql_cmd = f'''drop table if  exists {self.tb_name};
              drop table if  exists {self.tb_name_1};'''
          sql_result = self.pri_sh.execut_db_sql(sql=sql_cmd,
                                                 dbname=f'{self.db_name}')
          self.log.info(sql_result)
          self.log.info('---导入---')
          sql_cmd = f'''cd {self.package}/openGauss-tools-backup;\
              java -jar openGauss-tools-backup.jar \
              -m restore \
              -d {self.db_name} \
              -h {self.Primary_Node.db_host} \
              -p {self.Primary_Node.db_port} \
              -U {self.user} \
              -P {macro.COMMON_PASSWD} \
              -s public \
              -n public '''
          self.log.info(sql_cmd)
          msg = self.Primary_Node.sh(sql_cmd).result()
          self.log.info(msg)
          self.assertIn(self.constant.jdbcgsbackup_success, msg)
          self.assertNotIn(self.constant.jdbcgsbackup_failed[0] and
                           self.constant.jdbcgsbackup_failed[1] and
                           self.constant.jdbcgsbackup_failed[2], msg)
          self.log.info('---查询导入的表---')
          sql_cmd_list = [f'select * from {self.tb_name}',
                          f'select * from {self.tb_name_1}']
          sql_result = ''
          for cmd in sql_cmd_list:
              sql_result += self.pri_sh.execut_db_sql(sql=cmd,
                                                      dbname=f'{self.db_name}')
              self.log.info(sql_result)
          self.assertIn('3 rows', sql_result)
  
      def tearDown(self):
          self.log.info('---清理环境---')
          sql_cmd = f'''rm -rf {self.package};'''
          self.log.info(sql_cmd)
          result = self.Root_Node.sh(sql_cmd).result()
          self.log.info(result)
          cmd = f"rm -rf " \
                f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')};" \
                f"mv " \
                f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf_t_bak')} " \
                f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')}"
          self.log.info(cmd)
          self.Primary_Node.sh(cmd)
          sql_cmd = self.pri_sh.execut_db_sql(f'''drop database if exists \
              {self.db_name};
              drop user if exists {self.user} cascade;''')
          self.log.info(sql_cmd)
          self.log.info(
              '---Opengauss_Function_JdbcGsBackup_Case0038finish---')
  