]> git.apps.os.sepia.ceph.com Git - ceph-ansible.git/commitdiff
module_utils: don't add newline to the data
authorDimitri Savineau <dsavinea@redhat.com>
Thu, 14 Jan 2021 02:11:39 +0000 (21:11 -0500)
committerDimitri Savineau <savineau.dimitri@gmail.com>
Mon, 18 Jan 2021 16:29:30 +0000 (11:29 -0500)
When executing a command via the run_command method and passing some
data with stdin then the default behavior is to add append a newline.
This breaks the value of password used by our modules.

Signed-off-by: Dimitri Savineau <dsavinea@redhat.com>
module_utils/ca_common.py
tests/module_utils/test_ca_common.py

index 0a70a661cf574708179fb23d3c8e27de928862b9..8c75c4cec90d7bdee9aeaeea0b92cfd6c399008f 100644 (file)
@@ -76,7 +76,10 @@ def exec_command(module, cmd, stdin=None):
     Execute command(s)
     '''
 
-    rc, out, err = module.run_command(cmd, data=stdin)
+    binary_data = False
+    if stdin:
+        binary_data = True
+    rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data)
 
     return rc, cmd, out, err
 
index 23c8508573300da77009489395270e0ba2e4d615..2d0ea5ccf2cd49b46a606423ffcd38ab1bdc75e2 100644 (file)
@@ -1,4 +1,4 @@
-from mock.mock import patch
+from mock.mock import patch, MagicMock
 import os
 import ca_common
 import pytest
@@ -128,3 +128,17 @@ class TestCommon(object):
         ])
         result = ca_common.generate_ceph_cmd(sub_cmd, args, user='client.foo', container_image=image)
         assert result == expected_cmd
+
+    @pytest.mark.parametrize('stdin', [None, 'foo'])
+    def test_exec_command(self, stdin):
+        fake_module = MagicMock()
+        rc = 0
+        stderr = ''
+        stdout = 'ceph version 1.2.3'
+        fake_module.run_command.return_value = 0, stdout, stderr
+        expected_cmd = [self.fake_binary, '--version']
+        _rc, _cmd, _out, _err = ca_common.exec_command(fake_module, expected_cmd, stdin=stdin)
+        assert _rc == rc
+        assert _cmd == expected_cmd
+        assert _err == stderr
+        assert _out == stdout