pipeline {
    agent none

    options {
        timestamps()
    }

    parameters{
        choice choices: ['cpu', 'gpu'], description: 'cpu or gpu version', name: 'IMAGE_TYPE'
        string defaultValue: '0.6.0', description: 'server image version', name: 'IMAGE_VERSION', trim: true
        string defaultValue: 'suite.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_ann_acc.git"
        TEST_BRANCH = "0.6.0"
    }

    stages {
        stage("Setup env") {
            agent {
                kubernetes {
                    label 'dev-test'
                    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: hdf5-path
                              mountPath: /test
                              readOnly: true
                          volumes:
                          - name: kubeconf
                            secret:
                              secretName: test-cluster-config
                          - name: hdf5-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 Default Server") {
                    steps {
                        gitlabCommitStatus(name: 'Accuracy Test') {
                            container('milvus-testframework') {
                                script {
                                    print "In Deploy Default Server Stage"
                                    load "${env.WORKSPACE}/ci/jenkinsfile/deploy_default_server.groovy"
                                }
                            }
                        }
                    }
                }
                stage("Acc Test") {
                    steps {
                        gitlabCommitStatus(name: 'Accuracy Test') {
                            container('milvus-testframework') {
                                script {
                                    print "In Acc test stage"
                                    load "${env.WORKSPACE}/ci/jenkinsfile/acc_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 ann-accuracy test success !"
                    }
                }
                aborted {
                    script {
                        echo "Milvus ann-accuracy test aborted !"
                    }
                }
                failure {
                    script {
                        echo "Milvus ann-accuracy test failed !"
                    }
                }
            }
        }
    }
}
