summaryrefslogtreecommitdiffstats
path: root/tests/functional/afr/test_dir_gfid_heal_on_all_subvols.py
blob: dcce8b4187441b90fbe5542c1ac0a85f96b3ad9b (plain)
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
#  Copyright (C) 2017-2020  Red Hat, Inc. <http://www.redhat.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# pylint: disable=too-many-statements, too-many-locals
"""
Description:
    Test cases in this module tests whether directory with null gfid
    is getting the gfids assigned on both the subvols of a dist-rep
    volume when lookup comes on that directory from the mount point.
"""


from time import sleep
from glusto.core import Glusto as g
from glustolibs.gluster.exceptions import ExecutionError
from glustolibs.gluster.gluster_base_class import GlusterBaseClass, runs_on
from glustolibs.misc.misc_libs import upload_scripts
from glustolibs.gluster.brick_libs import get_all_bricks
from glustolibs.gluster.glusterfile import (get_fattr, delete_fattr)
from glustolibs.io.utils import get_mounts_stat


@runs_on([['replicated', 'distributed-replicated', 'distributed'],
          ['glusterfs']])
class AssignGfidsOnAllSubvols(GlusterBaseClass):

    @classmethod
    def setUpClass(cls):

        # Calling GlusterBaseClass setUpClass
        cls.get_super_method(cls, 'setUpClass')()

        # Upload io scripts for running IO on mounts
        cls.script_upload_path = ("/usr/share/glustolibs/io/scripts/"
                                  "file_dir_ops.py")
        ret = upload_scripts(cls.clients, cls.script_upload_path)
        if not ret:
            raise ExecutionError("Failed to upload IO scripts "
                                 "to clients %s" % cls.clients)
        g.log.info("Successfully uploaded IO scripts to clients %s",
                   cls.clients)

    def setUp(self):

        # Calling GlusterBaseClass setUpClass
        self.get_super_method(self, 'setUp')()

        # Setup Volume and Mount Volume
        ret = self.setup_volume_and_mount_volume(self.mounts)
        if not ret:
            raise ExecutionError("Failed to Setup_Volume and Mount_Volume")
        g.log.info("Successful in Setup Volume and Mount Volume")

    def tearDown(self):

        # Cleanup Volume
        ret = self.unmount_volume_and_cleanup_volume(self.mounts)
        if not ret:
            raise ExecutionError("Failed to create volume")
        g.log.info("Successful in cleaning up Volume %s", self.volname)

        self.get_super_method(self, 'tearDown')()

    def verify_gfid_and_retun_gfid(self, dirname):
        dir_gfids = dict()
        bricks_list = get_all_bricks(self.mnode, self.volname)
        for brick in bricks_list:
            brick_node, brick_path = brick.split(":")

            ret = get_fattr(brick_node, '%s/%s' % (brick_path, dirname),
                            'trusted.gfid')
            if ret is not None:
                self.assertIsNotNone(ret, "trusted.gfid is not present on"
                                     "%s/%s" % (brick, dirname))
                dir_gfids.setdefault(dirname, []).append(ret)
                for key in dir_gfids:
                    self.assertTrue(all(value == dir_gfids[key][0]
                                        for value in dir_gfids[key]),
                                    "gfid mismatch for %s" % dirname)
                    dir_gfid = dir_gfids.values()[0]
        return dir_gfid

    def test_dir_gfid_heal_on_all_subvols(self):
        """
        - Create a volume and mount it.
        - Create a directory on mount and check whether all the bricks have
          the same gfid.
        - Now delete gfid attr from all but one backend bricks,
        - Do lookup from the mount.
        - Check whether all the bricks have the same gfid assigned.
        """

        # Create a directory on the mount
        cmd = ("/usr/bin/env python %s create_deep_dir -d 0 -l 0 "
               "%s/dir1" % (self.script_upload_path,
                            self.mounts[0].mountpoint))
        ret, _, _ = g.run(self.clients[0], cmd)
        self.assertEqual(ret, 0, "Failed to create directory on mountpoint")
        g.log.info("Directory created successfully on mountpoint")

        # Verify gfids are same on all the bricks and get dir1 gfid
        bricks_list = get_all_bricks(self.mnode, self.volname)[1:]
        dir_gfid = self.verify_gfid_and_retun_gfid("dir1")

        # Delete gfid attr from all but one backend bricks
        for brick in bricks_list:
            brick_node, brick_path = brick.split(":")
            ret = delete_fattr(brick_node, '%s/dir1' % (brick_path),
                               'trusted.gfid')
            self.assertTrue(ret, 'Failed to delete gfid for brick '
                            'path %s:%s/dir1' % (brick_node, brick_path))
            g.log.info("Successfully deleted gfid xattr for %s:%s/dir1",
                       brick_node, brick_path)
        g.log.info("Successfully deleted gfid xattr for dir1 on the "
                   "following bricks %s", str(bricks_list[1:]))

        # Trigger heal from mount point
        sleep(10)
        for mount_obj in self.mounts:
            g.log.info("Triggering heal for %s:%s",
                       mount_obj.client_system, mount_obj.mountpoint)
            command = ('cd %s; ls -l' % mount_obj.mountpoint)
            ret, _, _ = g.run(mount_obj.client_system, command)
            self.assertFalse(ret, 'Failed to run lookup '
                             'on %s ' % mount_obj.client_system)
            sleep(10)

        ret = get_mounts_stat(self.mounts)
        self.assertTrue(ret, "Failed to stat lookup on clients")
        g.log.info('stat lookup on clients succeeded')

        # Verify that all gfids for dir1 are same and get the gfid
        dir_gfid_new = self.verify_gfid_and_retun_gfid("dir1")
        self.assertTrue(all(gfid in dir_gfid for gfid in dir_gfid_new),
                        'Previous gfid and new gfid are not equal, '
                        'which is not expected, previous gfid %s '
                        'and new gfid %s' % (dir_gfid, dir_gfid_new))
        g.log.info('gfid heal was successful from client lookup and all '
                   'backend bricks have same gfid xattr, no gfid mismatch')