def testSFileWindowsPathEscape(self): orig_path = "C:\\Program Files\\myfile" path = SFile.windows_path_escape(orig_path) self.assertEqual("C___Program Files\\myfile", path) path = SFile.windows_path_escape(path) self.assertEqual(orig_path, path)
def testIisService(self): # backup the iis conf directory and document roots test_backup_dir = os.path.join(self.basedir, "snap-iis-test") if os.path.isdir(test_backup_dir): shutil.rmtree(test_backup_dir) shutil.copytree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, os.path.join(test_backup_dir, "conf")) # run the backup test_base_dir = os.path.join(self.basedir, "snap-iis-basedir") backend = snap.backends.services.adapters.iis.Iis() backend.backup(test_base_dir) # ensure conf.d and document root were backed up bfiles = [] for root, dirs, files in os.walk(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT): for hfile in files: rfile = os.path.join(root, hfile) ffile = os.path.join(test_base_dir, rfile) self.assertTrue(os.path.isfile(ffile)) bfiles.append(os.path.join(root, hfile)) self.assertTrue(os.path.isfile(os.path.join(test_base_dir, "service-iis.xml"))) # run the restore shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ignore_errors=True) backend.restore(test_base_dir) # ensure the files backed up were restored for hfile in bfiles: self.assertTrue(os.path.isfile(hfile)) # ensure the features are enabled self.assertTrue( snap.backends.services.windowsdispatcher.WindowsDispatcher.is_feature_enabled( snap.backends.services.adapters.iis.Iis.WEBSERVER_FEATURE ) ) # restore backup shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ignore_errors=True) for root, dirs, files in os.walk(os.path.join(test_backup_dir, "conf")): for idir in dirs: fdir = os.path.join(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, idir) if not os.path.isdir(fdir): os.makedirs(fdir) for ifile in files: sfile = os.path.join(root, ifile) ffile = os.path.join(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ifile) if not os.path.isfile(ffile): shutil.copy(os.path.join(root, ifile), ffile) os.chmod(sfile, stat.S_IWRITE) os.remove(sfile) for bfile in bfiles: fbfile = os.path.join(test_base_dir, SFile.windows_path_escape(bfile)) os.chmod(fbfile, stat.S_IWRITE) os.remove(fbfile) shutil.rmtree(test_backup_dir)
def testBackupCertainFiles(self): os.mkdir(os.path.join(self.fs_root, "subdir1")) os.mkdir(os.path.join(self.fs_root, "subdir1", "subsubdir")) os.mkdir(os.path.join(self.fs_root, "subdir2")) f = open(os.path.join(self.fs_root, "subdir1", "foo"), 'w') f.write("foo") f.close() f = open(os.path.join(self.fs_root, "subdir2", "bar"), 'w') f.write("bar") f.close() f = open(os.path.join(self.fs_root, "subdir1", "subsubdir", "money"), 'w') f.write("money") f.close() backup_target = snap.backends.files.win.Win() backup_target.backup( self.basedir, include=[os.path.join(self.fs_root, "subdir1")], exclude=[os.path.join(self.fs_root, "subdir1", "subsubdir")]) self.assertTrue( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "foo"))))) self.assertFalse( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape( os.path.join(self.fs_root, "subdir2", "foo"))))) self.assertFalse( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "subsubdir", "money"))))) record = FilesRecordFile(os.path.join(self.basedir, "files.xml")) files = record.read() file_names = [] for sfile in files: file_names.append(sfile.path) self.assertEqual(1, len(files)) self.assertIn( SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "foo")), file_names) self.assertNotIn( SFile.windows_path_escape( os.path.join(self.fs_root, "subdir2", "bar")), file_names) self.assertNotIn( SFile.windows_path_escape( os.path.join(self.fs_root, "subdir1", "subsubdir", "bar")), file_names)
def testBackupCertainFiles(self): os.mkdir(os.path.join(self.fs_root, "subdir1")) os.mkdir(os.path.join(self.fs_root, "subdir1", "subsubdir")) os.mkdir(os.path.join(self.fs_root, "subdir2")) f = open(os.path.join(self.fs_root, "subdir1", "foo"), "w") f.write("foo") f.close() f = open(os.path.join(self.fs_root, "subdir2", "bar"), "w") f.write("bar") f.close() f = open(os.path.join(self.fs_root, "subdir1", "subsubdir", "money"), "w") f.write("money") f.close() backup_target = snap.backends.files.win.Win() backup_target.backup( self.basedir, include=[os.path.join(self.fs_root, "subdir1")], exclude=[os.path.join(self.fs_root, "subdir1", "subsubdir")], ) self.assertTrue( os.path.exists( os.path.join(self.basedir, SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "foo"))) ) ) self.assertFalse( os.path.exists( os.path.join(self.basedir, SFile.windows_path_escape(os.path.join(self.fs_root, "subdir2", "foo"))) ) ) self.assertFalse( os.path.exists( os.path.join( self.basedir, SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "subsubdir", "money")) ) ) ) record = FilesRecordFile(os.path.join(self.basedir, "files.xml")) files = record.read() file_names = [] for sfile in files: file_names.append(sfile.path) self.assertEqual(1, len(files)) self.assertIn(SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "foo")), file_names) self.assertNotIn(SFile.windows_path_escape(os.path.join(self.fs_root, "subdir2", "bar")), file_names) self.assertNotIn( SFile.windows_path_escape(os.path.join(self.fs_root, "subdir1", "subsubdir", "bar")), file_names )
drives = c.split()[1:] # loop through each drive and determine which are available for drive in drives: include_drive = True try: os.listdir(drive) except WindowsError, e: include_drive = False if include_drive: include.append(drive) # else apply path manipulation to specified includes else: for i in range(len(include)): include[i] = SFile.windows_path_escape(include[i]) for additional_exclude in Win.EXCLUDE_DIRS: if not additional_exclude in exclude: exclude.append(additional_exclude) # remove duplicates include = list(set(include)) exclude = list(set(exclude)) # determine which files have been modified since installation # and copy those to basedir sfiles = [] files = FileManager.get_all_files(include, exclude) for tfile in files: if snap.config.options.log_level_at_least('verbose'):
def testIisService(self): # backup the iis conf directory and document roots test_backup_dir = os.path.join(self.basedir, "snap-iis-test") if os.path.isdir(test_backup_dir): shutil.rmtree(test_backup_dir) shutil.copytree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, os.path.join(test_backup_dir, "conf")) # run the backup test_base_dir = os.path.join(self.basedir, "snap-iis-basedir") backend = snap.backends.services.adapters.iis.Iis() backend.backup(test_base_dir) # ensure conf.d and document root were backed up bfiles = [] for root, dirs, files in os.walk( snap.backends.services.adapters.iis.Iis.CONFIG_ROOT): for hfile in files: rfile = os.path.join(root, hfile) ffile = os.path.join(test_base_dir, rfile) self.assertTrue(os.path.isfile(ffile)) bfiles.append(os.path.join(root, hfile)) self.assertTrue( os.path.isfile(os.path.join(test_base_dir, "service-iis.xml"))) # run the restore shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ignore_errors=True) backend.restore(test_base_dir) # ensure the files backed up were restored for hfile in bfiles: self.assertTrue(os.path.isfile(hfile)) # ensure the features are enabled self.assertTrue( snap.backends.services.windowsdispatcher.WindowsDispatcher. is_feature_enabled( snap.backends.services.adapters.iis.Iis.WEBSERVER_FEATURE)) # restore backup shutil.rmtree(snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ignore_errors=True) for root, dirs, files in os.walk(os.path.join(test_backup_dir, "conf")): for idir in dirs: fdir = os.path.join( snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, idir) if not os.path.isdir(fdir): os.makedirs(fdir) for ifile in files: sfile = os.path.join(root, ifile) ffile = os.path.join( snap.backends.services.adapters.iis.Iis.CONFIG_ROOT, ifile) if not os.path.isfile(ffile): shutil.copy(os.path.join(root, ifile), ffile) os.chmod(sfile, stat.S_IWRITE) os.remove(sfile) for bfile in bfiles: fbfile = os.path.join(test_base_dir, SFile.windows_path_escape(bfile)) os.chmod(fbfile, stat.S_IWRITE) os.remove(fbfile) shutil.rmtree(test_backup_dir)