---
teardown:
  - do:
      ingest.delete_pipeline:
        id: "my_pipeline"
        ignore: 404

---
"Test Grok Pipeline":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "grok" : {
                  "field" : "field1",
                  "patterns" : ["%{NUMBER:val:float} %{NUMBER:status:int} <%{WORD:msg}>"]
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {field1: "123.42 400 <foo>"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.val: 123.42 }
  - match: { _source.status: 400 }
  - match: { _source.msg: "foo" }

---
"Test Grok Pipeline With Custom Pattern":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "grok" : {
                  "field" : "field1",
                  "patterns" : ["<%{MY_PATTERN:msg}>"],
                  "pattern_definitions" : {
                    "MY_PATTERN" : "foo"
                  }
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {field1: "<foo>"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.msg: "foo" }

---
"Test Grok Pipeline With Custom Pattern Sharing Same Name As Another":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "grok" : {
                  "field" : "field1",
                  "patterns" : ["<%{NUMBER:msg}>"],
                  "pattern_definitions" : {
                    "NUMBER" : "foo"
                  }
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {field1: "<foo>"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.msg: "foo" }

---
"Test simulate with grok debugging enabled":
  - do:
      ingest.simulate:
        body: >
          {
            "pipeline": {
              "description": "_description",
              "processors": [
                {
                  "grok" : {
                    "field" : "field",
                    "patterns" : ["%{ONE:one}", "%{TWO:two}"],
                    "pattern_definitions" : {
                      "ONE" : "1",
                      "TWO" : "2"
                    },
                    "trace_match" : true
                  }
                }
              ]
            },
            "docs": [
              {
                "_index": "index",
                "_id": "id",
                "_source": {
                  "field": "abc2xyz"
                }
              }
            ]
          }
  - length: { docs: 1 }
  - match: { docs.0.doc._source.field: "abc2xyz" }
  - match: { docs.0.doc._source.two: "2" }
  - length: { docs.0.doc._ingest: 2 }
  - match: { docs.0.doc._ingest._grok_match_index: "1" }
  - is_true: docs.0.doc._ingest.timestamp

---
"Test Grok Patterns Retrieval":
  - do:
      ingest.processor_grok: {}
  - length: { patterns: 320 }
  - match: { patterns.PATH: "(?:%{UNIXPATH}|%{WINPATH})" }


---
"Test simulate with invalid GROK pattern":
  - requires:
        cluster_features: [ "simulate.ingest.400_on_failure" ]
        reason: "simulate.ingest returned 500 on failure before"
  - skip:
        features: headers
  - do:
      catch: bad_request
      headers:
        Content-Type: application/json
      simulate.ingest:
        pipeline: "invalid-grok"
        body: >
          {
            "docs": [
              {
                "_index": "index-1",
                "_source": {
                  "foo": "bar"
                }
              }
            ],
            "pipeline_substitutions": {
              "invalid-grok": {
                "description": "invalid grok pattern",
                "processors": [
                  {
                    "grok": {
                      "field": "field",
                      "patterns": [
                        "%{INVALID_PATTERN:field}"
                      ]
                    }
                  }
                ]
              }
            }
          }
  - match: { status: 400 }
  - match: { error.reason: "[patterns] Invalid regex pattern found in: [%{INVALID_PATTERN:field}]. Unable to find pattern [INVALID_PATTERN] in Grok's pattern dictionary" }
  - match: { error.property_name: "patterns" }
  - match: { error.processor_type: "grok" }
