# Module function testings in core

**URL:** <https://forum.ansible.com/t/module-function-testings-in-core/12223>\
**Category:** Ansible Project\
**Created:** [January 20, 2013, 5:22pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223 "2013-01-20T17:22:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.chanial](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david.chanial](https://forum.ansible.com/u/david.chanial)\
**Post date:** [January 20, 2013, 5:22pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/1 "2013-01-20T17:22:44Z")

</div>

Hi,

_For the Gentoo Portage “emerge” module creation i confirm what i have thinked about creating the sysctl module :_

Why the python modules ; and/or **the python core modules must** leak the call of the main function assuming that the _INCLUDE\_ANSIBLE\_MODULE\_COMMON_ will insert _“ansible\_main()”_ too ?

This will not change something for python module developers based on _INCLUDE\_ANSIBLE\_MODULE\_COMMON_ .

And this will permit to ansible developers to add some tests for their modules (called on make tests) :

Assume for example :

> #!/usr/bin/python
> 
> # GPL lines
> 
> DOCUMENTATION = ‘’‘’‘’  
> def my\_smart\_function(input):  
> return input + ‘\_expanded’  
> def ansible\_test:
> 
> # first test
> 
> res = my\_smart\_function(‘test’)  
> if res != ‘test\_expanded’:  
> return False
> 
> # end
> 
> return True  
> def ansible\_main:  
> module = AnsibleModule(
> 
> # […]
> 
> )
> 
> # my smart code here, for example :
> 
> args = { ‘new\_state’: my\_smart\_function(module.params[‘input’]) }  
> module.exit\_json(changed=True, \*\*args)  
> sys.exit(0)
> 
> # this is magic, see lib/ansible/module\_common.py
> 
> #\<\<INCLUDE\_ANSIBLE\_MODULE\_COMMON\>\>

Here the idea.  
It will permit to call ansible\_main or ansible\_test in function of the needs, and permit a "one file module integration’'.  
Thinking for light module synced to managed hosts, we can imagine a

---

<div class="post-metadata">

**Author:** ![david.chanial](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david.chanial](https://forum.ansible.com/u/david.chanial)\
**Post date:** [January 20, 2013, 5:24pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/2 "2013-01-20T17:24:09Z")

</div>

Sorry for the prematured post; here the end :

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 20, 2013, 9:19pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/3 "2013-01-20T21:19:30Z")

</div>

> Sorry for the prematured post; here the end :  
> ----
> 
> Thinking for light module synced to managed hosts, we can imagine a wrapper  
> filtering the DOCUMENTATION string, and the ansible\_test implementation.

Are we seeing any signs of transferring the documentation string  
causing signfiicant performance implications? I can't imagine that  
being a large measurable chunk.

If you can produce some benchmarks I'm open to considering it though.

---

<div class="post-metadata">

**Author:** ![david.chanial](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david.chanial](https://forum.ansible.com/u/david.chanial)\
**Post date:** [January 20, 2013, 10:19pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/4 "2013-01-20T22:19:32Z")

</div>

This point is minor and symbolic.

Please look at the first post in the thread for the real subject.

David CHANIAL

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 21, 2013, 2:51pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/5 "2013-01-21T14:51:19Z")

</div>

I don't really follow what you are trying to say.

Can you rephrase that?

---

<div class="post-metadata">

**Author:** ![david.chanial](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david.chanial](https://forum.ansible.com/u/david.chanial)\
**Post date:** [January 21, 2013, 8:31pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/6 "2013-01-21T20:31:37Z")

</div>

> I don’t really follow what you are trying to say.  
> Can you rephrase that?

Hi,

_For the Gentoo Portage “emerge” module creation i confirm what i have thinked about creating the sysctl module :_

Why the python modules ; and/or **the python core modules must** leak the call of the main function assuming that the _INCLUDE\_ANSIBLE\_MODULE\_COMMON_ will insert _“ansible\_main()”_ too ?

This will not change something for python module developers based on _INCLUDE\_ANSIBLE\_MODULE\_COMMON_ .

And this will permit to ansible developers to add some tests for their modules (called on make tests) :

Assume for example :

> #!/usr/bin/python
> 
> # GPL lines
> 
> DOCUMENTATION = ‘’‘’‘’  
> def my\_smart\_function(input):  
> return input + ‘\_expanded’  
> def ansible\_test:
> 
> # first test
> 
> res = my\_smart\_function(‘test’)  
> if res != ‘test\_expanded’:  
> return False
> 
> # end
> 
> return True  
> def ansible\_main:  
> module = AnsibleModule(
> 
> # […]
> 
> )
> 
> # my smart code here, for example :
> 
> args = { ‘new\_state’: my\_smart\_function(module.params[‘input’]) }  
> module.exit\_json(changed=True, \*\*args)  
> sys.exit(0)
> 
> # this is magic, see lib/ansible/module\_common.py
> 
> #\<\<INCLUDE\_ANSIBLE\_MODULE\_COMMON\>\>

Here the idea.  
It will permit to call ansible\_main or ansible\_test in function of the needs, and permit a "one file module integration’'.  
Thinking for a symbolic light module synced to managed hosts, we can imagine a wrapper filtering the DOCUMENTATION string, and the ansible\_test implementation.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 21, 2013, 8:33pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/7 "2013-01-21T20:33:01Z")

</div>

> Here the idea.  
> It will permit to call ansible\_main or ansible\_test in function of the  
> needs, and permit a "one file module integration''.  
> Thinking for a symbolic light module synced to managed hosts, we can imagine  
> a wrapper filtering the DOCUMENTATION string, and the ansible\_test  
> implementation.

What does this gain us other than a possible bandwidth savings?

---

<div class="post-metadata">

**Author:** ![david.chanial](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david.chanial](https://forum.ansible.com/u/david.chanial)\
**Post date:** [January 21, 2013, 9:04pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/8 "2013-01-21T21:04:53Z")

</div>

forget this point please.

focus only on the test function.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 21, 2013, 10:15pm UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/9 "2013-01-21T22:15:44Z")

</div>

Ok.

I'm still confused what you want to do with a test function.

Can you explain your use case differently?

---

<div class="post-metadata">

**Author:** ![david.chanial](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@david.chanial](https://forum.ansible.com/u/david.chanial)\
**Post date:** [January 22, 2013, 5:48am UTC](https://forum.ansible.com/t/module-function-testings-in-core/12223/10 "2013-01-22T05:48:38Z")

</div>

I will come back until the end of the week with samples.
