include(AddFdbTest)

# We need some variables to configure the test setup
set(ENABLE_BUGGIFY ON CACHE BOOL "Enable buggify for tests")
set(RUN_IGNORED_TESTS OFF CACHE BOOL "Run tests that are marked for ignore")
set(TEST_KEEP_LOGS "FAILED" CACHE STRING "Which logs to keep (NONE, FAILED, ALL)")
set(TEST_KEEP_SIMDIR "NONE" CACHE STRING "Which simfdb directories to keep (NONE, FAILED, ALL)")
set(TEST_AGGREGATE_TRACES "NONE" CACHE STRING "Create aggregated trace files (NONE, FAILED, ALL)")
set(TEST_LOG_FORMAT "xml" CACHE STRING "Format for test trace files (xml, json)")
set(TEST_INCLUDE ".*" CACHE STRING "Include only tests that match the given regex")
set(TEST_EXCLUDE ".^" CACHE STRING "Exclude all tests matching the given regex")

# for the restart test we optimally want to use the last stable fdbserver
# to test upgrades
if(WITH_PYTHON)
  find_program(OLD_FDBSERVER_BINARY
    fdbserver
    HINTS /opt/foundationdb/old /usr/sbin /usr/libexec /usr/local/sbin /usr/local/libexec)
  if(OLD_FDBSERVER_BINARY)
    message(STATUS "Use old fdb at ${OLD_FDBSERVER_BINARY}")
  else()
    set(fdbserver_location ${CMAKE_BINARY_DIR}/bin/fdbserver)
    set(OLD_FDBSERVER_BINARY ${fdbserver_location} CACHE FILEPATH "Old fdbserver binary" FORCE)
    message(WARNING "\
  No old fdbserver binary found - using ${fdbserver_location} \
  It is recommended to install the current stable version from https://www.foundationdb.org/download/ \
  Or provide a path to another fdbserver")
  endif()

  set(TestRunner "${PROJECT_SOURCE_DIR}/tests/TestRunner/TestRunner.py")

  configure_file(${PROJECT_SOURCE_DIR}/tests/CTestCustom.ctest ${PROJECT_BINARY_DIR}/CTestCustom.ctest @ONLY)

  configure_testing(TEST_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
                    ERROR_ON_ADDITIONAL_FILES
                    IGNORE_PATTERNS ".*/CMakeLists.txt")

  add_fdb_test(TEST_FILES AsyncFileCorrectness.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES AsyncFileMix.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES AsyncFileRead.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES AsyncFileReadRandom.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES AsyncFileWrite.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES BackupContainers.txt IGNORE)
  add_fdb_test(TEST_FILES BandwidthThrottle.txt IGNORE)
  add_fdb_test(TEST_FILES BigInsert.txt IGNORE)
  add_fdb_test(TEST_FILES ConsistencyCheck.txt IGNORE)
  add_fdb_test(TEST_FILES DDMetricsExclude.txt IGNORE)
  add_fdb_test(TEST_FILES DataDistributionMetrics.txt IGNORE)
  add_fdb_test(TEST_FILES DiskDurability.txt IGNORE)
  add_fdb_test(TEST_FILES FileSystem.txt IGNORE)
  add_fdb_test(TEST_FILES Happy.txt IGNORE)
  add_fdb_test(TEST_FILES Mako.txt IGNORE)
  add_fdb_test(TEST_FILES IncrementalDelete.txt IGNORE)
  add_fdb_test(TEST_FILES KVStoreMemTest.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES KVStoreReadMostly.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES KVStoreTest.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES KVStoreTestRead.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES KVStoreTestWrite.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES KVStoreValueSize.txt UNIT IGNORE)
  add_fdb_test(TEST_FILES LayerStatusMerge.txt IGNORE)
  add_fdb_test(TEST_FILES ParallelRestoreApiCorrectnessAtomicRestore.txt IGNORE)
  add_fdb_test(TEST_FILES PureNetwork.txt IGNORE)
  add_fdb_test(TEST_FILES RRW2500.txt IGNORE)
  add_fdb_test(TEST_FILES RandomRead.txt IGNORE)
  add_fdb_test(TEST_FILES RandomRangeRead.txt IGNORE)
  add_fdb_test(TEST_FILES RandomReadWrite.txt IGNORE)
  add_fdb_test(TEST_FILES ReadAbsent.txt IGNORE)
  add_fdb_test(TEST_FILES ReadAfterWrite.txt IGNORE)
  add_fdb_test(TEST_FILES ReadHalfAbsent.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodCorrectnessUnits.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodCorrectnessBTree.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodCorrectnessPager.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodCorrectness.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodPerfTests.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodPerfSet.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodPerfPrefixCompression.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodPerfSequentialInsert.txt IGNORE)
  add_fdb_test(TEST_FILES RedwoodPerfRandomRangeScans.txt IGNORE)
  add_fdb_test(TEST_FILES RocksDBTest.txt IGNORE)
  add_fdb_test(TEST_FILES S3BlobStore.txt IGNORE)
  add_fdb_test(TEST_FILES SampleNoSimAttrition.txt IGNORE)
  if (NOT USE_UBSAN) # TODO re-enable in UBSAN after https://github.com/apple/foundationdb/issues/2410 is resolved
    add_fdb_test(TEST_FILES SimpleExternalTest.txt)
  else()
    add_fdb_test(TEST_FILES SimpleExternalTest.txt IGNORE)
  endif()
  add_fdb_test(TEST_FILES SlowTask.txt IGNORE)
  add_fdb_test(TEST_FILES SpecificUnitTest.txt IGNORE)
  add_fdb_test(TEST_FILES StorageMetricsSampleTests.txt IGNORE)
  add_fdb_test(TEST_FILES StreamingWrite.txt IGNORE)
  add_fdb_test(TEST_FILES ThreadSafety.txt IGNORE)
  add_fdb_test(TEST_FILES TraceEventMetrics.txt IGNORE)
  add_fdb_test(TEST_FILES PopulateTPCC.txt IGNORE)
  add_fdb_test(TEST_FILES TPCC.txt IGNORE)
  add_fdb_test(TEST_FILES default.txt IGNORE)
  add_fdb_test(TEST_FILES errors.txt IGNORE)
  add_fdb_test(TEST_FILES fail.txt IGNORE)
  add_fdb_test(TEST_FILES killall.txt IGNORE)
  add_fdb_test(TEST_FILES latency.txt IGNORE)
  add_fdb_test(TEST_FILES performance-fs.txt IGNORE)
  add_fdb_test(TEST_FILES performance.txt IGNORE)
  add_fdb_test(TEST_FILES ping.TXT IGNORE)
  add_fdb_test(TEST_FILES pingServers.TXT IGNORE)
  add_fdb_test(TEST_FILES pt.TXT IGNORE)
  add_fdb_test(TEST_FILES randomSelector.txt IGNORE)
  add_fdb_test(TEST_FILES selectorCorrectness.txt IGNORE)
  add_fdb_test(TEST_FILES fast/AtomicBackupCorrectness.toml)
  add_fdb_test(TEST_FILES fast/AtomicBackupToDBCorrectness.toml)
  add_fdb_test(TEST_FILES fast/AtomicOps.toml)
  add_fdb_test(TEST_FILES fast/AtomicOpsApiCorrectness.toml)
  add_fdb_test(TEST_FILES fast/BackupAzureBlobCorrectness.toml IGNORE)
  add_fdb_test(TEST_FILES fast/BackupS3BlobCorrectness.toml IGNORE)
  add_fdb_test(TEST_FILES fast/BackupCorrectness.toml)
  add_fdb_test(TEST_FILES fast/BackupCorrectnessClean.toml)
  add_fdb_test(TEST_FILES fast/BackupToDBCorrectness.toml)
  add_fdb_test(TEST_FILES fast/BackupToDBCorrectnessClean.toml)
  add_fdb_test(TEST_FILES fast/CacheTest.toml)
  add_fdb_test(TEST_FILES fast/CloggedSideband.toml)
  add_fdb_test(TEST_FILES fast/ConfigureLocked.toml)
  add_fdb_test(TEST_FILES fast/ConstrainedRandomSelector.toml)
  add_fdb_test(TEST_FILES fast/CycleAndLock.toml)
  add_fdb_test(TEST_FILES fast/CycleTest.toml)
  add_fdb_test(TEST_FILES fast/FuzzApiCorrectness.toml)
  add_fdb_test(TEST_FILES fast/FuzzApiCorrectnessClean.toml)
  add_fdb_test(TEST_FILES fast/IncrementalBackup.toml)
  add_fdb_test(TEST_FILES fast/IncrementTest.toml)
  add_fdb_test(TEST_FILES fast/InventoryTestAlmostReadOnly.toml)
  add_fdb_test(TEST_FILES fast/InventoryTestSomeWrites.toml)
  add_fdb_test(TEST_FILES fast/KillRegionCycle.toml)
  add_fdb_test(TEST_FILES fast/LocalRatekeeper.toml)
  add_fdb_test(TEST_FILES fast/LongStackWriteDuringRead.toml)
  add_fdb_test(TEST_FILES fast/LowLatency.toml)
  add_fdb_test(TEST_FILES fast/MemoryLifetime.toml)
  add_fdb_test(TEST_FILES fast/MoveKeysCycle.toml)
  add_fdb_test(TEST_FILES fast/ProtocolVersion.toml)
  add_fdb_test(TEST_FILES fast/RandomSelector.toml)
  add_fdb_test(TEST_FILES fast/RandomUnitTests.toml)
  add_fdb_test(TEST_FILES fast/ReadHotDetectionCorrectness.toml IGNORE) # TODO re-enable once read hot detection is enabled.
  add_fdb_test(TEST_FILES fast/ReportConflictingKeys.toml)
  add_fdb_test(TEST_FILES fast/SelectorCorrectness.toml)
  add_fdb_test(TEST_FILES fast/Sideband.toml)
  add_fdb_test(TEST_FILES fast/SidebandWithStatus.toml)
  add_fdb_test(TEST_FILES fast/SimpleAtomicAdd.toml)
  add_fdb_test(TEST_FILES fast/SpecialKeySpaceCorrectness.toml)
  add_fdb_test(TEST_FILES fast/SwizzledRollbackSideband.toml)
  add_fdb_test(TEST_FILES fast/SystemRebootTestCycle.toml)
  add_fdb_test(TEST_FILES fast/TaskBucketCorrectness.toml)
  add_fdb_test(TEST_FILES fast/TimeKeeperCorrectness.toml)
  add_fdb_test(TEST_FILES fast/TxnStateStoreCycleTest.toml)
  add_fdb_test(TEST_FILES fast/UDP.toml)
  add_fdb_test(TEST_FILES fast/Unreadable.toml)
  add_fdb_test(TEST_FILES fast/VersionStamp.toml)
  add_fdb_test(TEST_FILES fast/Watches.toml)
  add_fdb_test(TEST_FILES fast/WriteDuringRead.toml)
  add_fdb_test(TEST_FILES fast/WriteDuringReadClean.toml)
  add_fdb_test(TEST_FILES rare/CheckRelocation.toml)
  add_fdb_test(TEST_FILES rare/ClogUnclog.toml)
  add_fdb_test(TEST_FILES rare/CloggedCycleWithKills.toml)
  add_fdb_test(TEST_FILES rare/ConflictRangeCheck.toml)
  add_fdb_test(TEST_FILES rare/ConflictRangeRYOWCheck.toml)
  add_fdb_test(TEST_FILES rare/CycleRollbackClogged.toml)
  add_fdb_test(TEST_FILES rare/CycleWithKills.toml)
  add_fdb_test(TEST_FILES rare/Downgrade.toml)
  add_fdb_test(TEST_FILES rare/FuzzTest.toml)
  add_fdb_test(TEST_FILES rare/InventoryTestHeavyWrites.toml)
  add_fdb_test(TEST_FILES rare/LargeApiCorrectness.toml)
  add_fdb_test(TEST_FILES rare/LargeApiCorrectnessStatus.toml)
  add_fdb_test(TEST_FILES rare/RYWDisable.toml)
  add_fdb_test(TEST_FILES rare/RandomReadWriteTest.toml)
  add_fdb_test(TEST_FILES rare/SwizzledLargeApiCorrectness.toml)
  add_fdb_test(TEST_FILES rare/RedwoodCorrectnessBTree.toml)
  add_fdb_test(TEST_FILES rare/Throttling.toml)
  add_fdb_test(TEST_FILES rare/TransactionTagApiCorrectness.toml)
  add_fdb_test(TEST_FILES rare/TransactionTagSwizzledApiCorrectness.toml)
  add_fdb_test(TEST_FILES rare/WriteTagThrottling.toml)
  add_fdb_test(
    TEST_FILES restarting/from_7.0.0/SnapIncrementalRestore-1.toml
               restarting/from_7.0.0/SnapIncrementalRestore-2.toml)
  add_fdb_test(
    TEST_FILES restarting/from_7.0.0/ConfigureTestRestart-1.txt
               restarting/from_7.0.0/ConfigureTestRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_5.0.0/CycleTestRestart-1.txt
               restarting/from_5.0.0/CycleTestRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_5.0.0/StorefrontTestRestart-1.txt
               restarting/from_5.0.0/StorefrontTestRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_6.2.29/SnapTestAttrition-1.txt
               restarting/from_6.2.29/SnapTestAttrition-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_6.2.29/SnapTestSimpleRestart-1.txt
               restarting/from_6.2.29/SnapTestSimpleRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_6.2.29/SnapTestRestart-1.txt
               restarting/from_6.2.29/SnapTestRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_6.2.29/SnapCycleRestart-1.txt
               restarting/from_6.2.29/SnapCycleRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_5.1.7/DrUpgradeRestart-1.txt
               restarting/from_5.1.7/DrUpgradeRestart-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_5.2.0/ClientTransactionProfilingCorrectness-1.txt
               restarting/from_5.2.0/ClientTransactionProfilingCorrectness-2.txt)
  add_fdb_test(
    TEST_FILES restarting/from_7.0.0/UpgradeAndBackupRestore-1.toml
               restarting/from_7.0.0/UpgradeAndBackupRestore-2.toml)
  add_fdb_test(
    TEST_FILES restarting/to_6.3.10/CycleTestRestart-1.txt
               restarting/to_6.3.10/CycleTestRestart-2.txt)
  add_fdb_test(TEST_FILES slow/ApiCorrectness.toml)
  add_fdb_test(TEST_FILES slow/ApiCorrectnessAtomicRestore.toml)
  add_fdb_test(TEST_FILES slow/ApiCorrectnessSwitchover.toml)
  add_fdb_test(TEST_FILES slow/ClogWithRollbacks.toml)
  add_fdb_test(TEST_FILES slow/CloggedCycleTest.toml)
  add_fdb_test(TEST_FILES slow/CloggedStorefront.toml)
  add_fdb_test(TEST_FILES slow/CommitBug.toml)
  add_fdb_test(TEST_FILES slow/ConfigureTest.toml)
  add_fdb_test(TEST_FILES slow/CycleRollbackPlain.toml)
  add_fdb_test(TEST_FILES slow/DDBalanceAndRemove.toml)
  add_fdb_test(TEST_FILES slow/DDBalanceAndRemoveStatus.toml)
  add_fdb_test(TEST_FILES slow/DifferentClustersSameRV.toml)
  add_fdb_test(TEST_FILES slow/FastTriggeredWatches.toml)
  add_fdb_test(TEST_FILES slow/LowLatencyWithFailures.toml)
  add_fdb_test(TEST_FILES slow/MoveKeysClean.toml)
  add_fdb_test(TEST_FILES slow/MoveKeysSideband.toml)
  add_fdb_test(TEST_FILES slow/RyowCorrectness.toml)
  add_fdb_test(TEST_FILES slow/Serializability.toml)
  add_fdb_test(TEST_FILES slow/SharedBackupCorrectness.toml)
  add_fdb_test(TEST_FILES slow/SharedBackupToDBCorrectness.toml)
  add_fdb_test(TEST_FILES slow/StorefrontTest.toml)
  add_fdb_test(TEST_FILES slow/SwizzledApiCorrectness.toml)
  add_fdb_test(TEST_FILES slow/SwizzledCycleTest.toml)
  add_fdb_test(TEST_FILES slow/SwizzledDdBalance.toml)
  add_fdb_test(TEST_FILES slow/SwizzledRollbackTimeLapse.toml)
  add_fdb_test(TEST_FILES slow/SwizzledRollbackTimeLapseIncrement.toml)
  add_fdb_test(TEST_FILES slow/VersionStampBackupToDB.toml)
  add_fdb_test(TEST_FILES slow/VersionStampSwitchover.toml)
  add_fdb_test(TEST_FILES slow/WriteDuringReadAtomicRestore.toml)
  add_fdb_test(TEST_FILES slow/WriteDuringReadSwitchover.toml)
  add_fdb_test(TEST_FILES slow/ddbalance.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreNewBackupCorrectnessAtomicOp.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreNewBackupCorrectnessCycle.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreNewBackupCorrectnessMultiCycles.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreNewBackupWriteDuringReadAtomicRestore.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreOldBackupCorrectnessAtomicOp.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreOldBackupCorrectnessCycle.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreOldBackupCorrectnessMultiCycles.toml)
  add_fdb_test(TEST_FILES slow/ParallelRestoreOldBackupWriteDuringReadAtomicRestore.toml)
  add_fdb_test(TEST_FILES ParallelRestoreOldBackupApiCorrectnessAtomicRestore.toml IGNORE)
  # Note that status tests are not deterministic.
  add_fdb_test(TEST_FILES status/invalid_proc_addresses.txt)
  add_fdb_test(TEST_FILES status/local_6_machine_no_replicas_remain.txt)
  add_fdb_test(TEST_FILES status/separate_1_of_3_coordinators_remain.txt)
  add_fdb_test(TEST_FILES status/separate_2_of_3_coordinators_remain.txt)
  add_fdb_test(TEST_FILES status/separate_cannot_write_cluster_file.txt)
  add_fdb_test(TEST_FILES status/separate_idle.txt)
  add_fdb_test(TEST_FILES status/separate_initializing.txt)
  add_fdb_test(TEST_FILES status/separate_no_coordinators.txt)
  add_fdb_test(TEST_FILES status/separate_no_database.txt)
  add_fdb_test(TEST_FILES status/separate_no_servers.txt)
  add_fdb_test(TEST_FILES status/separate_not_enough_servers.txt)
  add_fdb_test(TEST_FILES status/single_process_too_many_config_params.txt)

  add_test(
    NAME multiversion_client/unit_tests
    COMMAND $<TARGET_FILE:fdbserver> -r unittests -f /fdbclient/multiversionclient/
  )

  verify_testing()
  if (NOT OPEN_FOR_IDE AND NOT WIN32)
    create_correctness_package()
    if (USE_VALGRIND)
      create_valgrind_correctness_package()
    endif()
  endif()
else()
  message(WARNING "Python not found, won't configure ctest")
endif()
