pipeline {
    agent none

    options {
        timestamps()
    }

    parameters{
        choice choices: ['gpu', 'cpu'], description: 'cpu or gpu version', name: 'IMAGE_TYPE'
        string defaultValue: 'master', description: 'server image version', name: 'IMAGE_VERSION', trim: true
        choice choices: ['poseidon', 'eros', 'apollo', 'athena'], description: 'server host', name: 'SERVER_HOST'
        string defaultValue: 'gpu_search_performance_sift1m.yaml', description: 'test suite config yaml', name: 'SUITE', trim: true
        string defaultValue: '09509e53-9125-4f5d-9ce8-42855987ad67', description: 'git credentials', name: 'GIT_USER', trim: true
    }

    environment {
        IMAGE_TAG = "${params.IMAGE_VERSION}-${params.IMAGE_TYPE}-ubuntu18.04-release"
        HELM_URL = "https://github.com/milvus-io/milvus-helm.git"
        HELM_BRANCH = "0.6.0"
        TEST_URL = "git@192.168.1.105:Test/milvus_benchmark.git"
        TEST_BRANCH = "master"
        TEST_LIB_URL = "http://192.168.1.105:6060/Test/milvus_metrics.git"
    }

    stages {
        stage("Setup env") {
            agent {
                kubernetes {
                    label "test-benchmark-${env.JOB_NAME}-${env.BUILD_NUMBER}"
                    defaultContainer 'jnlp'
                    yaml """
                        apiVersion: v1
                        kind: Pod
                        metadata:
                          labels:
                            app: milvus
                            componet: test
                        spec:
                          containers:
                          - name: milvus-testframework
                            image: registry.zilliz.com/milvus/milvus-test:v0.2
                            command:
                            - cat
                            tty: true
                            volumeMounts:
                            - name: kubeconf
                              mountPath: /root/.kube/
                              readOnly: true
                            - name: raw-data-path
                              mountPath: /poc
                              readOnly: true
                            - name: db-data-path
                              mountPath: /test
                              readOnly: false
                          volumes:
                          - name: kubeconf
                            secret:
                              secretName: test-cluster-config
                          - name: raw-data-path
                            flexVolume:
                              driver: "fstab/cifs"
                              fsType: "cifs"
                              secretRef:
                                name: "cifs-test-secret"
                              options:
                                networkPath: "//192.168.1.126/poc"
                                mountOptions: "vers=1.0"
                          - name: db-data-path
                            flexVolume:
                              driver: "fstab/cifs"
                              fsType: "cifs"
                              secretRef:
                                name: "cifs-test-secret"
                              options:
                                networkPath: "//192.168.1.126/test"
                                mountOptions: "vers=1.0"
                        """
                }
            }

            stages {
                stage("Deploy Test") {
                    steps {
                        gitlabCommitStatus(name: 'Deploy Test') {
                            container('milvus-testframework') {
                                script {
                                    print "In Deploy Test Stage"
                                    load "${env.WORKSPACE}/ci/jenkinsfile/deploy_test.groovy"
                                }
                            }
                        }
                    }
                }

                stage ("Cleanup Env") {
                    steps {
                        gitlabCommitStatus(name: 'Cleanup Env') {
                            container('milvus-testframework') {
                                script {
                                    load "${env.WORKSPACE}/ci/jenkinsfile/cleanup.groovy"
                                }
                            }
                        }
                    }
                }
            }
            post {
                always {
                    container('milvus-testframework') {
                        script {
                            load "${env.WORKSPACE}/ci/jenkinsfile/cleanup.groovy"
                        }
                    }
                }
                success {
                    script {
                        echo "Milvus benchmark test success !"
                    }
                }
                aborted {
                    script {
                        echo "Milvus benchmark test aborted !"
                    }
                }
                failure {
                    script {
                        echo "Milvus benchmark test failed !"
                    }
                }
            }
        }
    }
}
