mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Reimplement the class method on Runner.
This commit is contained in:
parent
195e6d617b
commit
746f1b92ae
2 changed files with 36 additions and 1 deletions
|
@ -123,6 +123,17 @@ class Runner(object):
|
|||
self._tmp_paths = {}
|
||||
random.seed()
|
||||
|
||||
# *****************************************************
|
||||
|
||||
@classmethod
|
||||
def parse_hosts(cls, host_list, override_hosts=None, extra_vars=None):
|
||||
''' parse the host inventory file, returns (hosts, groups) '''
|
||||
if override_hosts is None:
|
||||
inventory = ansible.inventory.Inventory(host_list, extra_vars)
|
||||
else:
|
||||
inventory = ansible.inventory.Inventory(override_hosts)
|
||||
|
||||
return inventory.host_list, inventory.groups
|
||||
|
||||
# *****************************************************
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import os
|
|||
import unittest
|
||||
|
||||
from ansible.inventory import Inventory
|
||||
from ansible.runner import Runner
|
||||
|
||||
class TestInventory(unittest.TestCase):
|
||||
|
||||
|
@ -141,3 +142,26 @@ class TestInventory(unittest.TestCase):
|
|||
vars = inventory.get_variables('thor', 'simple=yes')
|
||||
|
||||
assert vars == {"hammer":True, "simple": "yes"}
|
||||
|
||||
### Test Runner class method
|
||||
|
||||
def test_class_method(self):
|
||||
hosts, groups = Runner.parse_hosts(self.inventory_file)
|
||||
|
||||
expected_hosts = ['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||||
assert hosts == expected_hosts
|
||||
|
||||
expected_groups= {
|
||||
'ungrouped': ['jupiter', 'saturn'],
|
||||
'greek': ['zeus', 'hera', 'poseidon'],
|
||||
'norse': ['thor', 'odin', 'loki']
|
||||
}
|
||||
assert groups == expected_groups
|
||||
|
||||
def test_class_override(self):
|
||||
override_hosts = ['thor', 'odin']
|
||||
hosts, groups = Runner.parse_hosts(self.inventory_file, override_hosts)
|
||||
|
||||
assert hosts == override_hosts
|
||||
|
||||
assert groups == { 'ungrouped': override_hosts }
|
||||
|
|
Loading…
Reference in a new issue