Changeset 40897
- Timestamp:
- Oct 3, 2019, 11:51:21 AM (7 years ago)
- Location:
- branches/ipp-132_automate_jira_conf_backups
- Files:
-
- 1 deleted
- 6 edited
-
backups/backup_atlassian_applications.py (modified) (3 diffs)
-
backups/backup_atlassian_applications_test.py (modified) (2 diffs)
-
backups/full_atlassian_backup_test.py (modified) (2 diffs)
-
backups/utils (deleted)
-
utils/python/generic_utils.py (modified) (3 diffs)
-
utils/python/subprocess_utils.py (modified) (6 diffs)
-
utils/python/subprocess_utils_test.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-132_automate_jira_conf_backups/backups/backup_atlassian_applications.py
r40896 r40897 13 13 # a restructure would be needed 14 14 # Ideally, the utils should be added to the path. 15 import utils.subprocess_utils as sub_utils16 17 18 DEFAULT_CONFIG_FILE = path. abspath('./atlassian_backups.config')15 import subprocess_utils as sub_utils 16 17 18 DEFAULT_CONFIG_FILE = path.join(path.dirname(__file__), 'atlassian_backups.config') 19 19 20 20 … … 236 236 jira_tar_name = datetime.date.today().strftime("%Y_%m_%d") + "_jira_attachments.tar" 237 237 jira_tar_filepath = path.join(latest_paths[0], jira_tar_name) 238 jira_tar_args = ["- cf", jira_tar_filepath, self.jira_attachments_path]238 jira_tar_args = ["-C", "/", "-cf", jira_tar_filepath, self.jira_attachments_path.strip('/')] 239 239 sub_utils.tar_wrapper(jira_tar_args) 240 240 sub_utils.chmod_wrapper(['774', jira_tar_filepath]) … … 246 246 confluence_tar_name = datetime.date.today().strftime("%Y_%m_%d") + "_confluence_attachments.tar" 247 247 confluence_tar_filepath = path.join(latest_paths[0], confluence_tar_name) 248 confluence_tar_args = ["- cf", confluence_tar_filepath, self.conf_attachments_path]248 confluence_tar_args = ["-C", "/", "-cf", confluence_tar_filepath, self.conf_attachments_path.strip('/')] 249 249 sub_utils.tar_wrapper(confluence_tar_args) 250 250 sub_utils.chmod_wrapper(['774', confluence_tar_filepath]) -
branches/ipp-132_automate_jira_conf_backups/backups/backup_atlassian_applications_test.py
r40894 r40897 8 8 9 9 import full_atlassian_backup_test as fab_test 10 import utils.subprocess_utils as sub_utils10 import subprocess_utils as sub_utils 11 11 12 12 from backup_atlassian_applications import AtlassianBackups 13 13 14 TEST_DEFAULT_CONFIG_FILE = "./testing/test.config"14 TEST_DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), './testing/test.config') 15 15 16 16 … … 47 47 48 48 def test_default_config_loads_with_no_args(self): 49 assert os.path.exists("./atlassian_backups.config") 49 default_config = baa.DEFAULT_CONFIG_FILE 50 assert os.path.exists(default_config) 50 51 ab = AtlassianBackups() 51 52 assert ab.host_backup_path == "/export/ippops4.0/atlassian_backups" -
branches/ipp-132_automate_jira_conf_backups/backups/full_atlassian_backup_test.py
r40893 r40897 6 6 7 7 from backup_atlassian_applications import AtlassianBackups 8 9 TEST_DEFAULT_CONFIG_FILE = "./testing/test.config"10 11 8 12 9 def create_all_paths_and_folders(ab_class: AtlassianBackups, tmpdir) -> AtlassianBackups: … … 140 137 141 138 def test_entire_process(self, tmpdir): 142 ab = AtlassianBackups(config_file=TEST_DEFAULT_CONFIG_FILE) 139 test_config_file = os.path.join(os.path.dirname(__file__), 'testing', 'test.config') 140 141 ab = AtlassianBackups(config_file=test_config_file) 143 142 ab = create_all_paths_and_folders(ab, tmpdir) 144 143 -
branches/ipp-132_automate_jira_conf_backups/utils/python/generic_utils.py
r40895 r40897 16 16 17 17 if source_file is None or destination_path is None: 18 print("Error: source for copy cannot be None type") 19 raise ValidationError 18 raise ValidationError("Error: source for copy cannot be None type") 20 19 if destination_path is None: 21 print("Error: copy destination cannot be None type") 22 raise ValidationError 20 raise ValidationError("Error: copy destination cannot be None type") 23 21 if not path.exists(source_file): 24 print("Error: Copy Failure: source does not exist: ", source_file) 25 raise ValidationError 22 raise ValidationError(f"Error: Copy Failure: source does not exist: {source_file}") 26 23 if not path.exists(destination_path) or not path.isdir(destination_path): 27 24 print("Error: Copy Failure: destination path does not exist: ", destination_path) 28 raise ValidationError 25 raise ValidationError(f"Error: Copy Failure: destination path does not exist: {destination_path}") 29 26 30 27 try: 31 28 if verbose: 32 print( "Copying: {0} to {1}".format(source_file, destination_path))29 print(f"Copying: {source_file} to {destination_path}") 33 30 if path.isdir(source_file): 34 31 copy_tree(source_file, destination_path) … … 36 33 shutil.copy2(source_file, destination_path) 37 34 except IOError as e: 38 print("Error: copy failure.\n copying: {0}\n to: {1}".format(e.filename, e.filename2)) 39 print(e) 40 raise IOError 35 msg = f"Error: copy failure.\n copying: {source_file}\n to: {destination_path}" 36 raise IOError(msg) from e 41 37 return 0 42 38 … … 48 44 49 45 if source_paths is None: 50 print("Error: source for copy cannot be None type") 51 raise ValidationError 46 raise ValidationError("Error: source for copy cannot be None type") 52 47 if destination_paths is None: 53 print("Error: copy destination cannot be None type") 54 raise ValidationError 48 raise ValidationError("Error: copy destination cannot be None type") 55 49 56 50 if type(source_paths) is not list or len(source_paths) == 0: 57 print("Error: sources must be given as a list with at least one entry") 58 raise ValidationError 51 raise ValidationError("Error: sources must be given as a list with at least one entry") 59 52 if type(destination_paths) is not list or len(destination_paths) == 0: 60 print("Error: destinations must be given as a list with at least one entry") 61 raise ValidationError 53 raise ValidationError("Error: destinations must be given as a list with at least one entry") 62 54 63 55 for dpath in destination_paths: 64 56 if not path.exists(dpath): 65 print("Copy Warning: target path does not exist (skipping): ", dpath) 57 if verbose: 58 print("Copy Warning: target path does not exist (skipping): ", dpath) 66 59 continue 67 60 if not path.isdir(dpath): 68 print("Copy Warning: target path is not a directory (skipping): ", ) 61 if verbose: 62 print("Copy Warning: target path is not a directory (skipping): ", ) 69 63 continue 70 64 -
branches/ipp-132_automate_jira_conf_backups/utils/python/subprocess_utils.py
r40895 r40897 2 2 3 3 # Plans are to flesh this out a bit more to make some smarter copying etc 4 from generic_utils import ValidationError 4 5 5 6 … … 10 11 def _args_check(args, function_name): 11 12 if args is None: 12 print("Error: no arguments provided in ", function_name) 13 return 2 13 raise ValidationError(f"Error: no arguments provided in {function_name}") 14 14 if type(args) != list or len(args) == 0: 15 print("Error: at least 1 arg must be given for ", function_name) 16 return 2 15 raise ValidationError(f"Error: no arguments provided in {function_name}") 17 16 return 0 18 17 … … 20 19 def cp_wrapper(args: [str]): 21 20 if args is None: 22 r eturn 221 raise ValidationError(f"Error: no arguments provided to {cp_wrapper.__name__}") 23 22 command_and_args = ["cp"] + args 24 23 return simple_unix_wrapper(command_and_args) … … 27 26 def tar_wrapper(args: [str]): 28 27 if args is None: 29 r eturn 228 raise ValidationError(f"Error: no arguments provided to {tar_wrapper.__name__}") 30 29 command_and_args = ["tar"] + args 31 30 return simple_unix_wrapper(command_and_args) … … 34 33 def chmod_wrapper(args: [str]): 35 34 if args is None: 36 r eturn 235 raise ValidationError(f"Error: no arguments provided to {chmod_wrapper.__name__}") 37 36 command_and_args = ["chmod"] + args 38 37 return simple_unix_wrapper(command_and_args) … … 44 43 Only a single command should be called. 45 44 """ 46 arg_check = _args_check(command_and_args, simple_unix_wrapper.__name__) 47 if arg_check != 0: 48 return arg_check 45 _args_check(command_and_args, simple_unix_wrapper.__name__) 49 46 50 47 try: 51 48 subprocess.check_call(command_and_args) 52 except subprocess.CalledProcessError ase:53 print("CalledProcessError in python subprocess: ")54 print(e)55 r eturn 249 except (subprocess.CalledProcessError) as cpe: 50 raise SubprocessError(f"{type(cpe)} in python simple_unix_wrapper") from cpe 51 except (ValidationError) as ve: 52 raise ValidationError(f"{type(ve)} in python simple_unix_wrapper") from ve 56 53 return 0 -
branches/ipp-132_automate_jira_conf_backups/utils/python/subprocess_utils_test.py
r40885 r40897 1 1 import subprocess_utils as utils 2 2 import os 3 import pytest 4 import stat 3 5 4 6 from pathlib import Path 7 from generic_utils import ValidationError 5 8 6 9 … … 15 18 class TestCopyWrapper(object): 16 19 20 def test_copy_raises_validation_error_with_no_args(self): 21 with pytest.raises(ValidationError): 22 utils.cp_wrapper(None) 23 17 24 def test_copy_fails_with_invalid_args(self, tmpdir): 18 result = utils.cp_wrapper(["not a real file", "not a valid copy_location"])19 assert result == 225 with pytest.raises(utils.SubprocessError): 26 utils.cp_wrapper(["not a real file", "not a valid copy_location"]) 20 27 21 28 def test_copy_fails_if_source_does_not_exist(self, tmpdir): 22 result = utils.cp_wrapper(["not a real file", tmpdir])23 assert result == 229 with pytest.raises(utils.SubprocessError): 30 utils.cp_wrapper(["not a real file", tmpdir]) 24 31 25 32 def test_simple_copy(self, tmpdir): … … 68 75 assert not os.path.exists(expected_filepath_b) 69 76 70 utils.cp_wrapper([subdir, destination_dir]) 77 # fails without -r arg: 78 with pytest.raises(utils.SubprocessError): 79 utils.cp_wrapper([subdir, destination_dir]) 71 80 assert not os.path.exists(expected_filepath_a) 72 81 assert not os.path.exists(expected_filepath_b) 82 83 utils.cp_wrapper(["-r", subdir, destination_dir]) 84 assert os.path.exists(expected_filepath_a) 85 assert os.path.exists(expected_filepath_b) 73 86 74 87 75 88 class TestTarWrapper(object): 76 89 77 def test_tar_returns_safely_with_none_args(self): 78 79 result = utils.tar_wrapper(None) 80 assert result == 2 90 def test_tar_raise_validation_error_with_none_args(self): 91 with pytest.raises(ValidationError): 92 utils.tar_wrapper(None) 81 93 82 94 def test_tar_returns_safely_with_too_few_args(self): 83 84 result = utils.tar_wrapper(["only one arg"]) 85 assert result == 2 95 with pytest.raises(utils.SubprocessError): 96 utils.tar_wrapper(["only one arg"]) 86 97 87 98 def test_tar_return_error_if_invalid_args_given(self, tmpdir): 88 89 99 tar_filename = os.path.join(tmpdir, "tar_file.tar") 90 100 fake_file = os.path.join(tmpdir, "fake_file.txt") 91 101 assert not os.path.exists(fake_file) 92 result = utils.tar_wrapper(["-cf", tar_filename, fake_file])93 assert result == 2102 with pytest.raises(utils.SubprocessError): 103 utils.tar_wrapper(["-cf", tar_filename, fake_file]) 94 104 95 105 def test_simple_tar_of_files(self, tmpdir): … … 102 112 args = ["-cf", expected_file, file_a, file_b] 103 113 utils.tar_wrapper(args) 114 assert os.path.exists(expected_file) 104 115 105 assert os.path.exists(expected_file) 116 117 class TestChmodWrapper(object): 118 119 def test_chmod_gives_validation_error_when_no_args(self): 120 with pytest.raises(utils.ValidationError): 121 utils.chmod_wrapper(None) 122 123 def test_chmod_wrapper_raises_error_with_invalid_args(self): 124 with pytest.raises(utils.SubprocessError): 125 utils.chmod_wrapper(["only one arg"]) 126 127 def test_chmod_fails_if_no_permissions_given(self, tmpdir): 128 real_file = make_a_real_file(tmpdir, 'mr_file') 129 130 with pytest.raises(utils.SubprocessError): 131 utils.chmod_wrapper([real_file]) 132 133 with pytest.raises(utils.SubprocessError): 134 utils.chmod_wrapper(["dumb_arg", real_file]) 135 136 def test_can_chmod_file(self, tmpdir): 137 real_file = make_a_real_file(tmpdir, 'mr_file') 138 139 # check initial permissions 140 org_stats = os.stat(real_file) 141 # Default should be: rw-rw-r-- 142 assert bool(org_stats.st_mode & stat.S_IRUSR) is True 143 assert bool(org_stats.st_mode & stat.S_IWUSR) is True 144 assert bool(org_stats.st_mode & stat.S_IXUSR) is False 145 assert bool(org_stats.st_mode & stat.S_IRGRP) is True 146 assert bool(org_stats.st_mode & stat.S_IWGRP) is True 147 assert bool(org_stats.st_mode & stat.S_IXGRP) is False 148 assert bool(org_stats.st_mode & stat.S_IROTH) is True 149 assert bool(org_stats.st_mode & stat.S_IWOTH) is False 150 assert bool(org_stats.st_mode & stat.S_IXOTH) is False 151 152 # change them 153 utils.chmod_wrapper(["777", real_file]) 154 155 # check they changed 156 new_stats = os.stat(real_file) 157 assert bool(new_stats.st_mode & stat.S_IRUSR) is True 158 assert bool(new_stats.st_mode & stat.S_IWUSR) is True 159 assert bool(new_stats.st_mode & stat.S_IXUSR) is True 160 assert bool(new_stats.st_mode & stat.S_IRGRP) is True 161 assert bool(new_stats.st_mode & stat.S_IWGRP) is True 162 assert bool(new_stats.st_mode & stat.S_IXGRP) is True 163 assert bool(new_stats.st_mode & stat.S_IROTH) is True 164 assert bool(new_stats.st_mode & stat.S_IWOTH) is True 165 assert bool(new_stats.st_mode & stat.S_IXOTH) is True
Note:
See TracChangeset
for help on using the changeset viewer.
