Skip to content

Commit

Permalink
Dev: unittest: Adjust unit test for previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxin1300 committed Sep 27, 2024
1 parent 0fc92a5 commit 3988820
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
17 changes: 2 additions & 15 deletions test/unittests/test_qdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,10 @@ def test_qdevice_p12_on_cluster(self):
res = self.qdevice_with_ip_cluster_node.qdevice_p12_on_cluster
self.assertEqual(res, "/etc/corosync/qdevice/net/node1.com/qdevice-net-node.p12")

@mock.patch('crmsh.utils.check_port_open')
@mock.patch('crmsh.utils.InterfacesInfo.ip_in_local')
@mock.patch('crmsh.utils.ping_node')
@mock.patch('crmsh.utils.node_reachable_check')
@mock.patch('socket.getaddrinfo')
def test_check_qnetd_addr_port_error(self, mock_getaddrinfo, mock_ping, mock_in_local, mock_check):
mock_getaddrinfo.return_value = [(None, ("10.10.10.123",)),]
mock_in_local.return_value = False
mock_check.return_value = False
with self.assertRaises(ValueError) as err:
qdevice.QDevice.check_qnetd_addr("qnetd-node")
excepted_err_string = "ssh service on \"qnetd-node\" not available"
self.assertEqual(excepted_err_string, str(err.exception))

@mock.patch('crmsh.utils.InterfacesInfo.ip_in_local')
@mock.patch('crmsh.utils.ping_node')
@mock.patch('socket.getaddrinfo')
def test_check_qnetd_addr_local(self, mock_getaddrinfo, mock_ping, mock_in_local):
def test_check_qnetd_addr_local(self, mock_getaddrinfo, mock_reachable, mock_in_local):
mock_getaddrinfo.return_value = [(None, ("10.10.10.123",)),]
mock_in_local.return_value = True
with self.assertRaises(ValueError) as err:
Expand Down
6 changes: 3 additions & 3 deletions test/unittests/test_report_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ def test_process_node_list_single(self, mock_list_nodes, mock_local_mode):

@mock.patch('crmsh.report.core.local_mode')
@mock.patch('logging.Logger.error')
@mock.patch('crmsh.utils.ping_node')
@mock.patch('crmsh.utils.node_reachable_check')
@mock.patch('crmsh.utils.list_cluster_nodes')
def test_process_node_list(self, mock_list_nodes, mock_ping, mock_error, mock_local_mode):
def test_process_node_list(self, mock_list_nodes, mock_reachable, mock_error, mock_local_mode):
mock_local_mode.return_value = False
mock_ctx_inst = mock.Mock(node_list=["node1", "node2"], single=False, me="node1")
mock_ping.side_effect = ValueError("error")
mock_reachable.side_effect = ValueError("error")
core.process_node_list(mock_ctx_inst)
self.assertEqual(mock_ctx_inst.node_list, ["node1"])

Expand Down
41 changes: 15 additions & 26 deletions test/unittests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,37 @@ def test_sysconfig_set_bsc1145823():
sc = utils.parse_sysconfig(fname)
assert (sc.get("age") == "100")

@mock.patch("crmsh.utils.IP.is_ipv6")
@mock.patch("socket.getaddrinfo")
@mock.patch("socket.socket")
@mock.patch("crmsh.utils.closing")
def test_check_port_open_false(mock_closing, mock_socket, mock_is_ipv6):
mock_is_ipv6.return_value = False
def test_check_port_open_false(mock_closing, mock_socket, mock_getaddrinfo):
sock_inst = mock.Mock()
mock_socket.return_value = sock_inst
mock_closing.return_value.__enter__.return_value = sock_inst
sock_inst.connect_ex.return_value = 1
mock_getaddrinfo.return_value = [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 22))]

assert utils.check_port_open("10.10.10.1", 22) is False
assert utils.check_port_open("localhost", 22) is False

mock_is_ipv6.assert_called_once_with("10.10.10.1")
mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM, 6)
mock_closing.assert_called_once_with(sock_inst)
sock_inst.connect_ex.assert_called_once_with(("10.10.10.1", 22))
sock_inst.connect_ex.assert_called_once_with(("127.0.0.1", 22))

@mock.patch("crmsh.utils.IP.is_ipv6")
@mock.patch("socket.getaddrinfo")
@mock.patch("socket.socket")
@mock.patch("crmsh.utils.closing")
def test_check_port_open_true(mock_closing, mock_socket, mock_is_ipv6):
mock_is_ipv6.return_value = True
def test_check_port_open_true(mock_closing, mock_socket, mock_getaddrinfo):
sock_inst = mock.Mock()
mock_socket.return_value = sock_inst
mock_closing.return_value.__enter__.return_value = sock_inst
sock_inst.connect_ex.return_value = 0
mock_getaddrinfo.return_value = [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 22))]

assert utils.check_port_open("2001:db8:10::7", 22) is True
assert utils.check_port_open("localhost", 22) is True

mock_is_ipv6.assert_called_once_with("2001:db8:10::7")
mock_socket.assert_called_once_with(socket.AF_INET6, socket.SOCK_STREAM)
mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM, 6)
mock_closing.assert_called_once_with(sock_inst)
sock_inst.connect_ex.assert_called_once_with(("2001:db8:10::7", 22))
sock_inst.connect_ex.assert_called_once_with(("127.0.0.1", 22))

def test_valid_port():
assert utils.valid_port(1) is False
Expand Down Expand Up @@ -741,15 +739,6 @@ def test_get_iplist_from_name(mock_get_nodeid, mock_get_nodeinfo):
mock_get_nodeinfo.assert_called_once_with()


@mock.patch("crmsh.sh.ShellUtils.get_stdout_stderr")
def test_ping_node(mock_run):
mock_run.return_value = (1, None, "error data")
with pytest.raises(ValueError) as err:
utils.ping_node("node_unreachable")
assert str(err.value) == 'host "node_unreachable" is unreachable: error data'
mock_run.assert_called_once_with("ping -c 1 node_unreachable")


def test_calculate_quorate_status():
assert utils.calculate_quorate_status(3, 2) is True
assert utils.calculate_quorate_status(3, 1) is False
Expand Down Expand Up @@ -983,13 +972,13 @@ def test_is_block_device(mock_stat, mock_isblk):
mock_isblk.assert_called_once_with(12345)


@mock.patch('crmsh.utils.ping_node')
@mock.patch('crmsh.utils.node_reachable_check')
@mock.patch('crmsh.sh.ClusterShell.get_stdout_or_raise_error')
def test_check_all_nodes_reachable(mock_run, mock_ping):
def test_check_all_nodes_reachable(mock_run, mock_reachable):
mock_run.return_value = "1084783297 15sp2-1 member"
utils.check_all_nodes_reachable()
mock_run.assert_called_once_with("crm_node -l")
mock_ping.assert_called_once_with("15sp2-1")
mock_reachable.assert_called_once_with("15sp2-1")


@mock.patch('crmsh.sh.ShellUtils.get_stdout_stderr')
Expand Down

0 comments on commit 3988820

Please sign in to comment.