def test_permissions_non_recursive(self): """ tests permissions assigns permissions recursively and writes to log """ test_permissions = [ [{'path': '/tmp/scripts_test/app_user/sites/app_name/source', 'dir_permissions': '500'}, 'dr-x------'], [{'path': '/tmp/scripts_test/app_user/sites/app_name/source/app_name', 'dir_permissions': '770'}, 'drwxrwx---'], [{'path': '/tmp/scripts_test/app_user/sites/app_name/source/app_name/file', 'file_permissions': '400'}, '-r--------'], ] app_home_nested_file = os.path.join(self.app_home, 'app_name', 'file') runlog = CommandFileUtils(self.dist_version, self.log_file, self.log_level) for i in test_permissions: os.makedirs(os.path.join(self.app_home, 'app_name')) with open(app_home_nested_file, 'w') as file: file.write('some text') runlog.permissions(**i[0]) self.assertEqual(i[1], stat.filemode(os.stat(i[0]['path']).st_mode), stat.filemode(os.stat(i[0]['path']).st_mode)) if os.path.isdir(i[0]['path']): self.log('INFO: changed permissions of %s to %s' % (i[0]['path'], i[0]['dir_permissions'])) elif os.path.isfile(i[0]['path']): self.log('INFO: changed permissions of %s to %s' % (i[0]['path'], i[0]['file_permissions'])) os.chmod(self.app_home, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) for root, dirs, files in os.walk(self.app_home): for name in dirs: os.chmod(os.path.join(root, name), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) shutil.rmtree(self.app_home)
def test_permissions_recursive(self): """ tests permissions assigns permissions recursively and writes to log """ test_permissions = [ ['500', '700', '-r-x------', 'drwx------'], ['400', '500', '-r--------', 'dr-x------'], ['550', '770', '-r-xr-x---', 'drwxrwx---'], ['440', '550', '-r--r-----', 'dr-xr-x---'], ['644', '755', '-rw-r--r--', 'drwxr-xr-x'], ['755', '755', '-rwxr-xr-x', 'drwxr-xr-x'] ] app_home_nested_file = os.path.join(self.app_home, 'app_name', 'file') runlog = CommandFileUtils(self.dist_version, self.log_file, self.log_level) for i in test_permissions: os.makedirs(os.path.join(self.app_home, 'app_name')) with open(app_home_nested_file, 'w') as file: file.write('some text') runlog.permissions(self.app_home, i[0], i[1], recursive=True) app_home_files = [] app_home_dirs = [] for root, dirs, files in os.walk(self.app_home): for name in files: app_home_files.append(os.path.join(root, name)) for name in dirs: app_home_dirs.append(os.path.join(root, name)) app_home_dirs.append(self.app_home) for a in app_home_files: self.assertEqual(i[2], stat.filemode(os.stat(a).st_mode), stat.filemode(os.stat(a).st_mode)) for a in app_home_dirs: self.assertEqual(i[3], stat.filemode(os.stat(a).st_mode), stat.filemode(os.stat(a).st_mode)) self.log('INFO: changed permissions of %s files to %s and directories to %s' % ( self.app_home, i[0], i[1] )) os.chmod(self.app_home, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) for root, dirs, files in os.walk(self.app_home): for name in dirs: os.chmod(os.path.join(root, name), stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) shutil.rmtree(self.app_home)