summaryrefslogtreecommitdiffstats
path: root/test/unittests/test_ui_cluster.py
blob: a86936feb2135fbccd10f70c7985a561ce245f59 (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
import logging
import unittest
try:
    from unittest import mock
except ImportError:
    import mock

from crmsh import ui_cluster

logging.basicConfig(level=logging.INFO)

class TestCluster(unittest.TestCase):
    """
    Unitary tests for class utils.IP
    """
    @classmethod
    def setUpClass(cls):
        """
        Global setUp.
        """

    def setUp(self):
        """
        Test setUp.
        """
        self.ui_cluster_inst = ui_cluster.Cluster()

    def tearDown(self):
        """
        Test tearDown.
        """

    @classmethod
    def tearDownClass(cls):
        """
        Global tearDown.
        """

    @mock.patch('logging.Logger.info')
    @mock.patch('crmsh.service_manager.ServiceManager.service_is_active')
    @mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
    @mock.patch('crmsh.utils.is_qdevice_configured')
    def test_do_start_already_started(self, mock_qdevice_configured, mock_parse_nodes, mock_active, mock_info):
        mock_qdevice_configured.return_value = False
        context_inst = mock.Mock()
        mock_parse_nodes.return_value = ["node1", "node2"]
        mock_active.side_effect = [True, True]
        self.ui_cluster_inst.do_start(context_inst, "node1", "node2")
        mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2")
        mock_active.assert_has_calls([
            mock.call("pacemaker.service", remote_addr="node1"),
            mock.call("pacemaker.service", remote_addr="node2")
            ])
        mock_info.assert_has_calls([
            mock.call("The cluster stack already started on node1"),
            mock.call("The cluster stack already started on node2")
            ])

    @mock.patch('crmsh.qdevice.QDevice.check_qdevice_vote')
    @mock.patch('crmsh.bootstrap.start_pacemaker')
    @mock.patch('logging.Logger.info')
    @mock.patch('crmsh.utils.is_qdevice_configured')
    @mock.patch('crmsh.service_manager.ServiceManager.start_service')
    @mock.patch('crmsh.service_manager.ServiceManager.service_is_active')
    @mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
    def test_do_start(self, mock_parse_nodes, mock_active, mock_start, mock_qdevice_configured, mock_info, mock_start_pacemaker, mock_check_qdevice):
        context_inst = mock.Mock()
        mock_start_pacemaker.return_value = ["node1"]
        mock_parse_nodes.return_value = ["node1"]
        mock_active.side_effect = [False, False]
        mock_qdevice_configured.return_value = True

        self.ui_cluster_inst.do_start(context_inst, "node1")

        mock_active.assert_has_calls([
            mock.call("pacemaker.service", remote_addr="node1"),
            mock.call("corosync-qdevice.service", remote_addr="node1")
            ])
        mock_start.assert_called_once_with("corosync-qdevice", node_list=["node1"])
        mock_qdevice_configured.assert_called_once_with()
        mock_info.assert_called_once_with("The cluster stack started on node1")

    @mock.patch('crmsh.ui_cluster.Cluster._wait_for_dc')
    @mock.patch('crmsh.ui_cluster.Cluster._node_ready_to_stop_cluster_service')
    @mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
    def test_do_stop_return(self, mock_parse_nodes, mock_node_ready_to_stop_cluster_service, mock_dc):
        mock_parse_nodes.return_value = ["node1", "node2"]
        mock_node_ready_to_stop_cluster_service.side_effect = [False, False]

        context_inst = mock.Mock()
        self.ui_cluster_inst.do_stop(context_inst, "node1", "node2")

        mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2")
        mock_node_ready_to_stop_cluster_service.assert_has_calls([mock.call("node1"), mock.call("node2")])
        mock_dc.assert_not_called()

    @mock.patch('logging.Logger.debug')
    @mock.patch('logging.Logger.info')
    @mock.patch('crmsh.ui_cluster.ServiceManager')
    @mock.patch('crmsh.ui_cluster.Cluster._set_dlm')
    @mock.patch('crmsh.ui_cluster.Cluster._wait_for_dc')
    @mock.patch('crmsh.ui_cluster.Cluster._node_ready_to_stop_cluster_service')
    @mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
    def test_do_stop(self, mock_parse_nodes, mock_node_ready_to_stop_cluster_service, mock_dc,
                     mock_set_dlm, mock_service_manager, mock_info, mock_debug):
        mock_parse_nodes.return_value = ["node1", "node2"]
        mock_node_ready_to_stop_cluster_service.side_effect = [True, False]
        mock_service_manager_inst = mock.Mock()
        mock_service_manager.return_value = mock_service_manager_inst
        mock_service_manager_inst.stop_service.side_effect = [["node1"], ["node1"], ["node1"]]
        mock_service_manager_inst.service_is_active.return_value = True

        context_inst = mock.Mock()
        self.ui_cluster_inst.do_stop(context_inst, "node1", "node2")

        mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2")
        mock_node_ready_to_stop_cluster_service.assert_has_calls([mock.call("node1"), mock.call("node2")])
        mock_debug.assert_called_once_with("stop node list: ['node1']")
        mock_dc.assert_called_once_with("node1")
        mock_set_dlm.assert_called_once_with("node1")
        mock_service_manager_inst.stop_service.assert_has_calls([
            mock.call("pacemaker", node_list=["node1"]),
            mock.call("corosync-qdevice.service", node_list=["node1"]),
            mock.call("corosync", node_list=["node1"]),
            ])
        mock_info.assert_called_once_with("The cluster stack stopped on node1")

    @mock.patch('logging.Logger.info')
    @mock.patch('crmsh.ui_cluster.ServiceManager')
    def test_node_ready_to_stop_cluster_service_corosync(self, mock_service_manager, mock_info):
        mock_service_manager_inst = mock.Mock()
        mock_service_manager.return_value = mock_service_manager_inst
        mock_service_manager_inst.service_is_active.side_effect = [False, True, False]
        res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1")
        assert res is False
        mock_service_manager_inst.service_is_active.assert_has_calls([
            mock.call("corosync.service", remote_addr="node1"),
            mock.call("sbd.service", remote_addr="node1"),
            mock.call("pacemaker.service", remote_addr="node1"),
            ])
        mock_service_manager_inst.stop_service.assert_called_once_with("corosync", remote_addr="node1")
        mock_info.assert_called_once_with("The cluster stack stopped on node1")

    @mock.patch('logging.Logger.info')
    @mock.patch('crmsh.ui_cluster.ServiceManager')
    def test_node_ready_to_stop_cluster_service_pacemaker(self, mock_service_manager, mock_info):
        mock_service_manager_inst = mock.Mock()
        mock_service_manager.return_value = mock_service_manager_inst
        mock_service_manager_inst.service_is_active.side_effect = [True, True, False]
        res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1")
        assert res is False
        mock_service_manager_inst.service_is_active.assert_has_calls([
            mock.call("corosync.service", remote_addr="node1"),
            mock.call("sbd.service", remote_addr="node1"),
            mock.call("pacemaker.service", remote_addr="node1"),
            ])
        mock_service_manager_inst.stop_service.assert_called_once_with("corosync", remote_addr="node1")
        mock_info.assert_called_once_with("The cluster stack stopped on node1")

    @mock.patch('logging.Logger.info')
    @mock.patch('crmsh.ui_cluster.ServiceManager')
    def test_node_ready_to_stop_cluster_service(self, mock_service_manager, mock_info):
        mock_service_manager_inst = mock.Mock()
        mock_service_manager.return_value = mock_service_manager_inst
        mock_service_manager_inst.service_is_active.side_effect = [True, True, True]
        res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1")
        assert res is True
        mock_service_manager_inst.service_is_active.assert_has_calls([
            mock.call("corosync.service", remote_addr="node1"),
            mock.call("sbd.service", remote_addr="node1"),
            mock.call("pacemaker.service", remote_addr="node1"),
            ])
        mock_info.assert_not_called()