---
setup:

  - do:
      snapshot.create_repository:
        repository: test_repo_restore_1
        body:
          type: source
          settings:
            delegate_type: fs
            location: "test_repo_restore_1_loc"

  - do:
      indices.create:
        index: test_index
        body:
          settings:
            number_of_shards:   1
            number_of_replicas: 0

  - do:
      cluster.health:
        wait_for_status: green

---
"Create a source only snapshot and then restore it":
  - skip:
      features: ["allowed_warnings"]

  - do:
      index:
          index:  test_index
          id:     "1"
          body:   { foo: bar }
  - do:
      indices.flush:
        index: test_index

  - do:
      snapshot.create:
        repository: test_repo_restore_1
        snapshot: test_snapshot
        wait_for_completion: true
        body: |
          { "indices": "test_index" }

  - match: { snapshot.snapshot: test_snapshot }
  - match: { snapshot.state : SUCCESS }
  - match: { snapshot.shards.successful: 1 }
  - match: { snapshot.shards.failed : 0 }
  - is_true: snapshot.version
  - gt: { snapshot.version_id: 0}

  - do:
      indices.close:
        index : test_index

  - do:
      snapshot.restore:
        repository: test_repo_restore_1
        snapshot: test_snapshot
        wait_for_completion: true

  - do:
      indices.recovery:
        index: test_index

  - match: { test_index.shards.0.type: SNAPSHOT }
  - match: { test_index.shards.0.stage: DONE }
  - match: { test_index.shards.0.translog.recovered: 0}
  - match: { test_index.shards.0.translog.total: 0}
  - match: { test_index.shards.0.translog.total_on_start: 0}
  # for version >= 7.7, there should be 6, previous versions should have 5
  - gte: { test_index.shards.0.index.files.recovered: 5}
  - lte: { test_index.shards.0.index.files.recovered: 6}
  - match: { test_index.shards.0.index.files.reused: 0}
  - match: { test_index.shards.0.index.size.reused_in_bytes: 0}
  - gt: { test_index.shards.0.index.size.recovered_in_bytes: 0}

  - do:
      search:
        rest_total_hits_as_int: true
        index: test_index
        body:
          query:
            match_all: {}

  - match: {hits.total:      1    }
  - length: {hits.hits:      1    }
  - match: {hits.hits.0._id: "1" }

---
"Failed to snapshot indices with synthetic source":
  - skip:
      features: ["allowed_warnings"]
  - requires:
      cluster_features: [ "mapper.source.mode_from_index_setting" ]
      reason: "Source mode configured through index setting"

  - do:
      indices.create:
        index: test_synthetic
        body:
          settings:
            number_of_shards:   1
            number_of_replicas: 0
            index:
              mapping.source.mode: synthetic

  - do:
      snapshot.create:
        repository: test_repo_restore_1
        snapshot: test_snapshot_2
        wait_for_completion: true
        body: |
          { 
            "indices": "test_synthetic",
            "include_global_state": false
          }

  - match: { snapshot.snapshot: test_snapshot_2 }
  - match: { snapshot.state : PARTIAL }
  - match: { snapshot.shards.successful: 0 }
  - match: { snapshot.shards.failed : 1 }
  - match: { snapshot.failures.0.index: "test_synthetic" }
  - match: { snapshot.failures.0.reason : "IllegalStateException[Can't snapshot _source only on an index that has incomplete source ie. has _source disabled or filters the source]" }
  - is_true: snapshot.version
  - gt: { snapshot.version_id: 0}

  - do:
      snapshot.create:
        repository: test_repo_restore_1
        snapshot: test_snapshot_3
        wait_for_completion: true
        body: |
          { 
            "indices": "test_*",
            "include_global_state": false
          }

  - match: { snapshot.snapshot: test_snapshot_3 }
  - match: { snapshot.state : PARTIAL }
  - match: { snapshot.shards.successful: 1 }
  - match: { snapshot.shards.failed : 1 }
  - match: { snapshot.failures.0.index: "test_synthetic" }
  - match: { snapshot.failures.0.reason: "IllegalStateException[Can't snapshot _source only on an index that has incomplete source ie. has _source disabled or filters the source]" }
  - is_true: snapshot.version
  - gt: { snapshot.version_id: 0}
