# License for the specific language governing permissions and limitations
# under the License.
-
import mock
import paramiko
import uuid
class SSHPoolTestCase(test.TestCase):
"""Unit test for SSH Connection Pool."""
+ @mock.patch('cinder.ssh_utils.CONF')
+ @mock.patch('six.moves.builtins.open')
+ @mock.patch('paramiko.SSHClient')
+ @mock.patch('os.path.isfile', return_value=True)
+ def test_sshpool_remove(self, mock_isfile, mock_sshclient, mock_open,
+ mock_conf):
+ ssh_to_remove = mock.MagicMock()
+ mock_sshclient.side_effect = [mock.MagicMock(),
+ ssh_to_remove, mock.MagicMock()]
+ mock_conf.ssh_hosts_key_file.return_value = 'dummy'
+ sshpool = ssh_utils.SSHPool("127.0.0.1", 22, 10,
+ "test",
+ password="test",
+ min_size=3,
+ max_size=3)
+ self.assertIn(ssh_to_remove, list(sshpool.free_items))
+ sshpool.remove(ssh_to_remove)
+ self.assertNotIn(ssh_to_remove, list(sshpool.free_items))
+
+ @mock.patch('cinder.ssh_utils.CONF')
+ @mock.patch('six.moves.builtins.open')
+ @mock.patch('paramiko.SSHClient')
+ @mock.patch('os.path.isfile', return_value=True)
+ def test_sshpool_remove_object_not_in_pool(self, mock_isfile,
+ mock_sshclient, mock_open,
+ mock_conf):
+ # create an SSH Client that is not a part of sshpool.
+ ssh_to_remove = mock.MagicMock()
+ mock_sshclient.side_effect = [mock.MagicMock(), mock.MagicMock()]
+ mock_conf.ssh_hosts_key_file.return_value = 'dummy'
+ sshpool = ssh_utils.SSHPool("127.0.0.1", 22, 10,
+ "test",
+ password="test",
+ min_size=2,
+ max_size=2)
+ listBefore = list(sshpool.free_items)
+ self.assertNotIn(ssh_to_remove, listBefore)
+ sshpool.remove(ssh_to_remove)
+ self.assertEqual(listBefore, list(sshpool.free_items))
+
@mock.patch('cinder.ssh_utils.CONF')
@mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')