Debian Codegraph @

ansible_collections/community/sap_libs/tests/unit/plugins/modules/test_sapcar_extract.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 from ansible_collections.community.sap_libs.plugins.modules import sapcar_extract
24 from ansible_collections.community.sap_libs.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
25 from unittest.mock import patch
26 from ansible.module_utils import basic
27
28
29 def get_bin_path(*args, **kwargs):
30 """Function to return path of SAPCAR"""
31 return "/tmp/sapcar"
32
33
34 class Testsapcar_extract(ModuleTestCase):
35 """Main class for testing sapcar_extract module."""
36
37 def setUp(self):
38 """Setup."""
39 super(Testsapcar_extract, self).setUp()
40 self.module = sapcar_extract
41 self.mock_get_bin_path = patch.object(basic.AnsibleModule, 'get_bin_path', get_bin_path)
42 self.mock_get_bin_path.start()
43 self.addCleanup(self.mock_get_bin_path.stop) # ensure that the patching is 'undone'
44
45 def tearDown(self):
46 """Teardown."""
47 super(Testsapcar_extract, self).tearDown()
48
49 def test_without_required_parameters(self):
50 """Failure must occurs when all parameters are missing."""
51 with self.assertRaises(AnsibleFailJson):
52 with set_module_args({}):
53 self.module.main()
54
55 def test_sapcar_extract(self):
56 """Check that result is changed."""
57 args = {
58 'path': "/tmp/HANA_CLIENT_REV2_00_053_00_LINUX_X86_64.SAR",
59 'dest': "/tmp/test2",
60 'binary_path': "/tmp/sapcar"
61 }
62 with patch('os.path.isfile', return_value=True), \
63 patch('os.access', return_value=True), \
64 patch('os.path.exists', return_value=True), \
65 patch('os.listdir', return_value=[]), \
66 patch.object(basic.AnsibleModule, 'run_command') as run_command:
67 run_command.return_value = 0, 'file1\nfile2', ''
68 with self.assertRaises(AnsibleExitJson) as result:
69 with set_module_args(args):
70 sapcar_extract.main()
71 self.assertTrue(result.exception.args[0]['changed'])
72 self.assertGreaterEqual(run_command.call_count, 1)
73
74 def test_file_missing(self):
75 """Failure must occur when SAR file does not exist."""
76 args = {
77 'path': "/tmp/nonexistent.SAR",
78 'dest': "/tmp/test2",
79 'binary_path': "/tmp/sapcar"
80 }
81 with patch('os.path.isfile', return_value=False):
82 with self.assertRaises(AnsibleFailJson) as result:
83 with set_module_args(args):
84 sapcar_extract.main()
85 self.assertIn('File missing', result.exception.args[0]['msg'])