---
setup:

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

  - do:
      bulk:
        body:
          - index:
              _index: docs
              _id:    "1"
          - field: foo
          - index:
              _index: docs
              _id:    "2"
          - field: bar
          - index:
              _index: docs
              _id:    "3"
          - field: baz

  - do:
      snapshot.create_repository:
        repository: repository-fs
        body:
          type: fs
          settings:
            location: "repository-fs"

  # Remove the snapshot if a previous test failed to delete it.
  # Useful for third party tests that runs the test against a real external service.
  - do:
      snapshot.delete:
        repository: repository-fs
        snapshot: snapshot
        ignore: 404

  - do:
      snapshot.create:
        repository: repository-fs
        snapshot: snapshot
        wait_for_completion: true

  - do:
      indices.delete:
        index: docs

---
"Delete repository when a snapshot is mounted as an index":
  - requires:
      cluster_features: ["gte_v8.0.0"]
      reason: "starting 8.0, an error occurs when a repository used by snapshot backed indices is deleted"

  - do:
      searchable_snapshots.mount:
        repository: repository-fs
        snapshot: snapshot
        wait_for_completion: true
        body:
          index: docs
          renamed_index: mounted-docs

  - match: { snapshot.snapshot: snapshot }
  - match: { snapshot.shards.failed: 0 }
  - match: { snapshot.shards.successful: 1 }

  # Returns a repository conflict exception
  - do:
      catch: conflict
      snapshot.delete_repository:
        repository: repository-fs

  - do:
      search:
        index: mounted-docs
        body:
          query:
            match_all: { }

  - match: { hits.total.value: 3 }

  - do:
      indices.delete:
        index: mounted-docs

  - do:
      snapshot.delete_repository:
        repository: repository-fs

