|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +# Ensure traincheck is in path (standard pattern for this repo based on other tests) |
| 9 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 10 | + |
| 11 | +import traincheck.collect_trace as collect_trace |
| 12 | +from traincheck.config import config |
| 13 | + |
| 14 | + |
| 15 | +class TestPolicyInjection(unittest.TestCase): |
| 16 | + def setUp(self): |
| 17 | + self.test_dir = tempfile.mkdtemp() |
| 18 | + self.dummy_script = os.path.join(self.test_dir, "dummy_script.py") |
| 19 | + with open(self.dummy_script, "w") as f: |
| 20 | + f.write("import torch\n") |
| 21 | + |
| 22 | + def tearDown(self): |
| 23 | + shutil.rmtree(self.test_dir) |
| 24 | + |
| 25 | + @patch("traincheck.runner.ProgramRunner") |
| 26 | + @patch( |
| 27 | + "traincheck.instrumentor.instrument_file", |
| 28 | + side_effect=collect_trace.instrumentor.instrument_file, |
| 29 | + ) |
| 30 | + def test_policy_injection(self, mock_instrument_file, MockProgramRunner): |
| 31 | + # Setup mock runner |
| 32 | + mock_runner_instance = MockProgramRunner.return_value |
| 33 | + mock_runner_instance.run.return_value = ("output", 0) |
| 34 | + |
| 35 | + # Simulate command line arguments |
| 36 | + test_args = [ |
| 37 | + "collect_trace.py", |
| 38 | + "-p", |
| 39 | + self.dummy_script, |
| 40 | + "--sampling-interval", |
| 41 | + "3", |
| 42 | + "--warm-up-steps", |
| 43 | + "2", |
| 44 | + "--only-instr", |
| 45 | + ] |
| 46 | + |
| 47 | + with patch.object(sys, "argv", test_args): |
| 48 | + collect_trace.main() |
| 49 | + |
| 50 | + # Check if instrument_file was called with the correct args |
| 51 | + call_args = mock_instrument_file.call_args |
| 52 | + self.assertIsNotNone(call_args, "instrument_file was not called") |
| 53 | + _, kwargs = call_args |
| 54 | + self.assertEqual(kwargs.get("sampling_interval"), 3) |
| 55 | + self.assertEqual(kwargs.get("warm_up_steps"), 2) |
| 56 | + |
| 57 | + # Check if the policy ends up in instrumented source code |
| 58 | + runner_call_args = MockProgramRunner.call_args |
| 59 | + self.assertIsNotNone(runner_call_args, "ProgramRunner was not initialized") |
| 60 | + source_code = runner_call_args[0][0] # first arg is source_code |
| 61 | + |
| 62 | + self.assertIn("sampling_interval=3", source_code) |
| 63 | + self.assertIn("warm_up_steps=2", source_code) |
| 64 | + |
| 65 | + @patch("traincheck.runner.ProgramRunner") |
| 66 | + @patch( |
| 67 | + "traincheck.instrumentor.instrument_file", |
| 68 | + side_effect=collect_trace.instrumentor.instrument_file, |
| 69 | + ) |
| 70 | + @patch("traincheck.collect_trace.read_inv_file") |
| 71 | + def test_defaults_with_invariant( |
| 72 | + self, mock_read_inv, mock_instrument_file, MockProgramRunner |
| 73 | + ): |
| 74 | + # Setup mocks |
| 75 | + mock_runner_instance = MockProgramRunner.return_value |
| 76 | + mock_runner_instance.run.return_value = ("output", 0) |
| 77 | + mock_read_inv.return_value = [] # Return empty list of invariants |
| 78 | + |
| 79 | + test_args = [ |
| 80 | + "collect_trace.py", |
| 81 | + "-p", |
| 82 | + self.dummy_script, |
| 83 | + "-i", |
| 84 | + "dummy_inv.json", # Enable invariants |
| 85 | + "--only-instr", |
| 86 | + ] |
| 87 | + |
| 88 | + with patch.object(sys, "argv", test_args): |
| 89 | + collect_trace.main() |
| 90 | + |
| 91 | + call_args = mock_instrument_file.call_args |
| 92 | + _, kwargs = call_args |
| 93 | + |
| 94 | + # Should default to config values |
| 95 | + expected_interval = config.DEFAULT_CHECKING_POLICY["interval"] |
| 96 | + expected_warmup = config.DEFAULT_CHECKING_POLICY["warm_up"] |
| 97 | + |
| 98 | + self.assertEqual(kwargs.get("sampling_interval"), expected_interval) |
| 99 | + self.assertEqual(kwargs.get("warm_up_steps"), expected_warmup) |
| 100 | + |
| 101 | + @patch("traincheck.runner.ProgramRunner") |
| 102 | + @patch( |
| 103 | + "traincheck.instrumentor.instrument_file", |
| 104 | + side_effect=collect_trace.instrumentor.instrument_file, |
| 105 | + ) |
| 106 | + def test_defaults_without_invariant(self, mock_instrument_file, MockProgramRunner): |
| 107 | + # Setup mocks |
| 108 | + mock_runner_instance = MockProgramRunner.return_value |
| 109 | + mock_runner_instance.run.return_value = ("output", 0) |
| 110 | + |
| 111 | + test_args = ["collect_trace.py", "-p", self.dummy_script, "--only-instr"] |
| 112 | + |
| 113 | + with patch.object(sys, "argv", test_args): |
| 114 | + collect_trace.main() |
| 115 | + |
| 116 | + call_args = mock_instrument_file.call_args |
| 117 | + _, kwargs = call_args |
| 118 | + |
| 119 | + # Should default to config.INSTRUMENTATION_POLICY values |
| 120 | + expected_interval = config.INSTRUMENTATION_POLICY["interval"] |
| 121 | + expected_warmup = config.INSTRUMENTATION_POLICY["warm_up"] |
| 122 | + |
| 123 | + self.assertEqual(kwargs.get("sampling_interval"), expected_interval) |
| 124 | + self.assertEqual(kwargs.get("warm_up_steps"), expected_warmup) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + unittest.main() |
0 commit comments