Common test utility functions

27 views
Skip to first unread message

Orion Poplawski

unread,
Feb 11, 2021, 10:08:40 AM2/11/21
to Ansible Development
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

--
Orion Poplawski
he/him/his - surely the least important thing about me
Manager of NWRA Technical Systems 720-772-5637
NWRA, Boulder/CoRA Office FAX: 303-415-9702
3380 Mitchell Lane or...@nwra.com
Boulder, CO 80301 https://www.nwra.com/

Felix Fontein

unread,
Feb 11, 2021, 10:15:13 AM2/11/21
to ansibl...@googlegroups.com
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 ;) ) 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


Orion Poplawski

unread,
Feb 13, 2021, 5:57:51 PM2/13/21
to Felix Fontein, ansibl...@googlegroups.com
Thanks, I've used that in my project. I wonder if a tests dependency
tag is in order for galaxy.yml?

Felix Fontein

unread,
Feb 14, 2021, 8:17:29 AM2/14/21
to ansibl...@googlegroups.com
Hi,

> > I'm not very happy about this either. My personal recommendation
> > (slightly biased ;) ) 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


Idont Haveaname

unread,
May 15, 2022, 3:14:39 PMMay 15
to Ansible Development
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.

Felix Fontein

unread,
May 15, 2022, 3:31:12 PMMay 15
to ansibl...@googlegroups.com
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


Idont Haveaname

unread,
May 15, 2022, 3:34:45 PMMay 15
to Ansible Development
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 :)

Thanks !

Cédric S.

Idont Haveaname

unread,
May 15, 2022, 3:38:24 PMMay 15
to Ansible Development
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" :)

Regards,

Cédric S.

Felix Fontein

unread,
May 15, 2022, 4:23:36 PMMay 15
to ansibl...@googlegroups.com
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 :)

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" :)

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/ ;-)

Cheers,
Felix


Reply all
Reply to author
Forward
0 new messages