---
"Test webhook action with mustache integration":
  - do:
      cluster.health:
          wait_for_status: yellow

  # extract http host and port from master node
  - do:
      cluster.state: {}
  - set: { master_node: master }

  - do:
      nodes.info:
        metric: [ http ]
  - is_true: nodes.$master.http.publish_address
  - set: { nodes.$master.http.publish_address: host }

  - do:
      ingest.simulate:
        body:
          pipeline:
            description: _description
            processors: [ grok: { field: host, patterns : ["%{IPORHOST:hostname}:%{NUMBER:port:int}"]} ]
          docs: [ { _index: index, _id: id, _source: { host: $host } } ]
  - set: { docs.0.doc._source.hostname: hostname }
  - set: { docs.0.doc._source.port: port }

  - do:
      watcher.put_watch:
        id: "test_watch"
        body:
          trigger:
            schedule:
              interval: 1s
          input:
            simple:
              key: value
          condition:
            always: {}
          actions:
            output:
              webhook:
                method: PUT
                host: $hostname
                port: $port
                path: "/my_index/_doc/{{ctx.watch_id}}"
                body:
                  source:
                    watch_id: "{{ctx.watch_id}}"
                  params: {}

  - match: { _id: "test_watch" }
  - match: { created: true }

  - do:
      watcher.execute_watch:
        id: test_watch
        body:
          record_execution: true
          action_modes:
            _all: execute

  - match: { watch_record.state: "executed" }
  - match: { watch_record.result.actions.0.status: "success" }

  - do:
      get:
        index: my_index
        id:    test_watch

  - match: { _source:  { watch_id: "test_watch" } }
