# 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: sample-test-
spec:
  entrypoint: sample-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: sample-tests-image-suffix
        value: sample-tests
      - name: namespace
        value: kubeflow
      - name: is-integration-test
        value: "false"
  templates:
    - name: sample-test
      inputs:
        parameters:
          - name: target-image-prefix
          - name: test-results-gcs-dir
          - name: sample-tests-image-suffix
          - name: namespace
          - name: is-integration-test
      steps:
        - - name: build-sample-tests-image
            template: build-image-by-dockerfile
            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.sample-tests-image-suffix}}"
        - - name: run-integration-tests-loop
            template: run-sample-tests
            arguments:
              parameters:
                - name: namespace
                  value: "{{inputs.parameters.namespace}}"
                - name: sample-tests-image
                  value: "{{inputs.parameters.target-image-prefix}}{{inputs.parameters.sample-tests-image-suffix}}"
                - name: target-image-prefix
                  value: "{{inputs.parameters.target-image-prefix}}"
                - name: test-results-gcs-dir
                  value: "{{inputs.parameters.test-results-gcs-dir}}"
                - name: test-name
                  value: "{{item}}"
            withItems:
              # TODO(#5089): Re-enable the xgboost sample test after fixed the
              # component and sample.
              # - xgboost_training_cm
              - dataflow
              # Disabled ai_platform test because we are developing new component.
              # - ai_platform
              # TODO(#4361): Re-enable the kubeflow_tf_serving sample test after
              # fixed the sample
              # - kubeflow_tf_serving
              # Disable container_build because this approach will be deprecated soon.
              # - container_build
              # TODO: Re-enable test when tfx switch to v2
              # - parameterized_tfx_oss
            when: "{{inputs.parameters.is-integration-test}} == true"
    # Build and push image
    - name: build-image-by-dockerfile
      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-sample-tests
      retryStrategy:
        limit: 2
      inputs:
        parameters:
          - name: test-results-gcs-dir
          - name: target-image-prefix
          - 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",
          "--namespace", "{{inputs.parameters.namespace}}",
          "--results-gcs-dir", "{{inputs.parameters.test-results-gcs-dir}}",
          "--target-image-prefix", "{{inputs.parameters.target-image-prefix}}",
          "--test-name", "{{inputs.parameters.test-name}}",
        ]
