add some unit tests for general role stuff

This commit is contained in:
Adrian Likins 2018-07-16 17:49:32 -04:00
parent 5b4a7cc283
commit 62dab4b53d
4 changed files with 133 additions and 1 deletions

View file

@ -0,0 +1,13 @@
import logging
from ansible.playbook.role import metadata
log = logging.getLogger(__name__)
def test_role_metadata():
rmd = metadata.RoleMetadata()
log.debug('rmd: %s', rmd)
assert isinstance(rmd, metadata.RoleMetadata)

View file

@ -0,0 +1,87 @@
import logging
from ansible.playbook.role import definition
from ansible.playbook.role import requirement
log = logging.getLogger(__name__)
def test_role_requirement():
rr = requirement.RoleRequirement()
assert isinstance(rr, requirement.RoleRequirement)
assert isinstance(rr, definition.RoleDefinition)
def test_repo_url_to_repo_name():
repo_url = 'http://git.example.com/repos/repo.git'
res = requirement.RoleRequirement.repo_url_to_role_name(repo_url)
log.debug('res: %s', res)
assert res == 'repo'
def test_role_spec_parse():
res = requirement.RoleRequirement.role_spec_parse('foo.bar')
log.debug('res: %s', res)
assert isinstance(res, dict)
assert res['name'] == 'foo.bar'
assert res['src'] == 'foo.bar'
def test_role_spec_parse_example():
role_spec = 'git+http://git.example.com/repos/repo.git,v1.0'
res = requirement.RoleRequirement.role_spec_parse(role_spec)
# {'scm': 'git', 'src': 'http://git.example.com/repos/repo.git',
# 'version': 'v1.0', 'name': 'repo'}
log.debug('res: %s', res)
assert isinstance(res, dict)
assert res['name'] == 'repo'
assert res['scm'] == 'git'
assert res['src'] == 'http://git.example.com/repos/repo.git'
assert res['version'] == 'v1.0'
def test_role_yaml_parse_dict_old_style():
role_yaml = {'role': "galaxy.role,v1.2.3,role_name", 'other_vars': "here"}
res = requirement.RoleRequirement.role_yaml_parse(role_yaml)
log.debug('res: %s', res)
# {'scm': None, 'src': 'galaxy.role', 'version': 'v1.2.3', 'name': 'role_name'}
assert isinstance(res, dict)
assert res['name'] == 'role_name'
assert res['version'] == 'v1.2.3'
assert res['src'] == 'galaxy.role'
#
# def test_role_yaml_parse_dict_new_style():
# # NOTE: the example in the comments for role_yaml_parse fails
# role_yaml = {'src': 'git+http://github.com/some_galaxy/some_role,v1.2.3,role_name', 'other_vars': "here"}
#
# res = requirement.RoleRequirement.role_yaml_parse(role_yaml)
#
# log.debug('res: %s', res)
# # {'scm': None, 'src': 'galaxy.role', 'version': 'v1.2.3', 'name': 'role_name'}
# assert isinstance(res, dict)
# assert res['name'] == 'role_name'
# assert res['version'] == 'v1.2.3'
# assert res['src'] == 'galaxy.role'
#
def test_role_yaml_parse_string():
role_yaml = 'galaxy.role,v1.2.3,role_name'
res = requirement.RoleRequirement.role_yaml_parse(role_yaml)
log.debug('res: %s', res)
assert isinstance(res, dict)
assert res['name'] == 'role_name'
assert res['version'] == 'v1.2.3'
assert res['src'] == 'galaxy.role'

View file

@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import collections
import logging
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch, MagicMock
@ -35,6 +36,8 @@ from ansible.playbook.role import Role
from ansible.playbook.role.include import RoleInclude
from ansible.playbook.role import hash_params
log = logging.getLogger(__name__)
class TestHashParams(unittest.TestCase):
def test(self):
@ -378,3 +381,32 @@ class TestRole(unittest.TestCase):
r = Role.load(i, play=mock_play)
self.assertEqual(r.get_name(), "foo_complex")
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
def test_serialize(self):
fake_loader = DictDataLoader({
"/etc/ansible/roles/foo_vars/defaults/main/foo/bar.yml": """
foo: bar
""",
"/etc/ansible/roles/foo_vars/vars/main/bar/foo.yml": """
foo: bam
""",
})
mock_play = MagicMock()
mock_play.ROLE_CACHE = {}
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
r = Role.load(i, play=mock_play)
res = r.serialize()
log.debug('res: %s', res)
self.assertEqual(r._default_vars, dict(foo='bar'))
self.assertEqual(r._role_vars, dict(foo='bam'))
r2 = Role.load(i, play=mock_play)
r2.deserialize(res)
log.debug('r2: %s', r2)