ansible动态库存插件的单元测试

问题描述 投票:0回答:1
我一直在尝试寻找动态库存插件的示例单元测试,但找不到任何。是因为无法模拟库存、加载器、路径等内部对象,这些对象是 Inventory 插件类中解析方法所需的参数。 如何模拟这些对象并将其传递给解析方法?

ansible ansible-inventory
1个回答
0
投票
我能够为我的库存插件编写单元测试。

import pytest from unittest import mock from unittest.mock import patch from ansible.inventory.data import InventoryData from plugins.inventory.myinvnetoryplugin import InventoryModule from ansible.parsing.dataloader import DataLoader config_data = { 'plugin': 'my_inventory' } @patch('my_plugin.InventoryModule._read_config_data') @patch('my_plugin.InventoryModule.get_cache_key') # if you have caching enabled def test_parse(mock_key, mock_config): inventory = InventoryModule() inventory.load_cache_plugin = mock.MagicMock() inventory.inventory = InventoryData() loader = DataLoader() mock_config.return_value = config_data mock_key.return_value = 'test' # pass inventory, loader, path and cache parameter to parse method inventory.parse(inventory.inventory, loader, mock_conf, False) groups = inventory.inventory.get_groups_dict() # Write your asserts here assert 'test' in groups assert 'host1' in inventory.inventory.hosts
    
© www.soinside.com 2019 - 2024. All rights reserved.