Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Idea: Provide import hooks for module/library code #14133

Closed
nrb opened this issue Jan 26, 2016 · 7 comments
Closed

Feature Idea: Provide import hooks for module/library code #14133

nrb opened this issue Jan 26, 2016 · 7 comments

Comments

@nrb
Copy link

nrb commented Jan 26, 2016

Currently, when trying to write Python unit tests for Ansible module/library code, one must resort to doing their own import hacks, like described in https://nicolai86.eu/blog/posts/2014-02-05/testing-ansible-libraries/.

To avoid this, I wrote a new library, ansible_importer, that uses Python import hooks to find Ansible modules without peppering the file system with __init__.py files or having to rewrite the import code every time.

It may be useful for ansible itself to provide this functionality. I would propose that it be something like ansible.imports.activate('abs/path/to/yaml/code').

@bcoca
Copy link
Member

bcoca commented Jan 26, 2016

just produce the file using hacking/test-module

@nrb
Copy link
Author

nrb commented Jan 26, 2016

Ah, I wasn't aware of this, thanks.

However, that doesn't appear to do in-depth unit testing, like importing specific classes/functions and running them in a test case. Maybe I'm missing something?

@bcoca
Copy link
Member

bcoca commented Jan 26, 2016

no, but it produces the importable version of the script

Brian Coca

@nrb
Copy link
Author

nrb commented Jan 26, 2016

Thanks, I see that now on further reading. Looking at the setup.py file and my installed version of Ansible, it appears this isn't included with the installed distribution, as only the lib directory is packaged.

How would you suggest testing modules that live outside of ansible's core with this? For context, I'm trying to test modules in https://github.com/openstack/openstack-ansible/tree/master/playbooks/plugins.

@bcoca
Copy link
Member

bcoca commented Jan 26, 2016

those you should be able to test with a normal import, its only the library/task/modules that use ansible.module_utils. require extra magic as they are not importable.

@nrb
Copy link
Author

nrb commented Jan 26, 2016

Thanks, I'll play around with that.

The main thrust of the import hooks was to provide a way to get to the actual importable modules, since like you say, they do work. I think in that specific case we wanted to have our tests in the root tests directory.

@jimi-c
Copy link
Member

jimi-c commented Jan 26, 2016

As this appears to be more of a general use question rather than a bug/issue, I'm going to go ahead and close this now. If you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

@jimi-c jimi-c closed this as completed Jan 26, 2016
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants