Browse Source

Merge "Make tripleo_shell_script more robust"

tags/0.2.0
Zuul 3 months ago
committed by Gerrit Code Review
parent
commit
c15ee2a93b
2 changed files with 63 additions and 2 deletions
  1. +4
    -2
      plugins/modules/tripleo_shell_script.py
  2. +59
    -0
      tests/plugins/modules/test_tripleo_shell_script.py

+ 4
- 2
plugins/modules/tripleo_shell_script.py View File

@@ -114,8 +114,10 @@ class TripleoShellScript(object):
with open(dest, 'w') as fh:
fh.write(_SHELL_HEADER)
for k, v in shell_environment.items():
fh.write("export %(key)s=%(val)s\n" % {'key': k,
'val': quote(v)})
if v:
val = quote(str(v))
fh.write("export %(key)s=%(val)s\n" % {'key': k,
'val': val})
fh.write(shell_command)
fh.write("\n")
os.chmod(dest, 0o755)


+ 59
- 0
tests/plugins/modules/test_tripleo_shell_script.py View File

@@ -73,6 +73,65 @@ class TestTripleoShellScript(tests_base.TestCase):
mock_chmod.assert_called_once_with('/tmo/foo.sh', 0o755)
mock_exit_json.assert_called_once_with(changed=True)

@mock.patch('os.chmod')
def test_run_env_avoid_none(self, mock_chmod):
mock_module = mock.Mock()
mock_exit_json = mock.Mock()
mock_open = mock.mock_open()
mock_module.exit_json = mock_exit_json
params = {'dest': '/tmo/foo.sh',
'shell_command': 'foo',
'shell_environment': {
'OS_CLOUD': 'undercloud',
'FOO_BAR': None}
}
mock_module.params = params
results = {}

with mock.patch('plugins.modules.tripleo_shell_script.open',
mock_open):
tripleo_shell_script.TripleoShellScript(mock_module, results)

mock_calls = [
mock.call().write(tripleo_shell_script._SHELL_HEADER),
mock.call().write('export OS_CLOUD=undercloud\n'),
mock.call().write('foo'),
mock.call().write("\n")
]
mock_open.assert_has_calls(mock_calls)
mock_chmod.assert_called_once_with('/tmo/foo.sh', 0o755)
mock_exit_json.assert_called_once_with(changed=True)

@mock.patch('os.chmod')
def test_run_env_quote_int(self, mock_chmod):
mock_module = mock.Mock()
mock_exit_json = mock.Mock()
mock_open = mock.mock_open()
mock_module.exit_json = mock_exit_json
params = {'dest': '/tmo/foo.sh',
'shell_command': 'foo',
'shell_environment': {
'OS_CLOUD': 'undercloud',
'FOO_BAR': 1}
}
mock_module.params = params
results = {}

with mock.patch('plugins.modules.tripleo_shell_script.open',
mock_open):
tripleo_shell_script.TripleoShellScript(mock_module, results)

mock_calls = [
mock.call().write(tripleo_shell_script._SHELL_HEADER),
mock.call().write('export OS_CLOUD=undercloud\n'),
mock.call().write('export FOO_BAR=1\n'),
mock.call().write('foo'),
mock.call().write("\n")
]
mock_open.assert_has_calls(mock_calls)
mock_chmod.assert_called_once_with('/tmo/foo.sh', 0o755)
mock_exit_json.assert_called_once_with(changed=True)

@mock.patch('os.chmod')
def test_run_env_quoted(self, mock_chmod):
mock_module = mock.Mock()


Loading…
Cancel
Save