Skip to content

Commit

Permalink
Try adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Oct 17, 2020
1 parent f9a0695 commit a098a8e
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/TestLocalContent.py
@@ -0,0 +1,42 @@
"""Test ability to import playbooks."""
from ansiblelint.runner import Runner


def test_local_collection(default_rules_collection):
"""Assures local collections are found."""
playbook_path = 'test/local-content/test-collection.yml'
runner = Runner(default_rules_collection, playbook_path, [], [], [])
results = runner.run()

assert len(runner.playbooks) == 1
assert len(results) == 0


def test_roles_local_content(default_rules_collection):
"""Assures local content in roles is found."""
playbook_path = 'test/local-content/test-roles-success.yml'
runner = Runner(default_rules_collection, playbook_path, [], [], [])
results = runner.run()

assert len(runner.playbooks) == 1
assert len(results) == 0


def test_roles_local_content_failure(default_rules_collection):
"""Assures local content in roles is found, even if Ansible itself has trouble."""
playbook_path = 'test/local-content/test-roles-failure.yml'
runner = Runner(default_rules_collection, playbook_path, [], [], [])
results = runner.run()

assert len(runner.playbooks) == 1
assert len(results) == 0


def test_roles_local_content_failure_complete(default_rules_collection):
"""Role with local content that is not found."""
playbook_path = 'test/local-content/test-roles-failure-complete.yml'
runner = Runner(default_rules_collection, playbook_path, [], [], [])
results = runner.run()

assert len(runner.playbooks) == 1
assert len(results) == 0
@@ -0,0 +1,3 @@
namespace: testns
name: testcoll
version: 0.1.0
@@ -0,0 +1,15 @@
"""A filter plugin."""

def test_filter(a, b):
"""Test filter."""
return '{0}:{1}'.format(a, b)


class FilterModule(object):
"""Filter plugin."""

def filters(self):
"""Return filters."""
return {
'test_filter': test_filter
}
@@ -0,0 +1,12 @@
#!/usr/bin/python
"""A module."""

from ansible.module_utils.basic import AnsibleModule

def main():
"""Module code."""
module = AnsibleModule(dict())
module.exit_json(msg="Hello 2!")

if __name__ == '__main__':
main()
12 changes: 12 additions & 0 deletions test/local-content/roles/role1/library/test_module_1.py
@@ -0,0 +1,12 @@
#!/usr/bin/python
"""A module."""

from ansible.module_utils.basic import AnsibleModule

def main():
"""Module code."""
module = AnsibleModule(dict())
module.exit_json(msg="Hello 1!")

if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions test/local-content/roles/role1/tasks/main.yml
@@ -0,0 +1,3 @@
---
- name: Use local module 1
test_module_1:
11 changes: 11 additions & 0 deletions test/local-content/roles/role2/tasks/main.yml
@@ -0,0 +1,11 @@
---
- name: Use local module from other role that has been included before this one
# If it has not been included before, loading this role fails!
test_module_1:
- name: Use local module from other role that has been included before this one
# If it has not been included before, loading this role fails!
test_module_3:
- name: Use local test plugin
assert:
that:
- "'2' is b_test '12345'"
19 changes: 19 additions & 0 deletions test/local-content/roles/role2/test_plugins/b.py
@@ -0,0 +1,19 @@
"""A test plugin."""

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type


def compatibility_in_test(a, b):
"""Simple 'in' test."""
return a in b


class TestModule:
"""Test plugin."""

def tests(self):
"""Return tests."""
return {
'b_test': compatibility_in_test,
}
12 changes: 12 additions & 0 deletions test/local-content/roles/role3/library/test_module_3.py
@@ -0,0 +1,12 @@
#!/usr/bin/python
"""A module."""

from ansible.module_utils.basic import AnsibleModule

def main():
"""Module code."""
module = AnsibleModule(dict())
module.exit_json(msg="Hello 3!")

if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions test/local-content/roles/role3/tasks/main.yml
@@ -0,0 +1,3 @@
---
- name: Use local module 3
test_module_3:
10 changes: 10 additions & 0 deletions test/local-content/test-collection.yml
@@ -0,0 +1,10 @@
---
- name: Use module and filter plugin from local collection
hosts: localhost
tasks:
- name: Use module from local collection
testns.testcoll.test_module_2:
- name: Use filter from local collection
assert:
that:
- 1 | testns.testcoll.test_filter(2) == '1:2'
5 changes: 5 additions & 0 deletions test/local-content/test-roles-failed-complete.yml
@@ -0,0 +1,5 @@
---
- name: Include role which expects module that is local to other role which is not loaded
hosts: localhost
roles:
- role2
7 changes: 7 additions & 0 deletions test/local-content/test-roles-failed.yml
@@ -0,0 +1,7 @@
---
- name: Use roles with local module in wrong order, so that Ansible fails
hosts: localhost
roles:
- role2
- role3
- role1
7 changes: 7 additions & 0 deletions test/local-content/test-roles-success.yml
@@ -0,0 +1,7 @@
---
- name: Use roles with local modules and test plugins
hosts: localhost
roles:
- role1
- role3
- role2

0 comments on commit a098a8e

Please sign in to comment.