# Copyright 2018 The Kubeflow Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: integration-test-
spec:
  entrypoint: integration-test
  arguments:
    parameters:
    - name: image-build-context-gcs-uri
    - name: image-builder-image
      value: gcr.io/ml-pipeline-test/image-builder:v20200208-0.1.25-771-g4c571961
    - name: target-image-prefix
    - name: test-results-gcs-dir
    - name: initialization-test-image-suffix
      value: initialization_test
    - name: api-integration-test-image-suffix
      value: api_integration_test
    - name: frontend-integration-tests-image-suffix
      value: frontend-integration-tests
    - name: basic-e2e-tests-image-suffix
      value: basic-e2e-tests
    - name: cluster-type
      value: gke
    - name: namespace
      value: kubeflow
  templates:
  - name: integration-test
    inputs:
      parameters:
      - name: target-image-prefix
      - name: test-results-gcs-dir
      - name: initialization-test-image-suffix
      - name: api-integration-test-image-suffix
      - name: frontend-integration-tests-image-suffix
      - name: basic-e2e-tests-image-suffix
      - name: namespace
    steps:
    - - name: build-initialization-test-image
        template: build-image
        arguments:
          parameters:
          - name: docker-path
            value: .
          - name: docker-file
            value: test/initialization-test/Dockerfile
          - name: image-name
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.initialization-test-image-suffix}}"
      - name: build-api-integration-test-image
        template: build-image
        arguments:
          parameters:
          - name: docker-path
            value: .
          - name: docker-file
            value: test/api-integration-test/Dockerfile
          - name: image-name
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.api-integration-test-image-suffix}}"
      - name: build-frontend-integration-tests-image
        template: build-image
        arguments:
          parameters:
          - name: docker-path
            value: test/frontend-integration-test
          - name: image-name
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.frontend-integration-tests-image-suffix}}"
      - name: build-basic-e2e-tests-image
        template: build-image
        arguments:
          parameters:
          - name: docker-path
            value: .
          - name: docker-file
            value: test/sample-test/Dockerfile
          - name: image-name
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.basic-e2e-tests-image-suffix}}"
    - - name: run-initialization-tests
        template: run-initialization-tests
        arguments:
          parameters:
          - name: test-results-gcs-dir
            value: "{{inputs.parameters.test-results-gcs-dir}}"
          - name: initialization-test-image
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.initialization-test-image-suffix}}"
    - - name: run-api-integration-tests
        template: run-api-integration-tests
        arguments:
          parameters:
          - name: test-results-gcs-dir
            value: "{{inputs.parameters.test-results-gcs-dir}}"
          - name: api-integration-test-image
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.api-integration-test-image-suffix}}"
    - - name: run-frontend-integration-tests
        template: run-frontend-integration-tests
        arguments:
          parameters:
          - name: test-results-gcs-dir
            value: "{{inputs.parameters.test-results-gcs-dir}}"
          - name: frontend-integration-tests-image
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.frontend-integration-tests-image-suffix}}"
        #TODO: Uncomment to disable the test on Minikube
        #TODO: Change the cluster-type to "minikube" once https://github.com/kubeflow/pipelines/pull/1285 and related PRs are merged.
        #when: "{{workflow.parameters.cluster-type}} != none" #Do not run the test on Minikube
    - - name: run-basic-sample-tests
        template: run-basic-e2e-tests
        arguments:
          parameters:
            - name: test-results-gcs-dir
              value: "{{inputs.parameters.test-results-gcs-dir}}"
            - name: sample-tests-image
              value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.basic-e2e-tests-image-suffix}}"
            - name: namespace
              value: "{{inputs.parameters.namespace}}"
            - name: test-name
              value: "{{item}}"
        withItems:
          - exit_handler
          - recursion
          - sequential
          - parallel_join
          - volume_ops

  - name: upgrade-test-preparation
    inputs:
      parameters:
      - name: target-image-prefix
      - name: test-results-gcs-dir
      - name: api-integration-test-image-suffix
    steps:
    - - name: build-api-integration-test-image
        template: build-image
        arguments:
          parameters:
          - name: docker-path
            value: .
          - name: docker-file
            value: test/api-integration-test/Dockerfile
          - name: image-name
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.api-integration-test-image-suffix}}"
    - - name: run-upgrade-tests-preparation
        template: run-upgrade-tests-preparation
        arguments:
          parameters:
          - name: test-results-gcs-dir
            value: "{{inputs.parameters.test-results-gcs-dir}}"
          - name: api-integration-test-image
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.api-integration-test-image-suffix}}"

  - name: upgrade-test-verification
    inputs:
      parameters:
      - name: target-image-prefix
      - name: test-results-gcs-dir
      - name: api-integration-test-image-suffix
    steps:
    - - name: run-upgrade-tests-verification
        template: run-upgrade-tests-verification
        arguments:
          parameters:
          - name: test-results-gcs-dir
            value: "{{inputs.parameters.test-results-gcs-dir}}"
          - name: api-integration-test-image
            value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.api-integration-test-image-suffix}}"

  # Build and push image
  - name: build-image
    retryStrategy:
      limit: 1
    inputs:
      parameters:
      # GCS URI prefix pointing to a .tar.gz archive of Docker build context
      - name: image-build-context-gcs-uri
        value: "{{workflow.parameters.image-build-context-gcs-uri}}"
      # The relative code path to the Dockerfile
      - name: docker-path
      # Name of the Docker file to use. "Dockerfile" by default
      - name: docker-file
        value: Dockerfile
      - name: image-name
    outputs:
      parameters:
        - name: strict-image-name
          valueFrom:
            path: /outputs/strict-image-name/file
    container:
      image: "{{workflow.parameters.image-builder-image}}"
      imagePullPolicy: 'Always'
      command:
      - /build.sh
      args: [
        "--image-build-context-gcs-uri", "{{inputs.parameters.image-build-context-gcs-uri}}",
        "--docker_path", "{{inputs.parameters.docker-path}}",
        "--docker_file", "{{inputs.parameters.docker-file}}",
        "--image_name", "{{inputs.parameters.image-name}}",
      ]
      env:
      - name: DOCKER_HOST
        value: 127.0.0.1
    sidecars:
    - name: dind
      image: docker:17.10-dind
      command:
      - dockerd-entrypoint.sh
      securityContext:
        privileged: true
      mirrorVolumeMounts: true

  - name: run-initialization-tests
    inputs:
      parameters:
      - name: test-results-gcs-dir
      - name: initialization-test-image
    container:
      image: "{{inputs.parameters.initialization-test-image}}"
      command:
      - /go/src/github.com/kubeflow/pipelines/test/initialization-test/run_test.sh
      args: [
        "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
      ]

  - name: run-api-integration-tests
    inputs:
      parameters:
      - name: test-results-gcs-dir
      - name: api-integration-test-image
    container:
      image: "{{inputs.parameters.api-integration-test-image}}"
      command:
      - /go/src/github.com/kubeflow/pipelines/test/api-integration-test/run_test.sh
      args: [
        "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
      ]

  - name: run-frontend-integration-tests
    inputs:
      parameters:
      - name: test-results-gcs-dir
      - name: frontend-integration-tests-image
    container:
      image: "{{inputs.parameters.frontend-integration-tests-image}}"
      command:
      - ./run_test.sh
      args: [
        "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
      ]

  - name: run-basic-e2e-tests
    retryStrategy:
      limit: 2
    inputs:
      parameters:
      - name: test-results-gcs-dir
      - name: sample-tests-image
      - name: namespace
      - name: test-name
    container:
      image: "{{inputs.parameters.sample-tests-image}}"
      command:
      - "python3"
      - "/python/src/github.com/kubeflow/pipelines/test/sample-test/sample_test_launcher.py"
      args: [
        "sample_test", "run_test",
        "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
        "--namespace", "{{inputs.parameters.namespace}}",
        "--test-name", "{{inputs.parameters.test-name}}",
      ]

  - name: run-upgrade-tests-preparation
    inputs:
      parameters:
      - name: test-results-gcs-dir
      - name: api-integration-test-image
    container:
      image: "{{inputs.parameters.api-integration-test-image}}"
      command:
      - "/go/src/github.com/kubeflow/pipelines/test/api-integration-test/run_test.sh"
      args: [
        "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
        "--run_upgrade_tests_preparation",
      ]

  - name: run-upgrade-tests-verification
    inputs:
      parameters:
      - name: test-results-gcs-dir
      - name: api-integration-test-image
    container:
      image: "{{inputs.parameters.api-integration-test-image}}"
      command:
      - "/go/src/github.com/kubeflow/pipelines/test/api-integration-test/run_test.sh"
      args: [
        "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
        "--run_upgrade_tests_verification",
      ]
