async def test_get_nodes(): """Verify that get_nodes function returns the cluster nodes list.""" local_client = LocalClient() with patch('wazuh.core.cluster.local_client.LocalClient.execute', side_effect=async_local_client): expected_result = { 'items': [{ 'name': 'master' }, { 'name': 'worker1' }], 'totalItems': 2 } with patch('json.loads', return_value=expected_result): result = await control.get_nodes(lc=local_client) assert result == expected_result result_q = await control.get_nodes(lc=local_client, q='name=master') assert result_q == {'items': [{'name': 'master'}], 'totalItems': 1} with patch('json.loads', return_value=KeyError(1)): with pytest.raises(KeyError): await control.get_nodes(lc=local_client) with patch('wazuh.core.cluster.local_client.LocalClient.execute', side_effect=['timeout', 'error']): with pytest.raises(WazuhClusterError): await control.get_nodes(lc=local_client) with pytest.raises(json.JSONDecodeError): await control.get_nodes(lc=local_client)
async def test_get_node(): """Verify that get_node function returns the current node name.""" local_client = LocalClient() with patch('wazuh.core.cluster.local_client.LocalClient.execute', side_effect=async_local_client): expected_result = [{'items': [{'name': 'master'}]}, {'items': []}] for expected in expected_result: with patch('json.loads', return_value=expected): result = await control.get_node(lc=local_client) if len(expected['items']) > 0: assert result == expected['items'][0] else: assert result == {} with patch('json.loads', return_value=KeyError(1)): with pytest.raises(KeyError): await control.get_node(lc=local_client) with patch('wazuh.core.cluster.local_client.LocalClient.execute', side_effect=['timeout', 'error']): with pytest.raises(WazuhClusterError): await control.get_node(lc=local_client) with pytest.raises(json.JSONDecodeError): await control.get_node(lc=local_client)
async def test_get_health_nodes(mock_unix_connection): """Verify that get_health_nodes returns the health of all nodes.""" async def async_mock(lc=None, filter_node=None): return {'nodes': {'manager': {'info': {'name': 'master'}}}} local_client = LocalClient() with patch('wazuh.cluster.get_health', side_effect=async_mock): result = await cluster.get_health_nodes(lc=local_client) expected = await async_mock() assert result.affected_items == [expected['nodes']['manager']]
async def test_get_nodes_info(): """Verify that get_nodes_info returns the information of all nodes.""" async def async_mock(lc=None, filter_node=None): return {'items': ['master', 'worker1'], 'totalItems': 2} local_client = LocalClient() with patch('wazuh.cluster.get_nodes', side_effect=async_mock): result = await cluster.get_nodes_info(lc=local_client) expected = await async_mock() assert result.affected_items == expected['items']
async def test_get_agents(): """Verify that get_agents function returns the health of the agents connected through the current node.""" local_client = LocalClient() with patch('wazuh.core.cluster.local_client.LocalClient.execute', side_effect=async_local_client): expected_result = [{'items': [{'name': 'master'}]}, {'items': []}] for expected in expected_result: with patch('json.loads', return_value=expected): result = await control.get_agents(lc=local_client) assert result == expected with patch('json.loads', return_value=KeyError(1)): with pytest.raises(KeyError): await control.get_agents(lc=local_client)
async def test_get_nodes_info(): """Verify that get_nodes_info returns the information of all nodes.""" async def valid_node(lc=None, filter_node=None): return {'items': ['master', 'worker1'], 'totalItems': 2} local_client = LocalClient() common.cluster_nodes.set(['master', 'worker1', 'worker2']) with patch('wazuh.cluster.get_nodes', side_effect=valid_node): result = await cluster.get_nodes_info( lc=local_client, filter_node=['master', 'worker1', 'noexists']) expected = await valid_node() assert result.affected_items == expected['items'] assert result.total_affected_items == expected['totalItems'] assert result.failed_items[WazuhResourceNotFound(1730)] == {'noexists'} assert result.total_failed_items == 1
def test_crypto(mock_runningloop, mock_loop): with pytest.raises(WazuhException, match=".* 1119 .*"): local_client = LocalClient() loop.run_until_complete(local_client.start())