summaryrefslogtreecommitdiffstats
path: root/tests/functional/glusterfind/test_gfind_type_option.py
blob: 98e808f694aae1f90e38dde7163dd2a4ab35432b (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
#  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.glusterfile import (
    file_exists,
    remove_file,
    check_if_pattern_in_file)
from glustolibs.gluster.glusterfind_ops import (
    gfind_create,
    gfind_list,
    gfind_pre,
    gfind_query,
    gfind_delete)


@runs_on([["replicated", "distributed-replicated", "dispersed",
           "distributed", "distributed-dispersed", "arbiter",
           "distributed-arbiter"], ["glusterfs"]])
class TestGlusterfindTypeOption(GlusterBaseClass):
    """
    TestGlusterfindTypeOption contains tests which verifies the
    glusterfind functionality with --full --type options.
    """
    def setUp(self):
        """
        setup volume and mount volume
        Initiate necessary variables
        """
        # calling GlusterBaseClass setUp
        self.get_super_method(self, 'setUp')()

        # Setup Volume and Mount Volume
        g.log.info("Starting to Setup %s", self.volname)
        ret = self.setup_volume_and_mount_volume(mounts=self.mounts)
        if not ret:
            raise ExecutionError("Failed to Setup_Volume %s" % self.volname)
        g.log.info("Successful in Setup Volume %s", self.volname)
        self.session = "test-session-%s" % self.volname
        self.outfile = "/tmp/test-outfile-%s.txt" % self.volname

    def tearDown(self):
        """
        tearDown for every test
        Clean up and unmount the volume
        """
        # calling GlusterBaseClass tearDown
        self.get_super_method(self, 'tearDown')()

        # Delete the glusterfind sessions
        ret, _, _ = gfind_delete(self.mnode, self.volname, self.session)
        if ret:
            raise ExecutionError("Failed to delete session %s" % self.session)
        g.log.info("Successfully deleted session %s", self.session)

        # Remove the outfile created during 'glusterfind pre and query'
        ret = remove_file(self.mnode, self.outfile, force=True)
        if not ret:
            raise ExecutionError("Failed to remove the outfile")
        g.log.info("Successfully removed the outfile")

        # Cleanup the volume
        ret = self.unmount_volume_and_cleanup_volume(mounts=self.mounts)
        if not ret:
            raise ExecutionError("Failed to Cleanup Volume")
        g.log.info("Successful in Cleanup Volume")

    def _check_contents_of_outfile(self, gftype):
        """Check contents of outfile created by query and pre"""
        if gftype == 'f':
            content = self.list_of_files
        elif gftype == 'd':
            content = self.list_of_dirs
        else:
            content = self.list_of_files + self.list_of_dirs

        # Check if outfile is created or not
        ret = file_exists(self.mnode, self.outfile)
        self.assertTrue(ret, "Unexpected: File '%s' does not exist"
                        % self.outfile)

        for value in content:
            ret = check_if_pattern_in_file(self.mnode, value, self.outfile)
            self.assertEqual(ret, 0, "Entry for '%s' not listed in %s"
                             % (value, self.outfile))

    def test_gfind_full_type(self):
        """
        Verifying the glusterfind --full functionality with --type f,
        --type f and --type both

        * Create a volume
        * Create a session on the volume
        * Create various files on mount point
        * Create various directories on point
        * Perform glusterfind pre with --full --type f --regenerate-outfile
        * Check the contents of outfile
        * Perform glusterfind pre with --full --type d --regenerate-outfile
        * Check the contents of outfile
        * Perform glusterfind pre with --full --type both --regenerate-outfile
        * Check the contents of outfile
        * Perform glusterfind query with --full --type f
        * Check the contents of outfile
        * Perform glusterfind query with --full --type d
        * Check the contents of outfile
        * Perform glusterfind query with --full --type both
        * Check the contents of outfile
        """

        # Create some files and directories from the mount point
        cmd = ("cd {}; mkdir dir;mkdir .hiddendir;touch file;touch .hiddenfile"
               ";mknod blockfile b 1 5;mknod charfile b 1 5; mkfifo pipefile;"
               "touch fileforhardlink;touch fileforsoftlink;"
               "ln fileforhardlink hardlinkfile;ln -s fileforsoftlink "
               "softlinkfile".format(self.mounts[0].mountpoint))
        ret, _, _ = g.run(self.mounts[0].client_system, cmd)

        # Create list of files and dir to be used for checking
        self.list_of_files = ['file', '.hiddenfile', 'blockfile', 'charfile',
                              'pipefile', 'fileforhardlink', 'fileforsoftlink',
                              'hardlinkfile', 'softlinkfile']
        self.list_of_dirs = ['dir', '.hiddendir']

        self.assertEqual(ret, 0, "Failed to create files and dirs")
        g.log.info("Files and Dirs created successfully on mountpoint")

        # Create session for volume
        ret, _, _ = gfind_create(self.mnode, self.volname, self.session)
        self.assertEqual(ret, 0, ("Unexpected: Creation of a session for the"
                                  " volume %s failed" % self.volname))
        g.log.info("Successfully created a session for the volume %s",
                   self.volname)

        # Perform glusterfind list to check if session exists
        _, out, _ = gfind_list(self.mnode, volname=self.volname,
                               sessname=self.session)
        self.assertNotEqual(out, "No sessions found.",
                            "Failed to list the glusterfind session")
        g.log.info("Successfully listed the glusterfind session")

        # Perform glusterfind full pre for the session with --type option
        for gftype in ('f', 'd', 'both'):
            ret, _, _ = gfind_pre(
                self.mnode, self.volname, self.session, self.outfile,
                full=True, gftype=gftype, regenoutfile=True)
            self.assertEqual(ret, 0, "glusterfind pre command successful "
                             "with --type %s" % gftype)

            # Check the contents of the outfile
            self._check_contents_of_outfile(gftype)

        # Perform glusterfind full query with the --type option
        for gftype in ('f', 'd', 'both'):
            ret, _, _ = gfind_query(self.mnode, self.volname, self.outfile,
                                    full=True, gftype=gftype)
            self.assertEqual(ret, 0, "glusterfind query command successful "
                             "with --type %s" % gftype)

            # Check the contents of the outfile
            self._check_contents_of_outfile(gftype)