Skip to content

Commit 1fcb08c

Browse files
committed
Dev: unittest: Adjust unit test for previous change
1 parent 4e71655 commit 1fcb08c

File tree

2 files changed

+82
-43
lines changed

2 files changed

+82
-43
lines changed

test/unittests/test_ui_cluster.py

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,52 +80,91 @@ def test_do_start(self, mock_parse_nodes, mock_active, mock_start, mock_qdevice_
8080
mock_qdevice_configured.assert_called_once_with()
8181
mock_info.assert_called_once_with("The cluster stack started on node1")
8282

83-
@mock.patch('logging.Logger.info')
84-
@mock.patch('crmsh.utils.service_is_active')
83+
@mock.patch('crmsh.ui_cluster.Cluster._wait_for_dc')
84+
@mock.patch('crmsh.ui_cluster.Cluster._node_ready_to_stop_cluster_service')
8585
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
86-
def test_do_stop_already_stopped(self, mock_parse_nodes, mock_active, mock_info):
86+
def test_do_stop_return(self, mock_parse_nodes, mock_node_ready_to_stop_cluster_service, mock_dc):
87+
mock_parse_nodes.return_value = ["node1", "node2"]
88+
mock_node_ready_to_stop_cluster_service.side_effect = [False, False]
89+
8790
context_inst = mock.Mock()
88-
mock_parse_nodes.return_value = ["node1"]
89-
mock_active.side_effect = [False, False]
90-
self.ui_cluster_inst.do_stop(context_inst, "node1")
91-
mock_active.assert_has_calls([
92-
mock.call("corosync.service", remote_addr="node1"),
93-
mock.call("sbd.service", remote_addr="node1")
94-
])
95-
mock_info.assert_called_once_with("The cluster stack already stopped on node1")
91+
self.ui_cluster_inst.do_stop(context_inst, "node1", "node2")
92+
93+
mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2")
94+
mock_node_ready_to_stop_cluster_service.assert_has_calls([mock.call("node1"), mock.call("node2")])
95+
mock_dc.assert_not_called()
9696

9797
@mock.patch('logging.Logger.debug')
9898
@mock.patch('logging.Logger.info')
99-
@mock.patch('crmsh.utils.stop_service')
100-
@mock.patch('crmsh.utils.set_dlm_option')
101-
@mock.patch('crmsh.utils.is_quorate')
102-
@mock.patch('crmsh.utils.is_dlm_running')
103-
@mock.patch('crmsh.utils.get_dc')
104-
@mock.patch('crmsh.utils.check_function_with_timeout')
105-
@mock.patch('crmsh.utils.get_property')
10699
@mock.patch('crmsh.utils.service_is_active')
100+
@mock.patch('crmsh.utils.stop_service')
101+
@mock.patch('crmsh.ui_cluster.Cluster._set_dlm')
102+
@mock.patch('crmsh.ui_cluster.Cluster._wait_for_dc')
103+
@mock.patch('crmsh.ui_cluster.Cluster._node_ready_to_stop_cluster_service')
107104
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
108-
def test_do_stop(self, mock_parse_nodes, mock_active, mock_get_property, mock_check, mock_get_dc, mock_dlm_running, mock_is_quorate, mock_set_dlm, mock_stop, mock_info, mock_debug):
105+
def test_do_stop(self, mock_parse_nodes, mock_node_ready_to_stop_cluster_service, mock_dc,
106+
mock_set_dlm, mock_stop, mock_is_active, mock_info, mock_debug):
107+
mock_parse_nodes.return_value = ["node1", "node2"]
108+
mock_node_ready_to_stop_cluster_service.side_effect = [True, False]
109+
mock_stop.side_effect = [["node1"], ["node1"], ["node1"]]
110+
mock_is_active.return_value = True
111+
109112
context_inst = mock.Mock()
110-
mock_stop.side_effect = [["node1"], ["ndoe1"], ["node1"]]
111-
mock_parse_nodes.return_value = ["node1"]
112-
mock_active.side_effect = [True, True, True]
113-
mock_dlm_running.return_value = True
114-
mock_is_quorate.return_value = False
115-
mock_get_property.return_value = "20s"
113+
self.ui_cluster_inst.do_stop(context_inst, "node1", "node2")
116114

117-
self.ui_cluster_inst.do_stop(context_inst, "node1")
115+
mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2")
116+
mock_node_ready_to_stop_cluster_service.assert_has_calls([mock.call("node1"), mock.call("node2")])
117+
mock_debug.assert_called_once_with("stop node list: ['node1']")
118+
mock_dc.assert_called_once_with("node1")
119+
mock_set_dlm.assert_called_once_with("node1")
120+
mock_stop.assert_has_calls([
121+
mock.call("pacemaker", node_list=["node1"]),
122+
mock.call("corosync-qdevice.service", node_list=["node1"]),
123+
mock.call("corosync", node_list=["node1"]),
124+
])
125+
mock_info.assert_called_once_with("The cluster stack stopped on node1")
118126

119-
mock_active.assert_has_calls([
127+
@mock.patch('logging.Logger.info')
128+
@mock.patch('crmsh.utils.stop_service')
129+
@mock.patch('crmsh.utils.service_is_active')
130+
def test__node_ready_to_stop_cluster_service_corosync(self, mock_is_active, mock_stop, mock_info):
131+
mock_is_active.side_effect = [False, True, False]
132+
res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1")
133+
assert res is False
134+
mock_is_active.assert_has_calls([
120135
mock.call("corosync.service", remote_addr="node1"),
136+
mock.call("sbd.service", remote_addr="node1"),
121137
mock.call("pacemaker.service", remote_addr="node1"),
122-
mock.call("corosync-qdevice.service")
123138
])
124-
mock_stop.assert_has_calls([
125-
mock.call("pacemaker", node_list=["node1"]),
126-
mock.call("corosync-qdevice.service", node_list=["node1"]),
127-
mock.call("corosync", node_list=["node1"])
139+
mock_stop.assert_called_once_with("corosync", remote_addr="node1")
140+
mock_info.assert_called_once_with("The cluster stack stopped on node1")
141+
142+
@mock.patch('logging.Logger.info')
143+
@mock.patch('crmsh.utils.stop_service')
144+
@mock.patch('crmsh.utils.service_is_active')
145+
def test__node_ready_to_stop_cluster_service_pacemaker(self, mock_is_active, mock_stop, mock_info):
146+
mock_is_active.side_effect = [True, True, False]
147+
res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1")
148+
assert res is False
149+
mock_is_active.assert_has_calls([
150+
mock.call("corosync.service", remote_addr="node1"),
151+
mock.call("sbd.service", remote_addr="node1"),
152+
mock.call("pacemaker.service", remote_addr="node1"),
128153
])
154+
mock_stop.assert_called_once_with("corosync", remote_addr="node1")
129155
mock_info.assert_called_once_with("The cluster stack stopped on node1")
130-
mock_debug.assert_called_once_with("Quorum is lost; Set enable_quorum_fencing=0 and enable_quorum_lockspace=0 for dlm")
131-
mock_check.assert_called_once_with(mock_get_dc, wait_timeout=25)
156+
157+
@mock.patch('logging.Logger.info')
158+
@mock.patch('crmsh.utils.stop_service')
159+
@mock.patch('crmsh.utils.service_is_active')
160+
def test__node_ready_to_stop_cluster_service(self, mock_is_active, mock_stop, mock_info):
161+
mock_is_active.side_effect = [True, True, True]
162+
res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1")
163+
assert res is True
164+
mock_is_active.assert_has_calls([
165+
mock.call("corosync.service", remote_addr="node1"),
166+
mock.call("sbd.service", remote_addr="node1"),
167+
mock.call("pacemaker.service", remote_addr="node1"),
168+
])
169+
mock_info.assert_not_called()
170+
mock_stop.assert_not_called()

test/unittests/test_utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ def test_get_dlm_option_dict(mock_run):
15701570
"key1": "value1",
15711571
"key2": "value2"
15721572
}
1573-
mock_run.assert_called_once_with("dlm_tool dump_config")
1573+
mock_run.assert_called_once_with("dlm_tool dump_config", remote=None)
15741574

15751575

15761576
@mock.patch('crmsh.utils.get_dlm_option_dict')
@@ -1592,14 +1592,14 @@ def test_set_dlm_option(mock_get_dict, mock_run):
15921592
"key2": "value2"
15931593
}
15941594
utils.set_dlm_option(key2="test")
1595-
mock_run.assert_called_once_with('dlm_tool set_config "key2=test"')
1595+
mock_run.assert_called_once_with('dlm_tool set_config "key2=test"', remote=None)
15961596

15971597

1598-
@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_resource_configured')
1598+
@mock.patch('crmsh.utils.has_resource_configured')
15991599
def test_is_dlm_configured(mock_configured):
16001600
mock_configured.return_value = True
16011601
assert utils.is_dlm_configured() is True
1602-
mock_configured.assert_called_once_with(constants.DLM_CONTROLD_RA)
1602+
mock_configured.assert_called_once_with(constants.DLM_CONTROLD_RA, peer=None)
16031603

16041604

16051605
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
@@ -1608,7 +1608,7 @@ def test_is_quorate_exception(mock_run):
16081608
with pytest.raises(ValueError) as err:
16091609
utils.is_quorate()
16101610
assert str(err.value) == "Failed to get quorate status from corosync-quorumtool"
1611-
mock_run.assert_called_once_with("corosync-quorumtool -s", success_val_list=[0, 2])
1611+
mock_run.assert_called_once_with("corosync-quorumtool -s", remote=None, success_val_list=[0, 2])
16121612

16131613

16141614
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
@@ -1618,7 +1618,7 @@ def test_is_quorate(mock_run):
16181618
Quorate: Yes
16191619
"""
16201620
assert utils.is_quorate() is True
1621-
mock_run.assert_called_once_with("corosync-quorumtool -s", success_val_list=[0, 2])
1621+
mock_run.assert_called_once_with("corosync-quorumtool -s", remote=None, success_val_list=[0, 2])
16221622

16231623

16241624
@mock.patch('crmsh.utils.etree.fromstring')
@@ -1687,12 +1687,12 @@ def test_list_cluster_nodes(mock_run, mock_env, mock_isfile, mock_file2elem):
16871687

16881688

16891689
@mock.patch('os.getenv')
1690-
@mock.patch('crmsh.utils.get_stdout_stderr')
1690+
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
16911691
def test_get_property(mock_run, mock_env):
1692-
mock_run.return_value = (0, "data", None)
1692+
mock_run.return_value = "data"
16931693
mock_env.return_value = "cib.xml"
16941694
assert utils.get_property("no-quorum-policy") == "data"
1695-
mock_run.assert_called_once_with("CIB_file=cib.xml sudo --preserve-env=CIB_file crm configure get_property no-quorum-policy")
1695+
mock_run.assert_called_once_with("CIB_file=cib.xml sudo --preserve-env=CIB_file crm configure get_property no-quorum-policy", remote=None, no_raise=True)
16961696

16971697

16981698
@mock.patch('logging.Logger.warning')

0 commit comments

Comments
 (0)