Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/framework/test_zapi_factory.py

raw
1 # (c) 2018-2022, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 """ unit tests for Ansible module unit test helper zapi_factory """
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8
9 import pytest
10 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
11 from ansible_collections.netapp.ontap.tests.unit.framework import zapi_factory as uut
12
13 if not netapp_utils.has_netapp_lib():
14 pytestmark = pytest.mark.skip('skipping as missing required netapp_lib') # pragma: no cover
15
16 AGGR_INFO = {'num-records': 3,
17 'attributes-list':
18 {'aggr-attributes':
19 {'aggregate-name': 'aggr_name',
20 'aggr-raid-attributes': {
21 'state': 'online',
22 'disk-count': '4',
23 'encrypt-with-aggr-key': 'true'},
24 'aggr-snaplock-attributes': {'snaplock-type': 'snap_t'}}
25 },
26 }
27
28
29 def test_build_zapi_response_empty():
30 empty, valid = uut.build_zapi_response({})
31 assert valid == 'valid'
32 print(empty.to_string())
33 assert empty.to_string() == b'<results status="passed"/>'
34
35
36 def test_build_zapi_response_dict():
37 aggr_info, valid = uut.build_zapi_response(AGGR_INFO)
38 assert valid == 'valid'
39 print(aggr_info.to_string())
40 aggr_str = aggr_info.to_string()
41 assert b'<aggregate-name>aggr_name</aggregate-name>' in aggr_str
42 assert b'<aggr-snaplock-attributes><snaplock-type>snap_t</snaplock-type></aggr-snaplock-attributes>' in aggr_str
43 assert b'<results status="passed">' in aggr_str
44 assert b'<num-records>3</num-records>' in aggr_str
45
46
47 def test_build_zapi_error():
48 zapi1, valid = uut.build_zapi_error('54321', 'error_text')
49 assert valid == 'valid'
50 zapi2, valid = uut.build_zapi_error(54321, 'error_text')
51 assert valid == 'valid'
52 assert zapi1.to_string() == zapi2.to_string()
53 print(zapi1.to_string())
54 assert zapi1.to_string() == b'<results errno="54321" reason="error_text"/>'
55
56
57 def test_default_responses():
58 zrr = uut.zapi_responses()
59 assert zrr
60 assert 'empty' in zrr
61 print(zrr['empty'][0].to_string())
62 assert zrr['empty'][0].to_string() == uut.build_zapi_response({})[0].to_string()
63
64
65 def test_add_response():
66 zrr = uut.zapi_responses(
67 {'empty': uut.build_zapi_response({'k': 'v'}, 1)}
68 )
69 assert zrr
70 assert 'empty' in zrr
71 print(zrr['empty'][0].to_string())
72 assert zrr['empty'][0].to_string() == uut.build_zapi_response({'k': 'v'}, 1)[0].to_string()
73
74
75 def test_negative_add_response():
76 with pytest.raises(KeyError) as exc:
77 zrr = uut.zapi_responses(
78 {'empty': uut.build_zapi_response({})}, allow_override=False
79 )
80 print(exc.value)
81 assert 'duplicated key: empty' == exc.value.args[0]
82
83
84 def test_negative_add_default_error():
85 uut._DEFAULT_ERRORS['empty'] = uut.build_zapi_error(12345, 'hello')
86 with pytest.raises(KeyError) as exc:
87 zrr = uut.zapi_responses(allow_override=False)
88 print(exc.value)
89 assert 'duplicated key: empty' == exc.value.args[0]
90 del uut._DEFAULT_ERRORS['empty']
91
92
93 def test_negative_add_error():
94 with pytest.raises(KeyError) as exc:
95 zrr = uut.zapi_responses(
96 {'empty': uut.build_zapi_error(12345, 'hello')}, allow_override=False
97 )
98 print(exc.value)
99 assert 'duplicated key: empty' == exc.value.args[0]
100
101
102 def test_negative_key_does_not_exist():
103 zrr = uut.zapi_responses()
104 with pytest.raises(KeyError) as exc:
105 zrr['bad_key']
106 print(exc.value)
107 msg = 'bad_key not registered, list of valid keys:'
108 assert msg in exc.value.args[0]