Debian Codegraph @

ansible_collections/community/sap_libs/tests/unit/plugins/modules/test_sap_pyrfc.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
22 __metaclass__ = type
23
24 import sys
25 from unittest.mock import patch, MagicMock
26 from ansible_collections.community.sap_libs.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
27
28 sys.modules['pyrfc'] = MagicMock()
29 sys.modules['pyrfc.Connection'] = MagicMock()
30 from ansible_collections.community.sap_libs.plugins.modules import sap_pyrfc
31
32
33 class TestSAPRfcModule(ModuleTestCase):
34
35 def setUp(self):
36 super(TestSAPRfcModule, self).setUp()
37 self.module = sap_pyrfc
38
39 def tearDown(self):
40 super(TestSAPRfcModule, self).tearDown()
41
42 def test_without_required_parameters(self):
43 """Failure must occurs when all parameters are missing"""
44 with self.assertRaises(AnsibleFailJson):
45 with set_module_args({}):
46 self.module.main()
47
48 def test_error_module_not_found(self):
49 """tests fail module error"""
50
51 args = {
52 "function": "STFC_CONNECTION",
53 "parameters": {"REQUTEXT": "Hello SAP!"},
54 "connection": {"ashost": "s4hana.poc.cloud",
55 "sysnr": "01",
56 "client": "400",
57 "user": "DDIC",
58 "passwd": "Password1",
59 "lang": "EN"}
60 }
61
62 with self.assertRaises(AnsibleFailJson) as result:
63 self.module.HAS_PYRFC_LIBRARY = False
64 self.module.PYRFC_LIBRARY_IMPORT_ERROR = 'Module not found'
65 with set_module_args(args):
66 self.module.main()
67 self.assertEqual(
68 result.exception.args[0]['exception'], 'Module not found')
69
70 def test_success_communication(self):
71 """tests success"""
72 args = {
73 "function": "STFC_CONNECTION",
74 "parameters": {"REQUTEXT": "Hello SAP!"},
75 "connection": {"ashost": "s4hana.poc.cloud",
76 "sysnr": "01",
77 "client": "400",
78 "user": "DDIC",
79 "passwd": "Password1",
80 "lang": "EN"}
81 }
82 with patch.object(self.module, 'get_connection') as patch_call:
83 patch_call.call.return_value = 'Patched'
84 with self.assertRaises(AnsibleExitJson) as result:
85 with set_module_args(args):
86 self.module.main()
87 self.assertEqual(result.exception.args[0]['changed'], True)