def _cleanup_files(client_id, more_files=()): ''' Removes any files that might have been used by this client at some point. For simplicity's sake, we check both architectures. ''' # The following files may be vestiges from failed calls to delete-service, # old versions of installadm, etc. # More care must be taken to avoid unintentionally clobbering desired # config. cleanup = [_menulst_path(client_id), # x86 menu.lst file _pxegrub_path(client_id)[1], # x86 pxegrub symlink os.path.join(com.BOOT_DIR, client_id)] # SPARC symlink # Now add in any files passed by the caller cleanup.extend(more_files) # Search for pre-multihomed, subnet-specific SPARC directories # (e.g., /etc/netboot/192.168.0.0/010011AABB/) and add those. subnet = re.compile(dhcp.IP_PATTERN) for f in os.listdir(com.BOOT_DIR): if subnet.match(f): d = os.path.join(com.BOOT_DIR, f, client_id) if os.path.isdir(d): cleanup.append(d) # Finally, delete any files or directories from our list that # might exist on the system. for f in cleanup: if os.access(f, os.F_OK) or os.path.islink(f): force_delete(f)
def _cleanup_files(client_id, more_files=()): ''' Removes any files that might have been used by this client at some point. For simplicity's sake, we check both architectures. ''' # The following files may be vestiges from failed calls to delete-service, # old versions of installadm, etc. # More care must be taken to avoid unintentionally clobbering desired # config. cleanup = [ _menulst_path(client_id), # x86 menu.lst file _pxegrub_path(client_id)[1], # x86 pxegrub symlink os.path.join(com.BOOT_DIR, client_id) ] # SPARC symlink for boot_type in grub.NBP_TYPE.values(): cleanup.append(os.path.join(com.BOOT_DIR, client_id + '.' + boot_type)) # Now add in any files passed by the caller cleanup.extend(more_files) # Search for pre-multihomed, subnet-specific SPARC directories # (e.g., /etc/netboot/192.168.0.0/010011AABB/) and add those. subnet = re.compile(dhcp.IP_PATTERN) for f in os.listdir(com.BOOT_DIR): if subnet.match(f): d = os.path.join(com.BOOT_DIR, f, client_id) if os.path.isdir(d): cleanup.append(d) # Finally, delete any files or directories from our list that # might exist on the system. for f in cleanup: if os.access(f, os.F_OK) or os.path.islink(f): force_delete(f)
def test_nonexistent_file(self): '''force_delete does NOT fail on nonexistent paths''' path = tempfile.mktemp('.noexist', '', self.tempdir) try: force_delete(path) except (OSError, IOError) as err: self.fail(err)
def test_full_dir(self): '''force_delete removes directories with files''' tempdir = tempfile.mkdtemp(".dir", '', self.tempdir) tempfile.mkstemp('.file', '', self.tempdir) force_delete(tempdir) self.assertFalse(os.path.exists(tempdir), "Directory %s was not deleted" % tempdir)
def test_basic_file(self): '''Test that force_delete handles normal files''' handle, filename = tempfile.mkstemp('.file', '', self.tempdir) os.close(handle) force_delete(filename) self.assertFalse(os.path.exists(filename), "File %s was not deleted" % filename)
def test_full_dir(self): '''force_delete removes directories with files''' tempdir = tempfile.mkdtemp(".dir", '', self.tempdir) tempfile.mkstemp('.file', '', self.tempdir) force_delete(tempdir) self.assertFalse(os.path.exists(tempdir), "Directory %s was not deleted" % tempdir)
def test_nonexistent_file(self): '''force_delete does NOT fail on nonexistent paths''' path = tempfile.mktemp('.noexist', '', self.tempdir) try: force_delete(path) except (OSError, IOError) as err: self.fail(err)
def test_basic_file(self): '''Test that force_delete handles normal files''' handle, filename = tempfile.mkstemp('.file', '', self.tempdir) os.close(handle) force_delete(filename) self.assertFalse(os.path.exists(filename), "File %s was not deleted" % filename)
def test_dangling_symlink(self): '''force_delete removes dangling symlinks''' # mktemp (not mkstemp) used, so that the file is NOT created random_name = tempfile.mktemp() symlink_path = tempfile.mktemp('.symlink', '', self.tempdir) os.symlink(random_name, symlink_path) force_delete(symlink_path) self.assertFalse(os.path.exists(symlink_path), "Symlink %s was not deleted" % symlink_path)
def test_dangling_symlink(self): '''force_delete removes dangling symlinks''' # mktemp (not mkstemp) used, so that the file is NOT created random_name = tempfile.mktemp() symlink_path = tempfile.mktemp('.symlink', '', self.tempdir) os.symlink(random_name, symlink_path) force_delete(symlink_path) self.assertFalse(os.path.exists(symlink_path), "Symlink %s was not deleted" % symlink_path)
def test_symlink_dir(self): '''force_delete removes symlinks to directories''' tempdir = tempfile.mkdtemp(".dir", '', self.tempdir) symlink_path = tempfile.mktemp('.symlink', '', self.tempdir) os.symlink(tempdir, symlink_path) force_delete(symlink_path) self.assertFalse(os.path.exists(symlink_path), "Symlink %s was not deleted" % symlink_path) self.assertTrue(os.path.exists(tempdir), "force_delete removed the directory target, %s" % tempdir)
def test_symlink_file(self): '''force_delete removes symlinks to files''' handle, filename = tempfile.mkstemp('.file', '', self.tempdir) os.close(handle) symlink_path = tempfile.mktemp('.symlink', '', self.tempdir) os.symlink(filename, symlink_path) force_delete(symlink_path) self.assertFalse(os.path.exists(symlink_path), "Symlink %s was not deleted" % symlink_path) self.assertTrue(os.path.exists(filename), "force_delete removed the file target, %s" % filename)
def test_symlink_dir(self): '''force_delete removes symlinks to directories''' tempdir = tempfile.mkdtemp(".dir", '', self.tempdir) symlink_path = tempfile.mktemp('.symlink', '', self.tempdir) os.symlink(tempdir, symlink_path) force_delete(symlink_path) self.assertFalse(os.path.exists(symlink_path), "Symlink %s was not deleted" % symlink_path) self.assertTrue( os.path.exists(tempdir), "force_delete removed the directory target, %s" % tempdir)
def test_symlink_file(self): '''force_delete removes symlinks to files''' handle, filename = tempfile.mkstemp('.file', '', self.tempdir) os.close(handle) symlink_path = tempfile.mktemp('.symlink', '', self.tempdir) os.symlink(filename, symlink_path) force_delete(symlink_path) self.assertFalse(os.path.exists(symlink_path), "Symlink %s was not deleted" % symlink_path) self.assertTrue(os.path.exists(filename), "force_delete removed the file target, %s" % filename)
def test_empty_dir(self): '''force_delete removes empty directories''' tempdir = tempfile.mkdtemp(".dir", '', self.tempdir) force_delete(tempdir) self.assertFalse(os.path.exists(tempdir), "Directory %s was not deleted" % tempdir)
def test_empty_dir(self): '''force_delete removes empty directories''' tempdir = tempfile.mkdtemp(".dir", '', self.tempdir) force_delete(tempdir) self.assertFalse(os.path.exists(tempdir), "Directory %s was not deleted" % tempdir)