From 921ca671b7c5cb4a8d9f18ffa36085b2150a72fa Mon Sep 17 00:00:00 2001 From: Ivan Kolodyazhny Date: Thu, 28 Jul 2016 14:14:21 +0300 Subject: [PATCH] Mock write and read operations to filesystem Change-Id: I7b9ad7d3632c3ec870016c09359afc9c6b646a21 Closes-Bug: #1607322 --- os_brick/tests/remotefs/test_remotefs.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/os_brick/tests/remotefs/test_remotefs.py b/os_brick/tests/remotefs/test_remotefs.py index 0049e82..36b734f 100644 --- a/os_brick/tests/remotefs/test_remotefs.py +++ b/os_brick/tests/remotefs/test_remotefs.py @@ -16,6 +16,8 @@ import mock import os import tempfile +import six + from os_brick import exception from os_brick.privileged import rootwrap as priv_rootwrap from os_brick.remotefs import remotefs @@ -87,13 +89,22 @@ class RemoteFsClientTestCase(base.TestCase): mount_point = client.get_mount_point(share) vz_conf_dir = os.path.join('/etc/pstorage/clusters/', cluster_name) - tmp_dir = tempfile.mkdtemp() + tmp_dir = '/tmp/fake_dir/' + with mock.patch.object(tempfile, 'mkdtemp', return_value=tmp_dir): - client.mount(share) + mock_open = mock.mock_open() + with mock.patch.object(six.moves.builtins, "open", + mock_open, create=True): + client.mount(share) - saved_mds_list = open(os.path.join(tmp_dir, 'bs_list')).read().split() - self.assertEqual(set(mds_list), set(saved_mds_list)) + write_calls = [mock.call(tmp_dir + 'bs_list', 'w'), + mock.call().__enter__(), + mock.call().write('10.0.0.1\n'), + mock.call().write('10.0.0.2\n'), + mock.call().__exit__(None, None, None)] + + mock_open.assert_has_calls(write_calls) calls = [mock.call('mkdir', '-p', mount_point, check_exit_code=0), mock.call('cp', '-rf', tmp_dir, vz_conf_dir, run_as_root=True, root_helper='true'),