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   : go驱动
  Case Name   : 连接数据库，定义视图
  Description :
      1.新建用户并赋权
      2.配置pg_hba入口, sha256认证方法
      3.连接数据库，定义视图
      4.还原pg_hba文件
      5.删除用户
      6.刪除go_conn.go
  Expect      :
      1.执行成功
      2.执行成功
      3.执行成功，回显无panic提示
      4.执行成功
      5.执行成功
      6.执行成功
  History     :
  """
  import os
  import re
  import unittest
  
  from yat.test import Node
  from yat.test import macro
  
  from testcase.utils.ComGo import ComGo
  from testcase.utils.CommonSH import CommonSH
  from testcase.utils.Constant import Constant
  from testcase.utils.Logger import Logger
  
  
  class ConnGO43(unittest.TestCase):
      def setUp(self):
          self.pri_user = Node('PrimaryDbUser')
          self.pri_sh = CommonSH('PrimaryDbUser')
          self.pri_root = Node('PrimaryRoot')
          self.log = Logger()
          self.go = ComGo()
          self.cons = Constant()
          self.db_user = 'go_user_43'
          self.go_file = 'go_conn.go'
          self.table_name = 't_go_43'
          self.view_name = 'v_go_43'
          self.cur_os = self.pri_root.sh('cat /etc/system-release').result()
          self.pg_hba_path = os.path.join(macro.DB_INSTANCE_PATH,
                                          macro.PG_HBA_FILE_NAME)
          text = f'-----{os.path.basename(__file__)} start-----'
          self.log.info(text)
  
          text = f'----前置检查：已安装golang1.11.1以上版本----'
          self.log.info(text)
          res = self.go.check_install_go(self.pri_user, '1.11.1')
          self.assertTrue(res, f'执行失败: {text}')
  
      def test_1(self):
          text = '----step1: 新建用户并赋权 expect: 成功----'
          self.log.info(text)
          sql = f"drop user if exists {self.db_user} cascade;" \
              f"create user {self.db_user} with password " \
              f"'{macro.COMMON_PASSWD}';" \
              f"grant all privileges on database " \
              f"{self.pri_user.db_name} to {self.db_user};"
          self.log.info(sql)
          res = self.pri_sh.execut_db_sql(sql)
          self.log.info(res)
          expect = f'{self.cons.DROP_ROLE_SUCCESS_MSG}.*' \
              f'{self.cons.CREATE_ROLE_SUCCESS_MSG}.*' \
              f'{self.cons.GRANT_SUCCESS_MSG}'
          reg_res = re.search(expect, res, re.S)
          self.assertIsNotNone(reg_res, f'执行失败: {text}')
  
          text = '----step2: 配置pg_hba入口 expect: 成功----'
          self.log.info(text)
          cmd = f'cp {self.pg_hba_path} {self.pg_hba_path}_bak && ' \
              f'source {macro.DB_ENV_PATH} && ' \
              f'gs_guc reload -D {macro.DB_INSTANCE_PATH} ' \
              f'-h "host {self.pri_user.db_name} {self.db_user} ' \
              f'{self.pri_user.ssh_host}/32 sha256"'
          self.log.info(cmd)
          res = self.pri_user.sh(cmd).result()
          self.log.info(res)
          self.assertIn(self.cons.GSGUC_SUCCESS_MSG, res, f'执行失败: {text}')
  
          text = '----step3: 连接数据库，定义视图 expect: 成功----'
          self.log.info(text)
          conn_str = f'connStr := "host={self.pri_user.db_host} ' \
              f'port={self.pri_user.db_port} ' \
              f'user={self.db_user} ' \
              f'password={macro.COMMON_PASSWD} ' \
              f'dbname={self.pri_user.db_name} ' \
              f'sslmode=disable"'
  
          clean_sql = f"drop view if exists {self.view_name};" \
              f"drop view if exists {self.view_name}_1;" \
              f"drop view if exists tmp_{self.view_name};" \
              f"drop materialized view if exists {self.view_name}_2;" \
              f"drop materialized view if exists {self.view_name}_3;" \
              f"drop table if exists {self.table_name} cascade;"
          s_sql = f"{clean_sql}" \
              f"create table {self.table_name}(id int, name varchar(20));" \
              f"insert into {self.table_name} values (generate_series(1,100)," \
              f" generate_series(1,100)||'test');"
          sql_list = list()
          # 创建普通视图
          sql_list.append(f"create view {self.view_name} as select * from"
                          f" {self.table_name};")
          # 创建视图提供行级安全
          sql_list.append(f"create view {self.view_name}_1 with ("
                          f"security_barrier=0) as "
                          f"select * from {self.table_name};")
          # 创建临时视图
          sql_list.append(f"create temp view tmp_{self.view_name} as "
                          f"select * from {self.table_name};")
          # 创建增量物化视图
          sql_list.append(f"create incremental materialized view "
                          f"{self.view_name}_2 as "
                          f"select * from {self.table_name};")
          # 增量刷新
          sql_list.append(f"refresh incremental materialized view "
                          f"{self.view_name}_2;")
          # 创建全量物化视图
          sql_list.append(f"create materialized view {self.view_name}_3 as"
                          f" select * from {self.table_name};")
          # 全量刷新
          sql_list.append(f"refresh materialized view {self.view_name}_3;")
          # 查询
          q_list = list()
          q_list.append(f"select count(*) from {self.view_name};")
          q_list.append(f"select count(*) from {self.view_name}_1;")
          q_list.append(f"select count(*) from tmp_{self.view_name};")
          q_list.append(f"select count(*) from {self.view_name}_2;")
          q_list.append(f"select count(*) from {self.view_name}_3;")
          f_contant = f'{self.go.get_head()}\n\n' \
              f'func cleanup(db *sql.DB) {{\n' \
              f'    {self.go.exec_sql(clean_sql)}' \
              f'\n\n    db.Close()\n' \
              f'}}\n\n' \
              f'func main() {{\n' \
              f'    {conn_str}\n' \
              f'    {self.go.conndb("connStr")}\n\n' \
              f'    defer cleanup(db)\n\n' \
              f'    {self.go.exec_sql(s_sql)}\n\n'
          for s in sql_list:
              f_contant += f'    {self.go.exec_sql(s, False)}\n\n'
          i = 0
          for q in q_list:
              i += 1
              f_contant += f'    {self.go.query_row(q, **{f"n{i}":"int"})}\n\n'
          f_contant += f'}}'
          cmd = f'cat > {self.go_file} <<EOF\n' \
              f'{f_contant}\n' \
              f'EOF\n'
          if 'CentOS' in self.cur_os:
              cmd += f'source /etc/profile; go run {self.go_file}'
          else:
              cmd += f'go run {self.go_file}'
          cmd = cmd.replace('{', '{{')
          cmd = cmd.replace('}', '}}')
          self.log.info(cmd)
          res = self.pri_root.sh(cmd).result()
          self.log.info(res)
          self.assertNotIn('panic:', res, f'执行失败: {text}')
          self.assertIn('100\n'*5, res, f'执行失败: {text}')
  
      def tearDown(self):
          text = '----run teardown----'
          self.log.info(text)
  
          s4_text = '----step4: 还原pg_hba文件  expect: 成功----'
          self.log.info(s4_text)
          cmd = f'mv {self.pg_hba_path}_bak {self.pg_hba_path}'
          self.log.info(cmd)
          cmd_res = self.pri_user.sh(cmd).result()
          self.log.info(cmd_res)
  
          is_restart = self.pri_sh.restart_db_cluster()
  
          s5_text = '----step5: 删除用户  expect: 成功----'
          self.log.info(s5_text)
          sql = f'drop user if exists {self.db_user} cascade;'
          sql_res = self.pri_sh.execut_db_sql(sql)
          self.log.info(sql_res)
  
          s6_text = '----step6: 刪除go_conn.go expect: 成功----'
          self.log.info(s6_text)
          rm_cmd = f'rm -rf {self.go_file}'
          self.log.info(rm_cmd)
          rm_res = self.pri_root.sh(rm_cmd).result()
          self.log.info(rm_res)
  
          self.assertEqual(len(cmd_res), 0, f'执行失败: {s4_text}')
          self.assertTrue(is_restart, f'执行失败: {s4_text}')
          self.assertIn(self.cons.DROP_ROLE_SUCCESS_MSG,
                        sql_res,
                        f'执行失败: {s5_text}')
          self.assertIs(rm_res, '', f'执行失败: {s6_text}')
  
          text = f'-----{os.path.basename(__file__)} end-----'
          self.log.info(text)
  