def test_1(self):
        """ Test that pkg.migrate correctly moves files from the old
                layout to the new layout correctly."""

        hashes = [
            "2be802388acdf0e17c1ea0855be5d29715290d01",
            "0338a1ee2a98c7c9cedbff6e5a4a93a88ba05b72",
            "ff9ea25633b995eeb4c0ae896b9f7586f8effceb",
            "ff9ea25633b995eeb4c0ae896b9f7586f8effcec",
            "a24ba602e0f43bac4eb6223de54a003c63d9b8d9"
        ]

        old_paths = [self.touch_old_file(h) for h in hashes]

        self.update_file_layout(self.base_dir)
        for p in old_paths:
            if os.path.exists(p):
                raise RuntimeError("{0} should not exist".format(p))
            if os.path.exists(os.path.dirname(p)):
                raise RuntimeError("directory {0} should not " "exist")
        l = layout.get_preferred_layout()
        for h in hashes:
            if not os.path.exists(os.path.join(self.base_dir, l.lookup(h))):
                raise RuntimeError("file for {0} is missing".format(h))

        self.update_file_layout(self.base_dir)
        for h in hashes:
            if not os.path.exists(os.path.join(self.base_dir, l.lookup(h))):
                raise RuntimeError("file for {0} is missing".format(h))
Пример #2
0
        def test_1(self):
                """ Test that pkg.migrate correctly moves files from the old
                layout to the new layout correctly."""

                hashes = ["2be802388acdf0e17c1ea0855be5d29715290d01",
                    "0338a1ee2a98c7c9cedbff6e5a4a93a88ba05b72",
                    "ff9ea25633b995eeb4c0ae896b9f7586f8effceb",
                    "ff9ea25633b995eeb4c0ae896b9f7586f8effcec",
                    "a24ba602e0f43bac4eb6223de54a003c63d9b8d9"
                ]

                old_paths = [self.touch_old_file(h) for h in hashes]

                self.update_file_layout(self.base_dir)
                for p in old_paths:
                        if os.path.exists(p):
                                raise RuntimeError("%s should not exist" % p)
                        if os.path.exists(os.path.dirname(p)):
                                raise RuntimeError("directory %s should not "
                                    "exist")
                l = layout.get_preferred_layout()
                for h in hashes:
                        if not os.path.exists(os.path.join(self.base_dir,
                            l.lookup(h))):
                                raise RuntimeError("file for %s is missing" % h)

                self.update_file_layout(self.base_dir)
                for h in hashes:
                        if not os.path.exists(os.path.join(self.base_dir,
                            l.lookup(h))):
                                raise RuntimeError("file for %s is missing" % h)
Пример #3
0
    def test_1(self):
        """Verify base functionality works as expected."""

        t = tempfile.gettempdir()
        no_dir = os.path.join(t, "not_exist")

        # Test that a read only FileManager won't modify the file
        # system.
        fm = file_manager.FileManager(self.base_dir, readonly=True)
        self.assertEqual(os.listdir(self.base_dir), [])

        unmoved = "4b7c923af3a047d4685a39ad7bc9b0382ccde671"

        p = self.touch_old_file(unmoved)
        self.check_readonly(fm, unmoved, p)

        self.assertEqual(set(fm.walk()), set([unmoved]))

        # Test a FileManager that can write to the file system.
        fm = file_manager.FileManager(self.base_dir, False)

        hash1 = "584b6ab7d7eb446938a02e57101c3a2fecbfb3cb"
        hash2 = "584b6ab7d7eb446938a02e57101c3a2fecbfb3cc"
        hash3 = "994b6ab7d7eb446938a02e57101c3a2fecbfb3cc"
        hash4 = "cc1f76cdad188714d1c3b92a4eebb4ec7d646166"

        l = layout.V1Layout()

        self.assertEqual(l.lookup(hash1),
                         "58/584b6ab7d7eb446938a02e57101c3a2fecbfb3cb")

        # Test that looking up a file stored under the old system gets
        # moved to the correct location, that the new location is
        # correctly returned, and that the old location's parent
        # directory no longer exists as only a single file existed
        # there.  Finally, remove it for the next test if successful.
        p1 = self.touch_old_file(hash1)
        self.assertTrue(os.path.isfile(p1))
        self.assertTrue(os.path.isdir(os.path.dirname(p1)))
        self.assertEqual(fm.lookup(hash1),
                         os.path.join(self.base_dir, l.lookup(hash1)))
        self.assertTrue(not os.path.exists(p1))
        self.assertTrue(not os.path.exists(os.path.dirname(p1)))
        fm.remove(hash1)

        # Test that looking up a file stored under the old system gets
        # moved to the correct location, that the new location is
        # correctly returned, and that the old location's parent
        # directory still exists as multiple files were stored there.
        # Finally, remove file stored in the old location for the next
        # few tests.
        p1 = self.touch_old_file(hash1)
        self.touch_old_file(hash2)
        self.assertTrue(os.path.isfile(p1))
        self.assertTrue(os.path.isdir(os.path.dirname(p1)))
        self.assertEqual(fm.lookup(hash1),
                         os.path.join(self.base_dir, l.lookup(hash1)))
        self.assertTrue(not os.path.exists(p1))
        self.assertTrue(os.path.exists(os.path.dirname(p1)))
        fm.remove(hash2)

        # Test that looking up a file stored under the old system gets
        # moved and that it returns a file handle with the correct
        # contents.
        p4 = self.touch_old_file(hash4)
        self.assertTrue(os.path.isfile(p4))
        self.assertTrue(os.path.isdir(os.path.dirname(p4)))
        fh = fm.lookup(hash4, opener=True)
        try:
            self.assertEqual(fh.read(), misc.force_bytes(hash4))
        finally:
            fh.close()
        self.assertTrue(not os.path.exists(p4))
        self.assertTrue(not os.path.exists(os.path.dirname(p4)))

        p3 = self.touch_old_file(hash3)
        self.assertTrue(os.path.isfile(p3))
        self.assertTrue(os.path.isdir(os.path.dirname(p3)))
        fm.insert(hash3, p3)

        self.assertTrue(not os.path.exists(p3))
        self.assertTrue(not os.path.exists(os.path.dirname(p3)))

        fh = fm.lookup(hash3, opener=True)
        try:
            self.assertEqual(fh.read(), misc.force_bytes(hash3))
        finally:
            fh.close()

        # Test that walk returns the expected values.
        self.assertEqual(set(fm.walk()), set([unmoved, hash1, hash4, hash3]))

        # Test that walking with a different set of layouts works as
        # expected.
        fm2 = file_manager.FileManager(self.base_dir,
                                       readonly=True,
                                       layouts=[layout.get_preferred_layout()])

        fs = set([hash1, hash4, hash3])
        try:
            for i in fm2.walk():
                fs.remove(i)
        except file_manager.UnrecognizedFilePaths as e:
            self.assertEqual(e.fps, [p[len(self.base_dir) + 1:]])
        self.assertEqual(fs, set())

        # Test removing a file works and removes the containing
        # directory and that remove removes all instances of a hash
        # from the file manager.
        hash3_loc = os.path.join(self.base_dir, l.lookup(hash3))
        v0_hash3_loc = self.touch_old_file(hash3)

        self.assertTrue(os.path.isfile(hash3_loc))
        self.assertTrue(os.path.isfile(v0_hash3_loc))
        fm.remove(hash3)
        self.assertEqual(fm.lookup(hash3), None)
        self.assertTrue(not os.path.exists(hash3_loc))
        self.assertTrue(not os.path.exists(os.path.dirname(hash3_loc)))
        self.assertTrue(not os.path.exists(v0_hash3_loc))
        self.assertTrue(not os.path.exists(os.path.dirname(v0_hash3_loc)))
        self.assertTrue(os.path.isfile(fm.lookup(hash1)))

        rh2_fd, raw_hash_2_loc = tempfile.mkstemp(dir=self.base_dir)
        rh2_fh = os.fdopen(rh2_fd, "w")
        rh2_fh.write(hash2)
        rh2_fh.close()

        fm.insert(hash2, raw_hash_2_loc)
        h2_loc = fm.lookup(hash2)
        self.assertTrue(os.path.isfile(fm.lookup(hash2)))
        # Test that the directory has two files in it as expected.
        self.assertEqual(set(os.listdir(os.path.dirname(fm.lookup(hash2)))),
                         set([hash1, hash2]))
        # Test removing one of the two files doesn't remove the other.
        fm.remove(hash1)
        self.assertTrue(os.path.isfile(h2_loc))
        self.assertEqual(fm.lookup(hash2), h2_loc)
        self.assertEqual(fm.lookup(hash1), None)
        # Test that removing the second file works and removes the
        # containing directory as well.
        fm.remove(hash2)
        self.assertTrue(not os.path.exists(h2_loc))
        self.assertTrue(not os.path.exists(os.path.dirname(h2_loc)))

        # Test that setting the read_only property works and that none
        # of the activities has effected the location where unmoved has
        # been stored.
        fm.set_read_only()
        self.check_readonly(fm, unmoved, p)
Пример #4
0
        def test_1(self):
                """Verify base functionality works as expected."""

                t = tempfile.gettempdir()
                no_dir = os.path.join(t, "not_exist")

                # Test that a read only FileManager won't modify the file
                # system.
                fm = file_manager.FileManager(self.base_dir, readonly=True)
                self.assertEqual(os.listdir(self.base_dir), [])

                unmoved = "4b7c923af3a047d4685a39ad7bc9b0382ccde671"

                p = self.touch_old_file(unmoved)
                self.check_readonly(fm, unmoved, p)

                self.assertEqual(set(fm.walk()),
                    set([unmoved]))

                # Test a FileManager that can write to the file system.
                fm = file_manager.FileManager(self.base_dir, False)

                hash1 = "584b6ab7d7eb446938a02e57101c3a2fecbfb3cb"
                hash2 = "584b6ab7d7eb446938a02e57101c3a2fecbfb3cc"
                hash3 = "994b6ab7d7eb446938a02e57101c3a2fecbfb3cc"
                hash4 = "cc1f76cdad188714d1c3b92a4eebb4ec7d646166"

                l = layout.V1Layout()

                self.assertEqual(l.lookup(hash1),
                    "58/584b6ab7d7eb446938a02e57101c3a2fecbfb3cb")

                # Test that looking up a file stored under the old system gets
                # moved to the correct location, that the new location is
                # correctly returned, and that the old location's parent
                # directory no longer exists as only a single file existed
                # there.  Finally, remove it for the next test if successful.
                p1 = self.touch_old_file(hash1)
                self.assert_(os.path.isfile(p1))
                self.assert_(os.path.isdir(os.path.dirname(p1)))
                self.assertEqual(fm.lookup(hash1),
                    os.path.join(self.base_dir, l.lookup(hash1)))
                self.assert_(not os.path.exists(p1))
                self.assert_(not os.path.exists(os.path.dirname(p1)))
                fm.remove(hash1)

                # Test that looking up a file stored under the old system gets
                # moved to the correct location, that the new location is
                # correctly returned, and that the old location's parent
                # directory still exists as multiple files were stored there.
                # Finally, remove file stored in the old location for the next
                # few tests.
                p1 = self.touch_old_file(hash1)
                self.touch_old_file(hash2)
                self.assert_(os.path.isfile(p1))
                self.assert_(os.path.isdir(os.path.dirname(p1)))
                self.assertEqual(fm.lookup(hash1),
                    os.path.join(self.base_dir, l.lookup(hash1)))
                self.assert_(not os.path.exists(p1))
                self.assert_(os.path.exists(os.path.dirname(p1)))
                fm.remove(hash2)

                # Test that looking up a file stored under the old system gets
                # moved and that it returns a file handle with the correct
                # contents.
                p4 = self.touch_old_file(hash4)
                self.assert_(os.path.isfile(p4))
                self.assert_(os.path.isdir(os.path.dirname(p4)))
                fh = fm.lookup(hash4, opener=True)
                try:
                        self.assertEqual(fh.read(), hash4)
                finally:
                        fh.close()
                self.assert_(not os.path.exists(p4))
                self.assert_(not os.path.exists(os.path.dirname(p4)))

                p3 = self.touch_old_file(hash3)
                self.assert_(os.path.isfile(p3))
                self.assert_(os.path.isdir(os.path.dirname(p3)))
                fm.insert(hash3, p3)

                self.assert_(not os.path.exists(p3))
                self.assert_(not os.path.exists(os.path.dirname(p3)))

                fh = fm.lookup(hash3, opener=True)
                try:
                        self.assertEqual(fh.read(), hash3)
                finally:
                        fh.close()

                # Test that walk returns the expected values.
                self.assertEqual(set(fm.walk()),
                    set([unmoved, hash1, hash4, hash3]))

                # Test that walking with a different set of layouts works as
                # expected.
                fm2 = file_manager.FileManager(self.base_dir, readonly=True,
                    layouts=[layout.get_preferred_layout()])

                fs = set([hash1, hash4, hash3])
                try:
                        for i in fm2.walk():
                                fs.remove(i)
                except file_manager.UnrecognizedFilePaths, e:
                        self.assertEqual(e.fps, [p[len(self.base_dir) + 1:]])