| 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 sap_hdbsql |
| 24 |
from ansible_collections.community.sap_libs.tests.unit.plugins.modules.utils import ( |
| 25 |
AnsibleExitJson, |
| 26 |
AnsibleFailJson, |
| 27 |
ModuleTestCase, |
| 28 |
set_module_args, |
| 29 |
) |
| 30 |
from unittest.mock import patch |
| 31 |
from ansible.module_utils import basic |
| 32 |
|
| 33 |
|
| 34 |
def get_bin_path(*args, **kwargs): |
| 35 |
"""Function to return path of hdbsql""" |
| 36 |
return "/usr/sap/HDB/HDB01/exe/hdbsql" |
| 37 |
|
| 38 |
|
| 39 |
class Testsap_hdbsql(ModuleTestCase): |
| 40 |
"""Main class for testing sap_hdbsql module.""" |
| 41 |
|
| 42 |
def setUp(self): |
| 43 |
"""Setup.""" |
| 44 |
super(Testsap_hdbsql, self).setUp() |
| 45 |
self.module = sap_hdbsql |
| 46 |
self.mock_get_bin_path = patch.object(basic.AnsibleModule, 'get_bin_path', get_bin_path) |
| 47 |
self.mock_get_bin_path.start() |
| 48 |
self.addCleanup(self.mock_get_bin_path.stop) # ensure that the patching is 'undone' |
| 49 |
|
| 50 |
def tearDown(self): |
| 51 |
"""Teardown.""" |
| 52 |
super(Testsap_hdbsql, self).tearDown() |
| 53 |
|
| 54 |
def test_without_required_parameters(self): |
| 55 |
"""Failure must occurs when all parameters are missing.""" |
| 56 |
with self.assertRaises(AnsibleFailJson): |
| 57 |
with set_module_args({}): |
| 58 |
self.module.main() |
| 59 |
|
| 60 |
def test_sap_hdbsql(self): |
| 61 |
"""Check that result is processed.""" |
| 62 |
args = { |
| 63 |
'sid': "HDB", |
| 64 |
'instance': "01", |
| 65 |
'encrypted': False, |
| 66 |
'host': "localhost", |
| 67 |
'user': "SYSTEM", |
| 68 |
'password': "1234Qwer", |
| 69 |
'database': "HDB", |
| 70 |
'query': ["SELECT * FROM users;"] |
| 71 |
} |
| 72 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 73 |
run_command.return_value = 0, 'username,name\n testuser,test user \n myuser, my user \n', '' |
| 74 |
with self.assertRaises(AnsibleExitJson) as result: |
| 75 |
with set_module_args(args): |
| 76 |
sap_hdbsql.main() |
| 77 |
self.assertEqual(result.exception.args[0]['query_result'], [[ |
| 78 |
{'username': 'testuser', 'name': 'test user'}, |
| 79 |
{'username': 'myuser', 'name': 'my user'}, |
| 80 |
]]) |
| 81 |
self.assertEqual(run_command.call_count, 1) |
| 82 |
|
| 83 |
def test_hana_userstore_query(self): |
| 84 |
"""Check that result is processed with userstore.""" |
| 85 |
args = { |
| 86 |
'sid': "HDB", |
| 87 |
'instance': "01", |
| 88 |
'encrypted': False, |
| 89 |
'host': "localhost", |
| 90 |
'user': "SYSTEM", |
| 91 |
'userstore': True, |
| 92 |
'database': "HDB", |
| 93 |
'query': ["SELECT * FROM users;"] |
| 94 |
} |
| 95 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 96 |
run_command.return_value = 0, 'username,name\n testuser,test user \n myuser, my user \n', '' |
| 97 |
with self.assertRaises(AnsibleExitJson) as result: |
| 98 |
with set_module_args(args): |
| 99 |
sap_hdbsql.main() |
| 100 |
self.assertEqual(result.exception.args[0]['query_result'], [[ |
| 101 |
{'username': 'testuser', 'name': 'test user'}, |
| 102 |
{'username': 'myuser', 'name': 'my user'}, |
| 103 |
]]) |
| 104 |
self.assertEqual(run_command.call_count, 1) |
| 105 |
|
| 106 |
def test_hana_failed_no_passwd(self): |
| 107 |
"""Check that result is failed with no password.""" |
| 108 |
with self.assertRaises(AnsibleFailJson): |
| 109 |
args = { |
| 110 |
'sid': "HDB", |
| 111 |
'instance': "01", |
| 112 |
'encrypted': False, |
| 113 |
'host': "localhost", |
| 114 |
'user': "SYSTEM", |
| 115 |
'database': "HDB", |
| 116 |
'query': ["SELECT * FROM users;"] |
| 117 |
} |
| 118 |
with set_module_args(args): |
| 119 |
self.module.main() |
| 120 |
|
| 121 |
def test_changed_status_select(self): |
| 122 |
"""Verify SELECT query returns changed=False""" |
| 123 |
args = { |
| 124 |
'sid': "HDB", |
| 125 |
'instance': "01", |
| 126 |
'password': "pwd", |
| 127 |
'query': ["SELECT * FROM users;"] |
| 128 |
} |
| 129 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 130 |
run_command.return_value = 0, 'col1\nval1', '' |
| 131 |
with self.assertRaises(AnsibleExitJson) as result: |
| 132 |
with set_module_args(args): |
| 133 |
self.module.main() |
| 134 |
self.assertFalse(result.exception.args[0]['changed']) |
| 135 |
|
| 136 |
def test_changed_status_update(self): |
| 137 |
"""Verify UPDATE query returns changed=True""" |
| 138 |
args = { |
| 139 |
'sid': "HDB", |
| 140 |
'instance': "01", |
| 141 |
'password': "pwd", |
| 142 |
'query': ["UPDATE users SET name='test';"] |
| 143 |
} |
| 144 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 145 |
run_command.return_value = 0, '', '' # Updates often return empty string |
| 146 |
with self.assertRaises(AnsibleExitJson) as result: |
| 147 |
with set_module_args(args): |
| 148 |
self.module.main() |
| 149 |
self.assertTrue(result.exception.args[0]['changed']) |
| 150 |
|
| 151 |
def test_authentication_failure(self): |
| 152 |
"""Verify specific error message for Auth Failure""" |
| 153 |
args = { |
| 154 |
'sid': "HDB", |
| 155 |
'instance': "01", |
| 156 |
'password': "wrong", |
| 157 |
'query': ["SELECT 1 FROM DUMMY;"] |
| 158 |
} |
| 159 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 160 |
run_command.return_value = 10, '', 'Authentication failed' |
| 161 |
with self.assertRaises(AnsibleFailJson) as result: |
| 162 |
with set_module_args(args): |
| 163 |
self.module.main() |
| 164 |
self.assertIn("Authentication Failed", result.exception.args[0]['msg']) |
| 165 |
|
| 166 |
def test_insufficient_privilege(self): |
| 167 |
"""Verify specific error message for Privilege Error (Error 258)""" |
| 168 |
args = { |
| 169 |
'sid': "HDB", |
| 170 |
'instance': "01", |
| 171 |
'password': "pwd", |
| 172 |
'query': ["DROP TABLE important_stuff;"] |
| 173 |
} |
| 174 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 175 |
run_command.return_value = 1, '', '* 258: insufficient privilege' |
| 176 |
with self.assertRaises(AnsibleFailJson) as result: |
| 177 |
with set_module_args(args): |
| 178 |
self.module.main() |
| 179 |
self.assertIn("Authorization Error", result.exception.args[0]['msg']) |
| 180 |
|
| 181 |
def test_encrypted_connection_flags(self): |
| 182 |
"""Verify that encryption flags are correctly added to the command""" |
| 183 |
args = { |
| 184 |
'sid': "HDB", |
| 185 |
'instance': "01", |
| 186 |
'password': "pwd", |
| 187 |
'encrypted': True, |
| 188 |
'query': ["SELECT 1 FROM DUMMY;"] |
| 189 |
} |
| 190 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 191 |
run_command.return_value = 0, '1', '' |
| 192 |
with self.assertRaises(AnsibleExitJson): |
| 193 |
with set_module_args(args): |
| 194 |
self.module.main() |
| 195 |
|
| 196 |
# Get the actual command executed |
| 197 |
executed_cmd = run_command.call_args[0][0] |
| 198 |
self.assertIn('-e', executed_cmd) |
| 199 |
self.assertIn('-ssltrustcert', executed_cmd) |
| 200 |
self.assertIn('-sslcreatecert', executed_cmd) |
| 201 |
|
| 202 |
def test_properties_command_args(self): |
| 203 |
"""Verify that properties are correctly added to the command""" |
| 204 |
args = { |
| 205 |
'sid': "HDB", |
| 206 |
'instance': "01", |
| 207 |
'password': "pwd", |
| 208 |
'properties': ["key1=value1", "key2=value2"], |
| 209 |
'query': ["SELECT 1 FROM DUMMY;"] |
| 210 |
} |
| 211 |
with patch.object(basic.AnsibleModule, 'run_command') as run_command: |
| 212 |
run_command.return_value = 0, '1', '' |
| 213 |
with self.assertRaises(AnsibleExitJson): |
| 214 |
with set_module_args(args): |
| 215 |
self.module.main() |
| 216 |
|
| 217 |
executed_cmd = run_command.call_args[0][0] |
| 218 |
self.assertIn('-Z', executed_cmd) |
| 219 |
self.assertIn('key1=value1', executed_cmd) |
| 220 |
self.assertIn('key2=value2', executed_cmd) |