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

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

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

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

---
"Test index with final pipeline":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "bytes" : {
                  "field" : "bytes_source_field",
                  "target_field" : "bytes_target_field"
                }
              }
            ]
          }
  - match: { acknowledged: true }
  # final pipeline via index
  - do:
      indices.create:
        index: test
        body:
          settings:
            index:
              final_pipeline: "my_pipeline"
          aliases:
            test_alias: {}

  - do:
      index:
        index: test
        id: "1"
        body: {bytes_source_field: "1kb"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.bytes_source_field: "1kb" }
  - match: { _source.bytes_target_field: 1024 }
  # final pipeline via alias
  - do:
      index:
        index: test_alias
        id: "2"
        body: {bytes_source_field: "1kb"}

  - do:
      get:
        index: test
        id: "2"
  - match: { _source.bytes_source_field: "1kb" }
  - match: { _source.bytes_target_field: 1024 }
  # final pipeline via upsert
  - do:
      update:
        index: test
        id: "3"
        body:
          script:
            source: "ctx._source.ran_script = true"
            lang: "painless"
          upsert: { "bytes_source_field":"1kb" }
  - do:
      get:
        index: test
        id: "3"
  - match: { _source.bytes_source_field: "1kb" }
  - match: { _source.bytes_target_field: 1024 }
  # final pipeline via scripted upsert
  - do:
      update:
        index: test
        id: "4"
        body:
          script:
            source: "ctx._source.bytes_source_field = '1kb'"
            lang: "painless"
          upsert : {}
          scripted_upsert: true
  - do:
      get:
        index: test
        id: "4"
  - match: { _source.bytes_source_field: "1kb" }
  - match: { _source.bytes_target_field: 1024 }
  # final pipeline via doc_as_upsert
  - do:
      update:
        index: test
        id: "5"
        body:
          doc: { "bytes_source_field":"1kb" }
          doc_as_upsert: true
  - do:
      get:
        index: test
        id: "5"
  - match: { _source.bytes_source_field: "1kb" }
  - match: { _source.bytes_target_field: 1024 }
  # final pipeline via bulk upsert
  # note - bulk scripted upsert's execute the pipeline before the script, so any data referenced by the pipeline
  # needs to be in the upsert, not the script
  - do:
      bulk:
        refresh: true
        body: |
          {"update":{"_id":"6","_index":"test"}}
          {"script":"ctx._source.ran_script = true","upsert":{"bytes_source_field":"1kb"}}
          {"update":{"_id":"7","_index":"test"}}
          {"doc":{"bytes_source_field":"2kb"}, "doc_as_upsert":true}
          {"update":{"_id":"8","_index":"test"}}
          {"script": "ctx._source.ran_script = true","upsert":{"bytes_source_field":"3kb"}, "scripted_upsert" : true}
          {"update":{"_id":"6_alias","_index":"test_alias"}}
          {"script":"ctx._source.ran_script = true","upsert":{"bytes_source_field":"1kb"}}
          {"update":{"_id":"7_alias","_index":"test_alias"}}
          {"doc":{"bytes_source_field":"2kb"}, "doc_as_upsert":true}
          {"update":{"_id":"8_alias","_index":"test_alias"}}
          {"script": "ctx._source.ran_script = true","upsert":{"bytes_source_field":"3kb"}, "scripted_upsert" : true}

  - do:
      mget:
        body:
          docs:
            - { _index: "test", _id: "6" }
            - { _index: "test", _id: "7" }
            - { _index: "test", _id: "8" }
            - { _index: "test", _id: "6_alias" }
            - { _index: "test", _id: "7_alias" }
            - { _index: "test", _id: "8_alias" }
  - match: { docs.0._index: "test" }
  - match: { docs.0._id: "6" }
  - match: { docs.0._source.bytes_source_field: "1kb" }
  - match: { docs.0._source.bytes_target_field: 1024 }
  - is_false: docs.0._source.ran_script
  - match: { docs.1._index: "test" }
  - match: { docs.1._id: "7" }
  - match: { docs.1._source.bytes_source_field: "2kb" }
  - match: { docs.1._source.bytes_target_field: 2048 }
  - match: { docs.2._index: "test" }
  - match: { docs.2._id: "8" }
  - match: { docs.2._source.bytes_source_field: "3kb" }
  - match: { docs.2._source.bytes_target_field: 3072 }
  - match: { docs.2._source.ran_script: true }
  - match: { docs.3._index: "test" }
  - match: { docs.3._id: "6_alias" }
  - match: { docs.3._source.bytes_source_field: "1kb" }
  - match: { docs.3._source.bytes_target_field: 1024 }
  - is_false: docs.3._source.ran_script
  - match: { docs.4._index: "test" }
  - match: { docs.4._id: "7_alias" }
  - match: { docs.4._source.bytes_source_field: "2kb" }
  - match: { docs.4._source.bytes_target_field: 2048 }
  - match: { docs.5._index: "test" }
  - match: { docs.5._id: "8_alias" }
  - match: { docs.5._source.bytes_source_field: "3kb" }
  - match: { docs.5._source.bytes_target_field: 3072 }
  - match: { docs.5._source.ran_script: true }

  # Delete the index
  - do:
      indices.delete:
        index: test

---
"Test final pipeline when target index is changed":
  - do:
      ingest.put_pipeline:
        id: "final_pipeline_1"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "final_pipeline_1",
                  "value" : true
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "final_pipeline_2"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "final_pipeline_2",
                  "value" : true
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "change_target_index"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "_index",
                  "value" : "index_with_final_pipeline_2"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      indices.create:
        index: index_with_final_pipeline_1
        body:
          settings:
            index:
              final_pipeline: "final_pipeline_1"

  - do:
      indices.create:
        index: index_with_final_pipeline_2
        body:
          settings:
            index:
              final_pipeline: "final_pipeline_2"

  # changing the target index will change the final pipeline to the re-targeted index
  - do:
      index:
        index: index_with_final_pipeline_1
        id: "1"
        pipeline: "change_target_index"
        body: {foo: "bar"}

  # document not present in index that was replaced with re-targeted index
  - do:
      catch: missing
      get:
        index: index_with_final_pipeline_1
        id: "1"
  - match: { found: false }

  # document present in re-targeted index and re-targeted index's final pipeline was executed
  - do:
      get:
        index: index_with_final_pipeline_2
        id: "1"
  - match: { _source.foo: "bar" }
  - match: { _source.final_pipeline_2: true }
  - is_false: _source.final_pipeline_1

  # Delete the index
  - do:
      indices.delete:
        index: index_with_final_pipeline_1

  - do:
      indices.delete:
        index: index_with_final_pipeline_2
