| 
									
										
										
										
											2024-09-12 12:55:28 -05:00
										 |  |  | # -*- coding: utf-8; -*- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 10:29:31 -06:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2024-09-12 12:55:28 -05:00
										 |  |  | from unittest import TestCase | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  | from unittest.mock import MagicMock, patch, call | 
					
						
							| 
									
										
										
										
											2024-09-12 12:55:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | from wuttamess import util as mod | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestExists(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_basic(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         mod.exists(c, "/foo") | 
					
						
							|  |  |  |         c.run.assert_called_once_with("test -e /foo", warn=True) | 
					
						
							| 
									
										
										
										
											2024-11-20 10:29:31 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-23 11:56:30 -06:00
										 |  |  | class TestHomePath(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_basic(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         c.run.return_value.stdout = "/home/foo" | 
					
						
							|  |  |  |         path = mod.get_home_path(c, user="foo") | 
					
						
							|  |  |  |         self.assertEqual(path, "/home/foo") | 
					
						
							| 
									
										
										
										
											2024-11-23 11:56:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  | class TestIsSymlink(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_yes(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							|  |  |  |         c.run.return_value.failed = False | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         self.assertTrue(mod.is_symlink(c, "/foo")) | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  |         c.run.assert_called_once_with('test -L "$(echo /foo)"', warn=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_no(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							|  |  |  |         c.run.return_value.failed = True | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         self.assertFalse(mod.is_symlink(c, "/foo")) | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  |         c.run.assert_called_once_with('test -L "$(echo /foo)"', warn=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 10:29:31 -06:00
										 |  |  | class TestMakoRenderer(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_basic(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         renderer = mod.mako_renderer(c, env={"machine_is_live": True}) | 
					
						
							| 
									
										
										
										
											2024-11-20 10:29:31 -06:00
										 |  |  |         here = os.path.dirname(__file__) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         path = os.path.join(here, "files", "bar", "baz") | 
					
						
							| 
									
										
										
										
											2024-11-20 10:29:31 -06:00
										 |  |  |         rendered = renderer(path, vars={}) | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         self.assertEqual(rendered, "machine_is_live = True") | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestSetTimezone(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_symlink(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         with patch.object(mod, "is_symlink") as is_symlink: | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  |             is_symlink.return_value = True | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |             mod.set_timezone(c, "America/Chicago") | 
					
						
							|  |  |  |             c.run.assert_has_calls( | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     call("bash -c 'echo America/Chicago > /etc/timezone'"), | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             c.run.assert_called_with( | 
					
						
							|  |  |  |                 "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_not_symlink(self): | 
					
						
							|  |  |  |         c = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |         with patch.object(mod, "is_symlink") as is_symlink: | 
					
						
							| 
									
										
										
										
											2024-11-20 20:52:29 -06:00
										 |  |  |             is_symlink.return_value = False | 
					
						
							| 
									
										
										
										
											2025-08-31 12:52:36 -05:00
										 |  |  |             mod.set_timezone(c, "America/Chicago") | 
					
						
							|  |  |  |             c.run.assert_has_calls( | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     call("bash -c 'echo America/Chicago > /etc/timezone'"), | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             c.run.assert_called_with( | 
					
						
							|  |  |  |                 "cp /usr/share/zoneinfo/America/Chicago /etc/localtime" | 
					
						
							|  |  |  |             ) |