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

---
"Test index with default 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 }
# default pipeline via index
  - do:
      indices.create:
        index: test
        body:
          settings:
            index:
              default_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 }
# default 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 }
# default 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 }
# default 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 }
# default 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 }
# default 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 }

# explicit no default pipeline
  - do:
      index:
        index: test
        id: "9"
        pipeline: "_none"
        body: {bytes_source_field: "1kb"}

  - do:
      get:
        index: test
        id: "9"
  - match: { _source.bytes_source_field: "1kb" }
  - is_false: _source.bytes_target_field
# bad request
  - do:
      catch:  bad_request
      index:
        index: test
        id: "10"
        pipeline: ""
        body: {bytes_source_field: "1kb"}

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