-
Notifications
You must be signed in to change notification settings - Fork 18
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
Best practice / example for integration tests with pytest #193
Comments
I hacked something together for the ansible.scm collection, but I don't like it. I keep wondering about how to do this better myself, in a repeatable fashion. I'm thinking molecule might be a better collection test runner long term but I need to do thinking about how to glue it all together without just a single giant scenario, or one scenario for every role in the My gut tells me we need to teach molecule to be collection integration test aware, I'm just not sure how today. I'll probably be throwing some discussions/issues up and will let you know |
I saw you implantation in ansible.scm collection but if you don't like it I won't use it as my best practice example. I also see Molecule as the tool for integration testing, thus probably no need to have both, Imo it would be just good if in the end tox-ansible somehow magically put such integration tests up, without that I have to carry about to write pytest Python code to execute my integration plabooks / molecule scenarios. |
Running Molecule tests was the original function of |
No, I need it for ansible-test, that's why I also not used it before. |
I did some work here, and think I like where it is going........ So the integration support in tox is really doing nothing more than running The issue here is the integration tests are actually ansible roles. Molecule is a good candidate here since it can stand up and tear down infrastructure, so we just need to teach molecule how to build scenarios, one for each integration test role and then paramertize them for pytest. Although I think molecule should be able to do this by default, here's a PR with a default molecule scenario that will build molecule scenarios on the fly, one for each integration test, running https://github.com/ansible-collections/ansible.scm/pull/227/files and the bridge between pytest and molecule is here, a new fixture embedded in the pytest ansible plugin: this will require the user add a small pytest test to the integration directory: (it's all still WIP) The reason I think I like it is because the roles and responsiblities of each tool are pretty sane, and are core functionality tox: manages the matrix of python/ansible versions Right now, the tox-ansible plugin is "installing" the collection in the tox venv for both integration and unit, and molecule is doing the same into it's ephemeral directory structure......... If we assume molecule is in play here, it shouldn't need to do it... will put some thought to this. (edit: I should note I like the idea of molecule here because it can be used to test not just the integration tests within a collection but user facing content within the collection too, e.g. roles and playbooks, couple with tox, molecule will now get run across multiple python intepreters and ansible versions but both integration and user facing content) your thoughts appreciated here trying to get this right |
The ansible.scm collection has been updated to use molecule via pytest via tox-ansible as the integration test runner. https://github.com/ansible-collections/ansible.scm Still a little more than the user should need to do but much better than what was there. |
Just had time to look into this with more detail again. I really like the direction in which this is going as this streamlines Ansible collection testing and makes it also easy to provide further tests.
Yes, imo molecule should be able to detect integration targets in the same manner as And one further question: Why have you chosen to store the molecule scenarios in |
I commited them becasue I wasn't ready to modify the the tox-ansible plugin or GHA to run "molecule create" yet. If we do that, the scenarios for each target could be left out of the repo. The reason I didn't want to count on default being the first scenario to run is that if we are using pytest-xdist for multiproc, there isn't a really a guarentee that default would run first. (I think it almost always would but I didn't want to count on it) I've added a bunch of RFEs for molecule related to this, e.g. ansible/molecule#4003 Use of the extension directory is an "emerging" best practise. When event driven ansible content was added to the collections, it was put into the extensions directory. Although not really documented formally anywhere it's going to be the place moving forward. The molecule docs were updated for v6 to recommend it. I don't think putting them in the "tests" directory would be an immediate issue, but I could see a time in the future where something like ansible lint or the vscode extension assume the extension/molecule directory as a default to treat that content differently. Still looking into a couple other things to improve the whole process, will keep you posted :) |
Till now my integration tests were only Ansible tasks and running directly with
ansible-test integration
.With this setup they seems to be triggered via pytest. I see clearly the advantage (e.g. easily trigger them out of VSCode), but I don't know how to best migrate from "Ansible approach" to the pytest approach. I guess I need a pytest which will then trigger the Ansible tasks?
Is there any documentation or good examples of how to achieve that. Could there be something generic, which could be used in every collection?
The text was updated successfully, but these errors were encountered: