def test_daemon_extents_zero(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) chunk_size = len(handler.image) // 2 handler.extents["zero"] = [ { "start": 0, "length": chunk_size, "zero": False, "hole": False, }, { "start": chunk_size, "length": chunk_size, "zero": True, "hole": False, }, ] with Backend(http_server.url, http_server.cafile) as b: # Zero extents are available. assert list(b.extents()) == [ image.ZeroExtent(0, chunk_size, False, False), image.ZeroExtent(chunk_size, chunk_size, True, False), ] # Dirty extents are not available. with pytest.raises(errors.UnsupportedOperation): list(b.extents("dirty"))
def test_daemon_extents_dirty(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) chunk_size = len(handler.image) // 2 handler.extents["dirty"] = [ { "start": 0, "length": chunk_size, "dirty": True, "hole": False, }, { "start": chunk_size, "length": chunk_size, "dirty": False, "hole": False, }, ] with Backend(http_server.url, http_server.cafile) as b: # Both "zero" and "dirty" extents are available. assert list(b.extents("zero")) == [ image.ZeroExtent(0, b.size(), True, False), ] assert list(b.extents("dirty")) == [ image.DirtyExtent(0, chunk_size, True), image.DirtyExtent(chunk_size, chunk_size, False), ]
def test_daemon_read_from(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: check_read_from(handler, b) assert handler.dirty b.flush() assert not handler.dirty
def test_daemon_no_unix_socket_read_from(http_server): handler = Daemon(http_server, extents=False) with Backend(http_server.url, http_server.cafile) as b: check_read_from(handler, b) assert handler.dirty b.flush() assert not handler.dirty
def test_daemon_no_extents_open(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server, extents=False) with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == uhttp_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client emulates extents (all non-zero). assert list( b.extents()) == [image.ZeroExtent(0, b.size(), False, False)]
def test_old_proxy_open(http_server): handler = OldProxy(http_server) with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == http_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client emulates extents (all non-zero). assert list( b.extents()) == [image.ZeroExtent(0, b.size(), False, False)]
def test_daemon_readinto_end(http_server, uhttp_server, end_offset): _ = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: offset = b.size() + end_offset b.seek(offset) buf = bytearray(4096) n = b.readinto(buf) assert n == 0 assert b.tell() == offset
def test_daemon_open(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == uhttp_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client reports server extents (all zero). assert list( b.extents()) == [image.ZeroExtent(0, b.size(), True, False)]
def test_daemon_bad_unix_socket_open(http_server): handler = Daemon(http_server, extents=False) handler.unix_socket = "\0bad/socket" with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == http_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client emulates extents (all non-zero). assert list(b.extents()) == [ extent.ZeroExtent(0, b.size(), False, False) ]
def test_daemon_readinto_short(http_server, uhttp_server, size): handler = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: offset = b.size() - size buf = bytearray(8192) b.seek(offset) n = b.readinto(buf) assert n == size assert b.tell() == b.size() assert buf[:size] == handler.image[offset:] assert buf[size:] == b"\0" * (8192 - size)
def test_daemon_extents_error(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) def fail(req, resp, *args): raise http.Error(http.FORBIDDEN, "Fake error") handler.get = fail with pytest.raises(http.Error) as e: with Backend(http_server.url, None, secure=False) as b: list(b.extents()) assert e.value.code == http.FORBIDDEN
def test_old_daemon_zero_error(http_server): handler = OldDaemon(http_server) # With old daemon we emulate zero by putting zeros over the write. def fail(req, resp, *args): raise http.Error(http.FORBIDDEN, "Fake error") handler.put = fail with pytest.raises(http.Error) as e: with Backend(http_server.url, http_server.cafile) as b: b.zero(4096) assert e.value.code == http.FORBIDDEN
def test_daemon_open_error(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) # Backends is sending OPTIONS request during open. def fail(req, resp, *args): raise http.Error(http.FORBIDDEN, "Fake error") handler.options = fail with pytest.raises(http.Error) as e: with Backend(http_server.url, None, secure=False): pass assert e.value.code == http.FORBIDDEN
def test_old_daemon_size_error(http_server): handler = OldDaemon(http_server) # With old daemon we emulate HEAD with GET. Make it fail to test error # handling. def fail(req, resp, *args): raise http.Error(http.FORBIDDEN, "Fake error") handler.get = fail with pytest.raises(http.Error) as e: with Backend(http_server.url, None, secure=False) as b: b.size() assert e.value.code == http.FORBIDDEN
def test_daemon_close(http_server, uhttp_server): Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: pass # Closing twice does nothing. b.close() # But other operations should fail. error = "Operation on closed backend" buf = bytearray(4096) with pytest.raises(ValueError) as e: b.write(buf) assert str(e.value) == error with pytest.raises(ValueError) as e: b.readinto(buf) assert str(e.value) == error
def test_clone_unix(http_server, uhttp_server): Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as a: # Simulate download flow when we get size from server before cloning # the backend. a.size() with a.clone() as b: # Backends are created identical. assert a.size() == b.size() assert list(a.extents()) == list(b.extents()) assert a.tell() == b.tell() # Modifying one backend does not affect the other. assert a.write(b"x" * 4096) assert a.tell() == 4096 assert b.tell() == 0 # But both show the same content. buf = bytearray(4096) assert b.readinto(buf) assert buf == b"x" * 4096
def test_daemon_write_to_error(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: check_write_to_error(handler, b)
def test_daemon_read_from_error(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: check_read_from_error(handler, b)
def test_old_daemon_read_from(http_server): handler = OldDaemon(http_server) with Backend(http_server.url, http_server.cafile) as b: check_read_from(handler, b) assert not handler.dirty
def test_old_daemon_write_to(http_server): handler = OldDaemon(http_server) with Backend(http_server.url, http_server.cafile) as b: check_write_to(handler, b)
def test_daemon_open_insecure(http_server, uhttp_server): Daemon(http_server, uhttp_server) with Backend(http_server.url, None, secure=False) as b: assert b.server_address == uhttp_server.server_address
def test_old_proxy_read_from(http_server): handler = OldProxy(http_server) with Backend(http_server.url, http_server.cafile) as b: check_read_from(handler, b) assert not handler.dirty
def test_daemon_no_unix_socket_write_to(http_server): handler = Daemon(http_server, extents=False) with Backend(http_server.url, http_server.cafile) as b: check_write_to(handler, b)
def test_old_proxy_write_to(http_server): handler = OldProxy(http_server) with Backend(http_server.url, http_server.cafile) as b: check_write_to(handler, b)