---
setup:

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

  - do:
      snapshot.create_repository:
        repository: test_repo_readonly
        body:
          type: fs
          settings:
            readonly: true
            location: "test_repo_loc"

  - do:
      snapshot.create_repository:
        repository: test_repo_slow
        body:
          type: fs
          settings:
            max_snapshot_bytes_per_sec: "1b"
            location: "test_repo_loc"

---
"Analysis fails on readonly repositories":
  - requires:
      cluster_features: "gte_v7.14.0"
      reason: "abortWrites flag introduced in 7.14, and mixed-cluster support not required"

  - do:
      catch: bad_request
      snapshot.repository_analyze:
        repository: test_repo_readonly

  - match: { status: 400 }
  - match: { error.type: illegal_argument_exception }
  - match: { error.reason: "repository [test_repo_readonly] is read-only" }


---
"Analysis without details":
  - requires:
      cluster_features: "gte_v7.14.0"
      reason: "abortWrites flag introduced in 7.14, and mixed-cluster support not required"

  - do:
      snapshot.repository_analyze:
        repository: test_repo
        blob_count: 10
        concurrency: 5
        max_blob_size: 1mb
        read_node_count: 2
        early_read_node_count: 1
        rare_action_probability: 0.01
        max_total_data_size: 5mb

  - is_true: coordinating_node.id
  - is_true: coordinating_node.name
  - match: { repository:    test_repo }
  - match: { blob_count:    10 }
  - match: { concurrency:   5 }
  - match: { read_node_count: 2 }
  - match: { early_read_node_count: 1 }
  - match: { rare_action_probability: 0.01 }
  - match: { max_blob_size: 1mb }
  - match: { max_blob_size_bytes: 1048576 }
  - match: { max_total_data_size: 5mb }
  - match: { max_total_data_size_bytes: 5242880 }
  - is_true: seed
  - is_true: blob_path
  - is_false: details
  - is_true: listing_elapsed
  - is_true: delete_elapsed
  - gte: { listing_elapsed_nanos: 0}
  - gte: { delete_elapsed_nanos:  0}
  - match: { summary.write.count: 10}
  - gte: { summary.write.total_size_bytes: 0}
  - is_true: summary.write.total_size
  - gte: { summary.write.total_throttled_nanos: 0}
  - is_true: summary.write.total_throttled
  - gte: { summary.write.total_elapsed_nanos: 0}
  - is_true: summary.write.total_elapsed

  # extraordinarily unlikely but in theory up to half of the writes could be copies, and each of those writes could
  # attempt the copy early, fail with a NoSuchFileException, and thus not attempt to read the copy, so we can only
  # assert this:
  - gte: { summary.read.count: 5}
  - gte: { summary.read.total_size_bytes: 0}
  - is_true: summary.read.total_size
  - gte: { summary.read.total_wait_nanos: 0}
  - is_true: summary.read.total_wait
  - gte: { summary.read.max_wait_nanos: 0}
  - is_true: summary.read.max_wait
  - gte: { summary.read.total_throttled_nanos: 0}
  - is_true: summary.read.total_throttled
  - gte: { summary.read.total_elapsed_nanos: 0}
  - is_true: summary.read.total_elapsed

---
"Analysis with details":
  - requires:
      cluster_features: "gte_v7.14.0"
      reason: "abortWrites flag introduced in 7.14, and mixed-cluster support not required"

  - do:
      snapshot.repository_analyze:
        repository: test_repo
        blob_count: 10
        concurrency: 5
        max_blob_size: 1mb
        detailed: true

  - is_true: coordinating_node.id
  - is_true: coordinating_node.name
  - match: { repository:    test_repo }
  - match: { blob_count:    10 }
  - match: { concurrency:   5 }
  - match: { read_node_count: 10 }
  - match: { early_read_node_count: 2 }
  - match: { rare_action_probability: 0.02 }
  - match: { max_blob_size: 1mb }
  - is_true: seed
  - is_true: blob_path
  - is_true: details
  - is_true: listing_elapsed
  - is_true: delete_elapsed
  - gte: { listing_elapsed_nanos: 0}
  - gte: { delete_elapsed_nanos:  0}

---
"Analysis with ?human=false":
  - requires:
      cluster_features: "gte_v7.14.0"
      reason: "abortWrites flag introduced in 7.14, and mixed-cluster support not required"

  - do:
      snapshot.repository_analyze:
        repository: test_repo
        blob_count: 10
        concurrency: 5
        max_blob_size: 1mb
        detailed: false
        human: false

  - is_false: listing_elapsed
  - is_false: delete_elapsed
  - is_false: summary.write.total_size
  - is_false: summary.write.total_throttled
  - is_false: summary.write.total_elapsed
  - is_false: summary.read.total_size
  - is_false: summary.read.total_wait
  - is_false: summary.read.max_wait
  - is_false: summary.read.total_throttled
  - is_false: summary.read.total_elapsed

---
"Timeout with large blobs":
  - requires:
      cluster_features: "gte_v7.14.0"
      reason: "abortWrites flag introduced in 7.14, and mixed-cluster support not required"

  - do:
      catch: request
      snapshot.repository_analyze:
        repository: test_repo_slow
        blob_count: 1
        concurrency: 1
        max_blob_size: 2gb
        max_total_data_size: 2gb
        detailed: false
        human: false
        timeout: 1s

  - match: { status: 500 }
  - match: { error.type: repository_verification_exception }
  - match: { error.reason: "/.*test_repo_slow..Repository.analysis.timed.out.*/" }
  - match: { error.root_cause.0.type: repository_verification_exception }
  - match: { error.root_cause.0.reason: "/.*test_repo_slow..analysis.timed.out.after..1s.*/" }
