Common test utility functions

I'm starting to migrate my module project into a collection and I'm running into issues with missing test utility functions like:

tests/unit/plugins/lookup/test_pfsense.py:10: in <module>
     from units.compat.mock import patch
E ImportError: No module named units.compat.mock

tests/unit/plugins/modules/test_pfsense_aggregate.py:13: in <module>
     from units.modules.utils import set_module_args
E ImportError: No module named units.modules.utils

From poking around in the various collection repositories it appears that many collections are now carrying local copies of these. Is this really the plan here? It seems - not ideal.

Thanks for any insight,
   Orion

Hi,

I'm starting to migrate my module project into a collection and I'm
running into issues with missing test utility functions like:

tests/unit/plugins/lookup/test_pfsense.py:10: in <module>
     from units.compat.mock import patch
E ImportError: No module named units.compat.mock

tests/unit/plugins/modules/test_pfsense_aggregate.py:13: in <module>
     from units.modules.utils import set_module_args
E ImportError: No module named units.modules.utils

From poking around in the various collection repositories it appears
that many collections are now carrying local copies of these. Is
this really the plan here? It seems - not ideal.

I'm not very happy about this either. My personal recommendation
(slightly biased :wink: ) for collections would be to import them from
https://galaxy.ansible.com/community/internal_test_utils /
https://github.com/ansible-collections/community.internal_test_tools.
That of course requires you to install that collection for unit tests,
though...

Cheers,
Felix

Thanks, I've used that in my project. I wonder if a tests dependency tag is in order for galaxy.yml?

Hi,

> I'm not very happy about this either. My personal recommendation
> (slightly biased :wink: ) for collections would be to import them from
> https://galaxy.ansible.com/community/internal_test_utils /
> https://github.com/ansible-collections/community.internal_test_tools.
> That of course requires you to install that collection for unit
> tests, though...

Thanks, I've used that in my project. I wonder if a tests dependency
tag is in order for galaxy.yml?

good question. From how I understood it, adding things to galaxy.yml is
complicated since it is converted to MANIFEST.json during collection
build, and that might be strictly validated by Galaxy / AH itself.

In any case, there already exists a "proposal" for a test dependency
file: tests/requirements.yml

(See for example
https://github.com/ansible-collections/community.general/blob/main/tests/requirements.yml)

This file was created by the collection migration tool used when moving
content from ansible/ansible into collection repositories. It is not
used by ansible-test at the moment, and I have no idea whether there
are actual plans of supporting it eventually.

(Right now, dependencies are installed 'manually' during CI:
https://github.com/ansible-collections/community.general/blob/main/tests/utils/shippable/shippable.sh#L76-L89)

Cheers,
Felix

Hello,

I had a look to this interesting internal test collection, yet I cannot make it work, my ansible-test units command keeps complaining that the following path cannot be found:

from ansible_collections.community.internal_test_tools.tests.unit.compat import unittest
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import Mock, patch

What is the procedure to follow to set it up ?

Thanks !

Cédric S.

Hello,

I had a look to this interesting internal test collection, yet I
cannot make it work, my ansible-test units command keeps complaining
that the following path cannot be found:

from
ansible_collections.community.internal_test_tools.tests.unit.compat
import unittest from
ansible_collections.community.internal_test_tools.tests.unit.compat.mock
import Mock, patch

What is the procedure to follow to set it up ?

did you install the collection in the same path structure that the
collection you are running ansible-test on is in?

I.e. do you have

/path/to/ansible_collections/community/internal_test_tools/
/path/to/ansible_collections/yournamespace/yourcollection/

in the same directory structure?

Best,
Felix

Hello,

I didn’t, I’ve used ansible-galaxy install which did install the collection in .ansible/ansible_collections/…

Am I supposed to do a git clone inside the same path of the collection I’m developing ? This wasn’t clear for me :slight_smile:

Thanks !

Cédric S.

So, git cloning next to my work-in-progress collection allowed me to do import it,

Thanks again, it’s a bit fuzy to find correct examples of “Getting started with collection testing” :slight_smile:

Regards,

Cédric S.

Hi,

I didn't, I've used ansible-galaxy install which did install the
collection in .ansible/ansible_collections/...

Am I supposed to do a git clone inside the same path of the
collection I'm developing ? This wasn't clear for me :slight_smile:

you can also tell `ansible-galaxy collection install` to install it
there, so using `git clone` is not necessary.

That the collection has to live in that place is a requirement of
ansible-test.

Thanks again, it's a bit fuzy to find correct examples of "Getting
started with collection testing" :slight_smile:

That's indeed one of the may places where docs can be improved.
Contributors are always welcome:
https://github.com/ansible/ansible/tree/devel/docs/docsite/rst/ :wink:

Cheers,
Felix