summaryrefslogtreecommitdiffstats
path: root/tests/functional/dht/test_sparse_file_creation_and_deletion.py
blob: 7404ece902136de2e0ec76d88503b3d6e7a030aa (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
#  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 time import sleep
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.lib_utils import get_size_of_mountpoint


@runs_on([['distributed-replicated', 'distributed-arbiter',
           'distributed-dispersed', 'distributed'], ['glusterfs']])
class TestSparseFileCreationAndDeletion(GlusterBaseClass):

    def setUp(self):

        self.get_super_method(self, 'setUp')()

        # Changing dist_count to 5
        self.volume['voltype']['dist_count'] = 5

        # Creating Volume and mounting the volume
        ret = self.setup_volume_and_mount_volume([self.mounts[0]])
        if not ret:
            raise ExecutionError("Volume creation or mount failed: %s"
                                 % self.volname)

        # Assign a variable for the first_client
        self.first_client = self.mounts[0].client_system

    def tearDown(self):

        # Unmounting and cleaning volume
        ret = self.unmount_volume_and_cleanup_volume([self.mounts[0]])
        if not ret:
            raise ExecutionError("Unable to delete volume %s" % self.volname)

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

    def _create_two_sparse_files(self):
        """Create 2 sparse files from /dev/zero and /dev/null"""

        # Create a tuple to hold both the file names
        self.sparse_file_tuple = (
            "{}/sparse_file_zero".format(self.mounts[0].mountpoint),
            "{}/sparse_file_null".format(self.mounts[0].mountpoint)
            )

        # Create 2 spares file where one is created from /dev/zero and
        # another is created from /dev/null
        for filename, input_file in ((self.sparse_file_tuple[0], "/dev/zero"),
                                     (self.sparse_file_tuple[1], "/dev/null")):
            cmd = ("dd if={} of={} bs=1M seek=5120 count=1000"
                   .format(input_file, filename))
            ret, _, _ = g.run(self.first_client, cmd)
            self.assertEqual(ret, 0, 'Failed to create %s ' % filename)

        g.log.info("Successfully created sparse_file_zero and"
                   " sparse_file_null")

    def _check_du_and_ls_of_sparse_file(self):
        """Check du and ls -lks on spare files"""

        for filename in self.sparse_file_tuple:

            # Fetch output of ls -lks for the sparse file
            cmd = "ls -lks {}".format(filename)
            ret, out, _ = g.run(self.first_client, cmd)
            self.assertEqual(ret, 0, "Failed to get ls -lks for file %s "
                             % filename)
            ls_value = out.split(" ")[5]

            # Fetch output of du for the sparse file
            cmd = "du --block-size=1 {}".format(filename)
            ret, out, _ = g.run(self.first_client, cmd)
            self.assertEqual(ret, 0, "Failed to get du for file %s "
                             % filename)
            du_value = out.split("\t")[0]

            # Compare du and ls -lks value
            self. assertNotEqual(ls_value, du_value,
                                 "Unexpected: Sparse file size coming up same "
                                 "for du and ls -lks")

        g.log.info("Successfully checked sparse file size using ls and du")

    def _delete_two_sparse_files(self):
        """Delete sparse files"""

        for filename in self.sparse_file_tuple:
            cmd = "rm -rf {}".format(filename)
            ret, _, _ = g.run(self.first_client, cmd)
            self.assertEqual(ret, 0, 'Failed to delete %s ' % filename)

        g.log.info("Successfully remove both sparse files")

    def test_sparse_file_creation_and_deletion(self):
        """
        Test case:
        1. Create volume with 5 sub-volumes, start and mount it.
        2. Check df -h for available size.
        3. Create 2 sparse file one from /dev/null and one from /dev/zero.
        4. Find out size of files and compare them through du and ls.
           (They shouldn't match.)
        5. Check df -h for available size.(It should be less than step 2.)
        6. Remove the files using rm -rf.
        """
        # Check df -h for avaliable size
        available_space_at_start = get_size_of_mountpoint(
            self.first_client, self.mounts[0].mountpoint)
        self.assertIsNotNone(available_space_at_start,
                             "Failed to get available space on mount point")

        # Create 2 sparse file one from /dev/null and one from /dev/zero
        self._create_two_sparse_files()

        # Find out size of files and compare them through du and ls
        # (They shouldn't match)
        self._check_du_and_ls_of_sparse_file()

        # Check df -h for avaliable size(It should be less than step 2)
        available_space_now = get_size_of_mountpoint(
            self.first_client, self.mounts[0].mountpoint)
        self.assertIsNotNone(available_space_now,
                             "Failed to get avaliable space on mount point")
        ret = (int(available_space_at_start) > int(available_space_now))
        self.assertTrue(ret, "Available space at start not less than "
                        "available space now")

        # Remove the files using rm -rf
        self._delete_two_sparse_files()

        # Sleep for 180 seconds for the meta data in .glusterfs directory
        # to be removed
        sleep(180)

        # Check df -h after removing sparse files
        available_space_now = get_size_of_mountpoint(
            self.first_client, self.mounts[0].mountpoint)
        self.assertIsNotNone(available_space_now,
                             "Failed to get avaliable space on mount point")
        ret = int(available_space_at_start) - int(available_space_now) < 1500
        self.assertTrue(ret, "Available space at start and available space now"
                        " is not equal")