@@ -573,9 +573,90 @@ install(
573573# =============================================================================
574574if (BUILD_TESTS)
575575 enable_testing ()
576- find_package (GTest CONFIG REQUIRED )
576+ include (FBPythonBinary )
577+
578+ set (MCROUTER_TEST_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR } /python" )
579+ file (MAKE_DIRECTORY "${MCROUTER_TEST_CONFIG_DIR} " )
580+ configure_file (
581+ "${CMAKE_CURRENT_SOURCE_DIR } /CMake/mcrouter_test_config.py.in"
582+ "${MCROUTER_TEST_CONFIG_DIR} /config.py"
583+ @ONLY
584+ )
585+
586+ add_fb_python_library (
587+ mcrouter_test_config
588+ BASE_DIR "${MCROUTER_TEST_CONFIG_DIR} "
589+ NAMESPACE "mcrouter.test"
590+ SOURCES "${MCROUTER_TEST_CONFIG_DIR} /config.py"
591+ )
592+
593+ # JSON fixtures required by test_validate_config.py are referenced via
594+ # relative path "mcrouter/test/*.json" with WORKING_DIRECTORY set below.
595+ # In fbcode layout canonical JSONs live at fbcode/mcrouter/test/ (../test
596+ # from public_tld); in OSS after shipit they live at mcrouter/test/ in the
597+ # source tree. We copy them to binary dir only (no source-tree mutation)
598+ # and run tests with WORKING_DIRECTORY = binary dir so
599+ # "mcrouter/test/*.json" resolves in sandboxed/OSS executions.
600+ file (MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR } /mcrouter/test" )
601+ # fbcode layout: fbcode/mcrouter/test/ (../test from public_tld).
602+ set (_fbcode_test_dir "${CMAKE_CURRENT_SOURCE_DIR } /../test" )
603+ # OSS layout (after shipit): fixtures vendored under mcrouter/test/.
604+ set (_oss_test_dir "${CMAKE_CURRENT_SOURCE_DIR } /mcrouter/test" )
605+ if (EXISTS "${_fbcode_test_dir} /test_ascii.json"
606+ AND EXISTS "${_fbcode_test_dir} /invalid_config.json" )
607+ file (
608+ COPY
609+ "${_fbcode_test_dir} /test_ascii.json"
610+ "${_fbcode_test_dir} /invalid_config.json"
611+ DESTINATION "${CMAKE_CURRENT_BINARY_DIR } /mcrouter/test"
612+ )
613+ elseif (EXISTS "${_oss_test_dir} /test_ascii.json"
614+ AND EXISTS "${_oss_test_dir} /invalid_config.json" )
615+ file (
616+ COPY
617+ "${_oss_test_dir} /test_ascii.json"
618+ "${_oss_test_dir} /invalid_config.json"
619+ DESTINATION "${CMAKE_CURRENT_BINARY_DIR } /mcrouter/test"
620+ )
621+ else ()
622+ message (WARNING
623+ "mcrouter test JSON fixtures (test_ascii.json, invalid_config.json) were "
624+ "not found under ../test or mcrouter/test. test_validate_config.py will "
625+ "fail with FileNotFoundError at runtime. Only test_validate_config.py "
626+ "depends on these fixtures; test_bad_params.py and test_linenumbers.py "
627+ "do not."
628+ )
629+ endif ()
630+
631+ add_fb_python_unittest (
632+ mcrouter_oss_python_tests
633+ BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR } "
634+ NAMESPACE ""
635+ WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR } "
636+ SOURCES
637+ mcrouter/test/test_bad_params.py
638+ mcrouter/test/test_linenumbers.py
639+ mcrouter/test/test_validate_config.py
640+ DEPENDS mcrouter_test_config
641+ )
577642
578- # Add test subdirectories when ready
579- # add_subdirectory(mcrouter/lib/test)
580- # add_subdirectory(mcrouter/test)
643+ # Ensure mcrouter binary exists before Python tests that spawn it.
644+ # We avoid depending on the internal "${TARGET}.GEN_PY_EXE" name from
645+ # FBPythonBinary.cmake – it is an implementation detail that can change
646+ # across fbcode_builder revisions. Instead, make the
647+ # mcrouter_test_config.py_sources_built target (which add_fb_python_library
648+ # creates and which chains into the unittest exe build) depend on
649+ # mcrouter_server. That way ctest only runs after the server binary is
650+ # present. This target name is still an FBPythonBinary internal, so guard
651+ # on its existence too and fall back to a warning if the contract changes.
652+ if (TARGET mcrouter_server AND TARGET mcrouter_test_config.py_sources_built)
653+ add_dependencies (mcrouter_test_config.py_sources_built mcrouter_server )
654+ elseif (TARGET mcrouter_server)
655+ message (WARNING
656+ "Expected FBPythonBinary target mcrouter_test_config.py_sources_built "
657+ "was not found; cannot order the mcrouter_server build before the "
658+ "Python tests. Tests that spawn mcrouter may race the binary build if "
659+ "FBPythonBinary's internal target naming has changed."
660+ )
661+ endif ()
581662endif ()
0 commit comments