From b780127f49e044b677b19b1e5c7ee4966fbe9b14 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 3 Dec 2007 21:37:54 -0700 Subject: [PATCH] test_mutable: improve test coverage a bit, add test_filenode.py --- src/allmydata/filenode.py | 52 ++++++++++++++++++++++++- src/allmydata/test/test_filenode.py | 59 +++++++++++++++++++++++++++++ src/allmydata/test/test_mutable.py | 51 +++++++++++++++++++++++-- src/allmydata/test/test_web.py | 4 +- 4 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 src/allmydata/test/test_filenode.py diff --git a/src/allmydata/filenode.py b/src/allmydata/filenode.py index a251de7f..91eab290 100644 --- a/src/allmydata/filenode.py +++ b/src/allmydata/filenode.py @@ -1,6 +1,8 @@ from zope.interface import implements -from allmydata.interfaces import IFileNode, IFileURI +from twisted.internet import defer +from allmydata.interfaces import IFileNode, IFileURI, IURI +from allmydata import uri class FileNode: implements(IFileNode) @@ -46,3 +48,51 @@ class FileNode: downloader = self._client.getServiceNamed("downloader") return downloader.download_to_data(self.uri) + + +class LiteralFileNode: + implements(IFileNode) + + def __init__(self, my_uri, client): + u = IFileURI(my_uri) + assert isinstance(u, uri.LiteralFileURI) + self.uri = u.to_string() + self._client = client + + def get_uri(self): + return self.uri + + def is_readonly(self): + return True + + def get_readonly_uri(self): + return self.uri + + def get_size(self): + return len(IURI(self.uri).data) + + def __hash__(self): + return hash((self.__class__, self.uri)) + def __cmp__(self, them): + if cmp(type(self), type(them)): + return cmp(type(self), type(them)) + if cmp(self.__class__, them.__class__): + return cmp(self.__class__, them.__class__) + return cmp(self.uri, them.uri) + + def get_verifier(self): + return None + + def check(self): + return None + + def download(self, target): + data = IURI(self.uri).data + target.open(len(data)) + target.write(data) + target.close() + return defer.maybeDeferred(target.finish) + + def download_to_data(self): + data = IURI(self.uri).data + return defer.succeed(data) diff --git a/src/allmydata/test/test_filenode.py b/src/allmydata/test/test_filenode.py new file mode 100644 index 00000000..9929b778 --- /dev/null +++ b/src/allmydata/test/test_filenode.py @@ -0,0 +1,59 @@ + +from twisted.trial import unittest +from allmydata import filenode, uri, download + +class NotANode: + pass + +class Node(unittest.TestCase): + def test_chk_filenode(self): + u = uri.CHKFileURI(key="\x00"*16, + uri_extension_hash="\x00"*32, + needed_shares=3, + total_shares=10, + size=1000) + c = None + fn1 = filenode.FileNode(u, c) + fn2 = filenode.FileNode(u.to_string(), c) + self.failUnlessEqual(fn1, fn2) + self.failIfEqual(fn1, "I am not a filenode") + self.failIfEqual(fn1, NotANode()) + self.failUnlessEqual(fn1.get_uri(), u.to_string()) + self.failUnlessEqual(fn1.is_readonly(), True) + self.failUnlessEqual(fn1.get_readonly_uri(), u.to_string()) + self.failUnlessEqual(fn1.get_size(), 1000) + d = {} + d[fn1] = 1 # exercise __hash__ + v = fn1.get_verifier() + self.failUnless(isinstance(v, uri.CHKFileVerifierURI)) + + def test_literal_filenode(self): + DATA = "I am a short file." + u = uri.LiteralFileURI(data=DATA) + c = None + fn1 = filenode.LiteralFileNode(u, c) + fn2 = filenode.LiteralFileNode(u.to_string(), c) + self.failUnlessEqual(fn1, fn2) + self.failIfEqual(fn1, "I am not a filenode") + self.failIfEqual(fn1, NotANode()) + self.failUnlessEqual(fn1.get_uri(), u.to_string()) + self.failUnlessEqual(fn1.is_readonly(), True) + self.failUnlessEqual(fn1.get_readonly_uri(), u.to_string()) + self.failUnlessEqual(fn1.get_size(), len(DATA)) + d = {} + d[fn1] = 1 # exercise __hash__ + + v = fn1.get_verifier() + self.failUnlessEqual(v, None) + + self.failUnlessEqual(fn1.check(), None) + target = download.Data() + d = fn1.download(target) + def _check(res): + self.failUnlessEqual(res, DATA) + d.addCallback(_check) + + d.addCallback(lambda res: fn1.download_to_data()) + d.addCallback(_check) + return d + diff --git a/src/allmydata/test/test_mutable.py b/src/allmydata/test/test_mutable.py index e2f47caa..33194d6b 100644 --- a/src/allmydata/test/test_mutable.py +++ b/src/allmydata/test/test_mutable.py @@ -3,13 +3,13 @@ import itertools, struct from twisted.trial import unittest from twisted.internet import defer from twisted.python import failure, log -from allmydata import mutable, uri, dirnode2 +from allmydata import mutable, uri, dirnode2, upload from allmydata.dirnode2 import split_netstring from allmydata.util.hashutil import netstring, tagged_hash from allmydata.encode import NotEnoughPeersError from allmydata.interfaces import IURI, INewDirectoryURI, \ - IMutableFileURI - + IMutableFileURI, IFileNode, IUploadable, IFileURI +from allmydata.filenode import LiteralFileNode import sha class Netstring(unittest.TestCase): @@ -138,7 +138,13 @@ class FakeClient: u = IURI(u) if INewDirectoryURI.providedBy(u): return self.create_dirnode_from_uri(u) - assert IMutableFileURI.providedBy(u) + if IFileURI.providedBy(u): + if isinstance(u, uri.LiteralFileURI): + return LiteralFileNode(u, self) + else: + # CHK + raise RuntimeError("not simulated") + assert IMutableFileURI.providedBy(u), u res = FakeFilenode(self).init_from_uri(u) return res @@ -155,6 +161,18 @@ class FakeClient: results.sort() return results + def upload(self, uploadable, wait_for_numpeers=None): + assert IUploadable.providedBy(uploadable) + d = uploadable.get_size() + d.addCallback(lambda length: uploadable.read(length)) + #d.addCallback(self.create_mutable_file) + def _got_data(datav): + data = "".join(datav) + #newnode = FakeFilenode(self) + return uri.LiteralFileURI(data) + d.addCallback(_got_data) + return d + class Filenode(unittest.TestCase): def setUp(self): self.client = FakeClient() @@ -495,6 +513,31 @@ class Dirnode(unittest.TestCase): self.failUnlessEqual(sorted(children.keys()), sorted(["child"]))) + uploadable = upload.Data("some data") + d.addCallback(lambda res: n.add_file("newfile", uploadable)) + d.addCallback(lambda newnode: + self.failUnless(IFileNode.providedBy(newnode))) + d.addCallback(lambda res: n.list()) + d.addCallback(lambda children: + self.failUnlessEqual(sorted(children.keys()), + sorted(["child", "newfile"]))) + + d.addCallback(lambda res: n.create_empty_directory("subdir2")) + def _created2(subdir2): + self.subdir2 = subdir2 + d.addCallback(_created2) + + d.addCallback(lambda res: + n.move_child_to("child", self.subdir2)) + d.addCallback(lambda res: n.list()) + d.addCallback(lambda children: + self.failUnlessEqual(sorted(children.keys()), + sorted(["newfile", "subdir2"]))) + d.addCallback(lambda res: self.subdir2.list()) + d.addCallback(lambda children: + self.failUnlessEqual(sorted(children.keys()), + sorted(["child"]))) + return d d.addCallback(_then) diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py index 587d91e6..f089d4bd 100644 --- a/src/allmydata/test/test_web.py +++ b/src/allmydata/test/test_web.py @@ -33,7 +33,7 @@ class MyClient(service.MultiService): def create_node_from_uri(self, uri): return self.my_nodes[uri] - + def create_empty_dirnode(self, wait_for_numpeers=None): n = FakeDirectoryNode(self) r = defer.succeed(n.fake_create(wait_for_numpeers=1)) @@ -261,7 +261,7 @@ class WebMixin(object): self.failUnless("rw_uri" in data[1]) # mutable self.failUnlessEqual(data[1]["rw_uri"], self._foo_uri) self.failUnlessEqual(data[1]["ro_uri"], self._foo_readonly_uri) - + kidnames = sorted(data[1]["children"]) self.failUnlessEqual(kidnames, ["bar.txt", "blockingfile", "empty", "sub"]) -- 2.45.2