Debian Codegraph @

ansible_collections/community/sap_libs/tests/unit/plugins/modules/test_sap_snote.py

raw
1 #!/usr/bin/env python
2
3 # Copyright (c) 2022-2026 The Project Contributors.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # For a detailed list of copyright holders and contribution history,
18 # please refer to the CONTRIBUTORS.md file in the project root.
19
20 from __future__ import (absolute_import, division, print_function)
21 __metaclass__ = type
22
23 import sys
24 from unittest.mock import patch, MagicMock, Mock
25 from ansible_collections.community.sap_libs.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
26
27 sys.modules['pyrfc'] = MagicMock()
28 sys.modules['pyrfc.Connection'] = MagicMock()
29
30 from ansible_collections.community.sap_libs.plugins.modules import sap_snote
31
32
33 class TestSAPRfcModule(ModuleTestCase):
34
35 def setUp(self):
36 super(TestSAPRfcModule, self).setUp()
37 self.module = sap_snote
38
39 def tearDown(self):
40 super(TestSAPRfcModule, self).tearDown()
41
42 def define_rfc_connect(self, mocker):
43 return mocker.patch(self.module.call_rfc_method)
44
45 def test_without_required_parameters(self):
46 """Failure must occurs when all parameters are missing"""
47 with self.assertRaises(AnsibleFailJson):
48 with set_module_args({}):
49 self.module.main()
50
51 def test_error_module_not_found(self):
52 """tests fail module error"""
53
54 args = {
55 "conn_username": "ADMIN",
56 "conn_password": "Test1234",
57 "host": "10.1.8.9",
58 "snote_path": "/user/sap/trans/temp/000123456.txt"
59 }
60 with self.assertRaises(AnsibleFailJson) as result:
61 self.module.HAS_PYRFC_LIBRARY = False
62 self.module.ANOTHER_LIBRARY_IMPORT_ERROR = 'Module not found'
63 with set_module_args(args):
64 self.module.main()
65 self.assertEqual(result.exception.args[0]['exception'], 'Module not found')
66
67 def test_error_connection(self):
68 """tests fail module error"""
69
70 args = {
71 "conn_username": "ADMIN",
72 "conn_password": "Test1234",
73 "host": "10.1.8.9",
74 "snote_path": "/user/sap/trans/temp/000123456.txt"
75 }
76 with self.assertRaises(AnsibleFailJson) as result:
77 self.module.Connection.side_effect = Mock(side_effect=Exception('Test'))
78 with set_module_args(args):
79 self.module.main()
80 self.assertEqual(result.exception.args[0]['msg'], 'Something went wrong connecting to the SAP system.')
81
82 def test_error_wrong_path(self):
83 """tests fail wrong path extension"""
84
85 args = {
86 "conn_username": "ADMIN",
87 "conn_password": "Test1234",
88 "host": "10.1.8.9",
89 "snote_path": "/user/sap/trans/temp/000123456_00.tx"
90 }
91
92 with self.assertRaises(AnsibleFailJson) as result:
93 with set_module_args(args):
94 self.module.main()
95 self.assertEqual(result.exception.args[0]['msg'], 'The path must include the extracted snote file and ends with txt.')
96
97 def test_error_wrong_user(self):
98 """tests fail wrong path extension"""
99
100 args = {
101 "conn_username": "DDIC",
102 "conn_password": "Test1234",
103 "host": "10.1.8.9",
104 "snote_path": "/user/sap/trans/temp/000123456_00.tx"
105 }
106
107 with self.assertRaises(AnsibleFailJson) as result:
108 with set_module_args(args):
109 self.module.main()
110 self.assertEqual(result.exception.args[0]['msg'], 'User C(DDIC) or C(SAP*) not allowed for this operation.')
111
112 def test_success_absent(self):
113 """test absent execute snote"""
114
115 args = {
116 "conn_username": "ADMIN",
117 "conn_password": "Test1234",
118 "host": "10.1.8.9",
119 "state": "absent",
120 "snote_path": "/user/sap/trans/temp/000123456.txt"
121 }
122 with patch.object(self.module, 'call_rfc_method') as call:
123 call.return_value = {'EV_RC': 0}
124 with self.assertRaises(AnsibleExitJson) as result:
125 with patch.object(self.module, 'check_implementation') as check:
126 check.side_effect = [True, False]
127 with set_module_args(args):
128 self.module.main()
129 self.assertEqual(result.exception.args[0]['msg'], 'SNOTE "000123456" deimplemented.')
130
131 def test_success_absent_snot_only(self):
132 """test absent execute snote"""
133
134 args = {
135 "conn_username": "ADMIN",
136 "conn_password": "Test1234",
137 "host": "10.1.8.9",
138 "state": "absent",
139 "snote": "000123456"
140 }
141 with patch.object(self.module, 'call_rfc_method') as call:
142 call.return_value = {'EV_RC': 0}
143 with self.assertRaises(AnsibleExitJson) as result:
144 with patch.object(self.module, 'check_implementation') as check:
145 check.side_effect = [True, False]
146 with set_module_args(args):
147 self.module.main()
148 self.assertEqual(result.exception.args[0]['msg'], 'SNOTE "000123456" deimplemented.')
149
150 def test_nothing_to_do(self):
151 """test nothing to do"""
152
153 args = {
154 "conn_username": "ADMIN",
155 "conn_password": "Test1234",
156 "host": "10.1.8.9",
157 "state": "present",
158 "snote_path": "/user/sap/trans/temp/000123456.txt"
159 }
160 with patch.object(self.module, 'check_implementation') as check:
161 check.return_value = True
162 with self.assertRaises(AnsibleExitJson) as result:
163 with set_module_args(args):
164 self.module.main()
165 self.assertEqual(result.exception.args[0]['msg'], 'Nothing to do.')
166
167 def test_success_present_with_copy(self):
168 """test present execute snote"""
169
170 args = {
171 "conn_username": "ADMIN",
172 "conn_password": "Test1234",
173 "host": "10.1.8.9",
174 "state": "present",
175 "snote_path": "/user/sap/trans/temp/000123456.txt"
176 }
177 with patch.object(self.module, 'call_rfc_method') as call:
178 call.return_value = {'EV_RC': 0}
179 with self.assertRaises(AnsibleExitJson) as result:
180 with patch.object(self.module, 'check_implementation') as check:
181 check.side_effect = [False, True]
182 with patch.object(self.module, 'call_rfc_method') as callrfc:
183 callrfc.side_effect = [{'EV_RC': 0}, {'EV_RC': 0}, {'ET_MANUAL_ACTIVITIES': ''}]
184 with set_module_args(args):
185 self.module.main()
186 self.assertEqual(result.exception.args[0]['msg'], 'SNOTE "000123456" implemented.')
187
188 def test_success_present_implement_only(self):
189 """test present implement snote"""
190
191 args = {
192 "conn_username": "ADMIN",
193 "conn_password": "Test1234",
194 "host": "10.1.8.9",
195 "state": "present",
196 "snote": "000123456"
197 }
198 with patch.object(self.module, 'call_rfc_method') as call:
199 call.return_value = {'EV_RC': 0}
200 with self.assertRaises(AnsibleExitJson) as result:
201 with patch.object(self.module, 'check_implementation') as check:
202 check.side_effect = [False, True]
203 with patch.object(self.module, 'call_rfc_method') as callrfc:
204 callrfc.side_effect = [{'EV_RC': 0}, {'ET_MANUAL_ACTIVITIES': ''}]
205 with set_module_args(args):
206 self.module.main()
207 self.assertEqual(result.exception.args[0]['msg'], 'SNOTE "000123456" implemented.')