Skip to content

Commit

Permalink
Merge pull request #61 from Simranpal/feature/improve-pydbapi-extraction
Browse files Browse the repository at this point in the history
Feature/improve pydbapi extraction
  • Loading branch information
Simranpal authored May 12, 2020
2 parents a6120b7 + ab7b978 commit 3b95304
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
6 changes: 6 additions & 0 deletions salt-shaptools.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Fri May 8 23:37:02 UTC 2020 - Simranpal Singh <[email protected]>

- Version 0.3.5
* Add support to pass extra tar options

-------------------------------------------------------------------
Fri Mar 27 18:06:32 UTC 2020 - Simranpal Singh <[email protected]>

Expand Down
2 changes: 1 addition & 1 deletion salt-shaptools.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# See also https://en.opensuse.org/openSUSE:Specfile_guidelines

Name: salt-shaptools
Version: 0.3.4
Version: 0.3.5
Release: 0
Summary: Salt modules and states for SAP Applications and SLE-HA components management

Expand Down
13 changes: 11 additions & 2 deletions salt/modules/hanamod.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,8 @@ def extract_pydbapi(
name,
software_folders,
output_dir,
hana_version='20'):
hana_version='20',
additional_extract_options=None):
'''
Extract HANA pydbapi python client from the provided software folders
Expand All @@ -982,14 +983,22 @@ def extract_pydbapi(
standard way in SAP landscape
output_dir
Folder where the package is extracted
additional_extract_options
Additional options to pass to the tar extraction command
'''
if not isinstance(software_folders, list):
raise TypeError(
"software_folders must be a list, not {} type".format(type(software_folders).__name__)
)
current_platform = hana.HanaInstance.get_platform()
tar_options_str = ('{} -xvf'.format(additional_extract_options)
if additional_extract_options else '-xvf')
hana_client_pattern = re.compile('^HDB_CLIENT:{}.*:{}:.*'.format(
hana_version, current_platform))
try:
hana_client_folder = _find_sap_folder(software_folders, hana_client_pattern)
except SapFolderNotFoundError:
raise exceptions.CommandExecutionError('HANA client not found')
pydbapi_file = '{}/client/{}'.format(hana_client_folder, name)
__salt__['archive.tar'](options='xvf', tarfile=pydbapi_file, dest=output_dir)
__salt__['archive.tar'](options=tar_options_str, tarfile=pydbapi_file, cwd=output_dir)
return pydbapi_file
12 changes: 10 additions & 2 deletions salt/states/hanamod.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ def pydbapi_extracted(
software_folders,
output_dir,
hana_version='20',
force=False):
force=False,
additional_extract_options=None):
'''
Extract HANA pydbapi python client from the provided software folders
Expand All @@ -776,6 +777,8 @@ def pydbapi_extracted(
Folder where the package is extracted
force
Force new extraction if the file already is extracted
additional_extract_options
Additional options to pass to the tar extraction command
'''

ret = {'name': name,
Expand All @@ -799,7 +802,12 @@ def pydbapi_extracted(
__salt__['file.mkdir'](output_dir)

try:
client = __salt__['hana.extract_pydbapi'](name, software_folders, output_dir, hana_version)
client = __salt__['hana.extract_pydbapi'](
name,
software_folders,
output_dir,
hana_version,
additional_extract_options)
except exceptions.CommandExecutionError as err:
ret['comment'] = six.text_type(err)
return ret
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/modules/test_hanamod.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,13 +892,13 @@ def test_extract_pydbapi(self, mock_get_platform, mock_find_sap_folders, mock_co
mock_tar = MagicMock()
with patch.dict(hanamod.__salt__, {'archive.tar': mock_tar}):
pydbapi_file = hanamod.extract_pydbapi(
'PYDBAPI.tar.gz', ['1234', '5678'], '/tmp/output')
'PYDBAPI.tar.gz', ['1234', '5678'], '/tmp/output', additional_extract_options='-l')

mock_compile.assert_called_once_with('^HDB_CLIENT:20.*:LINUX_X86_64:.*')
mock_find_sap_folders.assert_called_once_with(
['1234', '5678'], compile_mocked)
mock_tar.assert_called_once_with(
options='xvf', tarfile='my_folder/client/PYDBAPI.tar.gz', dest='/tmp/output')
options='-l -xvf', tarfile='my_folder/client/PYDBAPI.tar.gz', cwd='/tmp/output')
assert pydbapi_file == 'my_folder/client/PYDBAPI.tar.gz'

@mock.patch('re.compile')
Expand All @@ -911,9 +911,17 @@ def test_extract_pydbapi_error(self, mock_get_platform, mock_find_sap_folders, m
mock_find_sap_folders.side_effect = hanamod.SapFolderNotFoundError
with pytest.raises(exceptions.CommandExecutionError) as err:
pydbapi_file = hanamod.extract_pydbapi(
'PYDBAPI.tar.gz', ['1234', '5678'], '/tmp/output')
'PYDBAPI.tar.gz', ['1234', '5678'], '/tmp/output', additional_extract_options='-l')

mock_compile.assert_called_once_with('^HDB_CLIENT:20.*:LINUX_X86_64:.*')
mock_find_sap_folders.assert_called_once_with(
['1234', '5678'], compile_mocked)
assert 'HANA client not found' in str(err.value)

def test_extract_pydbapi_software_folders_type_error(self):
software_folders = '1234'
with pytest.raises(TypeError) as err:
pydbapi_file = hanamod.extract_pydbapi(
'PYDBAPI.tar.gz', software_folders, '/tmp/output', additional_extract_options='-l')
assert 'software_folders must be a list, not {} type'.format(
type(software_folders).__name__) in str(err.value)
12 changes: 7 additions & 5 deletions tests/unit/states/test_hanamod.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_installed_config_file(self):
'cp.get_file': mock_cp,
'file.move': mock_mv,
'hana.create_conf_file': mock_create_xml,
'hana.update_hdb_pwd_file':mock_update_xml,
'hana.update_hdb_pwd_file': mock_update_xml,
'hana.update_conf_file': mock_update,
'hana.install': mock_install,
'file.remove': mock_remove}):
Expand Down Expand Up @@ -1079,11 +1079,12 @@ def test_pydbapi_extracted_error(self):
with patch.dict(hanamod.__salt__, {'file.mkdir': mock_mkdir,
'hana.extract_pydbapi': mock_extract_pydbapi}):
assert hanamod.pydbapi_extracted(
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output', force=True) == ret
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output',
additional_extract_options='-l', force=True) == ret

mock_mkdir.assert_called_once_with('/tmp/output')
mock_extract_pydbapi.assert_called_once_with(
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output', '20')
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output', '20', '-l')

def test_pydbapi_extracted_correct(self):
ret = {'name': 'PYDBAPI.tar',
Expand All @@ -1097,8 +1098,9 @@ def test_pydbapi_extracted_correct(self):
with patch.dict(hanamod.__salt__, {'file.mkdir': mock_mkdir,
'hana.extract_pydbapi': mock_extract_pydbapi}):
assert hanamod.pydbapi_extracted(
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output', force=True) == ret
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output',
force=True, additional_extract_options='-l') == ret

mock_mkdir.assert_called_once_with('/tmp/output')
mock_extract_pydbapi.assert_called_once_with(
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output', '20')
'PYDBAPI.tar', ['1234', '5678'], '/tmp/output', '20', '-l')

0 comments on commit 3b95304

Please sign in to comment.