.integration kubernetes:
  extends:
    - .rules:merge_request_pipelines:no_docs:no-community-mr
  tags:
    - kubernetes_integration
  stage: test kubernetes integration
  needs:
    - 'provision integration kubernetes'
  variables:
    KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: "k8s-runner-integration-tests-runner-$CI_PIPELINE_ID"
  before_script:
    - go install gotest.tools/gotestsum@latest
  script:
    # Note: We use hide-summary=output due to https://github.com/gotestyourself/gotestsum/issues/423
    - >
      gotestsum --format=testname --format-hide-empty-pkg --rerun-fails=3 \
        --hide-summary=output --packages=gitlab.com/gitlab-org/gitlab-runner/executors/kubernetes \
        --junitfile=junit_report.xml --junitfile-hide-empty-pkg -- \
        -timeout=10m -parallel=20 $EXTRA_GO_TEST_FLAGS \
        -tags=integration,kubernetes ./executors/kubernetes/...
  artifacts:
    when: always
    paths:
      - junit_report.xml
    reports:
      junit: junit_report.xml

provision integration kubernetes:
  extends:
  - .integration kubernetes
  needs:
  - 'prepare done'
  variables:
    KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: "k8s-runner-integration-tests-provisioner"
  script:
  - mage k8s:provisionIntegrationKubernetes $CI_PIPELINE_ID

integration kubernetes exec legacy:
  extends:
  - .integration kubernetes
  resource_group: "$CI_COMMIT_BRANCH-k8s-integration-exec-legacy"
  variables:
    CI_RUNNER_TEST_FEATURE_FLAG: "FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY"
    CI_RUNNER_TEST_FEATURE_FLAG_VALUE: "true"
    EXTRA_GO_TEST_FLAGS: "-run=TestRunIntegrationTestsWithFeatureFlag"

integration kubernetes attach:
  extends:
    - .integration kubernetes
  resource_group: "$CI_COMMIT_BRANCH-k8s-integration-attach"
  variables:
    CI_RUNNER_TEST_FEATURE_FLAG: "FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY"
    CI_RUNNER_TEST_FEATURE_FLAG_VALUE: "false"
    EXTRA_GO_TEST_FLAGS: "-run=TestRunIntegrationTestsWithFeatureFlag"

integration kubernetes:
  extends:
    - .integration kubernetes
  resource_group: "$CI_COMMIT_BRANCH-k8s-integration"
  variables:
    EXTRA_GO_TEST_FLAGS: "-skip=TestRunIntegrationTestsWithFeatureFlag"

destroy integration kubernetes:
  extends:
  - .integration kubernetes
  needs:
  - job: 'integration kubernetes'
    optional: true
  - job: 'integration kubernetes exec legacy'
    optional: true
  - job: 'integration kubernetes attach'
    optional: true
  variables:
    KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: "k8s-runner-integration-tests-provisioner"
  script:
  - mage k8s:destroyIntegrationKubernetes $CI_PIPELINE_ID
