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

- do:
    ingest.delete_pipeline:
      id: "outer"
      ignore: 404

---
"Test Pipeline Processor with Simple Inner Pipeline":
- do:
    ingest.put_pipeline:
      id: "inner"
      body:  >
        {
          "description" : "inner pipeline",
          "processors" : [
            {
              "set" : {
                "field": "foo",
                "value": "bar"
              }
            },
            {
              "set" : {
                "field": "baz",
                "value": "blub"
              }
            }
          ]
        }
- match: { acknowledged: true }

- do:
    ingest.put_pipeline:
      id: "outer"
      body:  >
        {
          "description" : "outer pipeline",
          "processors" : [
            {
              "pipeline" : {
                "name": "inner"
              }
            }
          ]
        }
- match: { acknowledged: true }

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

- do:
    get:
      index: test
      id: "1"
- match: { _source.foo: "bar" }
- match: { _source.baz: "blub" }

---
"Test Pipeline Processor with Circular Pipelines":
- requires:
    cluster_features: ["gte_v8.13.0"]
    reason: exception class changed in 8.13.0
- do:
    ingest.put_pipeline:
      id: "outer"
      body:  >
        {
          "description" : "outer pipeline",
          "processors" : [
            {
              "pipeline" : {
                "name": "inner"
              }
            }
          ]
        }
- match: { acknowledged: true }

- do:
    ingest.put_pipeline:
      id: "inner"
      body:  >
        {
          "description" : "inner pipeline",
          "processors" : [
            {
              "pipeline" : {
                "name": "outer"
              }
            }
          ]
        }
- match: { acknowledged: true }

- do:
    catch: /graph_structure_exception/
    index:
      index: test
      id: "1"
      pipeline: "outer"
      body: {}
- match: { error.root_cause.0.type: "graph_structure_exception" }
- match: { error.root_cause.0.reason: "Cycle detected for pipeline: outer" }

---
"Test Pipeline Processor with templating":
  - do:
      ingest.put_pipeline:
        id: "engineering-department"
        body:  >
          {
            "processors" : [
              {
                "set" : {
                  "field": "manager",
                  "value": "john"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "sales-department"
        body:  >
          {
            "processors" : [
              {
                "set" : {
                  "field": "manager",
                  "value": "jan"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "outer"
        body:  >
          {
            "processors" : [
              {
                "pipeline" : {
                  "name": "{{org}}-department"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "outer"
        body: >
          {
            "org": "engineering"
          }

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.manager: "john" }

  - do:
      index:
        index: test
        id: "2"
        pipeline: "outer"
        body: >
          {
            "org": "sales"
          }

  - do:
      get:
        index: test
        id: "2"
  - match: { _source.manager: "jan" }

  - do:
      catch: /illegal_state_exception/
      index:
        index: test
        id: "3"
        pipeline: "outer"
        body: >
          {
            "org": "legal"
          }
  - match: { error.root_cause.0.type: "illegal_state_exception" }
  - match: { error.root_cause.0.reason: "Pipeline processor configured for non-existent pipeline [legal-department]" }

---
"Test _ingest.pipeline metadata":
  - do:
      ingest.put_pipeline:
        id: "pipeline1"
        body:  >
          {
            "processors" : [
              {
                "append" : {
                  "field": "pipelines",
                  "value": "{{_ingest.pipeline}}"
                }
              },
              {
                "pipeline" : {
                  "name": "another_pipeline"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "another_pipeline"
        body:  >
          {
            "processors" : [
              {
                "append" : {
                  "field": "pipelines",
                  "value": "{{_ingest.pipeline}}"
                }
              },
              {
                "pipeline" : {
                  "name": "another_pipeline2"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "another_pipeline2"
        body:  >
          {
            "processors" : [
              {
                "append" : {
                  "field": "pipelines",
                  "value": "{{_ingest.pipeline}}"
                }
              }
            ]
          }
  - match: { acknowledged: true }

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

  - do:
      get:
        index: test
        id: "1"
  - length: { _source.pipelines: 3 }
  - match: { _source.pipelines.0: "pipeline1" }
  - match: { _source.pipelines.1: "another_pipeline" }
  - match: { _source.pipelines.2: "another_pipeline2" }

---
"Test ignore_missing_pipeline parameter":
  - do:
      ingest.put_pipeline:
        id: "my-pipeline"
        body:  >
          {
            "processors" : [
              {
                "pipeline" : {
                  "name": "non_existing_pipeline",
                  "ignore_missing_pipeline": false
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      catch: /illegal_state_exception/
      index:
        index: test
        id: "1"
        pipeline: "my-pipeline"
        body: {}
  - match: { error.root_cause.0.type: "illegal_state_exception" }
  - match: { error.root_cause.0.reason: "Pipeline processor configured for non-existent pipeline [non_existing_pipeline]" }

  - do:
      ingest.simulate:
        id: "my-pipeline"
        body: >
          {
            "docs": [
              {
                "_index": "index",
                "_id": "id",
                "_source": {
                  "foo": "bar"
                }
              }
            ]
          }
  - match: { docs.0.error.root_cause.0.type: "illegal_state_exception" }
  - match: { docs.0.error.root_cause.0.reason: "Pipeline processor configured for non-existent pipeline [non_existing_pipeline]" }

  - do:
      ingest.put_pipeline:
        id: "my-pipeline"
        body:  >
          {
            "processors" : [
              {
                "pipeline" : {
                  "name": "non_existing_pipeline",
                  "ignore_missing_pipeline": true
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my-pipeline"
        body: {}
  - match: { _index: "test" }
  - match: { _id: "1" }
  - match: { _version: 1 }
  - match: { result: "created" }

  - do:
      ingest.simulate:
        id: "my-pipeline"
        body: >
          {
            "docs": [
              {
                "_index": "index",
                "_id": "id",
                "_source": {
                  "foo": "bar"
                }
              }
            ]
          }
  - match: { docs.0.doc._index: "index" }
  - match: { docs.0.doc._id: "id" }
