def test_variable_manager_task_vars(self): fake_loader = DictDataLoader({}) mock_task = MagicMock() mock_task.get_vars.return_value = dict(foo="bar") v = VariableManager() self.assertEqual(v.get_vars(loader=fake_loader, task=mock_task).get("foo"), "bar")
def test_basic_playbook(self): fake_loader = DictDataLoader({ "test_file.yml":""" - hosts: all """, }) p = Playbook.load("test_file.yml", loader=fake_loader) plays = p.get_plays()
def setUp(self): self._fake_loader = DictDataLoader({ "foo.yml": """ - shell: echo "hello world" """ }) pass
def test_variable_manager_extra_vars(self): fake_loader = DictDataLoader({}) extra_vars = dict(a=1, b=2, c=3) v = VariableManager() v.set_extra_vars(extra_vars) for (key, val) in extra_vars.iteritems(): self.assertEqual(v.get_vars(loader=fake_loader).get(key), val) self.assertIsNot(v.extra_vars.get(key), val)
def test_variable_manager_play_vars(self): fake_loader = DictDataLoader({}) mock_play = MagicMock() mock_play.get_vars.return_value = dict(foo="bar") mock_play.get_roles.return_value = [] mock_play.get_vars_files.return_value = [] v = VariableManager() self.assertEqual(v.get_vars(loader=fake_loader, play=mock_play).get("foo"), "bar")
def test_variable_manager_play_vars_files(self): fake_loader = DictDataLoader({ "/path/to/somefile.yml": """ foo: bar """ }) mock_play = MagicMock() mock_play.get_vars.return_value = dict() mock_play.get_roles.return_value = [] mock_play.get_vars_files.return_value = ['/path/to/somefile.yml'] v = VariableManager() self.assertEqual(v.get_vars(loader=fake_loader, play=mock_play).get("foo"), "bar")
def test_play_iterator(self): fake_loader = DictDataLoader({ "test_play.yml": """ - hosts: all gather_facts: false roles: - test_role pre_tasks: - debug: msg="this is a pre_task" tasks: - debug: msg="this is a regular task" post_tasks: - debug: msg="this is a post_task" """, '/etc/ansible/roles/test_role/tasks/main.yml': """ - debug: msg="this is a role task" """, }) p = Playbook.load('test_play.yml', loader=fake_loader) hosts = [] for i in range(0, 10): host = MagicMock() host.get_name.return_value = 'host%02d' % i hosts.append(host) inventory = MagicMock() inventory.get_hosts.return_value = hosts inventory.filter_hosts.return_value = hosts itr = PlayIterator(inventory, p._entries[0]) task = itr.get_next_task_for_host(hosts[0]) print(task) self.assertIsNotNone(task) task = itr.get_next_task_for_host(hosts[0]) print(task) self.assertIsNotNone(task) task = itr.get_next_task_for_host(hosts[0]) print(task) self.assertIsNotNone(task) task = itr.get_next_task_for_host(hosts[0]) print(task) self.assertIsNotNone(task) task = itr.get_next_task_for_host(hosts[0]) print(task) self.assertIsNone(task)
def test_load_role_with_tasks(self): fake_loader = DictDataLoader({ "/etc/ansible/roles/foo/tasks/main.yml": """ - shell: echo 'hello world' """, }) i = RoleInclude.load('foo', loader=fake_loader) r = Role.load(i) self.assertEqual(str(r), 'foo') self.assertEqual(len(r._task_blocks), 1) assert isinstance(r._task_blocks[0], Block)
def test_load_role_with_handlers(self): fake_loader = DictDataLoader({ "/etc/ansible/roles/foo/handlers/main.yml": """ - name: test handler shell: echo 'hello world' """, }) i = RoleInclude.load('foo', loader=fake_loader) r = Role.load(i) self.assertEqual(len(r._handler_blocks), 1) assert isinstance(r._handler_blocks[0], Block)
def test_load_role_complex(self): # FIXME: add tests for the more complex uses of # params and tags/when statements fake_loader = DictDataLoader({ "/etc/ansible/roles/foo/tasks/main.yml": """ - shell: echo 'hello world' """, }) i = RoleInclude.load(dict(role='foo'), loader=fake_loader) r = Role.load(i) self.assertEqual(r.get_name(), "foo")
def test_bad_playbook_files(self): fake_loader = DictDataLoader({ # represents a playbook which is not a list of plays "bad_list.yml": """ foo: bar """, # represents a playbook where a play entry is mis-formatted "bad_entry.yml": """ - - "This should be a mapping..." """, }) vm = VariableManager() self.assertRaises(AnsibleParserError, Playbook.load, "bad_list.yml", vm, fake_loader) self.assertRaises(AnsibleParserError, Playbook.load, "bad_entry.yml", vm, fake_loader)
def test_play_with_roles(self): fake_loader = DictDataLoader({ '/etc/ansible/roles/foo/tasks.yml': """ - name: role task shell: echo "hello world" """, }) p = Play.load(dict( name="test play", hosts=['foo'], gather_facts=False, roles=['foo'], ), loader=fake_loader) tasks = p.compile()
def test_load_role_with_vars(self): fake_loader = DictDataLoader({ "/etc/ansible/roles/foo/defaults/main.yml": """ foo: bar """, "/etc/ansible/roles/foo/vars/main.yml": """ foo: bam """, }) i = RoleInclude.load('foo', loader=fake_loader) r = Role.load(i) self.assertEqual(r._default_vars, dict(foo='bar')) self.assertEqual(r._role_vars, dict(foo='bam'))
def test_basic_manager(self): fake_loader = DictDataLoader({}) v = VariableManager() self.assertEqual(v.get_vars(loader=fake_loader), dict()) self.assertEqual( v._merge_dicts( dict(a=1), dict(b=2) ), dict(a=1, b=2) ) self.assertEqual( v._merge_dicts( dict(a=1, c=dict(foo='bar')), dict(b=2, c=dict(baz='bam')) ), dict(a=1, b=2, c=dict(foo='bar', baz='bam')) )
def test_variable_manager_host_vars_file(self): fake_loader = DictDataLoader({ "host_vars/hostname1.yml": """ foo: bar """ }) v = VariableManager() v.add_host_vars_file("host_vars/hostname1.yml", loader=fake_loader) self.assertIn("hostname1", v._host_vars_files) self.assertEqual(v._host_vars_files["hostname1"], dict(foo="bar")) mock_host = MagicMock() mock_host.get_name.return_value = "hostname1" mock_host.get_vars.return_value = dict() mock_host.get_groups.return_value = () self.assertEqual(v.get_vars(loader=fake_loader, host=mock_host).get("foo"), "bar")
def test_manager_group_vars_file(self): fake_loader = DictDataLoader({ "group_vars/somegroup.yml": """ foo: bar """ }) v = VariableManager(loader=fake_loader) v.add_group_vars_file("group_vars/somegroup.yml") self.assertIn("somegroup", v._group_vars_files) self.assertEqual(v._group_vars_files["somegroup"], dict(foo="bar")) mock_host = MagicMock() mock_host.get_name.return_value = "hostname1" mock_host.get_vars.return_value = dict() mock_host.get_groups.return_value = ["somegroup"] self.assertEqual(v.get_vars(host=mock_host), dict(foo="bar"))
def test_empty_playbook(self): fake_loader = DictDataLoader({}) p = Playbook(loader=fake_loader)
def test_load_role_with_metadata(self): fake_loader = DictDataLoader({ '/etc/ansible/roles/foo/meta/main.yml': """ allow_duplicates: true dependencies: - bar galaxy_info: a: 1 b: 2 c: 3 """, '/etc/ansible/roles/bar/meta/main.yml': """ dependencies: - baz """, '/etc/ansible/roles/baz/meta/main.yml': """ dependencies: - bam """, '/etc/ansible/roles/bam/meta/main.yml': """ dependencies: [] """, '/etc/ansible/roles/bad1/meta/main.yml': """ 1 """, '/etc/ansible/roles/bad2/meta/main.yml': """ foo: bar """, '/etc/ansible/roles/recursive1/meta/main.yml': """ dependencies: ['recursive2'] """, '/etc/ansible/roles/recursive2/meta/main.yml': """ dependencies: ['recursive1'] """, }) i = RoleInclude.load('foo', loader=fake_loader) r = Role.load(i) role_deps = r.get_direct_dependencies() self.assertEqual(len(role_deps), 1) self.assertEqual(type(role_deps[0]), Role) self.assertEqual(len(role_deps[0].get_parents()), 1) self.assertEqual(role_deps[0].get_parents()[0], r) self.assertEqual(r._metadata.allow_duplicates, True) self.assertEqual(r._metadata.galaxy_info, dict(a=1, b=2, c=3)) all_deps = r.get_all_dependencies() self.assertEqual(len(all_deps), 3) self.assertEqual(all_deps[0].get_name(), 'bar') self.assertEqual(all_deps[1].get_name(), 'baz') self.assertEqual(all_deps[2].get_name(), 'bam') i = RoleInclude.load('bad1', loader=fake_loader) self.assertRaises(AnsibleParserError, Role.load, i) i = RoleInclude.load('bad2', loader=fake_loader) self.assertRaises(AnsibleParserError, Role.load, i) i = RoleInclude.load('recursive1', loader=fake_loader) self.assertRaises(AnsibleError, Role.load, i)