~jillrouleau/charms/trusty/percona-cluster/add-nrpe-checks

« back to all changes in this revision

Viewing changes to tests/basic_deployment.py

  • Committer: james.page at ubuntu
  • Date: 2015-04-20 10:53:43 UTC
  • mfrom: (51.2.22 percona-cluster)
  • Revision ID: james.page@ubuntu.com-20150420105343-3kljmjdzozpfg5tf
[freyes,r=james-page] Ensure VIP is tied to a good mysqld instance.

Re-license charm as GPL-2 for compatibility with bundled ocf script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import amulet
 
2
import os
 
3
import time
 
4
import telnetlib
 
5
import unittest
 
6
import yaml
 
7
from charmhelpers.contrib.openstack.amulet.deployment import (
 
8
    OpenStackAmuletDeployment
 
9
)
 
10
 
 
11
 
 
12
class BasicDeployment(OpenStackAmuletDeployment):
 
13
    def __init__(self, vip=None, units=1, series="trusty", openstack=None,
 
14
                 source=None, stable=False):
 
15
        super(BasicDeployment, self).__init__(series, openstack, source,
 
16
                                              stable)
 
17
        self.units = units
 
18
        self.master_unit = None
 
19
        self.vip = None
 
20
        if vip:
 
21
            self.vip = vip
 
22
        elif 'AMULET_OS_VIP' in os.environ:
 
23
            self.vip = os.environ.get('AMULET_OS_VIP')
 
24
        elif os.path.isfile('local.yaml'):
 
25
            with open('local.yaml', 'rb') as f:
 
26
                self.cfg = yaml.safe_load(f.read())
 
27
 
 
28
            self.vip = self.cfg.get('vip')
 
29
        else:
 
30
            amulet.raise_status(amulet.SKIP,
 
31
                                ("please set the vip in local.yaml or env var "
 
32
                                 "AMULET_OS_VIP to run this test suite"))
 
33
 
 
34
    def _add_services(self):
 
35
        """Add services
 
36
 
 
37
           Add the services that we're testing, where percona-cluster is local,
 
38
           and the rest of the service are from lp branches that are
 
39
           compatible with the local charm (e.g. stable or next).
 
40
           """
 
41
        this_service = {'name': 'percona-cluster',
 
42
                        'units': self.units}
 
43
        other_services = [{'name': 'hacluster'}]
 
44
        super(BasicDeployment, self)._add_services(this_service,
 
45
                                                   other_services)
 
46
 
 
47
    def _add_relations(self):
 
48
        """Add all of the relations for the services."""
 
49
        relations = {'percona-cluster:ha': 'hacluster:ha'}
 
50
        super(BasicDeployment, self)._add_relations(relations)
 
51
 
 
52
    def _configure_services(self):
 
53
        """Configure all of the services."""
 
54
        cfg_percona = {'sst-password': 'ubuntu',
 
55
                       'root-password': 't00r',
 
56
                       'dataset-size': '512M',
 
57
                       'vip': self.vip}
 
58
 
 
59
        cfg_ha = {'debug': True,
 
60
                  'corosync_mcastaddr': '226.94.1.4',
 
61
                  'corosync_key': ('xZP7GDWV0e8Qs0GxWThXirNNYlScgi3sRTdZk/IXKD'
 
62
                                   'qkNFcwdCWfRQnqrHU/6mb6sz6OIoZzX2MtfMQIDcXu'
 
63
                                   'PqQyvKuv7YbRyGHmQwAWDUA4ed759VWAO39kHkfWp9'
 
64
                                   'y5RRk/wcHakTcWYMwm70upDGJEP00YT3xem3NQy27A'
 
65
                                   'C1w=')}
 
66
 
 
67
        configs = {'percona-cluster': cfg_percona,
 
68
                   'hacluster': cfg_ha}
 
69
        super(BasicDeployment, self)._configure_services(configs)
 
70
 
 
71
    def run(self):
 
72
        # The number of seconds to wait for the environment to setup.
 
73
        seconds = 1200
 
74
 
 
75
        self._add_services()
 
76
        self._add_relations()
 
77
        self._configure_services()
 
78
        self._deploy()
 
79
 
 
80
        i = 0
 
81
        while i < 30 and not self.master_unit:
 
82
            self.master_unit = self.find_master()
 
83
            i += 1
 
84
            time.sleep(10)
 
85
 
 
86
        assert self.master_unit is not None, 'percona-cluster vip not found'
 
87
 
 
88
        output, code = self.master_unit.run('sudo crm_verify --live-check')
 
89
        assert code == 0, "'crm_verify --live-check' failed"
 
90
 
 
91
        resources = ['res_mysql_vip']
 
92
        resources += ['res_mysql_monitor:%d' % i for i in range(self.units)]
 
93
 
 
94
        assert sorted(self.get_pcmkr_resources()) == sorted(resources)
 
95
 
 
96
        for i in range(self.units):
 
97
            uid = 'percona-cluster/%d' % i
 
98
            unit = self.d.sentry.unit[uid]
 
99
            assert self.is_mysqld_running(unit), 'mysql not running: %s' % uid
 
100
 
 
101
    def find_master(self):
 
102
        for unit_id, unit in self.d.sentry.unit.items():
 
103
            if not unit_id.startswith('percona-cluster/'):
 
104
                continue
 
105
 
 
106
            # is the vip running here?
 
107
            output, code = unit.run('sudo ip a | grep "inet %s/"' % self.vip)
 
108
            print('---')
 
109
            print(unit_id)
 
110
            print(output)
 
111
            if code == 0:
 
112
                print('vip(%s) running in %s' % (self.vip, unit_id))
 
113
                return unit
 
114
 
 
115
    def get_pcmkr_resources(self, unit=None):
 
116
        if unit:
 
117
            u = unit
 
118
        else:
 
119
            u = self.master_unit
 
120
 
 
121
        output, code = u.run('sudo crm_resource -l')
 
122
 
 
123
        assert code == 0, 'could not get "crm resource list"'
 
124
 
 
125
        return output.split('\n')
 
126
 
 
127
    def is_mysqld_running(self, unit=None):
 
128
        if unit:
 
129
            u = unit
 
130
        else:
 
131
            u = self.master_unit
 
132
 
 
133
        output, code = u.run('pidof mysqld')
 
134
 
 
135
        if code != 0:
 
136
            return False
 
137
 
 
138
        return self.is_port_open(u, '3306')
 
139
 
 
140
    def is_port_open(self, unit=None, port='3306', address=None):
 
141
        if unit:
 
142
            addr = unit.info['public-address']
 
143
        elif address:
 
144
            addr = address
 
145
        else:
 
146
            raise Exception('Please provide a unit or address')
 
147
        try:
 
148
            telnetlib.Telnet(addr, port)
 
149
            return True
 
150
        except TimeoutError:  # noqa this exception only available in py3
 
151
            return False