summaryrefslogtreecommitdiffstats
path: root/tests/distaf/distaf_libs/distaflibs-gluster/distaflibs/gluster/gluster_base_class.py
blob: dbbe02de7deb09b3db5bfec21285f2e3b13943ba (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
#  This file is part of DiSTAF
#  Copyright (C) 2015-2016  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 distaf.util import tc
from distaflibs.gluster.volume_ops import (setup_vol, get_volume_info,
                                           cleanup_volume)
from distaflibs.gluster.mount_ops import GlusterMount


class GlusterBaseClass():
    """
        This is the base class for the distaf tests

        All tests can subclass this and then write test cases
    """

    def __init__(self, config_data):
        """
            Initialise the class with the config values
        """
        if config_data['gluster']['global_mode']:
            self.volname = config_data['gluster']['volumes'][0]['name']
            self.voltype = (config_data['gluster']['volumes'][0]['voltype']
                            ['type'])
            self.servers = []
            for server in config_data['gluster']['volumes'][0]['servers']:
                self.servers.append(server['host'])
            self.peers = config_data['gluster']['volumes'][0]['peers']
            self.mounts = []
            for mount in config_data['gluster']['mounts']:
                mount['client'] = mount['client']['host']
                mount['server'] = mount['server']['host']
                self.mounts.append(GlusterMount(mount))
            self.clients = []
            for mount_obj in self.mounts:
                self.clients.append(mount_obj.client_system)
            self.clients = filter(None, self.clients)

        else:
            self.voltype = config_data['voltype']
            self.volname = "%s-testvol" % self.voltype
            self.servers = []
            for server in config_data['servers']:
                self.servers.append(server['host'])
            self.clients = []
            for client in config_data['clients']:
                self.clients.append(client['host'])
            self.peers = []
            if config_data['peers'] is not None:
                for peer in config_data['peers']:
                    self.peers.append(peers['host'])
            self.mounts = []
            self.mount_proto = config_data['mount_proto']
            self.mountpoint = "/mnt/%s_mount" % self.mount_proto
            for client in self.clients:
                mount = {}
                mount['protocol'] = config_data['mount_proto']
                mount['mountpoint'] = "/mnt/%s_mount" % self.mount_proto
                mount['server'] = self.servers[0]
                mount['client'] = client
                mount['volname'] = self.volname
                mount['options'] = ""
                self.mounts.append(GlusterMount(mount))
        self.mnode = self.servers[0]
        self.config_data = config_data

    def _create_volume(self):
        """
         Create the volume with proper configurations
        """
        dist = rep = dispd = red = stripe = 1
        trans = ''
        if self.voltype == 'distribute':
            dist = (self.config_data['gluster']['volume_types'][self.voltype]
                    ['dist_count'])
            trans = (self.config_data['gluster']['volume_types'][self.voltype]
                     ['transport'])
        elif self.voltype == 'replicate':
            rep = (self.config_data['gluster']['volume_types'][self.voltype]
                   ['replica_count'])
            trans = (self.config_data['gluster']['volume_types'][self.voltype]
                     ['transport'])
        elif self.voltype == 'dist_rep':
            dist = (self.config_data['gluster']['volume_types'][self.voltype]
                    ['dist_count'])
            rep = (self.config_data['gluster']['volume_types'][self.voltype]
                   ['replica_count'])
            trans = (self.config_data['gluster']['volume_types'][self.voltype]
                     ['transport'])
        elif self.voltype == 'disperse':
            dispd = (self.config_data['gluster']['volume_types'][self.voltype]
                     ['disperse_count'])
            red = (self.config_data['gluster']['volume_types'][self.voltype]
                   ['redundancy_count'])
            trans = (self.config_data['gluster']['volume_types'][self.voltype]
                     ['transport'])
        elif self.voltype == 'dist_disperse':
            dist = self.config_data[self.voltype]['dist_count']
            dispd = self.config_data[self.voltype]['disperse_count']
            red = self.config_data[self.voltype]['redundancy_count']
            trans = self.config_data[self.voltype]['transport']
        else:
            tc.logger.error("The volume type is not present")
            return False
        ret = setup_vol(self.volname, dist, rep, dispd, red, stripe, trans,
                        servers=self.servers)
        if not ret:
            tc.logger.error("Unable to setup volume %s", self.volname)
            return False
        return True

    def setup(self):
        """
            Function to setup the volume for testing.
        """
        volinfo = get_volume_info(server=self.servers[0])
        if volinfo is not None and self.volname in volinfo.keys():
            tc.logger.debug("The volume %s is already present in %s",
                            self.volname, self.mnode)
            if not self.config_data['reuse_setup']:
                ret = cleanup_volume(self.volname, self.mnode)
                if not ret:
                    tc.logger.error("Unable to cleanup the setup")
                    return False
                return self._create_volume()
        else:
            return self._create_volume()
        return True

    def teardown(self):
        """
            The function to cleanup the test setup
        """
        return True

    def cleanup(self):
        """
            The function to cleanup the volume
        """
        return cleanup_volume(self.volname, self.mnode)