From 58507e0a8425ce9ddd0be5a4a9dc5755db432681 Mon Sep 17 00:00:00 2001
From: Ivan Kolodyazhny <e0ne@e0ne.info>
Date: Fri, 30 Jan 2015 19:19:03 +0200
Subject: [PATCH] Fix SSHPoolTestCase to work in parallel

Mock global config for tests in SSHPoolTestCase to make it
workable in a concurrency mode.

Change-Id: Ibf6f881d6dfd945abe9c7433367b0acf5b24722a
Partial-Bug: #1259463
---
 cinder/tests/test_utils.py | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/cinder/tests/test_utils.py b/cinder/tests/test_utils.py
index 7bfccc72b..ecaa497ef 100644
--- a/cinder/tests/test_utils.py
+++ b/cinder/tests/test_utils.py
@@ -1010,13 +1010,15 @@ class FakeTransport(object):
 
 class SSHPoolTestCase(test.TestCase):
     """Unit test for SSH Connection Pool."""
+    @mock.patch('cinder.ssh_utils.CONF')
     @mock.patch('__builtin__.open')
     @mock.patch('paramiko.SSHClient')
     @mock.patch('os.path.isfile', return_value=True)
     def test_ssh_default_hosts_key_file(self, mock_isfile, mock_sshclient,
-                                        mock_open):
+                                        mock_open, mock_conf):
         mock_ssh = mock.MagicMock()
         mock_sshclient.return_value = mock_ssh
+        mock_conf.ssh_hosts_key_file = '/var/lib/cinder/ssh_known_hosts'
 
         # create with customized setting
         sshpool = ssh_utils.SSHPool("127.0.0.1", 22, 10,
@@ -1032,13 +1034,15 @@ class SSHPoolTestCase(test.TestCase):
         mock_ssh.load_host_keys.assert_called_once_with(
             '/var/lib/cinder/ssh_known_hosts')
 
+    @mock.patch('cinder.ssh_utils.CONF')
     @mock.patch('__builtin__.open')
     @mock.patch('paramiko.SSHClient')
     @mock.patch('os.path.isfile', return_value=True)
     def test_ssh_host_key_file_kwargs(self, mock_isfile, mock_sshclient,
-                                      mock_open):
+                                      mock_open, mock_conf):
         mock_ssh = mock.MagicMock()
         mock_sshclient.return_value = mock_ssh
+        mock_conf.ssh_hosts_key_file = '/var/lib/cinder/ssh_known_hosts'
 
         # create with customized setting
         sshpool = ssh_utils.SSHPool("127.0.0.1", 22, 10,
@@ -1059,13 +1063,14 @@ class SSHPoolTestCase(test.TestCase):
 
         mock_ssh.assert_has_calls(expected, any_order=True)
 
+    @mock.patch('cinder.ssh_utils.CONF')
     @mock.patch('__builtin__.open')
     @mock.patch('os.path.isfile', return_value=True)
     @mock.patch('paramiko.RSAKey.from_private_key_file')
     @mock.patch('paramiko.SSHClient')
     def test_single_ssh_connect(self, mock_sshclient, mock_pkey, mock_isfile,
-                                mock_open):
-        CONF.ssh_hosts_key_file = '/var/lib/cinder/ssh_known_hosts'
+                                mock_open, mock_conf):
+        mock_conf.ssh_hosts_key_file = '/var/lib/cinder/ssh_known_hosts'
 
         # create with password
         sshpool = ssh_utils.SSHPool("127.0.0.1", 22, 10,
@@ -1125,12 +1130,14 @@ class SSHPoolTestCase(test.TestCase):
 
         self.assertNotEqual(first_id, third_id)
 
+    @mock.patch('cinder.ssh_utils.CONF')
     @mock.patch('__builtin__.open')
     @mock.patch('paramiko.SSHClient')
-    def test_missing_ssh_hosts_key_config(self, mock_sshclient, mock_open):
+    def test_missing_ssh_hosts_key_config(self, mock_sshclient, mock_open,
+                                          mock_conf):
         mock_sshclient.return_value = FakeSSHClient()
 
-        CONF.ssh_hosts_key_file = None
+        mock_conf.ssh_hosts_key_file = None
         # create with password
         self.assertRaises(exception.ParameterNotFound,
                           ssh_utils.SSHPool,
@@ -1179,6 +1186,9 @@ class SSHPoolTestCase(test.TestCase):
                           min_size=1,
                           max_size=1)
 
+    @mock.patch.multiple('cinder.ssh_utils.CONF',
+                         strict_ssh_host_key_policy=True,
+                         ssh_hosts_key_file='/var/lib/cinder/ssh_known_hosts')
     @mock.patch('__builtin__.open')
     @mock.patch('paramiko.SSHClient')
     @mock.patch('os.path.isfile', return_value=True)
@@ -1186,8 +1196,6 @@ class SSHPoolTestCase(test.TestCase):
                                         mock_open):
         mock_sshclient.return_value = FakeSSHClient()
 
-        CONF.strict_ssh_host_key_policy = True
-
         # create with customized setting
         sshpool = ssh_utils.SSHPool("127.0.0.1", 22, 10,
                                     "test",
-- 
2.45.2