# Unit test mock object issue?

**URL:** <https://forum.ansible.com/t/unit-test-mock-object-issue/18625>\
**Category:** Ansible Developer\
**Created:** [June 26, 2015, 7:50pm UTC](https://forum.ansible.com/t/unit-test-mock-object-issue/18625 "2015-06-26T19:50:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Tanner](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Bob\_Tanner](https://forum.ansible.com/u/Bob_Tanner)\
**Post date:** [June 26, 2015, 7:50pm UTC](https://forum.ansible.com/t/unit-test-mock-object-issue/18625/1 "2015-06-26T19:50:56Z")

</div>

Trying to fix my issue tracked at [https://github.com/ansible/ansible/issues/11320](https://github.com/ansible/ansible/issues/11320). From what I can tell v2 does not process includes properly.

So I started playing around with test/playbook/test\_playbook.py, here’s a simple test.

`  
def test\_basic\_include(self):  
fake\_loader = DictDataLoader({  
“test\_file.yml”:“”"

- include: good.yml
- hosts: all  
“”",  
})  
p = Playbook.load(“test\_file.yml”, loader=fake\_loader)

`

My first question: That is a valid playbook, right?

Test suite throws AnsibleParserError: ERROR! playbooks must be a list of plays

Debugging I’m finding the problem in tests/mock/ **init**.py:load\_from\_file()

`ansible/test/units/mock/loader.py(38)load_from_file() -> if path in self._file_mapping: (Epdb) type(path) <type 'unicode'> (Epdb) type(self._file_mapping) <type 'dict'> (Epdb) p path u'good.yml' (Epdb) p self._file_mapping {'test_file.yml': '\n - include: good.yml\n - hosts: all\n '} (Epdb) self._file_mapping.keys() ['test_file.yml']`

So looking for u’good.yml’ inside a dict() is going to return False and the include file is not going to be loaded.

Some interactive python shows this logic.

`

> > > file\_mapping={‘test\_file.yml’: '\n - include: good.yml\n - hosts: all\n ‘}  
> > > type(file\_mapping)  
> > > \<type ‘dict’\>  
> > > path=u’good.yml’  
> > > type(path)  
> > > \<type ‘unicode’\>  
> > > path in file\_mapping  
> > > False  
> > > `

So I do not know if the problem is in the mock object or the parser?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 30, 2015, 4:15am UTC](https://forum.ansible.com/t/unit-test-mock-object-issue/18625/2 "2015-06-30T04:15:10Z")

</div>

The include seems to be working correctly, failing when the file is  
not there also seems to be correct behavior. I'm not sure what you  
expected here.

That would be a correct playbook if good.yml contained a valid play.

---

<div class="post-metadata">

**Author:** ![Bob\_Tanner](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Bob\_Tanner](https://forum.ansible.com/u/Bob_Tanner)\
**Post date:** [July 1, 2015, 10:26pm UTC](https://forum.ansible.com/t/unit-test-mock-object-issue/18625/3 "2015-07-01T22:26:28Z")

</div>

I understand your view point.

I would expect something more specific like “… file good.yml does not exist or is not readable …” and then chain the exception to “ERROR! playbooks must be a list of plays”
