#
# Tests for gnocchi-threshold-alarm
#
#   user_id        : c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
#   project_id     : 709f6ed6-bfb3-4649-b303-0019a7f6aef2
#   alarm name     : gabbi-gnocchi-threshold-resource-alarm
#   resource_id    : gabbi-mock-resource
#   archive policy : gabbiliveceph
#

defaults:
    request_headers:
        x-auth-token: $ENVIRON['ADMIN_TOKEN']

tests:
    #
    # Setup gnocchi archive policy/resource type/resource/metrics
    #

    - name: SETUP. create archive policy gabbiliveceph
      desc: create archve policy 'gabbiliveceph' for tests
      POST: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/archive_policy
      status: 201
      request_headers:
          content-type: application/json
      data:
          name: gabbiliveceph
          back_window: 0
          definition:
              - granularity: 1 second
                points: 60
              - granularity: 20 second
                timespan: 1 minute
              - points: 5
                timespan: 5 minute
          aggregation_methods:
              - mean
              - min
              - max
      response_json_paths:
          $.name: gabbiliveceph
          $.back_window: 0
          $.aggregation_methods.`len`: 3

    - name: create resource type ceph_account
      desc: needed to create a resource
      POST: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/resource_type
      status: 201 || 409
      request_headers:
          content-type: application/json
      data:
          name: ceph_account

    - name: create resource of ceph_account type
      POST: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/resource/ceph_account
      request_headers:
          content-type: application/json
      data:
          id: 662e46f3-8c06-430c-8a9c-adcaedd1272c
          user_id:      27e342e4-4489-424f-a7e4-ba8ed9ad729c
          project_id:   d6d32769-c351-4758-b0a2-458fa1a065a3
          metrics:
              radosgw.objects:
                  archive_policy_name: gabbiliveceph
              radosgw.objects.size:
                  archive_policy_name: gabbiliveceph
              radosgw.objects.containers:
                  archive_policy_name: gabbiliveceph
              radosgw.api.request:
                  archive_policy_name: gabbiliveceph
              radosgw.containers.objects:
                  archive_policy_name: gabbiliveceph
              radosgw.containers.objects.size:
                  archive_policy_name: gabbiliveceph
      status: 201
      response_json_paths:
          $.user_id:    27e342e4-4489-424f-a7e4-ba8ed9ad729c
          $.project_id: d6d32769-c351-4758-b0a2-458fa1a065a3
          $.metrics.`len`: 6

    #
    # Actual tests
    #

    - name: search 'gabbi-gnocchi-threshold-resource-alarm' alarm doesnt exist
      desc: search for alarm using user_id, project_id, alarm_name
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      query_parameters:
          q.field:
              - user_id
              - project_id
              - name
          q.op:
              - eq
              - eq
              - eq
          q.value:
              - c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
              - 709f6ed6-bfb3-4649-b303-0019a7f6aef2
              - gabbi-gnocchi-threshold-resource-alarm
      method: GET
      response_json_paths:
          $.`len`: 0

    - name: create alarm 'gabbi-gnocchi-threshold-resource-alarm'
      desc: create a threshold alarm gabbi-gnocchi-threshold-resource-alarm
      POST: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      request_headers:
          content-type: application/json
      data:
          alarm_actions:
              - "http://site:8000/gnocchi-threshold-resource"
          alarm_id: null
          description: An gabbilive threshold based alarm
          enabled: true
          insufficient_data_actions:
              - "http://site:8000/nodata-gnocchi-threshold-resource"
          name: "gabbi-gnocchi-threshold-resource-alarm"
          ok_actions:
              - "http://site:8000/ok-gnocchi-threshold-resource"
          project_id: 709f6ed6-bfb3-4649-b303-0019a7f6aef2
          repeat_actions: false
          severity: moderate
          state: "insufficient data"
          type: gnocchi_resources_threshold
          gnocchi_resources_threshold_rule:
              evaluation_periods: 3
              metric: "radosgw.objects.size"
              resource_id: "662e46f3-8c06-430c-8a9c-adcaedd1272c"
              aggregation_method: "mean"
              granularity: 60
              threshold: 5.0
              comparison_operator: "ge"
              resource_type: ceph_account
          user_id: c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
      status: 201
      response_json_paths:
          $.name: gabbi-gnocchi-threshold-resource-alarm
          $.type: gnocchi_resources_threshold
          $.user_id: c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
          $.project_id: 709f6ed6-bfb3-4649-b303-0019a7f6aef2
          $.severity: moderate

    - name: retrieve history about 'gabbi-gnocchi-threshold-resource-alarm' creation
      desc: get history about alarm creation
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.alarm_id']/history
      request_headers:
          content-type: application/json
      poll:
          count: 5
          delay: 2
      response_json_paths:
          $.`len`: 1
          $.[0].type: creation
          $.[0].alarm_id: $RESPONSE['$.alarm_id']

    - name: update severity for alarm 'gabbi-gnocchi-threshold-resource-alarm'
      desc: update severity for alarm gabbi-gnocchi-threshold-resource-alarm
      PUT: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.[0].alarm_id']
      status: 200
      request_headers:
          content-type: application/json
      data:
          alarm_actions:
              - "http://site:8000/gnocchi-threshold-resource"
          alarm_id: null
          description: An gabbilive threshold based alarm
          enabled: true
          insufficient_data_actions:
              - "http://site:8000/nodata-gnocchi-threshold-resource"
          name: "gabbi-gnocchi-threshold-resource-alarm"
          ok_actions:
              - "http://site:8000/ok-gnocchi-threshold-resource"
          project_id: 709f6ed6-bfb3-4649-b303-0019a7f6aef2
          repeat_actions: false
          severity: low
          state: "insufficient data"
          type: gnocchi_resources_threshold
          gnocchi_resources_threshold_rule:
              evaluation_periods: 3
              metric: "radosgw.objects.size"
              resource_id: "662e46f3-8c06-430c-8a9c-adcaedd1272c"
              aggregation_method: "mean"
              granularity: 60
              threshold: 5.0
              comparison_operator: "ge"
              resource_type: ceph_account
          user_id: c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
      response_json_paths:
          $.name: gabbi-gnocchi-threshold-resource-alarm
          $.type: gnocchi_resources_threshold
          $.user_id: c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
          $.project_id: 709f6ed6-bfb3-4649-b303-0019a7f6aef2
          $.severity: low
          $.state: "insufficient data"

    - name: retrieve history for 'gabbi-gnocchi-threshold-resource-alarm'
      desc: get history for rule_change
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.alarm_id']/history
      request_headers:
          content-type: application/json
      poll:
          count: 5
          delay: 2
      response_json_paths:
          $.`len`: 2
          $.[0].type: rule change
          $.[0].alarm_id: $RESPONSE['$.alarm_id']
          $.[0].detail: '{"severity": "low"}'

    - name: update alarm state for 'gabbi-gnocchi-threshold-resource-alarm'
      desc: update state for alarm
      PUT: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.[0].alarm_id']/state
      request_headers:
          content-type: application/json
      data: '"ok"'
      status: 200
      response_strings:
          - "ok"

    - name: search 'gabbi-gnocchi-threshold-resource-alarm' alarm exist
      desc: search for alarm using user_id, project_id, alarm_name
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      query_parameters:
          q.field:
              - user_id
              - project_id
              - name
          q.op:
              - eq
              - eq
              - eq
          q.value:
              - c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
              - 709f6ed6-bfb3-4649-b303-0019a7f6aef2
              - gabbi-gnocchi-threshold-resource-alarm
      poll:
          count: 5
          delay: 2
      response_json_paths:
          $.`len`: 1

    - name: get info about 'gabbi-gnocchi-threshold-resource-alarm' alarm
      desc: access alarm using its ID
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.[0].alarm_id']
      response_json_paths:
          $.alarm_id: $RESPONSE['$.[0].alarm_id']
          $.alarm_actions: ["http://site:8000/gnocchi-threshold-resource"]
          $.name: gabbi-gnocchi-threshold-resource-alarm
          $.gnocchi_resources_threshold_rule.resource_id: "662e46f3-8c06-430c-8a9c-adcaedd1272c"
          $.gnocchi_resources_threshold_rule.metric: "radosgw.objects.size"
          $.gnocchi_resources_threshold_rule.resource_type: "ceph_account"
          $.user_id: c8ecb587-d38c-426f-a58d-22b8d4a9a1d3

    - name: get alarm state for 'gabbi-gnocchi-threshold-resource-alarm'
      desc: get state for alarm
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.alarm_id']/state
      request_headers:
          content-type: application/json
      status: 200
      response_strings:
          - "ok"

    #
    # Teardown
    #

    - name: CLEANUP. search 'gabbi-gnocchi-threshold-resource' alarm exist
      desc: Find alarm id using user_id, project_id, alarm_name
      GET: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      query_parameters:
          q.field:
              - user_id
              - project_id
              - name
          q.op:
              - eq
              - eq
              - eq
          q.value:
              - c8ecb587-d38c-426f-a58d-22b8d4a9a1d3
              - 709f6ed6-bfb3-4649-b303-0019a7f6aef2
              - gabbi-gnocchi-threshold-resource-alarm
      response_json_paths:
          $.`len`: 1

    - name: CLEANUP. delete threshold alarm 'gabbi-gnocchi-threshold-resource'
      DELETE: $ENVIRON['AODH_SERVICE_URL']/v2/alarms/$RESPONSE['$.[0].alarm_id']
      status: 204

    - name: CLEANUP. Get resource by name '662e46f3-8c06-430c-8a9c-adcaedd1272c'
      desc: retrieve resource by 662e46f3-8c06-430c-8a9c-adcaedd1272c to get its ID
      GET: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/resource/generic/662e46f3-8c06-430c-8a9c-adcaedd1272c
      status: 200

    - name: CLEANUP. delete test ceph_resource '662e46f3-8c06-430c-8a9c-adcaedd1272c'
      desc: delete ceph_account resource 662e46f3-8c06-430c-8a9c-adcaedd1272c
      DELETE: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/resource/generic/$RESPONSE['$.id']
      status: 204

    - name: CLEANUP. delete resource type ceph_account
      DELETE: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/resource_type/ceph_account
      status: 204

    - name: CLEANUP. delete archive
      DELETE: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/archive_policy/gabbiliveceph
      status: 204
      xfail: True

