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

---
"Test Pipeline With On Failure Block":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "_executed",
                  "value" : true
                }
              },
              {
                "date" : {
                  "field" : "date",
                  "target_field" : "date",
                  "formats" : ["yyyy"]
                }
              },
              {
                "uppercase" : {
                  "field": "field1"
                }
              }
            ],
            "on_failure" : [
              {
                "set" : {
                  "field" : "_failed",
                  "value" : true
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {field1: "value1"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.field1: "value1" }
  - match: { _source._executed: true }
  - match: { _source._failed: true }

---
"Test Pipeline With Nested Processor On Failures":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "rename" : {
                  "field" : "foofield",
                  "target_field" : "field1",
                  "on_failure" : [
                    {
                      "set" : {
                        "field" : "foofield",
                        "value" : "exists"
                      }
                    },
                    {
                      "rename" : {
                        "field" : "foofield2",
                        "target_field" : "field1",
                        "on_failure" : [
                          {
                            "set" : {
                            "field" : "foofield2",
                            "value" : "ran"
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {field1: "value1"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.field1: "value1" }
  - match: { _source.foofield: "exists" }
  - match: { _source.foofield2: "ran" }

---
"Test pipeline with empty on_failure in a processor":
  - do:
      catch: bad_request
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "fail" : {
                  "tag" : "emptyfail",
                  "message" : "_message",
                  "on_failure": []
                }
              }
            ],
            "on_failure": [
              {
                "set" : {
                  "field": "on_failure_executed",
                  "value": true
                }
              }
            ]
          }
  - match: { error.root_cause.0.type: "parse_exception" }
  - match: { error.root_cause.0.reason: "[on_failure] processors list cannot be empty" }
  - match: { error.root_cause.0.processor_type: "fail" }
  - match: { error.root_cause.0.processor_tag: "emptyfail" }
  - match: { error.root_cause.0.property_name: "on_failure" }

---
"Test pipeline with empty on_failure in pipeline":
  - do:
      catch: bad_request
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "foo",
                  "value" : "_message"
                }
              }
            ],
            "on_failure": []
          }
  - match: { error.root_cause.0.type: "parse_exception" }
  - match: { error.root_cause.0.reason: "pipeline [my_pipeline] cannot have an empty on_failure option defined" }

---
"Test pipeline with ignore_failure in a processor":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "fail" : {
                  "message" : "_message",
                  "ignore_failure": true
                }
              },
              {
                "set" : {
                  "field" : "field",
                  "value" : "value"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.field: "value" }
