# 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

trigger:
  action: http
  interval: 3s
  times: 10
  url: http://${consumer-py_host}:${consumer-py_9090}/test
  method: POST

verify:
  # verify with retry strategy
  retry:
    # max retry count
    count: 20
    # the interval between two retries, in millisecond.
    interval: 3s
  cases:
    # service list
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql service ls
      expected: expected/service.yml
    # service instance list
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance list --service-name=provider-py
      expected: expected/service-instance-provider-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance list --service-name=consumer-py
      expected: expected/service-instance-consumer-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance list --service-name=provider-py-kafka
      expected: expected/service-instance-provider-py-kafka.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql instance list --service-name=e2e-service-consumer
      expected: expected/service-instance-consumer-java.yml
    # service endpoint
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint list --keyword=test --service-name=consumer-py
      expected: expected/service-endpoint-consumer-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint list --keyword=users --service-name=e2e-service-consumer
      expected: expected/service-endpoint-consumer-java.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint list --keyword=users --service-name=provider-py
      expected: expected/service-endpoint-provider-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql endpoint list --keyword=users --service-name=provider-py-kafka
      expected: expected/service-endpoint-provider-py-kafka.yml
    # trace segment list
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls --service-name=consumer-py
      expected: expected/traces-list-consumer-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls --service-name=provider-py
      expected: expected/traces-list-provider-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls --service-name=provider-py-kafka
      expected: expected/traces-list-provider-py-kafka.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql trace ls --service-name=e2e-service-consumer
      expected: expected/traces-list-consumer-java.yml
    # trace detail
    - 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 --service-name=consumer-py\
            | yq e '.traces | select(.[].endpointnames[0]=="/test") | .[0].traceids[0]' -
        )
      expected: expected/trace-test-detail.yml
    # dependency service
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency service --service-name=consumer-py
      expected: expected/dependency-services-consumer-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency service --service-name=provider-py
      expected: expected/dependency-services-provider-py.yml
    # dependency instance
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency instance --service-name=consumer-py --dest-service-name=e2e-service-consumer
      expected: expected/dependency-instance-consumer-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency instance --service-name=e2e-service-consumer --dest-service-name=provider-py
      expected: expected/dependency-instance-provider-py.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql dependency instance --service-name=consumer-py --dest-service-name=provider-py-kafka
      expected: expected/dependency-instance-provider-py-kafka.yml
    # service metrics
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_sla --service-name=consumer-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_cpm --service-name=consumer-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_resp_time --service-name=consumer-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_apdex --service-name=consumer-py
      expected: expected/metrics-has-value.yml
    # service instance metrics
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_resp_time --instance-name=consumer1 --service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_cpm --instance-name=consumer1 --service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_sla --instance-name=consumer1 --service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    # service endpoint metrics
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_cpm --endpoint-name=/users --service-name=provider-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_resp_time --endpoint-name=/users --service-name=provider-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=endpoint_sla --endpoint-name=/users --service-name=provider-py
      expected: expected/metrics-has-value.yml
    # service relation metrics
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_relation_client_cpm  --service-name=consumer-py --dest-service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_relation_server_cpm  --service-name=consumer-py --dest-service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_relation_client_cpm  --service-name=consumer-py --dest-service-name=provider-py-kafka
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_relation_server_cpm  --service-name=consumer-py --dest-service-name=provider-py-kafka
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_relation_client_cpm  --service-name=e2e-service-consumer --dest-service-name=provider-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_relation_server_cpm  --service-name=e2e-service-consumer --dest-service-name=provider-py
      expected: expected/metrics-has-value.yml
    # service instance relation metrics
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_relation_client_cpm  --instance-name=consumer-py-instance  --service-name=consumer-py --dest-instance-name=consumer1 --dest-service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_relation_server_cpm  --instance-name=consumer-py-instance  --service-name=consumer-py --dest-instance-name=consumer1 --dest-service-name=e2e-service-consumer
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_relation_client_cpm  --instance-name=consumer-py-instance  --service-name=consumer-py --dest-instance-name=provider-py-kafka-instance --dest-service-name=provider-py-kafka
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_relation_server_cpm  --instance-name=consumer-py-instance  --service-name=consumer-py --dest-instance-name=provider-py-kafka-instance --dest-service-name=provider-py-kafka
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_relation_client_cpm  --instance-name=consumer1 --service-name=e2e-service-consumer --dest-instance-name=provider-py-instance --dest-service-name=provider-py
      expected: expected/metrics-has-value.yml
    - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression=service_instance_relation_server_cpm  --instance-name=consumer1 --service-name=e2e-service-consumer --dest-instance-name=provider-py-instance --dest-service-name=provider-py
      expected: expected/metrics-has-value.yml
