def test_chunk_content(): coll_name = uuid.uuid4().hex coll = Collection.create("/", coll_name) myFactory = Faker() content = myFactory.text() do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) data = [] for chk in resc.chunk_content(): data.append(chk) res = b"".join([s for s in data]) assert res == content.encode() resc.obj = None assert resc.chunk_content() == None TEST_URL = "http://www.google.fr" resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url=TEST_URL) data = [] for chk in resc.chunk_content(): data.append(chk) res = b"".join(data) assert res coll.delete()
def test_put(): myFactory = Faker() content = myFactory.text() # Create a new resource with a random name resc_name = uuid.uuid4().hex resc = Resource.create('/', resc_name) resc.put(content.encode()) assert resc.get_size() == len(content) # Force small chunk size for testing cfg.chunk_size = 4 fh = io.BytesIO(content.encode()) resc.put(fh) assert resc.get_size() == len(content) resc.delete() resc = Resource.create('/', resc_name, url="http://www.google.fr", mimetype="text/plain") with pytest.raises(NotImplementedError): resc.put("test") resc.delete()
def test_size(): coll_name = uuid.uuid4().hex coll = Collection.create('/', coll_name) myFactory = Faker() content = myFactory.text() chk = hashlib.sha224(content.encode()).hexdigest() do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc = Resource.find(resc.path) assert resc.get_size() == len(content) resc.obj = None assert resc.get_size() == 0 resc.delete() # Size for a reference resc = Resource.create(coll.path, resc_name, url="http://www.google.fr", mimetype="text/plain") resc = Resource.find(resc.path) # Size stored in the tree entry assert resc.get_size() == 0 resc.delete() coll.delete()
def test_get_child(): # Create a new collection with a random name coll_name = uuid.uuid4().hex coll1 = Collection.create('/', coll_name) coll2 = Collection.create(coll1.path, uuid.uuid4().hex) coll3 = Collection.create(coll1.path, uuid.uuid4().hex) coll4 = Collection.create(coll1.path, uuid.uuid4().hex) resc1 = Resource.create(coll1.path, uuid.uuid4().hex, url="http://www.google.fr") resc2 = Resource.create(coll1.path, uuid.uuid4().hex, url="http://www.google.fr") coll_childs, resc_childs = coll1.get_child() assert set(coll_childs) == set([coll2.name, coll3.name, coll4.name]) assert set(resc_childs) == set([resc1.get_name(), resc2.get_name()]) assert coll1.get_child_resource_count() == 2 coll_root = Collection.find("/") # Test for a resource where the url has been lost somehow resc3 = Resource.create(coll_root.path, uuid.uuid4().hex) resc3.update(object_url=None) resc3 = Resource.find(resc3.path) coll_childs, resc_childs = coll_root.get_child() assert set(coll_childs) == set(["1/", "2/", coll1.name]) assert set(resc_childs) == set([resc3.get_name(), 'g']) coll1.delete()
def test_update(): coll_name = uuid.uuid4().hex coll = Collection.create('/', coll_name) myFactory = Faker() content = myFactory.text() chk = hashlib.sha224(content.encode()).hexdigest() metadata = {"test": "val", "test_json": '["t", "e", "s", "t"]'} # Simple update do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc.update(mimetype="text/plain") resc = Resource.find(resc.path) assert resc.get_mimetype() == "text/plain" resc.delete() # update with metadata and a username for notification do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc.update(username="******", metadata=metadata) resc = Resource.find(resc.path) assert resc.get_cdmi_user_meta()['test'] == metadata['test'] resc.delete() # Update with a change of url (new dataObject) do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) DataObject.delete_id(do.uuid) do = DataObject.create(content.encode()) resc.update(object_url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc = Resource.find(resc.path) assert resc.get_size() == len(content) resc.delete() # Update for a reference resc = Resource.create(coll.path, resc_name, url="http://www.google.fr") resc.update(mimetype="text/plain") resc = Resource.find(resc.path) # Mimetype stored in the tree entry assert resc.get_mimetype() == "text/plain" resc.delete() coll.delete()
def test_metadata(): coll_name = uuid.uuid4().hex coll = Collection.create('/', coll_name) myFactory = Faker() content = myFactory.text() chk = hashlib.sha224(content.encode()).hexdigest() metadata = {"test": "val", "test_list": ['t', 'e', 's', 't']} # Checksum passed at creation do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid), metadata=metadata) resc = Resource.find(resc.path) meta_dict = resc.get_cdmi_user_meta() assert meta_dict['test'] == metadata['test'] assert meta_dict['test_list'] == metadata['test_list'] sys_meta = resc.get_cdmi_sys_meta() assert "radon_create_ts" in sys_meta assert "radon_modify_ts" in sys_meta resc.delete() # Checksum passed at creation do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid), mimetype="text/plain") resc = Resource.find(resc.path) assert resc.get_mimetype() == "text/plain" resc.delete() # Mimetype for a reference resc = Resource.create(coll.path, resc_name, url="http://www.google.fr", mimetype="text/plain") resc = Resource.find(resc.path) # Mimetype stored in the tree entry assert resc.get_mimetype() == "text/plain" resc.delete() coll.delete()
def setup_module(): cfg.dse_keyspace = TEST_KEYSPACE initialise() connect() create_tables() create_root() try: coll = Collection.create("/", "coll1") ref1 = Resource.create("/", "test.url", url=TEST_URL) resc = Resource.create("/coll1", "test.txt") ref2 = Resource.create("/coll1", "test.url", url=TEST_URL) except: # If collections or resources already exist pass
def put(self, args): "Put a file to a path." is_reference = args["--ref"] if is_reference: url = args["<url>"] dest_path = args["<dest>"] # Get the full destination path of the new resource dest_path = self.get_full_path(dest_path) else: src = args["<src>"] # Absolutize local path local_path = os.path.abspath(src) # Check that local file exists if not os.path.exists(local_path): self.print_error("File '{}' doesn't exist".format(local_path)) return errno.ENOENT if args["<dest>"]: dest_path = args["<dest>"] # We try to put the new file in a subcollection if dest_path.endswith('/'): dest_path = "{}{}".format(dest_path, os.path.basename(local_path)) else: # PUT to same name in pwd on server dest_path = os.path.basename(local_path) # Get the full destination path of the new resource dest_path = self.get_full_path(dest_path) # Check resource objects on the database resc = Resource.find(dest_path) if resc: self.print_error(MSG_RESC_EXIST.format(dest_path)) return parent, name = split(dest_path) try: if is_reference: resc = Resource.create(parent, name, url=url) else: resc = Resource.create(parent, name) with open(local_path, "rb") as fh: resc.put(fh) print(resc) except NoSuchCollectionError: self.print_error(MSG_COLL_NOT_EXIST.format(os.path.dirname(dest_path)))
def test_dict(): coll_name = uuid.uuid4().hex coll = Collection.create("/", coll_name) myFactory = Faker() content = myFactory.text() # Read/Write resource stored in Cassandra do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc = Resource.find(resc.path) resc_dict = resc.full_dict(User.find(USR1_NAME)) assert resc_dict['size'] == len(content) assert resc_dict['can_read'] assert resc_dict['can_write'] assert resc_dict['uuid'] == resc.uuid resc_dict = resc.simple_dict(User.find(USR1_NAME)) assert resc_dict['name'] == resc_name assert resc_dict['is_reference'] == False assert resc_dict['can_read'] assert resc_dict['can_write'] assert resc_dict['id'] == resc.uuid assert resc.simple_dict() == resc.to_dict() resc.delete() coll.delete()
def create_reference(self, parent, name, url, mimetype="application/cdmi-object"): """Create a new reference""" resource = Resource.create(name=name, container=parent, url=url, mimetype=mimetype) return resource
def test_delete_all(): coll1_name = uuid.uuid4().hex coll1 = Collection.create("/", coll1_name) coll2 = Collection.create(coll1.path, uuid.uuid4().hex) coll5 = Collection.create(coll2.path, uuid.uuid4().hex) resc1 = Resource.create(coll1.path, uuid.uuid4().hex, url="http://www.google.fr") Collection.delete_all("/{}/".format(coll1_name)) assert Collection.find(coll1_name) == None assert Collection.delete_all("/unknown/") == None
def test_delete(): coll1_name = uuid.uuid4().hex coll1 = Collection.create("/", coll1_name) coll2 = Collection.create(coll1.path, uuid.uuid4().hex) coll3 = Collection.create(coll1.path, uuid.uuid4().hex) coll4 = Collection.create(coll2.path, uuid.uuid4().hex) coll5 = Collection.create(coll4.path, uuid.uuid4().hex) resc1 = Resource.create(coll1.path, uuid.uuid4().hex, url="http://www.google.fr") coll1.delete() assert Collection.find(coll1_name) == None
def test_path(): coll_name = uuid.uuid4().hex coll = Collection.create('/', coll_name) myFactory = Faker() content = myFactory.text() chk = hashlib.sha224(content.encode()).hexdigest() do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc = Resource.find(resc.path) assert resc.path == "{}{}".format(coll.path, resc_name) assert resc.get_path() == "{}{}".format(coll.path, resc_name) resc.delete() coll.delete()
def create_resource(self, parent, name, content, mimetype): """Create a new resource""" uuid = None seq_num = 0 for chk in chunkstring(content, CHUNK_SIZE): if uuid is None: uuid = self.create_data_object(chk) else: self.append_data_object(uuid, seq_num, chk) seq_num += 1 if uuid is None: # Content is null uuid = self.create_empty_data_object() url = "cassandra://{}".format(uuid) resource = Resource.create(name=name, container=parent, url=url, mimetype=mimetype, size=len(content)) return resource
def test_user_can(): myFactory = Faker() content = myFactory.text() # Create a new resource with a random name do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create('/', resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) usr1 = User.find(USR1_NAME) usr2 = User.find(USR2_NAME) # usr1 should be admin assert resc.user_can(usr1, "read") # usr2 should not be admin, on root collection, only read assert resc.user_can(usr2, "read") assert not resc.user_can(usr2, "write") resc.delete()
def test_resource(): coll_name = uuid.uuid4().hex with pytest.raises(NoSuchCollectionError): resc = Resource.create("/{}".format(coll_name), uuid.uuid4().hex) coll = Collection.create("/", coll_name) do = create_data_object() resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) assert str(resc) == resc.path assert resc.get_name() == resc_name with pytest.raises(ResourceConflictError): resc = Resource.create(coll.path, resc_name) resc.delete() # Check resource is gone resc = Resource.find(resc.path) assert resc == None do = create_data_object() resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc.delete() resc = Resource.create(coll.path, resc_name, url="http://www.google.fr") assert resc.get_name() == resc_name + '?' resc.delete() # Check deleting resource also deleted data object do = DataObject.find(do.uuid) assert do == None do = create_data_object() resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid), size=do.size) assert resc.get_size() == do.size resc.delete() coll.delete()
def test_create_acl_fail(mocker): list_read = [GRP1_NAME] list_write = [GRP1_NAME] myFactory = Faker() content = myFactory.text() # Create a new resource with a random name do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create('/', resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) mocker.patch('radon.model.resource.acemask_to_str', return_value="wrong_oper") resc.create_acl_list(list_read, list_write) resc = Resource.find('/{}'.format(resc_name)) # Test get_acl_list wrong operation name acl_list = resc.get_acl_list() assert acl_list == ([], []) resc.delete()
def setup_module(): cfg.dse_keyspace = TEST_KEYSPACE initialise() connect() create_tables() create_default_users() create_root() pwd = uuid.uuid4().hex email = uuid.uuid4().hex grp1 = Group.create(name="grp1") u1 = User.create(name="user1", password=pwd, email=email, administrator=True) u2 = User.create(name="user2", password=pwd, email=email, administrator=False) grp1.add_users(["user2"]) try: coll = Collection.create("/", "1") coll = Collection.create("/1", "11") coll = Collection.create("/1", "12") coll = Collection.create("/1", "13") coll = Collection.create("/", "2") coll = Collection.create("/2", "21") coll = Collection.create("/2/21", "211") coll = Collection.create("/2/21", "212") r = Resource.create("/1/11", "a") r = Resource.create("/1/11", "b", url=TEST_URL) r = Resource.create("/1/12", "c") r = Resource.create("/1/13", "d") r = Resource.create("/2/21/211", "e") r = Resource.create("/2/21/212", "f") r = Resource.create("/", "g") except: # If collections or resources already exist pass
def new_resource(request, parent): """Manage the forms to create a new resource""" parent_collection = Collection.find(parent) # Inherits perms from container by default. if not parent_collection: raise Http404() # User must be able to write to this collection if not parent_collection.user_can(request.user, "write"): raise PermissionDenied read_access, write_access = parent_collection.get_acl_list() initial = { "metadata": {}, "read_access": read_access, "write_access": write_access, } if request.method == "POST": form = ResourceNewForm(request.POST, files=request.FILES, initial=initial) if form.is_valid(): data = form.cleaned_data try: name = data["name"] metadata = {} for k, v in json.loads(data["metadata"]): if k in metadata: if isinstance(metadata[k], list): metadata[k].append(v) else: metadata[k] = [metadata[k], v] else: metadata[k] = v resource = Resource.create( container=parent_collection.path, name=name, metadata=metadata, mimetype=data["file"].content_type, creator=request.user.name, size=data["file"].size, ) res = resource.put(data["file"]) resource.create_acl_list(data["read_access"], data["write_access"]) messages.add_message( request, messages.INFO, u"New resource '{}' created".format(resource.get_name()), ) except ResourceConflictError: messages.add_message( request, messages.ERROR, "That name is in use within the current collection", ) return redirect("archive:view", path=parent_collection.path) else: form = ResourceNewForm(initial=initial) ctx = {"form": form, "container": parent_collection, "groups": Group.objects.all()} return render(request, "archive/resource/new.html", ctx)
def test_acl(): list_read = [GRP1_NAME] list_write = [GRP1_NAME] coll_name = uuid.uuid4().hex coll = Collection.create('/', coll_name) myFactory = Faker() content = myFactory.text() # Read/Write resource stored in Cassandra do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc.create_acl_list(list_read, list_write) resc = Resource.find(resc.path) assert resc.get_acl_dict()[GRP1_NAME].acemask == 95 # read/write assert resc.get_authorized_actions( User.find(USR2_NAME)) == {'delete', 'read', 'edit', 'write'} read_access, write_access = resc.get_acl_list() assert read_access == [GRP1_NAME] assert write_access == [GRP1_NAME] cdmi_acl = resc.get_acl_metadata() assert 'cdmi_acl' in cdmi_acl cdmi_acl = [{ 'identifier': GRP1_NAME, 'acetype': 'ALLOW', 'aceflags': "INHERITED", 'acemask': "READ" }] resc.update_acl_cdmi(cdmi_acl) resc.delete() # Read resource stored in Cassandra do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc.create_acl_list(list_read, []) resc = Resource.find(resc.path) assert resc.get_acl_dict()[GRP1_NAME].acemask == 9 # read assert resc.get_authorized_actions(User.find(USR2_NAME)) == {'read'} read_access, write_access = resc.get_acl_list() assert read_access == [GRP1_NAME] assert write_access == [] resc.delete() # Write resource stored in Cassandra do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) resc.create_acl_list([], list_write) resc = Resource.find(resc.path) assert resc.get_acl_dict()[GRP1_NAME].acemask == 86 # write assert resc.get_authorized_actions( User.find(USR2_NAME)) == {'edit', 'delete', 'write'} read_access, write_access = resc.get_acl_list() assert read_access == [] assert write_access == [GRP1_NAME] resc.delete() # Resource stored in Cassandra, acl inherited from root do = DataObject.create(content.encode()) resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="{}{}".format(cfg.protocol_cassandra, do.uuid)) assert resc.get_authorized_actions(User.find(USR2_NAME)) == {"read"} resc.delete() # Read/Write resource stored as a reference resc_name = uuid.uuid4().hex resc = Resource.create(coll.path, resc_name, url="http://www.google.fr") resc.create_acl_list(list_read, list_write) resc = Resource.find(resc.path) assert resc.get_acl_dict()[GRP1_NAME].acemask == 95 assert resc.get_authorized_actions( User.find(USR2_NAME)) == {'delete', 'read', 'edit', 'write'} resc.delete() coll.delete()