---
setup:
  - do:
      cluster.health:
        wait_for_status: yellow

---
"Test Reindex With Unsupported Mode":
  - requires:
      reason: "requires a cluster with the migration reindex feature"
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_migration/reindex
          capabilities: [migration_reindex]
  - do:
      catch: /illegal_argument_exception/
      indices.migrate_reindex:
        body: |
          {
            "mode": "unsupported_mode",
            "source": {
              "index": "my-data-stream"
            }
          }

---
"Test Reindex With Nonexistent Data Stream":
  - requires:
      reason: "requires a cluster with the migration reindex feature"
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_migration/reindex
          capabilities: [migration_reindex]
  - do:
      catch: /resource_not_found_exception/
      indices.migrate_reindex:
        body: |
          {
            "mode": "upgrade",
            "source": {
              "index": "my-data-stream"
            }
          }

  - do:
      catch: /resource_not_found_exception/
      indices.migrate_reindex:
        body: |
          {
            "mode": "upgrade",
            "source": {
              "index": "my-data-stream1,my-data-stream2"
            }
          }


---
"Test Reindex With Bad Data Stream Name":
  - requires:
      reason: "requires a cluster with the migration reindex feature"
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_migration/reindex
          capabilities: [migration_reindex]
  - do:
      catch: /illegal_argument_exception/
      indices.migrate_reindex:
        body: |
          {
            "mode": "upgrade",
            "source": {
              "index": "my-data-stream*"
            }
          }

---
"Test Reindex With Existing Data Stream":
  - requires:
      reason: "requires a cluster with the migration reindex feature"
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_migration/reindex
          capabilities: [migration_reindex]
  - do:
      indices.put_index_template:
        name: my-template1
        body:
          index_patterns: [my-data-stream*]
          template:
            mappings:
              properties:
                '@timestamp':
                  type: date
                'foo':
                  type: keyword
          data_stream: {}

  - do:
      indices.create_data_stream:
        name: my-data-stream
  - is_true: acknowledged

  - do:
      indices.migrate_reindex:
        body: |
          {
            "mode": "upgrade",
            "source": {
              "index": "my-data-stream"
            }
          }
  - match: { acknowledged: true }

  - do:
      indices.cancel_migrate_reindex:
        index: "my-data-stream"
  - match: { acknowledged: true }

  - do:
      catch: /resource_not_found_exception/
      indices.cancel_migrate_reindex:
        index: "my-data-stream"
