Skip to content

Commit

Permalink
276 Update some tests and run_all_tests.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandoquesada committed Oct 23, 2024
1 parent 9472fff commit 88dde27
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 106 deletions.
10 changes: 5 additions & 5 deletions .github/scripts/run_coverage_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

cd /vdms/tests

# # Run S3 C++ PMGD Based Tests
# echo 'Checking for the available disk space due MinIO requires at least 1gb free'
# df -h
# Run S3 C++ PMGD Based Tests
echo 'Checking for the available disk space due MinIO requires at least 1gb free'
df -h

chmod +x ./run_all_tests.py

# echo 'Running run_all_tests.py script for remote C++ tests (-t=ru)'
# python ./run_all_tests.py -t=ru -u ${AWS_ACCESS_KEY_ID} -p ${AWS_SECRET_ACCESS_KEY}
echo 'Running run_all_tests.py script for remote C++ tests (-t=ru)'
python ./run_all_tests.py -t=ru -u ${AWS_ACCESS_KEY_ID} -p ${AWS_SECRET_ACCESS_KEY}

echo 'Running run_all_tests script for C++ tests (-t=ut)'
python ./run_all_tests.py -t=ut -k
Expand Down
104 changes: 37 additions & 67 deletions tests/TestScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
NEO4J_OPS_IO_TEST_TYPE,
NEO4J_E2E_TEST_TYPE,
NEO4J_BACKEND_TEST_TYPE,
DEFAULT_PYTHON_TEST_FILTER,
main,
)

Expand Down Expand Up @@ -1298,84 +1299,53 @@ def test_run_prep_certs_script_invalid_file(self, mock_print, mock_exists):
self.assertEqual(str(context.exception), expected_message)

#### Tests for run_python_tests() ####
@patch("run_all_tests.subprocess.Popen")
@patch("run_all_tests.print")
def test_run_python_tests(self, mock_print, mock_popen):
instance = ConcreteClass()
# Set up the mock for Popen
mock_process = MagicMock()
mock_process.communicate.return_value = (b"output", b"errors")
mock_process.pid = 12345
mock_popen.return_value = mock_process

# Set up TestingArgs
@patch('run_all_tests.subprocess.run')
def test_run_python_tests_default_filter(self, mock_subprocess_run):
# Arrange
mock_subprocess_run.return_value = None # simulate successful run
testing_args = TestingArgs()
testing_args.test_name = "test_module.TestClass"

# Mock file descriptors
stderrFD = MagicMock()
stdoutFD = MagicMock()

# Mock write_to_fd method
instance.write_to_fd = MagicMock()

# Call the method
instance.run_python_tests(testing_args, stderrFD, stdoutFD)

# Check if the correct print statements were made
mock_print.assert_any_call("Running Python tests...")
mock_print.assert_any_call("Test filter:", testing_args.test_name)

# Check if the process was added to the list
self.assertIn(mock_process, run_all_tests.processList)

# Check if the output and errors were handled correctly
instance.write_to_fd.assert_any_call(
"stdout", "run_python_tests", "output", stdoutFD, run_all_tests.DEBUG_MODE
)
instance.write_to_fd.assert_any_call(
"stderr", "run_python_tests", "errors", stderrFD, run_all_tests.DEBUG_MODE
)

@patch("run_all_tests.subprocess.Popen")
def test_run_python_tests_exception(self, mock_popen):
testing_args.test_name = DEFAULT_PYTHON_TEST_FILTER
instance = ConcreteClass()

# Mock DEFAULT_DIR_REPO
run_all_tests.DEFAULT_DIR_REPO = "/path/to/repo"

# Set DEBUG_MODE for testing purposes
run_all_tests.DEBUG_MODE = True
# Act
instance.run_python_tests(testing_args, None, None)

# Set up the mock for Popen to return a mock process with a kill method
mock_process = MagicMock()
mock_process.communicate.return_value = (b"output", b"errors")
mock_process.pid = 12345
mock_popen.return_value = mock_process
# Assert
expected_cmd = ['python3', '-m', 'coverage', 'run', '-a', f'--include="{run_all_tests.DEFAULT_DIR_REPO}/*"', f'--omit="{run_all_tests.DEFAULT_DIR_REPO}/client/python/vdms/queryMessage_pb2.py,{run_all_tests.DEFAULT_DIR_REPO}/tests/*"', '-m', 'unittest']
expected_cmd.extend(testing_args.test_name.split())
expected_cmd.append('-v')
mock_subprocess_run.assert_called_once_with(expected_cmd, text=True, check=True)

# Set up TestingArgs
@patch('run_all_tests.subprocess.run')
def test_run_python_tests_custom_filter(self, mock_subprocess_run):
# Arrange
custom_filter = "custom_test"
mock_subprocess_run.return_value = None # simulate successful run
testing_args = TestingArgs()
testing_args.test_name = "test_module.TestClass"
testing_args.test_name = custom_filter
instance = ConcreteClass()

# Mock file descriptors
stderrFD = MagicMock()
stdoutFD = MagicMock()
# Act
instance.run_python_tests(testing_args, None, None)

# Mock write_to_fd method
instance.write_to_fd = MagicMock()
expected_cmd = ['python3', '-m', 'coverage', 'run', '-a', f'--include="{run_all_tests.DEFAULT_DIR_REPO}/*"', f'--omit="{run_all_tests.DEFAULT_DIR_REPO}/client/python/vdms/queryMessage_pb2.py,{run_all_tests.DEFAULT_DIR_REPO}/tests/*"', '-m', 'unittest']
expected_cmd.extend(testing_args.test_name.split())
expected_cmd.append('-v')
mock_subprocess_run.assert_called_once_with(expected_cmd, text=True, check=True)

# Set up the mock to raise an exception after Popen is called
mock_process.communicate.side_effect = Exception("Test Exception")
@patch('run_all_tests.subprocess.run')
def test_run_python_tests_exception(self, mock_subprocess_run):
# Arrange
mock_subprocess_run.side_effect = subprocess.CalledProcessError(1, 'cmd') # simulate command failure
testing_args = TestingArgs()
testing_args.test_name = DEFAULT_PYTHON_TEST_FILTER
instance = ConcreteClass()

# Call the method and check for exception
# Act & Assert
with self.assertRaises(Exception) as context:
instance.run_python_tests(testing_args, stderrFD, stdoutFD)

# Check the exception message
self.assertTrue("run_python_tests() error:" in str(context.exception))

# Check if the process's kill method was called
mock_process.kill.assert_called()
instance.run_python_tests(testing_args, None, None)

self.assertTrue("run_python_tests() error: " in str(context.exception))

#### Tests for write_to_fd() ####
@patch("run_all_tests.print")
Expand Down
41 changes: 7 additions & 34 deletions tests/run_all_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,11 +947,9 @@ def run_google_tests(self, testingArgs: TestingArgs, stderrFD, stdoutFD):
- stdoutFD: The file descriptor for capturing stdout output.
Global Variables:
- processList (list): A global list to keep track of running processes.
- DEBUG_MODE (bool): A global flag indicating whether debug information
should be printed.
"""
global processList
try:
stop_on_failure_value = ""
if testingArgs.stop_tests_on_failure:
Expand Down Expand Up @@ -1085,52 +1083,27 @@ def run_python_tests(self, testingArgs: TestingArgs, stderrFD, stdoutFD):
- stdoutFD: The file descriptor for capturing stdout output.
Global Variables:
- processList (list): A global list to keep track of running processes.
- DEBUG_MODE (bool): A global flag indicating whether debug information
should be printed.
"""
global processList

pythonProcess = None
try:
print("Running Python tests...")
print("Test filter:", testingArgs.test_name)

# To avoid BASH injection, the test_name is escaped
test_name = testingArgs.test_name
if testingArgs.test_name != DEFAULT_PYTHON_TEST_FILTER:
test_name = quote(testingArgs.test_name)

# Run the command and capture the output
cmd = f'python3 -m coverage run -a --include="{DEFAULT_DIR_REPO}/*" --omit="{DEFAULT_DIR_REPO}/client/python/vdms/queryMessage_pb2.py,{DEFAULT_DIR_REPO}/tests/*" -m unittest'
cmd = cmd + " " + test_name
cmd = cmd + " -v"
pythonProcess = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
stdout, stderr = pythonProcess.communicate()
test_name = testingArgs.test_name

# Decode the byte strings
output = stdout.decode("utf-8")
errors = stderr.decode("utf-8")
cmd = ['python3', '-m', 'coverage', 'run', '-a', f'--include="{DEFAULT_DIR_REPO}/*"', f'--omit="{DEFAULT_DIR_REPO}/client/python/vdms/queryMessage_pb2.py,{DEFAULT_DIR_REPO}/tests/*"', '-m', 'unittest']
cmd.extend(test_name.split())
cmd.append('-v')

# Print the command output
self.write_to_fd("stdout", "run_python_tests", output, stdoutFD, DEBUG_MODE)

# Check for errors
if errors:
self.write_to_fd(
"stderr", "run_python_tests", errors, stderrFD, DEBUG_MODE
)

if DEBUG_MODE:
print("Using python3 pid for tests:", pythonProcess.pid)

processList.append(pythonProcess)
subprocess.run(
cmd, text=True, check=True
)

except Exception as e:
if pythonProcess is not None:
pythonProcess.kill()
raise Exception("run_python_tests() error: " + str(e))

def write_to_fd(self, message_type, function_name, message, writer, verbose=False):
Expand Down

0 comments on commit 88dde27

Please sign in to comment.