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

tests:
    - name: list alarms none
      desc: Lists alarms, none yet exist
      url: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      method: GET
      response_strings:
          - "[]"

    - name: list servers none
      desc: List servers, none yet exists
      url: $ENVIRON['NOVA_SERVICE_URL']/servers
      method: GET
      response_strings:
          - "[]"

    - name: create stack
      desc: Create an autoscaling stack
      url: $ENVIRON['HEAT_SERVICE_URL']/stacks
      method: POST
      request_headers:
          content-type: application/json
      data: <@create_stack.json
      status: 201

    - name: control stack status
      desc: Checks the stack have been created successfully
      url: $ENVIRON['HEAT_SERVICE_URL']/stacks/integration_test
      redirects: true
      method: GET
      status: 200
      poll:
          count: 300
          delay: 1
      response_json_paths:
          $.stack.stack_status: "CREATE_COMPLETE"

    - name: list servers grow
      desc: Wait the autoscaling stack grow to two servers
      url: $ENVIRON['NOVA_SERVICE_URL']/servers/detail
      method: GET
      poll:
          count: 600
          delay: 1
      response_json_paths:
          $.servers[0].metadata.'metering.server_group': $RESPONSE['$.stack.id']
          $.servers[1].metadata.'metering.server_group': $RESPONSE['$.stack.id']
          $.servers[0].status: ACTIVE
          $.servers[1].status: ACTIVE
          $.servers.`len`: 2

    - name: check gnocchi resources
      desc: Check the gnocchi resources for this two servers exists
      url: $ENVIRON['GNOCCHI_SERVICE_URL']/v1/resource/instance
      method: GET
      poll:
          count: 30
          delay: 1
      response_strings:
          - '"id": "$RESPONSE["$.servers[0].id"]"'
          - '"id": "$RESPONSE["$.servers[1].id"]"'

    - name: check event
      desc: Check panko for new instance.create.end event
      url: $ENVIRON['PANKO_SERVICE_URL']/v2/events
      method: GET
      request_headers:
          content-type: application/json
      data:
          q:
            - field: event_type
              op: eq
              type: string
              value: compute.instance.create.end
            - field: resource_id
              op: eq
              type: string
              value: $HISTORY['list servers grow'].$RESPONSE['$.servers[0].id']
      poll:
          count: 30
          delay: 1
      response_json_paths:
          $.`len`: 1
          $[0].event_type: compute.instance.create.end
          $[0].traits[?(@.name='resource_id')].value: $HISTORY['list servers grow'].$RESPONSE['$.servers[0].id']

    - name: check alarm
      desc: Check the aodh alarm and its state
      url: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      method: GET
      poll:
          count: 30
          delay: 1
      response_strings:
          - "integration_test-cpu_alarm_high-"
      response_json_paths:
          $[0].state: alarm

    - name: get stack location for update
      desc: Get the stack location
      url: $ENVIRON['HEAT_SERVICE_URL']/stacks/integration_test
      method: GET
      status: 302

    - name: update stack
      desc: Update an autoscaling stack
      url: $LOCATION
      method: PUT
      request_headers:
          content-type: application/json
      data: <@update_stack.json
      status: 202

    - name: control stack status
      desc: Checks the stack have been created successfully
      url: $ENVIRON['HEAT_SERVICE_URL']/stacks/integration_test
      redirects: true
      method: GET
      status: 200
      poll:
          count: 300
          delay: 1
      response_json_paths:
          $.stack.stack_status: "UPDATE_COMPLETE"

    - name: list servers shrink
      desc: Wait the autoscaling stack shrink to one server
      url: $ENVIRON['NOVA_SERVICE_URL']/servers/detail
      method: GET
      poll:
          count: 600
          delay: 1
      response_json_paths:
          $.servers[0].metadata.'metering.server_group': $RESPONSE['$.stack.id']
          $.servers[0].status: ACTIVE
          $.servers.`len`: 1

    - name: get stack location
      desc: Get the stack location
      url: $ENVIRON['HEAT_SERVICE_URL']/stacks/integration_test
      method: GET
      status: 302

    - name: delete stack
      desc: Delete the stack
      url: $LOCATION
      method: DELETE
      status: 204

    - name: get deleted stack
      desc: Check the stack have been deleted
      url: $ENVIRON['HEAT_SERVICE_URL']/stacks/integration_test
      redirects: true
      method: GET
      poll:
          count: 300
          delay: 1
      status: 404

    - name: list alarms deleted
      desc: List alarms, no more exist
      url: $ENVIRON['AODH_SERVICE_URL']/v2/alarms
      method: GET
      response_strings:
          - "[]"

    - name: list servers deleted
      desc: List servers, no more exists
      url: $ENVIRON['NOVA_SERVICE_URL']/servers
      method: GET
      response_strings:
          - "[]"
