summaryrefslogtreecommitdiffstats
path: root/tests/functional/dht/test_copy_dir_subvol_down.py
blob: 8835bcadaf5096b9b43964775a768686c1320079 (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
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
#  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 glusto.core import Glusto as g
from glustolibs.gluster.exceptions import ExecutionError
from glustolibs.gluster.gluster_base_class import GlusterBaseClass, runs_on
from glustolibs.gluster.glusterdir import mkdir
from glustolibs.io.utils import collect_mounts_arequal, validate_io_procs
from glustolibs.misc.misc_libs import upload_scripts
from glustolibs.gluster.dht_test_utils import (find_hashed_subvol,
                                               find_new_hashed)
from glustolibs.gluster.volume_libs import get_subvols
from glustolibs.gluster.brick_libs import bring_bricks_offline


@runs_on([['distributed', 'distributed-replicated',
           'distributed-arbiter', 'distributed-dispersed'],
          ['glusterfs']])
class TestCopyDirSubvolDown(GlusterBaseClass):
    @classmethod
    def setUpClass(cls):
        cls.get_super_method(cls, 'setUpClass')()

        # Check for the default dist_count value and override it if required
        if cls.default_volume_type_config['distributed']['dist_count'] <= 2:
            cls.default_volume_type_config['distributed']['dist_count'] = 4
        else:
            cls.default_volume_type_config[cls.voltype]['dist_count'] = 3

        # 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 setUp
        self.get_super_method(self, 'setUp')()

        # Setup Volume and Mount Volume
        ret = self.setup_volume_and_mount_volume([self.mounts[0]])
        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):

        # Unmount and cleanup original volume
        ret = self.unmount_volume_and_cleanup_volume(mounts=[self.mounts[0]])
        if not ret:
            raise ExecutionError("Failed to umount the vol & cleanup Volume")
        g.log.info("Successful in umounting the volume and Cleanup")

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

    def _create_src(self, m_point):
        """
        Create the source directory and files under the
        source directory.
        """
        # Create source dir
        ret = mkdir(self.mounts[0].client_system, "{}/src_dir".format(m_point))
        self.assertTrue(ret, "mkdir of src_dir failed")

        # Create files inside source dir
        cmd = ("/usr/bin/env python %s create_files "
               "-f 100 %s/src_dir/" % (
                   self.script_upload_path, m_point))
        proc = g.run_async(self.mounts[0].client_system,
                           cmd, user=self.mounts[0].user)
        g.log.info("IO on %s:%s is started successfully",
                   self.mounts[0].client_system, m_point)

        # Validate IO
        self.assertTrue(
            validate_io_procs([proc], self.mounts[0]),
            "IO failed on some of the clients"
        )

    def _copy_files_check_contents(self, m_point, dest_dir):
        """
        Copy files from source directory to destination
        directory when it hashes to up-subvol and check
        if all the files are copied properly.
        """
        # pylint: disable=protected-access
        # collect arequal checksum on src dir
        ret, src_checksum = collect_mounts_arequal(
            self.mounts[0], '{}/src_dir'.format(m_point))
        self.assertTrue(ret, ("Failed to get arequal on client"
                              " {}".format(self.clients[0])))

        # copy src_dir to dest_dir
        command = "cd {}; cp -r  src_dir {}".format(m_point, dest_dir)
        ret, _, _ = g.run(self.mounts[0].client_system, command)
        self.assertEqual(ret, 0, "Failed to copy of src dir to"
                                 " dest dir")
        g.log.info("Successfully copied src dir to dest dir.")

        # collect arequal checksum on destination dir
        ret, dest_checksum = collect_mounts_arequal(
            self.mounts[0], '{}/{}'.format(m_point, dest_dir))
        self.assertTrue(ret, ("Failed to get arequal on client"
                              " {}".format(self.mounts[0])))

        # Check if the contents of src dir are copied to
        # dest dir
        self.assertEqual(src_checksum,
                         dest_checksum,
                         'All the contents of src dir are not'
                         ' copied to dest dir')
        g.log.info('Successfully copied the contents of src dir'
                   ' to dest dir')

    def _copy_when_dest_hash_down(self, m_point, dest_dir):
        """
        Copy files from source directory to destination
        directory when it hashes to down-subvol.
        """
        # pylint: disable=protected-access
        # copy src_dir to dest_dir (should fail as hash subvol for dest
        # dir is down)
        command = "cd {}; cp -r  src_dir {}".format(m_point, dest_dir)
        ret, _, _ = g.run(self.mounts[0].client_system, command)
        self.assertEqual(ret, 1, "Unexpected : Copy of src dir to"
                                 " dest dir passed")
        g.log.info("Copy of src dir to dest dir failed as expected.")

    def test_copy_existing_dir_dest_subvol_down(self):
        """
        Case 1:
        - Create directory from mount point.
        - Copy dir ---> Bring down dht sub-volume where destination
          directory hashes to down sub-volume.
        - Copy directory and make sure destination dir does not exist
        """
        # pylint: disable=protected-access
        m_point = self.mounts[0].mountpoint

        # Create source dir
        ret = mkdir(self.mounts[0].client_system, "{}/src_dir".format(m_point))
        self.assertTrue(ret, "mkdir of src_dir failed")
        g.log.info("Directory src_dir created successfully")

        # Get subvol list
        subvols = (get_subvols(self.mnode, self.volname))['volume_subvols']
        self.assertIsNotNone(subvols, "Failed to get subvols")

        # Find out the destination dir name such that it hashes to
        # different subvol
        newdir = find_new_hashed(subvols, "/", "src_dir")
        dest_dir = str(newdir.newname)
        dest_count = newdir.subvol_count

        # Kill the brick/subvol to which the destination dir hashes
        ret = bring_bricks_offline(
            self.volname, subvols[dest_count])
        self.assertTrue(ret, ('Error in bringing down subvolume %s',
                              subvols[dest_count]))
        g.log.info('DHT subvol %s is offline', subvols[dest_count])

        # Copy src_dir to dest_dir (should fail as hash subvol for dest
        # dir is down)
        self._copy_when_dest_hash_down(m_point, dest_dir)

    def test_copy_existing_dir_dest_subvol_up(self):
        """
        Case 2:
        - Create files and directories from mount point.
        - Copy dir ---> Bring down dht sub-volume where destination
          directory should not hash to down sub-volume
        - copy dir and make sure destination dir does not exist
        """
        # pylint: disable=protected-access
        m_point = self.mounts[0].mountpoint

        # Create source dir and create files inside it
        self._create_src(m_point)

        # Get subvol list
        subvols = (get_subvols(self.mnode, self.volname))['volume_subvols']
        self.assertIsNotNone(subvols, "Failed to get subvols")

        # Find out hashed brick/subvol for src dir
        src_subvol, src_count = find_hashed_subvol(subvols, "/", "src_dir")
        self.assertIsNotNone(src_subvol, "Could not find srchashed")
        g.log.info("Hashed subvol for src_dir is %s", src_subvol._path)

        # Find out the destination dir name such that it hashes to
        # different subvol
        newdir = find_new_hashed(subvols, "/", "src_dir")
        dest_dir = str(newdir.newname)
        dest_count = newdir.subvol_count

        # Remove the hashed subvol for dest and src dir from the
        # subvol list
        for item in (subvols[src_count], subvols[dest_count]):
            subvols.remove(item)

        # Bring down a DHT subvol
        ret = bring_bricks_offline(self.volname, subvols[0])
        self.assertTrue(ret, ('Error in bringing down subvolume %s',
                              subvols[0]))
        g.log.info('DHT subvol %s is offline', subvols[0])

        # Create files on source dir and
        # perform copy of src_dir to dest_dir
        self._copy_files_check_contents(m_point, dest_dir)

    def test_copy_new_dir_dest_subvol_up(self):
        """
        Case 3:
        - Copy dir ---> Bring down dht sub-volume where destination
          directory should not hash to down sub-volume
        - Create files and directories from mount point.
        - copy dir and make sure destination dir does not exist
        """
        # pylint: disable=protected-access
        # pylint: disable=too-many-statements
        m_point = self.mounts[0].mountpoint

        # Get subvols
        subvols = (get_subvols(self.mnode, self.volname))['volume_subvols']
        self.assertIsNotNone(subvols, "Failed to get subvols")

        # Find out hashed brick/subvol for src dir
        src_subvol, src_count = find_hashed_subvol(
            subvols, "/", "src_dir")
        self.assertIsNotNone(src_subvol, "Could not find srchashed")
        g.log.info("Hashed subvol for src_dir is %s", src_subvol._path)

        # Find out the destination dir name such that it hashes to
        # different subvol
        newdir = find_new_hashed(subvols, "/", "src_dir")
        dest_dir = str(newdir.newname)
        dest_count = newdir.subvol_count

        # Remove the hashed subvol for dest and src dir from the
        # subvol list
        for item in (subvols[src_count], subvols[dest_count]):
            subvols.remove(item)

        # Bring down a dht subvol
        ret = bring_bricks_offline(self.volname, subvols[0])
        self.assertTrue(ret, ('Error in bringing down subvolume %s',
                              subvols[0]))
        g.log.info('DHT subvol %s is offline', subvols[0])

        # Create source dir and create files inside it
        self._create_src(m_point)

        # Create files on source dir and
        # perform copy of src_dir to dest_dir
        self._copy_files_check_contents(m_point, dest_dir)

    def test_copy_new_dir_dest_subvol_down(self):
        """
         Case 4:
        - Copy dir ---> Bring down dht sub-volume where destination
          directory hashes to down sub-volume
        - Create directory from mount point.
        - Copy dir and make sure destination dir does not exist
        """
        # pylint: disable=protected-access
        m_point = self.mounts[0].mountpoint

        # Get subvol list
        subvols = (get_subvols(self.mnode, self.volname))['volume_subvols']
        self.assertIsNotNone(subvols, "Failed to get subvols")

        # Find out the destination dir name such that it hashes to
        # different subvol
        newdir = find_new_hashed(subvols, "/", "src_dir")
        dest_dir = str(newdir.newname)
        dest_count = newdir.subvol_count

        # Bring down the hashed-subvol for dest dir
        ret = bring_bricks_offline(self.volname, subvols[dest_count])
        self.assertTrue(ret, ('Error in bringing down subvolume %s',
                              subvols[dest_count]))
        g.log.info('DHT subvol %s is offline', subvols[dest_count])

        # Create source dir
        ret = mkdir(self.mounts[0].client_system, "{}/src_dir".format(m_point))
        self.assertTrue(ret, "mkdir of src_dir failed")
        g.log.info("Directory src_dir created successfully")

        # Copy src_dir to dest_dir (should fail as hash subvol for dest
        # dir is down)
        self._copy_when_dest_hash_down(m_point, dest_dir)