mkenv foo
----

# Open foo with default (disabled) WAL failover configuration in
# the context of a single-store node.

open flag= envs=(foo) open=foo
----
OK

mkenv bar
----

# Open foo with default (disabled) WAL failover configuration in
# the context of a two-store node.

open flag= envs=(foo,bar) open=foo
----
OK

# Open foo with WAL failover set to 'among-stores' in the context
# of a single-store node. WAL failover will remain disabled.

open flag=among-stores envs=(foo) open=foo
----
OK

# Open foo with 'disabled' WAL failover in the context of a single-store
# node. This is permitted and WAL failover will remain disbaled.
open flag=disabled envs=(foo) open=foo
----
OK

# Open foo with WAL failover set to 'among-stores' in the context of a two-store
# node. The UnhealthyOperationLatencyThreshold func should return true,
# indicating that Pebble IS allowed to failover to the secondary.

open flag=among-stores envs=(foo,bar) open=foo
----
OK
secondary = bar/auxiliary/wals-among-stores
UnhealthyOperationLatencyThreshold() = (100ms,true)

# Try to open foo with WAL failover default. The engine should
# refuse to open (the operator is required to explicitly disable
# WAL failover).

open flag= envs=(foo,bar) open=foo
----
open error: directory "bar/auxiliary/wals-among-stores" may contain relevant WALs

# Opening with the "disabled" setting should succeed.

open flag=disabled envs=(foo,bar) open=foo
----
OK
WALRecoveryDir: bar/auxiliary/wals-among-stores

# And now foo should be able to be opened without setting
# the disabled flag.

open flag= envs=(foo,bar) open=foo
----
OK

# Specifying an explicit path (in enabled or disabled mode) should
# error in multi-store configurations.

open flag=path=/mnt/data/foo envs=(foo,bar) open=foo
----
open error: storage: cannot use explicit path --wal-failover option with multiple stores

open flag=disabled,prev_path=/mnt/data/foo envs=(foo,bar) open=foo
----
open error: storage: cannot use explicit prev_path --wal-failover option with multiple stores

# The last store should should use the first store's dir as its
# secondary.

open flag=among-stores envs=(foo,bar) open=bar
----
OK
secondary = foo/auxiliary/wals-among-stores
UnhealthyOperationLatencyThreshold() = (100ms,true)

# Disable it.

open flag=disabled envs=(foo,bar) open=bar
----
OK
WALRecoveryDir: foo/auxiliary/wals-among-stores

open flag=disabled envs=(foo,bar) open=foo
----
OK
WALRecoveryDir: bar/auxiliary/wals-among-stores

# Test specifying an explicit path.

open flag=path=bax envs=(foo) open=foo
----
OK
secondary = bax
UnhealthyOperationLatencyThreshold() = (100ms,true)

# Test opening foo again without specifying a WAL failover flag.
# It should fail because the secondary may contain relevant logs.

open flag= envs=(foo) open=foo
----
open error: directory "bax" may contain relevant WALs

# Try to disable it without setting a prev_path. It should fail
# as well.

open flag=disabled envs=(foo) open=foo
----
open error: directory "bax" may contain relevant WALs

# Changing the secondary location without specifying prev_path=bax
# should fail.

open flag=path=baz envs=(foo) open=foo
----
open error: directory "bax" may contain relevant WALs

# Changing the secondary location WITH specifying prev_path=bax
# should succeed.

open flag=path=baz,prev_path=bax envs=(foo) open=foo
----
OK
secondary = baz
UnhealthyOperationLatencyThreshold() = (100ms,true)
WALRecoveryDir: bax

# Disabling WAL failover WITH specifying prev_path should succeed too.

open flag=disabled,prev_path=baz envs=(foo) open=foo
----
OK
WALRecoveryDir: baz

# Make an env with encryption-at-rest.

mkenv ear-a encrypted-at-rest
----

# Ensure that we error if an explicit path is not encrypted.

open flag=path=baz envs=(ear-a) open=ear-a
----
open error: must provide --enterprise-encryption flag for "baz", used as WAL failover path for encrypted store "ear-a"

# And succeed if it is.

open flag=path=baz envs=(ear-a) open=ear-a path-encrypted
----
OK
secondary = baz
UnhealthyOperationLatencyThreshold() = (100ms,true)

# Disabling should error if no encryption flags for prev_path is provided.

open flag=disabled,prev_path=baz envs=(ear-a) open=ear-a
----
open error: must provide --enterprise-encryption flag for "baz", used as WAL failover path for encrypted store "ear-a"

# And succeed if they are.

open flag=disabled,prev_path=baz envs=(ear-a) open=ear-a prev-path-encrypted
----
OK
WALRecoveryDir: baz

# Attempt to use 'among-stores' with an encrypted store and an unencrypted store.

open flag=among-stores envs=(ear-a, foo) open=foo
----
open error: storage: must provide --enterprise-encryption flag for all stores or none if using WAL failover

# Ensure we perform the same check when epening the encrypted store.

open flag=among-stores envs=(ear-a, foo) open=ear-a
----
open error: storage: must provide --enterprise-encryption flag for all stores or none if using WAL failover

# Using among-stores with all encrypted envs should succeed.

mkenv ear-b encrypted-at-rest
----

open flag=among-stores envs=(ear-a, ear-b) open=ear-a
----
OK
secondary = ear-b/auxiliary/wals-among-stores
UnhealthyOperationLatencyThreshold() = (100ms,true)

open flag=disabled envs=(ear-a, ear-b) open=ear-a
----
OK
WALRecoveryDir: ear-b/auxiliary/wals-among-stores

# Open ear-a using an explicit path.

open flag=path=bax envs=(ear-a) open=ear-a path-encrypted
----
OK
secondary = bax
UnhealthyOperationLatencyThreshold() = (100ms,true)

# Switching the explicit path without specifying the previous path's
# encryption options should error.

open flag=path=baz,prev_path=bax envs=(ear-a) open=ear-a path-encrypted
----
open error: must provide --enterprise-encryption flag for "bax", used as WAL failover path for encrypted store "ear-a"

# Switching the explicit path without specifying the new path's
# encryption options should error.

open flag=path=baz,prev_path=bax envs=(ear-a) open=ear-a prev-path-encrypted
----
open error: must provide --enterprise-encryption flag for "baz", used as WAL failover path for encrypted store "ear-a"

# Providing both should succeed.

open flag=path=baz,prev_path=bax envs=(ear-a) open=ear-a path-encrypted prev-path-encrypted
----
OK
secondary = baz
UnhealthyOperationLatencyThreshold() = (100ms,true)
WALRecoveryDir: bax

# Opening an unencrypted store with an encrypted WAL failover
# path should error.

open flag=path=bax envs=(foo) open=foo path-encrypted
----
open error: must provide --enterprise-encryption flag for store "foo", specified WAL failover path "bax" is encrypted
