#
# Copyright (C) 2015 Red Hat, Inc.
#
# 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.
#

name: E2E Chaos Tests

on:
  workflow_dispatch:
  schedule:
    - cron: '0 6 * * *' # Every day at 6am

concurrency:
  # Only run once for latest commit per ref and cancel other (previous) runs.
  group: ci-e2e-chaos-tests-${{ github.ref }}
  cancel-in-progress: true

env:
  CHAOS_MESH_VERSION: 2.6.0
  CHAOS_TESTS_MODULE: chaos-tests
  MAVEN_ARGS: -B -C -V -ntp -Dhttp.keepAlive=false -e

jobs:
  chaos-tests:
    name: K8S Chaos Test
    runs-on: ubuntu-latest
    timeout-minutes: 45
    if: github.repository == 'fabric8io/kubernetes-client'
    strategy:
      fail-fast: false
      matrix:
        kubernetes: [v1.25.0]
        java-version: [11,17]
        http-client: [okhttp,jdk,jetty,vertx]
        chaos-test: ["network-delay.yaml", "network-loss.yaml", "network-duplicate.yaml"]
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Minikube-Kubernetes
        uses: manusa/actions-setup-minikube@v2.11.0
        with:
          minikube version: v1.31.2
          kubernetes version: ${{ matrix.kubernetes }}
          github token: ${{ secrets.GITHUB_TOKEN }}
      - name: Setup Java ${{ matrix.java-version }}
        uses: actions/setup-java@v4
        with:
          java-version: "${{ matrix.java-version }}"
          distribution: "temurin"
          cache: maven
      - name: Setup Kubectl # Chaos-Mesh setup requires a functional kubectl version for all cluster versiones
        uses: azure/setup-kubectl@v4
        with:
          version: ${{ matrix.kubernetes }}
      - name: Install Chaos Mesh
        run: |
          curl -sSL https://mirrors.chaos-mesh.org/v${CHAOS_MESH_VERSION}/install.sh | bash
          kubectl wait --for=condition=Ready pods --all-namespaces --all --timeout=600s
      - name: Run Chaos Tests
        env:
          CHAOS_TEST: ${{ matrix.chaos-test }}
        run: |
          ./mvnw ${MAVEN_ARGS} -Pitests -P"enable-snapshots" -P"httpclient-${{ matrix.http-client }}" verify -pl chaos-tests