mvadkert / rpms / ansible

Forked from rpms/ansible 6 years ago
Clone
Blob Blame History Raw
From 212a1b305da847798557c73b24417a8615c0245e Mon Sep 17 00:00:00 2001
From: Toshio Kuratomi <a.badger@gmail.com>
Date: Tue, 28 Mar 2017 16:21:06 -0700
Subject: [PATCH] Fix for tests run with no .ssh user dir

When building in automated build systems, there are sometimes cases
where the user doing the building does not have a .ssh directory.  In
this case, we need to mock out some os.path functions so that the
add_host_key() function we're testing won't complain or try to create
one.
---
 test/units/module_utils/test_known_hosts.py | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/test/units/module_utils/test_known_hosts.py b/test/units/module_utils/test_known_hosts.py
index 0bc8e55..afd93fc 100644
--- a/test/units/module_utils/test_known_hosts.py
+++ b/test/units/module_utils/test_known_hosts.py
@@ -16,12 +16,13 @@
 # You should have received a copy of the GNU General Public License
 # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 
-from ansible.compat.tests import unittest
-from ansible.module_utils import known_hosts
-
 import json
+import os.path
+
 import ansible.module_utils.basic
-from ansible.compat.tests.mock import Mock
+from ansible.compat.tests import unittest
+from ansible.compat.tests.mock import Mock, patch
+from ansible.module_utils import known_hosts
 from units.mock.procenv import swap_stdin_and_argv
 
 
@@ -91,15 +92,21 @@ class TestAnsibleModuleKnownHosts(unittest.TestCase):
             ansible.module_utils.basic._ANSIBLE_ARGS = None
             self.module = ansible.module_utils.basic.AnsibleModule(argument_spec=dict())
 
+            get_bin_path = Mock()
+            get_bin_path.return_value = keyscan_cmd = "/custom/path/ssh-keyscan"
+            self.module.get_bin_path = get_bin_path
+
             run_command = Mock()
             run_command.return_value = (0, "Needs output, otherwise thinks ssh-keyscan timed out'", "")
             self.module.run_command = run_command
 
-            get_bin_path = Mock()
-            get_bin_path.return_value = keyscan_cmd = "/custom/path/ssh-keyscan"
-            self.module.get_bin_path = get_bin_path
+            append_to_file = Mock()
+            append_to_file.return_value = (None,)
+            self.module.append_to_file = append_to_file
 
-            for u in self.urls:
-                if self.urls[u]['is_ssh_url']:
-                    known_hosts.add_host_key(self.module, self.urls[u]['get_fqdn'], port=self.urls[u]['port'])
-                    run_command.assert_called_with(keyscan_cmd + self.urls[u]['add_host_key_cmd'])
+            with patch('os.path.isdir', return_value=True):
+                with patch('os.path.exists', return_value=True):
+                    for u in self.urls:
+                        if self.urls[u]['is_ssh_url']:
+                            known_hosts.add_host_key(self.module, self.urls[u]['get_fqdn'], port=self.urls[u]['port'])
+                            run_command.assert_called_with(keyscan_cmd + self.urls[u]['add_host_key_cmd'])
-- 
2.9.3