def test_writer(self): td = tempfile.mkdtemp() try: gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir/z", self.lg) saved_tmppath = '' saved_fd = None with gdf.writer() as dw: assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") assert os.path.isdir(gdf.datadir) saved_tmppath = dw.tmppath assert os.path.dirname(saved_tmppath) == gdf.datadir assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) dw.write("123") saved_fd = dw.fd # At the end of previous with block a close on fd is called. # Calling os.close on the same fd will raise an OSError # exception and we must catch it. try: os.close(saved_fd) except OSError: pass else: self.fail("Exception expected") assert not os.path.exists(saved_tmppath) finally: shutil.rmtree(td)
def test_put_is_dir(self): td = tempfile.mkdtemp() the_path = os.path.join(td, "vol0", "bar") the_dir = os.path.join(the_path, "dir") try: os.makedirs(the_dir) gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir", self.lg) origmd = gdf.metadata.copy() origfmd = _metadata[the_dir] newmd = gdf.metadata.copy() # FIXME: This is a hack to get to the code-path; it is not clear # how this can happen normally. newmd['Content-Type'] = '' newmd['X-Object-Meta-test'] = '1234' with gdf.writer() as dw: try: dw.put(newmd, extension='.data') except DiskFileError: pass else: self.fail("Expected to encounter" " 'already-exists-as-dir' exception") assert gdf.metadata == origmd assert _metadata[the_dir] == origfmd finally: shutil.rmtree(td)
def test_put_obj_path(self): the_obj_path = os.path.join("b", "a") the_file = os.path.join(the_obj_path, "z") td = tempfile.mkdtemp() try: gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", the_file, self.lg) assert gdf._obj == "z" assert gdf._obj_path == the_obj_path assert gdf.name == os.path.join("bar", "b", "a") assert gdf.datadir == os.path.join(td, "vol0", "bar", "b", "a") assert gdf.data_file is None body = '1234\n' etag = md5() etag.update(body) etag = etag.hexdigest() metadata = { 'X-Timestamp': '1234', 'Content-Type': 'file', 'ETag': etag, 'Content-Length': '5', } with gdf.writer() as dw: assert dw.tmppath is not None tmppath = dw.tmppath dw.write(body) dw.put(metadata) assert gdf.data_file == os.path.join(td, "vol0", "bar", "b", "a", "z") assert os.path.exists(gdf.data_file) assert not os.path.exists(tmppath) finally: shutil.rmtree(td)
def test_writer_err_on_unlink(self): td = tempfile.mkdtemp() try: gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir/z", self.lg) saved_tmppath = '' with gdf.writer() as dw: assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") assert os.path.isdir(gdf.datadir) saved_tmppath = dw.tmppath assert os.path.dirname(saved_tmppath) == gdf.datadir assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) dw.write("123") os.unlink(saved_tmppath) assert not os.path.exists(saved_tmppath) finally: shutil.rmtree(td)
def test_put_rename_ENOENT(self): td = tempfile.mkdtemp() the_cont = os.path.join(td, "vol0", "bar") try: os.makedirs(the_cont) gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg) assert gdf._obj == "z" assert gdf._obj_path == "" assert gdf.name == "bar" assert gdf.datadir == the_cont assert gdf.data_file is None body = '1234\n' etag = md5() etag.update(body) etag = etag.hexdigest() metadata = { 'X-Timestamp': '1234', 'Content-Type': 'file', 'ETag': etag, 'Content-Length': '5', } def mock_sleep(*args, **kwargs): # Return without sleep, no need to dely unit tests return def mock_rename(*args, **kwargs): raise OSError(errno.ENOENT, os.strerror(errno.ENOENT)) with mock.patch("gluster.swift.obj.diskfile.sleep", mock_sleep): with mock.patch("os.rename", mock_rename): try: with gdf.writer() as dw: assert dw.tmppath is not None tmppath = dw.tmppath dw.write(body) dw.put(metadata) except GlusterFileSystemOSError: pass else: self.fail("Expected exception DiskFileError") finally: shutil.rmtree(td)
def test_writer_err_on_close(self): td = tempfile.mkdtemp() try: gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir/z", self.lg) saved_tmppath = '' with gdf.writer() as dw: assert gdf.datadir == os.path.join(td, "vol0", "bar", "dir") assert os.path.isdir(gdf.datadir) saved_tmppath = dw.tmppath assert os.path.dirname(saved_tmppath) == gdf.datadir assert os.path.basename(saved_tmppath)[:3] == '.z.' assert os.path.exists(saved_tmppath) dw.write("123") # Closing the fd prematurely should not raise any exceptions. os.close(dw.fd) assert not os.path.exists(saved_tmppath) finally: shutil.rmtree(td)
def test_put_ENOSPC(self): td = tempfile.mkdtemp() the_cont = os.path.join(td, "vol0", "bar") try: os.makedirs(the_cont) gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "z", self.lg) assert gdf._obj == "z" assert gdf._obj_path == "" assert gdf.name == "bar" assert gdf.datadir == the_cont assert gdf.data_file is None body = '1234\n' etag = md5() etag.update(body) etag = etag.hexdigest() metadata = { 'X-Timestamp': '1234', 'Content-Type': 'file', 'ETag': etag, 'Content-Length': '5', } def mock_open(*args, **kwargs): raise OSError(errno.ENOSPC, os.strerror(errno.ENOSPC)) with mock.patch("os.open", mock_open): try: with gdf.writer() as dw: assert dw.tmppath is not None dw.write(body) dw.put(metadata) except DiskFileNoSpace: pass else: self.fail("Expected exception DiskFileNoSpace") finally: shutil.rmtree(td)
def test_put_w_marker_dir_create(self): td = tempfile.mkdtemp() the_cont = os.path.join(td, "vol0", "bar") the_dir = os.path.join(the_cont, "dir") try: os.makedirs(the_cont) gdf = DiskFile(td, "vol0", "p57", "ufo47", "bar", "dir", self.lg) assert gdf.metadata == {} newmd = { 'ETag': 'etag', 'X-Timestamp': 'ts', 'Content-Type': 'application/directory'} with gdf.writer() as dw: dw.put(newmd, extension='.dir') assert gdf.data_file == the_dir for key,val in newmd.items(): assert gdf.metadata[key] == val assert _metadata[the_dir][key] == val assert gdf.metadata[X_OBJECT_TYPE] == DIR_OBJECT assert _metadata[the_dir][X_OBJECT_TYPE] == DIR_OBJECT finally: shutil.rmtree(td)