|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +import tempfile |
| 4 | +import yaml |
| 5 | +import copy |
| 6 | +from unittest.mock import patch, MagicMock |
| 7 | +import launcher |
| 8 | + |
| 9 | +class TestLauncher(unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 13 | + self.addCleanup(self.temp_dir.cleanup) |
| 14 | + |
| 15 | + # Paths |
| 16 | + self.mock_root_dir = os.path.join(self.temp_dir.name, "root") |
| 17 | + self.mock_onedragon_dir = os.path.join(self.temp_dir.name, "onedragon") |
| 18 | + os.makedirs(self.mock_root_dir, exist_ok=True) |
| 19 | + os.makedirs(self.mock_onedragon_dir, exist_ok=True) |
| 20 | + |
| 21 | + self.config_yml_path = os.path.join(self.mock_root_dir, "config.yml") |
| 22 | + |
| 23 | + # Mock configuration data |
| 24 | + self.config_data = { |
| 25 | + 'script_list': [ |
| 26 | + { |
| 27 | + 'display_name': 'Test Script 1', |
| 28 | + # Timeout values corresponding to Mon (100) through Sun (700), i.e., indices 0-6 |
| 29 | + 'weekly_timeouts': [100, 200, 300, 400, 500, 600, 700], |
| 30 | + 'run_timeout_seconds': 0 |
| 31 | + }, |
| 32 | + { |
| 33 | + 'display_name': 'Test Script 2 (No timeouts)', |
| 34 | + 'run_timeout_seconds': 50 |
| 35 | + } |
| 36 | + ] |
| 37 | + } |
| 38 | + with open(self.config_yml_path, 'w', encoding='utf-8') as f: |
| 39 | + yaml.dump(self.config_data, f) |
| 40 | + |
| 41 | + def test_get_week_num(self): |
| 42 | + wk = launcher.get_week_num() |
| 43 | + self.assertIsInstance(wk, int) |
| 44 | + self.assertTrue(0 <= wk <= 6) |
| 45 | + |
| 46 | + @patch('launcher.get_week_num') |
| 47 | + @patch('launcher.get_root_dir') |
| 48 | + @patch('launcher.get_path_under_onedragon') |
| 49 | + def test_generate_OneDragon_script_chain_success(self, mock_get_path, mock_get_root, mock_get_week): |
| 50 | + mock_get_root.return_value = self.mock_root_dir |
| 51 | + |
| 52 | + # Create output directory for script_chain config |
| 53 | + out_dir = os.path.join(self.mock_onedragon_dir, "config", "script_chain") |
| 54 | + os.makedirs(out_dir, exist_ok=True) |
| 55 | + mock_get_path.return_value = out_dir |
| 56 | + |
| 57 | + # Test for Wednesday (weekday index 2) |
| 58 | + wednesday_index = 2 |
| 59 | + wednesday_timeout = 300 |
| 60 | + mock_get_week.return_value = wednesday_index |
| 61 | + |
| 62 | + launcher.generate_OneDragon_script_chain() |
| 63 | + |
| 64 | + output_file = os.path.join(out_dir, "01.yml") |
| 65 | + self.assertTrue(os.path.exists(output_file)) |
| 66 | + |
| 67 | + with open(output_file, 'r', encoding='utf-8') as f: |
| 68 | + output_data = yaml.safe_load(f) |
| 69 | + |
| 70 | + # Assertions |
| 71 | + scripts = output_data.get('script_list', []) |
| 72 | + self.assertEqual(scripts[0]['run_timeout_seconds'], wednesday_timeout) |
| 73 | + self.assertEqual(scripts[1]['run_timeout_seconds'], 50) # Unchanged since no weekly_timeouts |
| 74 | + |
| 75 | + @patch('launcher.get_week_num') |
| 76 | + @patch('launcher.get_root_dir') |
| 77 | + @patch('launcher.get_path_under_onedragon') |
| 78 | + def test_generate_OneDragon_script_chain_invalid_timeout_length(self, mock_get_path, mock_get_root, mock_get_week): |
| 79 | + mock_get_root.return_value = self.mock_root_dir |
| 80 | + out_dir = os.path.join(self.mock_onedragon_dir, "config", "script_chain") |
| 81 | + os.makedirs(out_dir, exist_ok=True) |
| 82 | + mock_get_path.return_value = out_dir |
| 83 | + mock_get_week.return_value = 0 |
| 84 | + |
| 85 | + # Write config with wrong timeout list size (6 instead of 7) |
| 86 | + invalid_config = copy.deepcopy(self.config_data) |
| 87 | + invalid_config['script_list'][0]['weekly_timeouts'] = [100, 200, 300, 400, 500, 600] |
| 88 | + with open(self.config_yml_path, 'w', encoding='utf-8') as f: |
| 89 | + yaml.dump(invalid_config, f) |
| 90 | + |
| 91 | + with self.assertRaises(AssertionError): |
| 92 | + launcher.generate_OneDragon_script_chain() |
| 93 | + |
| 94 | + @patch('launcher.get_path_under_onedragon') |
| 95 | + @patch('subprocess.run') |
| 96 | + def test_run_launcher(self, mock_run, mock_get_path): |
| 97 | + mock_get_path.return_value = "/mock/src/dir" |
| 98 | + |
| 99 | + mock_res = MagicMock() |
| 100 | + mock_res.returncode = 123 |
| 101 | + mock_run.return_value = mock_res |
| 102 | + |
| 103 | + ret = launcher.run_launcher() |
| 104 | + |
| 105 | + self.assertEqual(ret, 123) |
| 106 | + mock_get_path.assert_called_once_with("src") |
| 107 | + mock_run.assert_called_once() |
| 108 | + args, kwargs = mock_run.call_args |
| 109 | + self.assertEqual(kwargs['cwd'], "/mock/src/dir") |
| 110 | + self.assertIn("script_chainer.win_exe.launcher", args[0]) |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + unittest.main() |
0 commit comments