summaryrefslogtreecommitdiffstats
path: root/tests/functional/arbiter/test_afr_read_write.py
blob: 09e6a3a2a0ec0c37054d1b0fcaa5d90dc16c869f (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
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
#  Copyright (C) 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.

from random import sample
from glusto.core import Glusto as g
from glustolibs.gluster.gluster_base_class import GlusterBaseClass, runs_on
from glustolibs.gluster.exceptions import ExecutionError
from glustolibs.gluster.volume_libs import (
    verify_all_process_of_volume_are_online,
    wait_for_volume_process_to_be_online)
from glustolibs.gluster.brick_libs import (
    get_all_bricks,
    bring_bricks_offline,
    bring_bricks_online,
    are_bricks_offline)
from glustolibs.gluster.heal_libs import (monitor_heal_completion,
                                          is_volume_in_split_brain)
from glustolibs.gluster.glusterdir import mkdir
from glustolibs.io.utils import validate_io_procs


@runs_on([['arbiter', 'distributed-arbiter'], ['glusterfs']])
class TestAfrReadWrite(GlusterBaseClass):

    """
    Description:
        Arbiter test writes and reads from a file
    """
    def setUp(self):
        # Calling GlusterBaseClass
        self.get_super_method(self, 'setUp')()

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

    def tearDown(self):
        """
        Cleanup and umount volume
        """
        # Cleanup and umount volume
        ret = self.unmount_volume_and_cleanup_volume(mounts=self.mounts)
        if not ret:
            raise ExecutionError("Failed to umount the vol & cleanup Volume")

        # Calling GlusterBaseClass teardown
        self.get_super_method(self, 'tearDown')()

    def _bring_bricks_online_heal(self, mnode, volname, bricks_list):
        """
        Bring bricks online and monitor heal completion
        """
        # Bring bricks online
        ret = bring_bricks_online(
            mnode, volname, bricks_list,
            bring_bricks_online_methods=['volume_start_force'])
        self.assertTrue(ret, 'Failed to bring bricks online')

        # Wait for volume processes to be online
        ret = wait_for_volume_process_to_be_online(mnode, volname)
        self.assertTrue(ret, ("Failed to wait for volume {} processes to "
                              "be online".format(volname)))

        # Verify volume's all process are online
        ret = verify_all_process_of_volume_are_online(mnode, volname)
        self.assertTrue(ret, ("Volume {} : All process are not online".format
                              (volname)))
        g.log.info("Volume %s : All process are online", volname)

        # Monitor heal completion
        ret = monitor_heal_completion(mnode, volname)
        self.assertTrue(ret, 'Heal has not yet completed')

        # Check for split-brain
        ret = is_volume_in_split_brain(mnode, volname)
        self.assertFalse(ret, 'Volume is in split-brain state')

    def test_afr_read_write(self):
        """
        Test read and write of file
        Description:
        - Get the bricks from the volume
        - Creating directory test_write_and_read_file
        - Write from 1st client
        - Read from 2nd client
        - Select brick to bring offline
        - Bring brick offline
        - Validating IO's on client1
        - Validating IO's on client2
        - Bring bricks online
        - Wait for volume processes to be online
        - Verify volume's all process are online
        - Monitor heal completion
        - Check for split-brain
        - Bring 2nd brick offline
        - Check if brick is offline
        - Write from 1st client
        - Read from 2nd client
        - Bring bricks online
        - Wait for volume processes to be online
        - Verify volume's all process are online
        - Monitor heal completion
        - Check for split-brain

        - Get arequal after getting bricks online
        """
        # pylint: disable=too-many-branches,too-many-statements,too-many-locals
        # Get the bricks from the volume
        bricks_list = get_all_bricks(self.mnode, self.volname)
        g.log.info("Brick List : %s", bricks_list)

        # Creating directory test_write_and_read_file
        ret = mkdir(self.mounts[0].client_system,
                    "{}/test_write_and_read_file"
                    .format(self.mounts[0].mountpoint))
        self.assertTrue(ret, "Failed to create directory")
        g.log.info("Directory 'test_write_and_read_file' on %s created "
                   "successfully", self.mounts[0])

        # Write from 1st client
        cmd_to_write = (
            'cd %s/test_write_and_read_file ; for i in `seq 1 5000` ;'
            'do echo -e "Date:`date`\n" >> test_file ;echo -e "'
            '`cal`\n" >> test_file ; done ; cd ..'
            % self.mounts[0].mountpoint)
        proc1 = g.run_async(self.mounts[0].client_system,
                            cmd_to_write)

        # Read from 2nd client
        cmd = ('cd %s/ ;for i in {1..30};'
               'do cat test_write_and_read_file/test_file;done'
               % self.mounts[1].mountpoint)
        proc2 = g.run_async(self.mounts[1].client_system, cmd)

        # Bring brick offline
        bricks_to_bring_offline = sample(bricks_list, 2)
        ret = bring_bricks_offline(self.volname, bricks_to_bring_offline[0])
        self.assertTrue(ret, 'Failed to bring bricks {} offline'.
                        format(bricks_to_bring_offline))

        # Check brick is offline
        ret = are_bricks_offline(self.mnode, self.volname,
                                 [bricks_to_bring_offline[0]])
        self.assertTrue(ret, 'Bricks {} are not offline'.
                        format(bricks_to_bring_offline[0]))

        # Validating IO's
        for proc, mount in zip([proc1, proc2], self.mounts):
            ret = validate_io_procs([proc], mount)
            self.assertTrue(ret, "IO failed on client")
        g.log.info("Successfully validated all IO's")

        self._bring_bricks_online_heal(self.mnode, self.volname, bricks_list)

        # Bring down second brick
        ret = bring_bricks_offline(self.volname, bricks_to_bring_offline[1])
        self.assertTrue(ret, 'Failed to bring bricks {} offline'.
                        format(bricks_to_bring_offline[1]))

        # Check if brick is offline
        ret = are_bricks_offline(self.mnode, self.volname,
                                 [bricks_to_bring_offline[1]])
        self.assertTrue(ret, 'Bricks {} are not offline'.
                        format(bricks_to_bring_offline[1]))

        # Write from 1st client
        ret, _, _ = g.run(self.mounts[0].client_system, cmd_to_write)
        self.assertEqual(ret, 0, "Failed to write to file")
        g.log.info("Successfully written to file")

        # Read from 2nd client
        cmd = ('cd %s/ ;cat test_write_and_read_file/test_file'
               % self.mounts[0].mountpoint)
        ret, _, _ = g.run(self.mounts[0].client_system, cmd)
        self.assertEqual(ret, 0, "Failed to read file on mountpoint")
        g.log.info("Successfully read file on mountpoint")

        self._bring_bricks_online_heal(self.mnode, self.volname, bricks_list)