Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_template.py

raw
1 # (c) 2018-2025, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit test template for ONTAP Ansible module '''
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8 import pytest
9
10 from ansible_collections.netapp.ontap.tests.unit.compat import unittest
11 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
12 # pylint: disable=unused-import
13 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import set_module_args, \
14 AnsibleFailJson, AnsibleExitJson, patch_ansible
15
16 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_cg_snapshot \
17 import NetAppONTAPCGSnapshot as my_module # module under test
18
19 if not netapp_utils.has_netapp_lib():
20 pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
21
22
23 class MockONTAPConnection(object):
24 ''' mock server connection to ONTAP host '''
25
26 def __init__(self, kind=None, parm1=None):
27 ''' save arguments '''
28 self.type = kind
29 self.parm1 = parm1
30 self.xml_in = None
31 self.xml_out = None
32
33 def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument
34 ''' mock invoke_successfully returning xml data '''
35 self.xml_in = xml
36 if self.type == 'vserver':
37 xml = self.build_vserver_info(self.parm1)
38 self.xml_out = xml
39 return xml
40
41 @staticmethod
42 def build_vserver_info(vserver):
43 ''' build xml data for vserser-info '''
44 xml = netapp_utils.zapi.NaElement('xml')
45 attributes = netapp_utils.zapi.NaElement('attributes-list')
46 attributes.add_node_with_children('vserver-info',
47 **{'vserver-name': vserver})
48 xml.add_child_elem(attributes)
49 # print(xml.to_string())
50 return xml
51
52
53 class TestMyModule(unittest.TestCase):
54 ''' a group of related Unit Tests '''
55
56 def setUp(self):
57 self.server = MockONTAPConnection()
58
59 def test_module_fail_when_required_args_missing(self):
60 ''' required arguments are reported as errors '''
61 with pytest.raises(AnsibleFailJson) as exc:
62 set_module_args({})
63 my_module()
64 print('Info: %s' % exc.value.args[0]['msg'])
65
66 def test_ensure_command_called(self):
67 ''' a more interesting test '''
68 # TODO: change argument names/values
69 set_module_args({
70 'vserver': 'vserver',
71 'volumes': 'volumes',
72 'snapshot': 'snapshot',
73 'hostname': 'hostname',
74 'username': 'username',
75 'password': 'password',
76 'use_rest': 'never'
77 })
78 my_obj = my_module()
79 my_obj.server = self.server
80 with pytest.raises(AnsibleFailJson) as exc:
81 # It may not be a good idea to start with apply
82 # More atomic methods can be easier to mock
83 # Hint: start with get methods, as they are called first
84 my_obj.apply()
85 # TODO: change message, and maybe test contents
86 msg = 'Error fetching CG ID for CG commit snapshot'
87 assert exc.value.args[0]['msg'] == msg