# 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.

# This file is used to show how to write configuration files and can be used to test.

setup:
  env: compose
  file: docker-compose.yml
  timeout: 20m
  init-system-environment: ../../script/env
  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

verify:
  # verify with retry strategy
  retry:
    # max retry count
    count: 20
    # the interval between two retries, in millisecond.
    interval: 3s
  cases:
    # service
    - expected: expected/service.yml
      query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql browser service ls test-ui
    # version
    - expected: expected/version.yml
      query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql browser version ls --service-name test-ui
    # page
    - expected: expected/page.yml
      query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql browser page ls --service-name test-ui
    # browser error log
    - expected: expected/error-log.yml
      query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql browser logs ls --service-name test-ui --version-name v1.0.0
    # browser service metrics
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_pv --service-id dGVzdC11aQ==.1 |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_error_sum --service-id dGVzdC11aQ==.1 |yq e 'to_entries' -
    # browser version metrics
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_single_version_pv --service-id dGVzdC11aQ==.1 --instance-name v1.0.0 |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_single_version_error_sum --service-id dGVzdC11aQ==.1 --instance-name v1.0.0 |yq e 'to_entries' -
    # browser page metrics
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_pv --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_error_sum --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_js_error_sum --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    #  # browser performance metrics
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_dom_analysis_avg --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_dom_ready_avg --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_load_page_avg --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    - expected: expected/metrics-has-value.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear \
          --name=browser_app_page_ttl_avg --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries' -
    - expected: expected/metrics-has-value-percentile.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics multiple-linear \
          --name=browser_app_page_dom_ready_percentile --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries | with(.[] ; .value=(.value | to_entries))' -
    - expected: expected/metrics-has-value-percentile.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics multiple-linear \
          --name=browser_app_page_load_page_percentile --service-id dGVzdC11aQ==.1 --endpoint-name index.html |yq e 'to_entries | with(.[] ; .value=(.value | to_entries))' -
    # dependency service
    - expected: expected/dependency.yml
      query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency service --service-id dGVzdC11aTxicm93c2VyPg==.1
    # trace
    - expected: expected/traces.yml
      query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls
    - expected: expected/trace-detail.yml
      query: |
        swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace $( \
          swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls | grep -A 5 'index.html' | tail -n1 | awk -F ' ' '{print $2}')
