Unit testing an inventory plugin

This topic has been raised 3 years ago but it provided no answer.

I want to be able to unittest an Inventory plugin I am developing.

Basically, all I need is a way to execute the parse(self, inventory, loader, path, cache=True) method of my class extending BaseInventoryPlugin, Constructable.

I have been able to come up with something simple that was working:

def test_parse_with_valid_config(self):
source = os.path.join(os.getcwd(), ‘test/valid_test_conf.yml’)
loader = DataLoader()
inventory = InventoryData()
inventory.current_source = source
plugin = InventoryModule()

trick to be able to invoke the parse function only

plugin._redirected_names = [‘myplugin’]
plugin._load_name = u’odewe’

end trick

plugin.parse(inventory, loader, inventory.current_source)
#assertion on constructed inventory can be implemented here.

But since I started to use options to pass variables, this doesn’t cut it anymore.
I’ve tried debugging all I can to see how option were set but it seems impossible to set them without calling the cli itself.

AFAIK, I have to call the cli with subprocess this way:

subprocess.call([sys.executable, os.path.join(os.path.dirname(sys.executable), “ansible-inventory”), ‘-i’, ‘config.yml’, ‘–playbook-dir’, ‘.’, ‘–list’])

but then I have no way to access the internal inventory produced.

All I’m left with now would be to dump the result to a file and build python objects from the file to be able to make assertions, but that would really be ugly.

Kind regards