def test_drives(authenticated_user, tempdir): creds = StorageCreds(user=authenticated_user, service_url="storage") nstart = len(Drive.list_toplevel_drives(creds=creds)) drive_name = "test å∫ç∂ something" drive = Drive(name=drive_name, creds=creds, autocreate=True) assert (drive.metadata().name() == drive_name) assert (drive.metadata().acl().is_owner()) drive2_name = "test/this/is/a/../../dir" drive2 = Drive(name=drive2_name, creds=creds) drives = Drive.list_toplevel_drives(creds=creds) assert (len(drives) == nstart + 2) drives = drive2.list_drives() assert (len(drives) == 0) drives = drive.list_drives() assert (len(drives) == 0) filename = __file__ files = drive.list_files() assert (len(files) == 0) filemeta = drive.upload(filename=filename) assert (filemeta.is_complete()) assert (filemeta.acl().is_owner()) assert (filemeta.acl().is_readable()) assert (filemeta.acl().is_writeable()) assert (filemeta.uploaded_by() == authenticated_user.guid()) assert (filemeta.uploaded_when() is not None) upload_datetime = filemeta.uploaded_when() (_, filename) = os.path.split(filename) assert (filemeta.filename() == filename) files = drive.list_files() assert (len(files) == 1) assert (files[0].filename() == filemeta.filename()) assert (not files[0].is_complete()) files = drive.list_files(include_metadata=True) assert (len(files) == 1) assert (files[0].filename() == filemeta.filename()) assert (files[0].is_complete()) assert (files[0].uid() == filemeta.uid()) assert (files[0].filesize() == filemeta.filesize()) assert (files[0].checksum() == filemeta.checksum()) assert (files[0].compression_type() == filemeta.compression_type()) assert (files[0].uploaded_by() == filemeta.uploaded_by()) assert (files[0].uploaded_when() == filemeta.uploaded_when()) assert (files[0].acl().is_owner()) assert (files[0].uploaded_by() == authenticated_user.guid()) assert (files[0].uploaded_when() == upload_datetime) f = files[0].open() filename = f.download(dir=tempdir) # make sure that the two files are identical with open(filename, "rb") as FILE: data1 = FILE.read() # remove this tmp file os.unlink(filename) with open(__file__, "rb") as FILE: data2 = FILE.read() assert (data1 == data2) assert (files[0].uid() == filemeta.uid()) assert (files[0].filesize() == filemeta.filesize()) assert (files[0].checksum() == filemeta.checksum()) assert (files[0].compression_type() == filemeta.compression_type()) assert (files[0].uploaded_by() == filemeta.uploaded_by()) assert (files[0].uploaded_when() == filemeta.uploaded_when()) assert (files[0].acl().is_owner()) assert (files[0].uploaded_by() == authenticated_user.guid()) assert (files[0].uploaded_when() == upload_datetime) versions = f.list_versions() assert (len(versions) == 1) assert (versions[0].filename() == filemeta.filename()) assert (versions[0].uploaded_when() == filemeta.uploaded_when()) new_filemeta = drive.upload(filename=__file__, force_par=True) versions = f.list_versions() assert (len(versions) == 2) filename = new_filemeta.open().download(dir=tempdir) # make sure that the two files are identical with open(filename, "rb") as FILE: data1 = FILE.read() # remove this tmp file os.unlink(filename) with open(__file__, "rb") as FILE: data2 = FILE.read() assert (data1 == data2) # should be in upload order assert (versions[0].uid() == filemeta.uid()) assert (versions[1].uid() == new_filemeta.uid()) filename = new_filemeta.open().download(dir=tempdir, force_par=True) # make sure that the two files are identical with open(filename, "rb") as FILE: data1 = FILE.read() # remove this tmp file os.unlink(filename) assert (data1 == data2) # try to upload a file with path to the drive filemeta = drive.upload(filename=__file__, uploaded_name="/test/one/../two/test.py") assert (filemeta.filename() == "test/two/test.py") # cannot create a new Drive with non-owner ACLs with pytest.raises(PermissionError): drive = Drive(name="broken_acl", creds=creds, aclrules=ACLRules.owner("12345@z0-z0")) drive = Drive(name="working_acl", creds=creds, aclrules=ACLRules.owner(authenticated_user.guid()))
def test_run_calc(aaai_services, authenticated_user): # create and register the cluster on which this job will take place... cluster = Cluster.create(service_url="compute", user=aaai_services["compute"]["user"]) user = authenticated_user assert (user.is_logged_in()) # ensure that the main account has money deposit(user, 100.0, "Adding money to the account", accounting_url="accounting") # get a handle to the financial account used to pay for the job account = Account(user=user, account_name="deposits", accounting_url="accounting") assert (account.balance() >= 100.0) # Upload a directory that will contain all of the input creds = StorageCreds(user=user, service_url="storage") drive = Drive(name="sim", creds=creds, autocreate=True) uploaded = drive.upload(_testdata()) location = uploaded.location() print(drive.list_files(dir="example_sim/input")) print(location) # create a request for a job to be run using: # 1. 'image_name' as the specified container image for the software # 2. 'location' as the location containing all input files # authorise it using the authenticated user (who may be different to the # user who pays for the job - hence the need for a different # authorisation for the request and for the cheque) r = RunRequest(image="docker://test_image:latest", input=location) # now write a cheque which will provide authorisation to spend money from # this account to pay for this request. This will be written to the access # service to give it the authority to create a transation in the account. # This cheque authorises only a single transaction, performable only # by the service whose canonical URL is supplied, and the access service # should check that the requested resource signature matches that # authorised by the cheque cheque = Cheque.write(account=account, recipient_url="access", resource=r.fingerprint(), max_spend=50.0) func = "run_calculation" args = {} args["request"] = r.to_data() args["authorisation"] = Authorisation(user=user, resource=r.fingerprint()).to_data() args["cheque"] = cheque.to_data() access_service = Service("access") result = access_service.call_function(func, args) print(result) pending_uids = cluster.get_pending_job_uids() print(pending_uids) for uid in pending_uids: job = cluster.submit_job(uid) print(job) pending_uids = cluster.get_pending_job_uids() print(pending_uids)