def pre_test(){
   
    sh '''
    sudo rmtaos||echo 'no taosd installed'
    '''
    sh '''
    cd ${WKC}
    git reset --hard
    git checkout $BRANCH_NAME
    git pull
    git submodule update
    cd ${WK}
    git reset --hard
    git checkout $BRANCH_NAME
    git pull
    export TZ=Asia/Harbin
    date
    rm -rf ${WK}/debug
    mkdir debug
    cd debug
    cmake .. > /dev/null
    make > /dev/null
    make install > /dev/null
    pip3 install ${WKC}/src/connector/python
    '''
    return 1
}
pipeline {
  agent none
  environment{
    
      WK = '/data/lib/jenkins/workspace/TDinternal'
      WKC= '/data/lib/jenkins/workspace/TDinternal/community'
  }

  stages {
      stage('Parallel test stage') {
      parallel {
        stage('pytest') {
          agent{label 'slad1'}
          steps {
            pre_test()
            sh '''
            cd ${WKC}/tests
            find pytest -name '*'sql|xargs rm -rf
            ./test-all.sh pytest
            date'''
          }
        }
        stage('test_b1') {
          agent{label 'master'}
          steps {
            pre_test()
            
            sh '''
            cd ${WKC}/tests
            ./test-all.sh b1
            date'''
            
            
          }
        }

        stage('test_crash_gen') {
          agent{label "slad2"}
          steps {
            pre_test()
            sh '''
            cd ${WKC}/tests/pytest
            '''
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/pytest
                ./crash_gen.sh -a -p -t 4 -s 2000
                '''
            }
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/pytest
                rm -rf /var/lib/taos/*
                rm -rf /var/log/taos/*
                ./handle_crash_gen_val_log.sh
                '''
            }
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/pytest
                rm -rf /var/lib/taos/*
                rm -rf /var/log/taos/*
                ./handle_taosd_val_log.sh
                '''
            }
            
            sh'''
            systemctl start taosd 
            sleep 10
            '''
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/gotest
                bash batchtest.sh
                '''
            }
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/examples/python/PYTHONConnectorChecker
                python3 PythonChecker.py
                '''
            }
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/examples/JDBC/JDBCDemo/
                mvn clean package >/dev/null 
                java -jar target/JdbcRestfulDemo-jar-with-dependencies.jar
                '''
            }
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cp -rf ${WKC}/tests/examples/nodejs ${JENKINS_HOME}/workspace/
                cd ${JENKINS_HOME}/workspace/nodejs
                node nodejsChecker.js host=localhost
                '''
            }
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${JENKINS_HOME}/workspace/C#NET/src/CheckC#
                dotnet run
                '''
            }
            sh '''
            systemctl stop taosd 
            cd ${WKC}/tests
            ./test-all.sh b2
            date
            '''
            sh '''
            cd ${WKC}/tests
            ./test-all.sh full unit
            date'''
          }
        }

        stage('test_valgrind') {
          agent{label "slad3"}

          steps {
            pre_test()
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                sh '''
                cd ${WKC}/tests/pytest
                nohup taosd >/dev/null &
                sleep 10
                python3 concurrent_inquiry.py -c 1
                
                '''
            }
            sh '''
            cd ${WKC}/tests
            ./test-all.sh full jdbc
            date'''
            sh '''
            cd ${WKC}/tests/pytest
            ./valgrind-test.sh 2>&1 > mem-error-out.log
            ./handle_val_log.sh
          
            date
            cd ${WKC}/tests
            ./test-all.sh b3
            date'''
            sh '''
            date
            cd ${WKC}/tests
            ./test-all.sh full example
            date'''
          }
        }
       
       stage('arm64_build'){
                agent{label 'arm64'}
                steps{
                    sh '''
                    cd ${WK}
                    git fetch
                    git checkout develop
                    git pull
                    cd ${WKC}
                    git fetch
                    git checkout develop
                    git pull
                    git submodule update                    
                    cd ${WKC}/packaging
                    ./release.sh -v cluster -c aarch64 -n 2.0.0.0 -m 2.0.0.0
                    
                    '''
                }
            }
            stage('arm32_build'){
                agent{label 'arm32'}
                steps{
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                        sh '''
                        cd ${WK}
                        git fetch
                        git checkout develop
                        git pull
                        cd ${WKC}
                        git fetch
                        git checkout develop
                        git pull
                        git submodule update
                        cd ${WKC}/packaging
                        ./release.sh -v cluster -c aarch32 -n 2.0.0.0 -m 2.0.0.0
                        
                        '''
                    }
                    
                }
            }
      }
    }

  }
  post {  
        success {
            emailext (
                subject: "PR-result: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' SUCCESS",
                body: """<!DOCTYPE html>
                <html>
                <head>
                <meta charset="UTF-8">
                </head>
                <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                    <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                        <tr>
                            <td><br />
                                <b><font color="#0B610B"><font size="6">构建信息</font></font></b>
                                <hr size="2" width="100%" align="center" /></td>
                        </tr>
                        <tr>
                            <td>
                                <ul>
                                <div style="font-size:18px">
                                    <li>构建名称>>分支：${env.BRANCH_NAME}</li>
                                    <li>构建结果：<span style="color:green"> Successful </span></li>
                                    <li>构建编号：${BUILD_NUMBER}</li>
                                    <li>触发用户：${env.CHANGE_AUTHOR}</li>
                                    <li>提交信息：${env.CHANGE_TITLE}</li>
                                    <li>构建地址：<a href=${BUILD_URL}>${BUILD_URL}</a></li>
                                    <li>构建日志：<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
                                    
                                </div>
                                </ul>
                            </td>
                        </tr>
                    </table></font>
                </body>
                </html>""",
                to: "yqliu@taosdata.com,pxiao@taosdata.com",
                from: "support@taosdata.com"
            )
        }
        failure {
            emailext (
                subject: "PR-result: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' FAIL",
                body: """<!DOCTYPE html>
                <html>
                <head>
                <meta charset="UTF-8">
                </head>
                <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                    <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                        <tr>
                            <td><br />
                                <b><font color="#0B610B"><font size="6">构建信息</font></font></b>
                                <hr size="2" width="100%" align="center" /></td>
                        </tr>
                        <tr>
                            <td>
                                <ul>
                                <div style="font-size:18px">
                                    <li>构建名称>>分支：${env.BRANCH_NAME}</li>
                                    <li>构建结果：<span style="color:red"> Failure </span></li>
                                    <li>构建编号：${BUILD_NUMBER}</li>
                                    <li>触发用户：${env.CHANGE_AUTHOR}</li>
                                    <li>提交信息：${env.CHANGE_TITLE}</li>
                                    <li>构建地址：<a href=${BUILD_URL}>${BUILD_URL}</a></li>
                                    <li>构建日志：<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
                                    
                                </div>
                                </ul>
                            </td>
                        </tr>
                    </table></font>
                </body>
                </html>""",
                to: "yqliu@taosdata.com,pxiao@taosdata.com",
                from: "support@taosdata.com"
            )
        }
    }
}