-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update environment variable generation for safer execution (#493)
* Update executor.py to handle spaces in binary path * Update test_executor.py for new path * Update test_executor.py * Update test_executor.py to pass test * Update test_executor.py * update environment variables to remove shell=True * pass binary path as a string * add testing * specify system for default initialization * update gpu default argument test --------- Co-authored-by: David Sinden <[email protected]>
- Loading branch information
Showing
4 changed files
with
87 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import unittest | ||
from kwave.options.simulation_execution_options import SimulationExecutionOptions | ||
from kwave.ksensor import kSensor | ||
from unittest.mock import patch | ||
|
||
|
||
class TestSimulationExecutionOptions(unittest.TestCase): | ||
def setUp(self): | ||
# Set up a default kSensor object for testing | ||
self.sensor = kSensor() | ||
self.sensor.record = ["p", "u"] | ||
self.sensor.record_start_index = 10 | ||
|
||
def test_default_initialization(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "linux"): | ||
options = SimulationExecutionOptions() | ||
self.assertFalse(options.is_gpu_simulation) | ||
self.assertEqual(options.binary_name, "kspaceFirstOrder-OMP") | ||
self.assertTrue(options.delete_data) | ||
self.assertEqual(options.num_threads, None) # TODO: confusing logic here | ||
self.assertEqual(options.verbose_level, 0) | ||
|
||
def test_gpu_simulation_initialization(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "linux"): | ||
options = SimulationExecutionOptions(is_gpu_simulation=True) | ||
self.assertTrue(options.is_gpu_simulation) | ||
self.assertEqual(options.binary_name, "kspaceFirstOrder-CUDA") | ||
|
||
def test_binary_name_extension_on_windows(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "windows"): | ||
options = SimulationExecutionOptions() | ||
self.assertTrue(options.binary_name.endswith(".exe")) | ||
|
||
def test_get_options_string(self): | ||
options = SimulationExecutionOptions() | ||
options_string = options.get_options_string(self.sensor) | ||
self.assertIn("--p_raw", options_string) | ||
self.assertIn("--u_raw", options_string) | ||
self.assertIn("-s 10", options_string) | ||
|
||
def test_env_vars_linux(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "linux"): | ||
options = SimulationExecutionOptions() | ||
env_vars = options.env_vars | ||
self.assertIn("OMP_PLACES", env_vars) | ||
self.assertEqual(env_vars["OMP_PLACES"], "cores") | ||
self.assertIn("OMP_PROC_BIND", env_vars) | ||
self.assertEqual(env_vars["OMP_PROC_BIND"], "SPREAD") | ||
|
||
def test_thread_binding_linux(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "linux"): | ||
options = SimulationExecutionOptions(thread_binding=True) | ||
env_vars = options.env_vars | ||
self.assertEqual(env_vars["OMP_PROC_BIND"], "SPREAD") | ||
|
||
def test_thread_binding_darwin(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "darwin"): | ||
options = SimulationExecutionOptions(thread_binding=True) | ||
with self.assertRaises(ValueError, msg="Thread binding is not supported in MacOS."): | ||
_ = options.env_vars | ||
|
||
def test_env_vars_darwin(self): | ||
with patch("kwave.options.simulation_execution_options.PLATFORM", "darwin"): | ||
options = SimulationExecutionOptions() | ||
env_vars = options.env_vars | ||
self.assertNotIn("OMP_PLACES", env_vars) | ||
self.assertNotIn("OMP_PROC_BIND", env_vars) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |