Skip to content

Commit

Permalink
feat: Add Python test with site-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronNg committed May 14, 2024
1 parent 071d602 commit 354845c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 38 deletions.
31 changes: 29 additions & 2 deletions .github/scripts/e2e-test-server-linux-and-mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ max=11000
range=$((max - min + 1))
PORT=$((RANDOM % range + min))

# Install numpy for Python
export PYTHONHOME=$(pwd)/python/
echo "Set Python HOME to $PYTHONHOME"
./python/bin/python3 -m ensurepip
./python/bin/python3 -m pip install --upgrade pip
./python/bin/python3 -m pip install numpy --target=$PYTHONHOME/lib/python/site-packages/

# Start the binary file
"$BINARY_PATH" 127.0.0.1 $PORT >/tmp/server.log &

Expand All @@ -41,14 +48,35 @@ response1=$(curl --connect-timeout 60 -o /tmp/python-file-execution-res.log -s -
"file_execution_path": "'$PYTHON_FILE_EXECUTION_PATH'"
}')


error_occurred=0

# Verify the response
if [[ "$response1" -ne 200 ]]; then
echo "The python file execution curl command failed with status code: $response1"
cat /tmp/python-file-execution-res.log
error_occurred=1
fi

# Verify the output of the Python file in output.txt
OUTPUT_FILE="./output.txt"
EXPECTED_OUTPUT="1 2 3" # Replace with the expected content

if [[ -f "$OUTPUT_FILE" ]]; then
actual_output=$(cat "$OUTPUT_FILE")
if [[ "$actual_output" != "$EXPECTED_OUTPUT" ]]; then
echo "The output of the Python file does not match the expected output."
echo "Expected: $EXPECTED_OUTPUT"
echo "Actual: $actual_output"
error_occurred=1
else
echo "The output of the Python file matches the expected output."
fi
else
echo "Output file $OUTPUT_FILE does not exist."
error_occurred=1
fi


if [[ "$error_occurred" -eq 1 ]]; then
echo "Server test run failed!!!!!!!!!!!!!!!!!!!!!!"
echo "Server Error Logs:"
Expand All @@ -62,7 +90,6 @@ echo "----------------------"
echo "Log server:"
cat /tmp/server.log


echo "Server test run successfully!"

# Kill the server process
Expand Down
25 changes: 25 additions & 0 deletions .github/scripts/e2e-test-server-windows.bat
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ set /a max=11000
set /a range=max-min+1
set /a PORT=%min% + %RANDOM% %% %range%

rem Install numpy for Python
set "PYTHONHOME=%cd%\python"
echo Set Python HOME to %PYTHONHOME%
%PYTHONHOME%\python.exe -m ensurepip
%PYTHONHOME%\python.exe -m pip install --upgrade pip
%PYTHONHOME%\python.exe -m pip install numpy --target=%PYTHONHOME%\Lib\site-packages\

rem Start the binary file
start "" /B "%BINARY_PATH%" "127.0.0.1" %PORT% > "%TEMP%\server.log" 2>&1

Expand Down Expand Up @@ -74,6 +81,24 @@ echo Log python file execution:
type %TEMP%\response1.log
echo.

rem Verification step: Check the contents of output.txt
set "expected_output=1 2 3"
set "actual_output="
if exist "output.txt" (
for /f "delims=" %%x in (output.txt) do set "actual_output=%%x"
if "!actual_output!"=="!expected_output!" (
echo Verification succeeded: output.txt contains the expected data.
) else (
echo Verification failed: output.txt does not contain the expected data.
echo Expected: !expected_output!
echo Actual: !actual_output!
set "error_occurred=1"
)
) else (
echo Verification failed: output.txt does not exist.
set "error_occurred=1"
)

echo ----------------------
echo Server logs:
type %TEMP%\server.log
Expand Down
6 changes: 6 additions & 0 deletions .github/scripts/python-file-to-test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import sys;
for path in sys.path:
print(path)

import numpy as np
print("Numpy version: " + np.__version__)

with open('output.txt', 'w') as file:
file.write(' '.join(map(str, np.array([1, 2, 3]))))
36 changes: 0 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,6 @@ find_library(TRANTOR
HINTS "${THIRD_PARTY_PATH}/lib/"
)

# Copy the python3 related files to the build folder
if (WIN32)
# Copy the whole library
file(COPY ${THIRD_PARTY_PATH}/python DESTINATION ${CMAKE_BINARY_DIR}/Release)
else() # APPLE and LINUX
# Copy library files
file(GLOB PYTHON_LIBS ${THIRD_PARTY_PATH}/lib/python*)
if(PYTHON_LIBS)
list(GET PYTHON_LIBS 0 FIRST_PYTHON_LIB)
file(COPY ${FIRST_PYTHON_LIB} DESTINATION ${CMAKE_BINARY_DIR}/python/lib)
endif()

# Create symbolic link ".../lib/python" for target ".../lib/python3.*"
file(GLOB PYTHON_LIBS_DES ${CMAKE_BINARY_DIR}/python/lib/python*.*)
if(PYTHON_LIBS_DES)
list(GET PYTHON_LIBS_DES 0 FIRST_PYTHON_LIB)
set(SYMLINK_PATH ${CMAKE_BINARY_DIR}/python/lib/python)
if(EXISTS ${SYMLINK_PATH})
file(REMOVE_RECURSE ${SYMLINK_PATH})
endif()
file(CREATE_LINK ${FIRST_PYTHON_LIB} ${SYMLINK_PATH} SYMBOLIC)
endif()

# Copy executable files
file(GLOB PYTHON_EXECUTABLES ${THIRD_PARTY_PATH}/bin/python*)
foreach(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLES})
file(COPY ${PYTHON_EXECUTABLE} DESTINATION ${CMAKE_BINARY_DIR}/python/bin)
endforeach()

# Copy dynamic library files
file(GLOB PYTHON_DLS ${THIRD_PARTY_PATH}/lib/libpython*)
foreach(PYTHON_DL ${PYTHON_DLS})
file(COPY ${PYTHON_DL} DESTINATION ${CMAKE_BINARY_DIR}/python)
endforeach()
endif()

if(WIN32)
# dlfcn for dynamic library
set(dlfcn-win32_DIR "${THIRD_PARTY_PATH}/lib/cmake/dlfcn-win32")
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ else
tar -czvf cortex.python-runtime.tar.gz cortex.python-runtime
endif


run-e2e-test:
ifeq ($(RUN_TESTS),false)
@echo "Skipping tests"
Expand Down

0 comments on commit 354845c

Please sign in to comment.