|
24 | 24 | import io
|
25 | 25 | import sys
|
26 | 26 | from unittest.mock import call
|
| 27 | +from pathlib import Path |
| 28 | +import shutil |
| 29 | + |
27 | 30 | from wifi_client_test_netplan import (
|
28 | 31 | netplan_renderer,
|
29 | 32 | check_and_get_renderer,
|
|
38 | 41 | print_journal_entries,
|
39 | 42 | main,
|
40 | 43 | get_interface_info,
|
| 44 | + restore_netplan_files, |
| 45 | + backup_netplan_files, |
| 46 | + cleanup_backup, |
41 | 47 | )
|
42 | 48 |
|
43 | 49 |
|
@@ -931,3 +937,173 @@ def test_main_ping_test_failure(
|
931 | 937 | self.assertEqual(mock_apply.call_count, 2)
|
932 | 938 | self.assertEqual(mock_print_journal.call_count, 1)
|
933 | 939 | mock_ping.assert_called_once_with("wlan0", "networkd")
|
| 940 | + |
| 941 | + |
| 942 | +# Mock BACKUP_PATH for testing |
| 943 | +BACKUP_PATH = "/tmp/test_netplan_backup" |
| 944 | +NETPLAN_DIR = "/tmp/lib/netplan" |
| 945 | + |
| 946 | + |
| 947 | +@patch("wifi_client_test_netplan.BACKUP_PATH", "/tmp/test_netplan_backup") |
| 948 | +@patch("wifi_client_test_netplan.NETPLAN_DIR", "/tmp/lib/netplan") |
| 949 | +class TestNetplanBackupFunctions(TestCase): |
| 950 | + |
| 951 | + def setUp(self): |
| 952 | + self.assertEqual(BACKUP_PATH, "/tmp/test_netplan_backup") |
| 953 | + self.assertEqual(NETPLAN_DIR, "/tmp/lib/netplan") |
| 954 | + """Set up test fixtures before each test method.""" |
| 955 | + Path(BACKUP_PATH).mkdir(parents=True, exist_ok=True) |
| 956 | + Path(NETPLAN_DIR).mkdir(parents=True, exist_ok=True) |
| 957 | + |
| 958 | + def tearDown(self): |
| 959 | + """Clean up after each test method.""" |
| 960 | + # Clean up test directories |
| 961 | + shutil.rmtree(Path(BACKUP_PATH), ignore_errors=True) |
| 962 | + shutil.rmtree(Path(NETPLAN_DIR), ignore_errors=True) |
| 963 | + |
| 964 | + @patch("glob.glob") |
| 965 | + @patch("builtins.print") |
| 966 | + def test_backup_netplan_files_no_files_found(self, mock_print, mock_glob): |
| 967 | + """Test backup when no YAML files are found.""" |
| 968 | + mock_glob.return_value = [] |
| 969 | + |
| 970 | + result = backup_netplan_files() |
| 971 | + self.assertTrue(result) |
| 972 | + |
| 973 | + @patch("glob.glob") |
| 974 | + @patch("shutil.copy2") |
| 975 | + @patch("pathlib.Path.mkdir") |
| 976 | + @patch("builtins.print") |
| 977 | + def test_backup_netplan_files_success( |
| 978 | + self, mock_print, mock_mkdir, mock_copy2, mock_glob |
| 979 | + ): |
| 980 | + """Test successful backup of netplan files.""" |
| 981 | + mock_glob.return_value = [ |
| 982 | + NETPLAN_DIR + "/config1.yaml", |
| 983 | + NETPLAN_DIR + "/config2.yaml", |
| 984 | + ] |
| 985 | + |
| 986 | + result = backup_netplan_files() |
| 987 | + |
| 988 | + self.assertTrue(result) |
| 989 | + self.assertEqual(mock_copy2.call_count, 2) |
| 990 | + mock_mkdir.assert_called_once_with(parents=True, exist_ok=True) |
| 991 | + |
| 992 | + @patch("os.path.exists") |
| 993 | + @patch("glob.glob") |
| 994 | + @patch("os.remove") |
| 995 | + @patch("os.makedirs") |
| 996 | + @patch("shutil.copy2") |
| 997 | + @patch("builtins.print") |
| 998 | + def test_restore_netplan_files_success( |
| 999 | + self, |
| 1000 | + mock_print, |
| 1001 | + mock_copy2, |
| 1002 | + mock_makedirs, |
| 1003 | + mock_remove, |
| 1004 | + mock_glob, |
| 1005 | + mock_exists, |
| 1006 | + ): |
| 1007 | + """Test successful restore of netplan files.""" |
| 1008 | + mock_exists.return_value = True |
| 1009 | + mock_glob.side_effect = [ |
| 1010 | + [NETPLAN_DIR + "/old1.yaml"], # Existing files to remove |
| 1011 | + [ |
| 1012 | + f"{BACKUP_PATH}/config1.yaml", |
| 1013 | + f"{BACKUP_PATH}/config2.yaml", |
| 1014 | + ], # Backup files |
| 1015 | + ] |
| 1016 | + |
| 1017 | + result = restore_netplan_files() |
| 1018 | + |
| 1019 | + self.assertTrue(result) |
| 1020 | + mock_remove.assert_called_once_with(NETPLAN_DIR + "/old1.yaml") |
| 1021 | + self.assertEqual(mock_copy2.call_count, 2) |
| 1022 | + |
| 1023 | + @patch("os.path.exists") |
| 1024 | + @patch("glob.glob") |
| 1025 | + @patch("os.remove") |
| 1026 | + @patch("shutil.copy2") |
| 1027 | + @patch("builtins.print") |
| 1028 | + def test_restore_netplan_files_copy_error( |
| 1029 | + self, mock_print, mock_copy2, mock_remove, mock_glob, mock_exists |
| 1030 | + ): |
| 1031 | + """Test restore when copy operation fails.""" |
| 1032 | + mock_exists.return_value = True |
| 1033 | + mock_glob.side_effect = [[], [f"{BACKUP_PATH}/config1.yaml"]] |
| 1034 | + mock_copy2.side_effect = OSError("Permission denied") |
| 1035 | + |
| 1036 | + result = restore_netplan_files() |
| 1037 | + |
| 1038 | + self.assertFalse(result) |
| 1039 | + |
| 1040 | + @patch("os.path.exists") |
| 1041 | + @patch("builtins.print") |
| 1042 | + def test_cleanup_backup_not_exists(self, mock_print, mock_exists): |
| 1043 | + """Test cleanup when backup directory doesn't exist.""" |
| 1044 | + mock_exists.return_value = False |
| 1045 | + |
| 1046 | + result = cleanup_backup() |
| 1047 | + |
| 1048 | + self.assertTrue(result) |
| 1049 | + |
| 1050 | + @patch("os.path.exists") |
| 1051 | + @patch("shutil.rmtree") |
| 1052 | + @patch("builtins.print") |
| 1053 | + def test_cleanup_backup_success( |
| 1054 | + self, mock_print, mock_rmtree, mock_exists |
| 1055 | + ): |
| 1056 | + """Test successful cleanup of backup directory.""" |
| 1057 | + mock_exists.return_value = True |
| 1058 | + |
| 1059 | + result = cleanup_backup() |
| 1060 | + |
| 1061 | + self.assertTrue(result) |
| 1062 | + mock_rmtree.assert_called_once_with(BACKUP_PATH) |
| 1063 | + |
| 1064 | + @patch("os.path.exists") |
| 1065 | + @patch("shutil.rmtree") |
| 1066 | + @patch("builtins.print") |
| 1067 | + def test_cleanup_backup_error(self, mock_print, mock_rmtree, mock_exists): |
| 1068 | + """Test cleanup when rmtree operation fails.""" |
| 1069 | + mock_exists.return_value = True |
| 1070 | + mock_rmtree.side_effect = OSError("Permission denied") |
| 1071 | + |
| 1072 | + result = cleanup_backup() |
| 1073 | + |
| 1074 | + self.assertFalse(result) |
| 1075 | + |
| 1076 | + @patch("os.path.exists") |
| 1077 | + @patch("glob.glob") |
| 1078 | + @patch("os.remove") |
| 1079 | + @patch("builtins.print") |
| 1080 | + def test_restore_netplan_files_remove_error( |
| 1081 | + self, mock_print, mock_remove, mock_glob, mock_exists |
| 1082 | + ): |
| 1083 | + """Test restore when removing existing files fails.""" |
| 1084 | + mock_exists.return_value = True |
| 1085 | + mock_glob.side_effect = [ |
| 1086 | + [NETPLAN_DIR + "/old1.yaml"], # Existing files to remove |
| 1087 | + [f"{BACKUP_PATH}/config1.yaml"], # Backup files |
| 1088 | + ] |
| 1089 | + mock_remove.side_effect = OSError("Permission denied") |
| 1090 | + |
| 1091 | + result = restore_netplan_files() |
| 1092 | + |
| 1093 | + self.assertFalse(result) |
| 1094 | + |
| 1095 | + @patch("os.path.exists") |
| 1096 | + @patch("glob.glob") |
| 1097 | + @patch("os.makedirs") |
| 1098 | + @patch("builtins.print") |
| 1099 | + def test_restore_netplan_files_makedirs_error( |
| 1100 | + self, mock_print, mock_makedirs, mock_glob, mock_exists |
| 1101 | + ): |
| 1102 | + """Test restore when makedirs operation fails.""" |
| 1103 | + mock_exists.return_value = True |
| 1104 | + mock_glob.side_effect = [[], [f"{BACKUP_PATH}/config1.yaml"]] |
| 1105 | + mock_makedirs.side_effect = OSError("Permission denied") |
| 1106 | + |
| 1107 | + result = restore_netplan_files() |
| 1108 | + |
| 1109 | + self.assertFalse(result) |
0 commit comments