diff --git a/engine/e2e-test/main.py b/engine/e2e-test/main.py index ef1af5748..cd54cdb43 100644 --- a/engine/e2e-test/main.py +++ b/engine/e2e-test/main.py @@ -1,7 +1,7 @@ import pytest - -from test_cli_engine_list import TestCliEngineList +from test_api_engine_list import TestApiEngineList from test_cli_engine_get import TestCliEngineGet +from test_cli_engine_list import TestCliEngineList if __name__ == "__main__": pytest.main([__file__, "-v"]) diff --git a/engine/e2e-test/test_api_engine_list.py b/engine/e2e-test/test_api_engine_list.py new file mode 100644 index 000000000..b09919cf3 --- /dev/null +++ b/engine/e2e-test/test_api_engine_list.py @@ -0,0 +1,20 @@ +import pytest +import requests +from test_runner import start_server, stop_server + + +class TestApiEngineList: + + @pytest.fixture(autouse=True) + def setup_and_teardown(self): + # Setup + start_server() + + yield + + # Teardown + stop_server() + + def test_engines_list_api_run_successfully(self): + response = requests.get("http://localhost:3928/engines") + assert response.status_code == 200 diff --git a/engine/e2e-test/test_runner.py b/engine/e2e-test/test_runner.py index 6df74f20f..c1b0a1d83 100644 --- a/engine/e2e-test/test_runner.py +++ b/engine/e2e-test/test_runner.py @@ -1,7 +1,8 @@ import platform +import select import subprocess -import os -from typing import List, Tuple +import time +from typing import List def run(name: str, arguments: List[str]): @@ -9,5 +10,47 @@ def run(name: str, arguments: List[str]): executable = "build\\cortex-cpp.exe" else: executable = "build/cortex-cpp" - result = subprocess.run([executable] + arguments, capture_output=True, text=True) + print("Command name", name) + print("Running command: ", [executable] + arguments) + if len(arguments) == 0: + result = subprocess.run(executable, capture_output=True, text=True, timeout=5) + else: + result = subprocess.run( + [executable] + arguments, capture_output=True, text=True, timeout=5 + ) return result.returncode, result.stdout, result.stderr + + +def start_server(timeout=5): + if platform.system() == "Windows": + executable = "build\\cortex-cpp.exe" + else: + executable = "build/cortex-cpp" + process = subprocess.Popen( + executable, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True + ) + + success_message = "Server started" + start_time = time.time() + while time.time() - start_time < timeout: + # Use select to check if there's data to read from stdout or stderr + readable, _, _ = select.select([process.stdout, process.stderr], [], [], 0.1) + + for stream in readable: + line = stream.readline() + if line: + print(line.strip()) # Print output for debugging + if success_message in line: + # have to wait a bit for server to really up and accept connection + time.sleep(0.3) + return True, process # Success condition met + + # Check if the process has ended + if process.poll() is not None: + return False, process # Process ended without success message + + return False, process # Timeout reached + + +def stop_server(): + run("Stop server", ["stop"]) diff --git a/engine/main.cc b/engine/main.cc index 1450d887c..949f9e0d2 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -27,7 +27,8 @@ void RunServer() { auto config = file_manager_utils::GetCortexConfig(); - LOG_INFO << "Host: " << config.apiServerHost << " Port: " << config.apiServerPort << "\n"; + LOG_INFO << "Host: " << config.apiServerHost + << " Port: " << config.apiServerPort << "\n"; // Create logs/ folder and setup log to file std::filesystem::create_directory(config.logFolderPath + "/" + @@ -72,7 +73,8 @@ void RunServer() { LOG_INFO << "Server started, listening at: " << config.apiServerHost << ":" << config.apiServerPort; LOG_INFO << "Please load your model"; - drogon::app().addListener(config.apiServerHost, std::stoi(config.apiServerPort)); + drogon::app().addListener(config.apiServerHost, + std::stoi(config.apiServerPort)); drogon::app().setThreadNum(drogon_thread_num); LOG_INFO << "Number of thread is:" << drogon::app().getThreadNum();