File: collection_test.py

package info (click to toggle)
python-irodsclient 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,352 kB
  • sloc: python: 16,650; xml: 525; sh: 104; awk: 5; sql: 3; makefile: 3
file content (529 lines) | stat: -rw-r--r-- 19,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#! /usr/bin/env python

from datetime import datetime, timezone
import os
import sys
import socket
import shutil
import unittest
import time
from irods.meta import iRODSMetaCollection
from irods.exception import CollectionDoesNotExist
from irods.models import Collection, DataObject
import irods.test.helpers as helpers
import irods.keywords as kw
from irods.test.helpers import my_function_name, unique_name
from irods.collection import iRODSCollection

RODSUSER = "nonadmin"


class TestCollection(unittest.TestCase):

    class WrongUserType(RuntimeError):
        pass

    @classmethod
    def setUpClass(cls):
        adm = helpers.make_session()
        if adm.users.get(adm.username).type != "rodsadmin":
            raise cls.WrongUserType(
                "Must be an iRODS admin to run tests in class {0.__name__}".format(cls)
            )
        cls.logins = helpers.iRODSUserLogins(adm)
        cls.logins.create_user(RODSUSER, "abc123")

    @classmethod
    def tearDownClass(cls):
        # TODO(#553): Skipping this will result in an interpreter seg fault for Py3.6 but not 3.11; why?
        del cls.logins

    def setUp(self):
        self.sess = helpers.make_session()

        self.test_coll_path = "/{}/home/{}/test_dir".format(
            self.sess.zone, self.sess.username
        )
        self.test_coll = self.sess.collections.create(self.test_coll_path)

    def tearDown(self):
        """Delete the test collection after each test"""
        self.test_coll.remove(recurse=True, force=True)
        self.sess.cleanup()

    def test_get_collection(self):
        # path = "/tempZone/home/rods"
        coll = self.sess.collections.get(self.test_coll_path)
        self.assertEqual(self.test_coll_path, coll.path)

    def test_irods_collection_information(self):
        coll = self.sess.collections.get(self.test_coll_path)
        self.assertIsNotNone(coll.create_time)
        self.assertIsNotNone(coll.modify_time)
        self.assertFalse(coll.inheritance)
        self.assertIsNotNone(coll.owner_name)
        self.assertIsNotNone(coll.owner_zone)

    def test_append_to_collection(self):
        """Append a new file to the collection"""
        pass

    def test_remove_from_collection(self):
        """Delete a file from a collection"""
        pass

    def test_update_in_collection(self):
        """Modify a file in a collection"""
        pass

    def test_create_recursive_collection(self):
        # make path with recursion
        root_coll_path = self.test_coll_path + "/recursive/collection/test"
        self.sess.collections.create(root_coll_path, recurse=True)

        # confirm col create
        coll = self.sess.collections.get(root_coll_path)
        self.assertEqual(root_coll_path, coll.path)

        # delete test collection
        coll.remove(force=True)

        # confirm delete
        with self.assertRaises(CollectionDoesNotExist):
            self.sess.collections.get(root_coll_path)

    def test_remove_deep_collection(self):
        # depth = 100
        depth = 20  # placeholder
        root_coll_path = self.test_coll_path + "/deep_collection"

        # make test collection
        helpers.make_deep_collection(
            self.sess,
            root_coll_path,
            depth=depth,
            objects_per_level=1,
            object_content=None,
        )

        # delete test collection
        self.sess.collections.remove(root_coll_path, recurse=True, force=True)

        # confirm delete
        with self.assertRaises(CollectionDoesNotExist):
            self.sess.collections.get(root_coll_path)

    def test_rename_collection(self):
        # test args
        args = {"collection": self.test_coll_path, "old_name": "foo", "new_name": "bar"}

        # make collection
        path = "{collection}/{old_name}".format(**args)
        coll = helpers.make_collection(self.sess, path)

        # get collection id
        saved_id = coll.id

        # rename coll
        new_path = "{collection}/{new_name}".format(**args)
        coll.move(new_path)
        # self.sess.collections.move(path, new_path)

        # get updated collection
        coll = self.sess.collections.get(new_path)

        # compare ids
        self.assertEqual(coll.id, saved_id)

        # remove collection
        coll.remove(recurse=True, force=True)

    def test_move_coll_to_coll(self):
        # test args
        args = {
            "base_collection": self.test_coll_path,
            "collection1": "foo",
            "collection2": "bar",
        }

        # make collection1 and collection2 in base collection
        path1 = "{base_collection}/{collection1}".format(**args)
        coll1 = helpers.make_collection(self.sess, path1)
        path2 = "{base_collection}/{collection2}".format(**args)
        coll2 = helpers.make_collection(self.sess, path2)

        # get collection2's id
        saved_id = coll2.id

        # move collection2 into collection1
        self.sess.collections.move(path2, path1)

        # get updated collection
        path2 = "{base_collection}/{collection1}/{collection2}".format(**args)
        coll2 = self.sess.collections.get(path2)

        # compare ids
        self.assertEqual(coll2.id, saved_id)

        # remove collection
        coll1.remove(recurse=True, force=True)

    def test_repr_coll(self):
        coll_name = self.test_coll.name
        coll_id = self.test_coll.id

        self.assertEqual(
            repr(self.test_coll),
            "<iRODSCollection {coll_id} {coll_name}>".format(**locals()),
        )

    def test_walk_collection_topdown(self):
        depth = 20

        # files that will be ceated in each subcollection
        filenames = ["foo", "bar", "baz"]

        # make nested collections
        coll_path = self.test_coll_path
        for d in range(depth):
            # create subcollection with files
            coll_path += "/sub" + str(d)
            helpers.make_collection(self.sess, coll_path, filenames)

        # now walk nested collections
        colls = self.test_coll.walk()
        current_coll_name = self.test_coll.name
        for d in range(depth + 1):
            # get next result
            collection, subcollections, data_objects = next(colls)

            # check collection name
            self.assertEqual(collection.name, current_coll_name)

            # check subcollection name
            if d < depth:
                sub_coll_name = "sub" + str(d)
                self.assertEqual(sub_coll_name, subcollections[0].name)
            else:
                # last coll has no subcolls
                self.assertListEqual(subcollections, [])

            # check data object names
            for data_object in data_objects:
                self.assertIn(data_object.name, filenames)

            # iterate
            current_coll_name = sub_coll_name

        # that should be it
        with self.assertRaises(StopIteration):
            next(colls)

    def test_walk_collection(self):
        depth = 20

        # files that will be ceated in each subcollection
        filenames = ["foo", "bar", "baz"]

        # make nested collections
        coll_path = self.test_coll_path
        for d in range(depth):
            # create subcollection with files
            coll_path += "/sub" + str(d)
            helpers.make_collection(self.sess, coll_path, filenames)

        # now walk nested collections
        colls = self.test_coll.walk(topdown=False)
        sub_coll_name = ""
        for d in range(depth - 1, -2, -1):
            # get next result
            collection, subcollections, data_objects = next(colls)

            # check collection name
            if d >= 0:
                coll_name = "sub" + str(d)
                self.assertEqual(collection.name, coll_name)
            else:
                # root collection
                self.assertEqual(collection.name, self.test_coll.name)

            # check subcollection name
            if d < depth - 1:
                self.assertEqual(sub_coll_name, subcollections[0].name)
            else:
                # last coll has no subcolls
                self.assertListEqual(subcollections, [])

            # check data object names
            for data_object in data_objects:
                self.assertIn(data_object.name, filenames)

            # iterate
            sub_coll_name = coll_name

        # that should be it
        with self.assertRaises(StopIteration):
            next(colls)

    def test_collection_metadata(self):
        self.assertIsInstance(self.test_coll.metadata, iRODSMetaCollection)

    def test_register_collection(self):
        tmp_dir = helpers.irods_shared_tmp_dir()
        loc_server = self.sess.host in ("localhost", socket.gethostname())
        if not (tmp_dir) and not (loc_server):
            self.skipTest("Requires access to server-side file(s)")

        # test vars
        file_count = 10
        dir_name = "register_test_dir"
        dir_path = os.path.join((tmp_dir or "/tmp"), dir_name)
        coll_path = "{}/{}".format(self.test_coll.path, dir_name)

        # make test dir
        helpers.make_flat_test_dir(dir_path, file_count)

        # register test dir
        self.sess.collections.register(dir_path, coll_path)

        # confirm collection presence
        coll = self.sess.collections.get(coll_path)

        # confirm object count in collection
        query = (
            self.sess.query().count(DataObject.id).filter(Collection.name == coll_path)
        )
        obj_count = next(query.get_results())[DataObject.id]
        self.assertEqual(file_count, int(obj_count))

        # remove coll but leave directory on disk
        coll.unregister()

        # delete test dir
        shutil.rmtree(dir_path)

    def test_register_collection_with_checksums(self):
        tmp_dir = helpers.irods_shared_tmp_dir()
        loc_server = self.sess.host in ("localhost", socket.gethostname())
        if not (tmp_dir) and not (loc_server):
            self.skipTest("Requires access to server-side file(s)")

        # test vars
        file_count = 10
        dir_name = "register_test_dir_with_chksums"
        dir_path = os.path.join((tmp_dir or "/tmp"), dir_name)
        coll_path = "{}/{}".format(self.test_coll.path, dir_name)

        # make test dir
        helpers.make_flat_test_dir(dir_path, file_count)

        # register test dir
        options = {kw.VERIFY_CHKSUM_KW: ""}
        self.sess.collections.register(dir_path, coll_path, **options)

        # confirm collection presence
        coll = self.sess.collections.get(coll_path)

        # confirm object count in collection
        query = (
            self.sess.query().count(DataObject.id).filter(Collection.name == coll_path)
        )
        obj_count = next(query.get_results())[DataObject.id]
        self.assertEqual(file_count, int(obj_count))

        # confirm object checksums
        objs = next(coll.walk())[2]
        for obj in objs:
            # don't use obj.path (aka logical path)
            phys_path = obj.replicas[0].path
            digest = helpers.compute_sha256_digest(phys_path)
            self.assertEqual(obj.checksum, "sha2:{}".format(digest))

        # remove coll but leave directory on disk
        coll.unregister()

        # delete test dir
        shutil.rmtree(dir_path)

    def test_collection_with_trailing_slash__323(self):
        Home = helpers.home_collection(self.sess)
        subcoll, dataobj = [
            unique_name(my_function_name(), time.time()) for x in range(2)
        ]
        subcoll_fullpath = "{}/{}".format(Home, subcoll)
        subcoll_unnormalized = subcoll_fullpath + "/"
        try:
            # Test create and exists with trailing slashes.
            self.sess.collections.create(subcoll_unnormalized)
            c1 = self.sess.collections.get(subcoll_unnormalized)
            c2 = self.sess.collections.get(subcoll_fullpath)
            self.assertEqual(c1.id, c2.id)
            self.assertTrue(self.sess.collections.exists(subcoll_unnormalized))

            # Test data put to unnormalized collection name.
            with open(dataobj, "wb") as f:
                f.write(b"hello")
            self.sess.data_objects.put(dataobj, subcoll_unnormalized)
            self.assertEqual(
                self.sess.query(DataObject)
                .filter(DataObject.name == dataobj)
                .one()[DataObject.collection_id],
                c1.id,
            )
        finally:
            if self.sess.collections.exists(subcoll_fullpath):
                self.sess.collections.remove(subcoll_fullpath, recurse=True, force=True)
            if os.path.exists(dataobj):
                os.unlink(dataobj)

    def test_concatenation__323(self):
        coll = iRODSCollection.normalize_path("/zone/", "/home/", "/dan//", "subdir///")
        self.assertEqual(coll, "/zone/home/dan/subdir")

    def test_object_paths_with_dot_and_dotdot__323(self):

        normalize = iRODSCollection.normalize_path
        session = self.sess
        home = helpers.home_collection(session)

        # Test requirement for collection names to be absolute
        with self.assertRaises(iRODSCollection.AbsolutePathRequired):
            normalize("../public", enforce_absolute=True)

        # Test '.' and double slashes
        public_home = normalize(home, "..//public/.//")
        self.assertEqual(public_home, "/{sess.zone}/home/public".format(sess=session))

        # Test that '..' cancels last nontrival path element
        subpath = normalize(home, "./collA/coll2/./../collB")
        self.assertEqual(subpath, home + "/collA/collB")

        # Test multiple '..'
        home1 = normalize("/zone", "holmes", "public/../..", "home", "user")
        self.assertEqual(home1, "/zone/home/user")
        home2 = normalize("/zone", "holmes", "..", "home", "public", "..", "user")
        self.assertEqual(home2, "/zone/home/user")

    def test_update_mtime_of_collection_using_touch_operation_as_non_admin__525(self):
        user_session = self.logins.session_for_user(RODSUSER)

        # Capture mtime of the home collection.
        home_collection_path = helpers.home_collection(user_session)
        collection = user_session.collections.get(home_collection_path)
        old_mtime = collection.modify_time

        # Set the mtime to an earlier time.
        new_mtime = 1400000000
        user_session.collections.touch(
            home_collection_path, seconds_since_epoch=new_mtime
        )

        # Compare mtimes for correctness.
        collection = user_session.collections.get(home_collection_path)
        self.assertEqual(
            datetime.fromtimestamp(new_mtime, timezone.utc), collection.modify_time
        )
        self.assertGreater(old_mtime, collection.modify_time)

    def test_touch_operation_does_not_create_new_collections__525(self):
        user_session = self.logins.session_for_user(RODSUSER)

        # The collection should not exist.
        home_collection = helpers.home_collection(user_session)
        collection_path = "{home_collection}/test_touch_operation_does_not_create_new_collections__525".format(
            **locals()
        )
        with self.assertRaises(CollectionDoesNotExist):
            user_session.collections.get(collection_path)

        # Show the touch operation throws an exception if the target collection
        # does not exist.
        with self.assertRaises(CollectionDoesNotExist):
            user_session.collections.touch(collection_path)

        # Show the touch operation did not create a new collection.
        with self.assertRaises(CollectionDoesNotExist):
            user_session.collections.get(collection_path)

    def test_touch_operation_does_not_work_when_given_a_data_object__525(self):
        try:
            user_session = self.logins.session_for_user(RODSUSER)
            home_collection = helpers.home_collection(user_session)

            # Create a data object.
            data_object_path = "{home_collection}/test_touch_operation_does_not_work_when_given_a_data_object__525.txt".format(
                **locals()
            )
            self.assertFalse(user_session.data_objects.exists(data_object_path))
            user_session.data_objects.touch(data_object_path)
            self.assertTrue(user_session.data_objects.exists(data_object_path))

            # Show the touch operation for collections throws an exception when
            # given a path pointing to a data object.
            with self.assertRaises(CollectionDoesNotExist):
                user_session.collections.touch(data_object_path)

        finally:
            user_session.data_objects.unlink(data_object_path, force=True)

    def test_touch_operation_ignores_unsupported_options__525(self):
        user_session = self.logins.session_for_user(RODSUSER)

        home_collection = helpers.home_collection(user_session)
        path = "{home_collection}/test_touch_operation_ignores_unsupported_options__525".format(
            **locals()
        )

        try:
            # Capture mtime of the home collection.
            collection = user_session.collections.create(path)
            old_mtime = collection.modify_time

            # Capture the current time.
            time.sleep(2)  # Guarantees the mtime is different.
            new_mtime = int(time.time())

            # The touch API for the iRODS server will attempt to create a new data object
            # if the "no_create" option is set to false. The PRC's collection interface will
            # ignore that option if passed.
            #
            # The following arguments don't make sense for collections and will also be ignored.
            #
            #   - replica_number
            #   - leaf_resource_name
            #
            # They are included to prove the PRC handles them appropriately (i.e. unsupported
            # parameters are removed from the request).
            user_session.collections.touch(
                path,
                no_create=False,
                replica_number=525,
                seconds_since_epoch=new_mtime,
                leaf_resource_name="ufs525",
            )

            # Compare mtimes for correctness.
            collection = user_session.collections.get(path)
            self.assertEqual(
                datetime.fromtimestamp(int(new_mtime), timezone.utc),
                collection.modify_time,
            )

        finally:
            if collection:
                user_session.collections.remove(path, recurse=True, force=True)

    def test_subcollections_member_excludes_root_collection__571(self):

        root_coll = self.sess.collections.get("/")

        # Assert that none of the root collection's immediate children (as listed in the object's
        # 'subcollections' property) point to the root subcollection.
        self.assertEqual(root_coll.path, "/")
        self.assertEqual([], [_ for _ in root_coll.subcollections if _.path == "/"])


if __name__ == "__main__":
    # let the tests find the parent irods lib
    sys.path.insert(0, os.path.abspath("../.."))
    unittest.main()