summaryrefslogtreecommitdiffstats
path: root/tests/functional/afr/test_file_permission_and_mode.py
blob: 038847aa70c633e56e3d6a38cfad2219237d60f2 (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
#  Copyright (C) 2015-2019  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.gluster_base_class import (GlusterBaseClass, runs_on)
from glustolibs.gluster.brick_libs import get_all_bricks
from glustolibs.gluster.glusterfile import get_file_stat
from glustolibs.gluster.exceptions import ExecutionError


@runs_on([['replicated'],
          ['glusterfs']])
class FileModeAndPermissionsTest(GlusterBaseClass):
    """
     Description:
    """

    @classmethod
    def create_user(cls, host, user):
        """
        Creates a user on a host
        """
        g.log.info("Creating user '%s' for %s...", user, host)
        command = "useradd %s" % user
        _, _, err = g.run(host, command)

        if 'already exists' in err:
            g.log.warn("User '%s' is already exists on %s", user, host)
        else:
            g.log.info("User '%s' is created successfully on %s", user, host)

    @classmethod
    def delete_user(cls, host, user):
        """
        Deletes a user on a host
        """
        g.log.info('Deleting user %s from %s...', user, host)
        command = "userdel -r %s" % user
        _, _, err = g.run(host, command)

        if 'does not exist' in err:
            g.log.warn('User %s is already deleted on %s', user, host)
        else:
            g.log.info('User %s successfully deleted on %s', user, host)

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

        # Create user qa
        for mount_object in self.mounts:
            self.create_user(mount_object.client_system, 'qa')

        for server in self.servers:
            self.create_user(server, 'qa')

        # Setup Volume and Mount Volume
        g.log.info("Starting to 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")
        g.log.info("Successful in Setup Volume and Mount Volume")

        self.bricks_list = get_all_bricks(self.mnode, self.volname)
        self.assertIsNotNone(self.bricks_list, "unable to get list of bricks")

    def tearDown(self):
        """
        Cleanup and umount volume
        """

        # Delete user
        for mount_object in self.mounts:
            self.delete_user(mount_object.client_system, 'qa')

        for server in self.servers:
            self.delete_user(server, 'qa')

        # Cleanup and umount volume
        g.log.info("Starting to Unmount Volume and Cleanup Volume")
        ret = self.unmount_volume_and_cleanup_volume(mounts=self.mounts)
        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 test_file_permissions(self):
        """
        Description:
        - create file file.txt on mountpoint
        - change uid, gid and permission from client
        - check uid, gid and permission on client and all servers
        """

        # create file
        cmd = ("dd if=/dev/urandom of=%s/file.txt bs=1M count=1"
               % self.mounts[0].mountpoint)
        ret, _, _ = g.run(self.clients[0], cmd)
        self.assertEqual(ret, 0, "File creation failed")

        # Adding servers and client in single dict to check permissions
        nodes_to_check = {}
        all_bricks = get_all_bricks(self.mnode, self.volname)
        for brick in all_bricks:
            node, brick_path = brick.split(':')
            nodes_to_check[node] = brick_path
        nodes_to_check[self.mounts[0].client_system] = \
            self.mounts[0].mountpoint

        # check file is created on all bricks and client
        for node in nodes_to_check:
            filepath = nodes_to_check[node] + "/file.txt"
            stat_dict = get_file_stat(node, filepath)
            self.assertIsNotNone(stat_dict, "stat on %s failed" % filepath)
            self.assertEqual(stat_dict['filetype'], 'regular file',
                             "Expected regular file but found %s"
                             % stat_dict['filetype'])

        # get file stat info from client
        fpath = self.mounts[0].mountpoint + "/file.txt"
        stat_dict = get_file_stat(self.clients[0], fpath)
        self.assertIsNotNone(stat_dict, "stat on %s failed" % fpath)
        self.assertEqual(stat_dict['uid'], '0', "Expected uid 0 but found %s"
                         % stat_dict['uid'])
        self.assertEqual(stat_dict['gid'], '0', "Expected gid 0 but found %s"
                         % stat_dict['gid'])
        self.assertEqual(stat_dict['access'], '644', "Expected permission 644"
                         " but found %s" % stat_dict['access'])

        # change uid, gid and permission from client
        cmd = ("chown qa %s" % fpath)
        ret, _, _ = g.run(self.clients[0], cmd)
        self.assertEqual(ret, 0, "chown failed")

        cmd = ("chgrp qa %s" % fpath)
        ret, _, _ = g.run(self.clients[0], cmd)
        self.assertEqual(ret, 0, "chgrp failed")

        cmd = ("chmod 777 %s" % fpath)
        ret, _, _ = g.run(self.clients[0], cmd)
        self.assertEqual(ret, 0, "chown failed")

        # Verify that the changes are successful on bricks and client
        for node in nodes_to_check:
            filepath = nodes_to_check[node] + "/file.txt"
            stat_dict = get_file_stat(node, filepath)
            self.assertIsNotNone(stat_dict, "stat on %s failed" % fpath)
            self.assertEqual(stat_dict['username'], 'qa',
                             "Expected qa but found %s"
                             % stat_dict['username'])
            self.assertEqual(stat_dict['groupname'], 'qa',
                             "Expected gid qa but found %s"
                             % stat_dict['groupname'])
            self.assertEqual(stat_dict['access'], '777',
                             "Expected permission 777  but found %s"
                             % stat_dict['access'])