testMaster.py 5.28 KB
Newer Older
1
#
Grégory Wisniewski's avatar
Grégory Wisniewski committed
2
# Copyright (C) 2009-2010  Nexedi SA
3
#
4 5 6 7
# 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 (at your option) any later version.
8
#
9 10 11 12
# 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.
13
#
14 15
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17

18
import unittest
19
from neo.tests.functional import NEOCluster, NEOFunctionalTest
20
from neo.protocol import NodeStates
21

22 23
MASTER_NODE_COUNT = 3

Vincent Pelletier's avatar
Vincent Pelletier committed
24

25
class MasterTests(NEOFunctionalTest):
26 27

    def setUp(self):
28
        NEOFunctionalTest.setUp(self)
29
        self.neo = NEOCluster([], master_node_count=MASTER_NODE_COUNT,
30 31 32 33 34
                temp_dir=self.getTempDirectory())
        self.neo.stop()
        self.neo.start()
        self.storage = self.neo.getZODBStorage()
        self.neoctl = self.neo.getNEOCTL()
35 36

    def tearDown(self):
37
        self.neo.stop()
38 39

    def testStoppingSecondaryMaster(self):
40
        # Wait for masters to stabilize
41
        self.neo.expectAllMasters(MASTER_NODE_COUNT)
42 43

        # Kill
44
        killed_uuid_list = self.neo.killSecondaryMaster()
45 46 47 48
        # Test sanity check.
        self.assertEqual(len(killed_uuid_list), 1)
        uuid = killed_uuid_list[0]
        # Check node state has changed.
49
        self.neo.expectMasterState(uuid, None)
50

51
    def testStoppingPrimaryWithTwoSecondaries(self):
52
        # Wait for masters to stabilize
53
        self.neo.expectAllMasters(MASTER_NODE_COUNT)
54 55

        # Kill
56
        killed_uuid_list = self.neo.killPrimary()
57 58 59
        # Test sanity check.
        self.assertEqual(len(killed_uuid_list), 1)
        uuid = killed_uuid_list[0]
Vincent Pelletier's avatar
Vincent Pelletier committed
60
        # Check the state of the primary we just killed
61
        self.neo.expectMasterState(uuid, (None, NodeStates.UNKNOWN))
62
        self.assertEqual(self.neo.getPrimary(), None)
63
        # Check that a primary master arised.
64
        self.neo.expectPrimary(timeout=10)
65
        # Check that the uuid really changed.
66
        new_uuid = self.neo.getPrimary()
67 68
        self.assertNotEqual(new_uuid, uuid)

69
    def testStoppingPrimaryWithOneSecondary(self):
70
        self.neo.expectAllMasters(MASTER_NODE_COUNT,
71
                state=NodeStates.RUNNING)
72

Vincent Pelletier's avatar
Vincent Pelletier committed
73
        # Kill one secondary master.
74
        killed_uuid_list = self.neo.killSecondaryMaster()
Vincent Pelletier's avatar
Vincent Pelletier committed
75 76
        # Test sanity checks.
        self.assertEqual(len(killed_uuid_list), 1)
77
        self.neo.expectMasterState(killed_uuid_list[0], None)
78
        self.assertEqual(len(self.neo.getMasterList()), 2)
Vincent Pelletier's avatar
Vincent Pelletier committed
79

80
        killed_uuid_list = self.neo.killPrimary()
Vincent Pelletier's avatar
Vincent Pelletier committed
81 82 83 84
        # Test sanity check.
        self.assertEqual(len(killed_uuid_list), 1)
        uuid = killed_uuid_list[0]
        # Check the state of the primary we just killed
85
        self.neo.expectMasterState(uuid, (None, NodeStates.UNKNOWN))
86
        self.assertEqual(self.neo.getPrimary(), None)
Vincent Pelletier's avatar
Vincent Pelletier committed
87
        # Check that a primary master arised.
88
        self.neo.expectPrimary(timeout=10)
Vincent Pelletier's avatar
Vincent Pelletier committed
89
        # Check that the uuid really changed.
90
        new_uuid = self.neo.getPrimary()
Vincent Pelletier's avatar
Vincent Pelletier committed
91 92 93
        self.assertNotEqual(new_uuid, uuid)

    def testMasterSequentialStart(self):
94
        self.neo.expectAllMasters(MASTER_NODE_COUNT,
95
                state=NodeStates.RUNNING)
96
        master_list = self.neo.getMasterProcessList()
97 98

        # Stop the cluster (so we can start processes manually)
99
        self.neo.killMasters()
Vincent Pelletier's avatar
Vincent Pelletier committed
100 101 102 103

        # Start the first master.
        first_master = master_list[0]
        first_master.start()
104
        first_master_uuid = first_master.getUUID()
Vincent Pelletier's avatar
Vincent Pelletier committed
105
        # Check that the master node we started elected itself.
106
        self.neo.expectPrimary(first_master_uuid, timeout=30)
107
        # Check that no other node is known as running.
108
        self.assertEqual(len(self.neo.getMasterList(
109
            state=NodeStates.RUNNING)), 1)
Vincent Pelletier's avatar
Vincent Pelletier committed
110 111 112

        # Start a second master.
        second_master = master_list[1]
113
        # Check that the second master is known as being down.
114
        self.assertEqual(self.neo.getMasterNodeState(second_master.getUUID()),
115
                None)
Vincent Pelletier's avatar
Vincent Pelletier committed
116 117
        second_master.start()
        # Check that the second master is running under his known UUID.
118
        self.neo.expectMasterState(second_master.getUUID(),
119
                NodeStates.RUNNING)
Vincent Pelletier's avatar
Vincent Pelletier committed
120
        # Check that the primary master didn't change.
121
        self.assertEqual(self.neo.getPrimary(), first_master_uuid)
Vincent Pelletier's avatar
Vincent Pelletier committed
122 123 124

        # Start a third master.
        third_master = master_list[2]
125
        # Check that the third master is known as being down.
126
        self.assertEqual(self.neo.getMasterNodeState(third_master.getUUID()),
127
                None)
Vincent Pelletier's avatar
Vincent Pelletier committed
128 129
        third_master.start()
        # Check that the third master is running under his known UUID.
130
        self.neo.expectMasterState(third_master.getUUID(),
131
                NodeStates.RUNNING)
Vincent Pelletier's avatar
Vincent Pelletier committed
132
        # Check that the primary master didn't change.
133
        self.assertEqual(self.neo.getPrimary(), first_master_uuid)
Vincent Pelletier's avatar
Vincent Pelletier committed
134

135 136 137 138 139 140
def test_suite():
    return unittest.makeSuite(MasterTests)

if __name__ == "__main__":
    unittest.main(defaultTest="test_suite")