|
6 | 6 | import unittest |
7 | 7 | import tempfile |
8 | 8 | import shutil |
| 9 | +from subprocess import check_output, CalledProcessError |
9 | 10 | from irods import MAX_PASSWORD_LENGTH |
10 | 11 | from irods.exception import GroupDoesNotExist, UserDoesNotExist |
11 | 12 | from irods.meta import iRODSMetaCollection, iRODSMeta |
12 | | -from irods.models import User, Group, UserMeta |
| 13 | +from irods.models import User, Group, UserMeta, Quota |
13 | 14 | from irods.session import iRODSSession |
14 | 15 | import irods.exception as ex |
15 | 16 | import irods.test.helpers as helpers |
16 | | -from irods.user import Bad_password_change_parameter |
| 17 | +from irods.user import iRODSUser, Bad_password_change_parameter |
17 | 18 | from six.moves import range |
18 | 19 |
|
19 | | -class TestGroup(unittest.TestCase): |
| 20 | +def user_quotas_implemented(): |
| 21 | + return getattr(iRODSUser, "set_quota", None) is not None |
| 22 | + |
| 23 | +def icommands_available(): |
| 24 | + try: |
| 25 | + which_output = check_output(['which', 'ihelp']) |
| 26 | + return b'ihelp' in which_output |
| 27 | + except CalledProcessError: |
| 28 | + pass # 'which' returned nonzero status |
| 29 | + return False |
| 30 | + |
| 31 | +def set_user_quota(session, user_name, bytes_total): |
| 32 | + success = False |
| 33 | + if user_quotas_implemented(): |
| 34 | + try: |
| 35 | + session.users.set_quota(user_name, bytes_total) |
| 36 | + success = True |
| 37 | + except: |
| 38 | + pass |
| 39 | + if not success and icommands_available(): |
| 40 | + return_code = os.system("iadmin suq {} total {}".format(user_name, bytes_total)) |
| 41 | + if return_code == 0: |
| 42 | + success = True |
| 43 | + return success |
| 44 | + |
| 45 | +class TestUserAndGroup(unittest.TestCase): |
20 | 46 |
|
21 | 47 | def setUp(self): |
22 | 48 | self.sess = helpers.make_session() |
@@ -525,6 +551,60 @@ def alice_login(pw): |
525 | 551 | if user_alice: |
526 | 552 | user_alice.remove() |
527 | 553 |
|
| 554 | + def test_set_and_query_group_quota(self): |
| 555 | + #import pdb |
| 556 | + #pdb.set_trace() |
| 557 | + bob = quota_group = None |
| 558 | + ses = self.sess |
| 559 | + test_object = None |
| 560 | + my_quota = 10000 |
| 561 | + my_object_size = 1400 |
| 562 | + try: |
| 563 | + bob = self.sess.users.create('bob', 'rodsuser') |
| 564 | + ses.users.modify('bob', 'password', 'bpass') |
| 565 | + quota_group = self.sess.groups.create('quota_group_for_bob') |
| 566 | + quota_group.addmember(bob.name) |
| 567 | + quota_group.set_quota(my_quota) |
| 568 | + with iRODSSession(user = 'bob', password = 'bpass', host = ses.host, port = ses.port, zone = ses.zone) as user_sess: |
| 569 | + test_object = user_sess.data_objects.create('/tempZone/home/public/bob_file_testing_group_quota') |
| 570 | + with test_object.open('w') as f: |
| 571 | + f.write(b'_' * my_object_size) |
| 572 | + ses.groups.calculate_usage() |
| 573 | + for i in ses.query(Quota).filter(Quota.user_id == quota_group.id): |
| 574 | + self.assertEqual(int(i[Quota.over]), -my_quota + my_object_size) |
| 575 | + finally: |
| 576 | + if test_object: |
| 577 | + test_object.unlink(force = True) |
| 578 | + if quota_group: |
| 579 | + quota_group.remove() |
| 580 | + if bob: |
| 581 | + ses.users.remove(bob.name) |
| 582 | + |
| 583 | + @unittest.skipIf(not user_quotas_implemented() |
| 584 | + and not icommands_available(), "Can't set up user quota for removal in test") |
| 585 | + def test_set_and_query_user_quota(self): |
| 586 | + bob = None |
| 587 | + if self.sess.server_version >= (4, 3): |
| 588 | + self.skipTest('iRODS servers 4.3.0 and higher have dropped user quotas in favor of group quotas.') |
| 589 | + ses = self.sess |
| 590 | + test_object = None |
| 591 | + try: |
| 592 | + bob = self.sess.users.create('bob', 'rodsuser') |
| 593 | + ses.users.modify('bob', 'password', 'bpass') |
| 594 | + set_user_quota(ses, 'bob', 10000) |
| 595 | + with iRODSSession(user = 'bob', password = 'bpass', host = ses.host, port = ses.port, zone = ses.zone) as user_sess: |
| 596 | + test_object = user_sess.data_objects.create('/tempZone/home/public/bobfile') |
| 597 | + with test_object.open('w') as f: |
| 598 | + f.write(b'_'*1000) |
| 599 | + ses.users.calculate_usage() |
| 600 | + for i in ses.query(Quota).filter(Quota.user_id == bob.id): |
| 601 | + self.assertEqual(int(i[Quota.over]), -9000) |
| 602 | + finally: |
| 603 | + if test_object: |
| 604 | + test_object.unlink(force = True) |
| 605 | + if bob: |
| 606 | + ses.users.remove(bob.name) |
| 607 | + |
528 | 608 |
|
529 | 609 | if __name__ == '__main__': |
530 | 610 | # let the tests find the parent irods lib |
|
0 commit comments