# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

# Rover process Istio e2e with BanyanDB storage.
# Ported from apache/skywalking test/e2e-v2/cases/rover/process/istio.
# Intended for CI; requires Kind, Istio, SkyWalking Helm, and Rover (not practical to run locally without sufficient resources).

setup:
  env: kind
  file: kind.yaml
  timeout: 25m
  init-system-environment: ../../../../../script/env
  kind:
    import-images:
      - apache/skywalking-banyandb:${TAG}-testing
    expose-ports:
      - namespace: istio-system
        resource: service/skywalking-ui
        port: 80
  steps:
    - name: set PATH
      command: export PATH=/tmp/skywalking-infra-e2e/bin:$PATH
    - name: install yq
      command: bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh yq
    - name: install swctl
      command: bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh swctl
    - name: install kubectl
      command: bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh kubectl
    - name: install istio
      command: |
        bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh istioctl
        istioctl install -y --set profile=demo
        kubectl label namespace default istio-injection=enabled
    - name: Install helm
      command: bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh helm
    - name: Load BanyanDB image into kind
      command: kind load docker-image apache/skywalking-banyandb:${TAG}-testing --name kind
    - name: Deploy BanyanDB
      command: envsubst < test/e2e-v2/cases/rover/process/istio/banyandb/banyandb-deployment.yaml | kubectl apply -f -
    - name: Wait for BanyanDB
      command: kubectl rollout status deployment/banyandb -n istio-system --timeout=120s
    - name: Install SkyWalking
      # To enable BanyanDB access logs for debugging, add the following --set flags to the helm install command:
      #   --set "banyandb.standalone.env[0].name=BYDB_ENABLE_INGESTION_ACCESS_LOG"
      #   --set "banyandb.standalone.env[0].value=\"true\""
      #   --set "banyandb.standalone.env[1].name=BYDB_ENABLE_QUERY_ACCESS_LOG"
      #   --set "banyandb.standalone.env[1].value=\"true\""
      #   --set "banyandb.standalone.env[2].name=BYDB_ACCESS_LOG_ROOT_PATH"
      #   --set "banyandb.standalone.env[2].value=/tmp"
      # Note: access logs use Go channels with internal mutex locks, which adds contention to the request path.
      command: |
        rm -rf skywalking-helm
        helm pull oci://ghcr.io/apache/skywalking-helm/skywalking-helm \
          --version "0.0.0-${SW_KUBERNETES_COMMIT_SHA}" --untar
        helm -n istio-system install skywalking \
          oci://ghcr.io/apache/skywalking-helm/skywalking-helm \
          --version "0.0.0-${SW_KUBERNETES_COMMIT_SHA}" \
          --timeout 15m \
          --set fullnameOverride=skywalking \
          --set eckOperator.enabled=false \
          --set elasticsearch.enabled=false \
          --set oap.replicas=1 \
          --set ui.image.repository=ghcr.io/apache/skywalking/ui \
          --set ui.image.tag=${SW_OAP_COMMIT} \
          --set oap.image.tag=${SW_OAP_COMMIT} \
          --set oap.image.repository=ghcr.io/apache/skywalking/oap \
          --set oap.storageType=banyandb \
          --set oap.env.SW_HEALTH_CHECKER=default \
          -f test/e2e-v2/cases/rover/process/istio/banyandb/values-banyandb.yaml
    - name: Deploy demo services
      command: |
        kubectl apply -f https://raw.githubusercontent.com/istio/istio/$ISTIO_VERSION/samples/bookinfo/platform/kube/bookinfo.yaml
        kubectl apply -f https://raw.githubusercontent.com/istio/istio/$ISTIO_VERSION/samples/bookinfo/networking/bookinfo-gateway.yaml
        kubectl apply -f https://raw.githubusercontent.com/istio/istio/$ISTIO_VERSION/samples/bookinfo/networking/destination-rule-all.yaml
        kubectl apply -f https://raw.githubusercontent.com/istio/istio/$ISTIO_VERSION/samples/bookinfo/networking/virtual-service-all-v1.yaml
    - name: Install SkyWalking Rover
      command: envsubst < test/e2e-v2/cases/rover/process/istio/banyandb/rover.yaml | kubectl apply -f -
  wait:
    - namespace: istio-system
      resource: deployments/skywalking-oap
      for: condition=available
    - namespace: default
      resource: pod
      for: condition=Ready

verify:
  retry:
    count: 20
    interval: 10s
  cases:
    - query: swctl --display yaml --base-url=http://${service_skywalking_ui_host}:${service_skywalking_ui_80}/graphql service ls
      expected: expected/service.yml
    - query: swctl --display yaml --base-url=http://${service_skywalking_ui_host}:${service_skywalking_ui_80}/graphql instance list --service-name=productpage.default
      expected: expected/service-instance.yml
    - query: |
        swctl --display yaml --base-url=http://${service_skywalking_ui_host}:${service_skywalking_ui_80}/graphql process list --service-name=productpage.default --instance-name=$( \
          swctl --display yaml --base-url=http://${service_skywalking_ui_host}:${service_skywalking_ui_80}/graphql instance list --service-name=productpage.default | yq e '.[0].name' - \
        )
      expected: expected/process.yml

cleanup:
  on: always
  collect:
    on: failure
    output-dir: $SW_INFRA_E2E_LOG_DIR/banyandb-data
    items:
      - namespace: istio-system
        label-selector: app=banyandb
        paths:
          - /tmp/trace/
          - /tmp/stream/
          - /tmp/measure/
          - /tmp/property/
          - /tmp/schema-property/
