def test_set_ownership(self, getgrnam, getpwnam, chown, read_link): vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = '/scratch/dir' fake_passwd = namedtuple('fake_passwd', 'pw_uid') getpwnam.return_value(fake_passwd(pw_uid=100)) fake_group = namedtuple('fake_group', 'gr_gid') getgrnam.return_value(fake_group(gr_gid=101)) vfs.set_ownership('/some/file', 'fred', None) read_link.assert_called() chown.assert_called_with(read_link.return_value, uid=getpwnam.return_value.pw_uid) read_link.reset_mock() chown.reset_mock() vfs.set_ownership('/some/file', None, 'users') read_link.assert_called() chown.assert_called_with(read_link.return_value, gid=getgrnam.return_value.gr_gid) read_link.reset_mock() chown.reset_mock() vfs.set_ownership('/some/file', 'joe', 'admins') read_link.assert_called() chown.assert_called_with(read_link.return_value, uid=getpwnam.return_value.pw_uid, gid=getgrnam.return_value.gr_gid)
def test_set_permissions(self, chmod, read_link): vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = '/scratch/dir' vfs.set_permissions('/some/file', 0o777) read_link.assert_called() chmod.assert_called_with(read_link.return_value, 0o777)
def test_set_permissions(self): global commands, files commands = [] files = {} self.stubs.Set(processutils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" vfs.read_file("/some/file") vfs.set_permissions("/some/file", 0o777) self.assertEqual(files["/scratch/dir/some/file"]["mode"], 0o777) root_helper = nova.utils._get_root_helper() self.assertEqual(commands, [{'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('cat', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('chmod', '777', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}])
def test_read_file(self, read_file, read_link): vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = '/scratch/dir' self.assertEqual(read_file.return_value, vfs.read_file('/some/file')) read_link.assert_called() read_file.assert_called()
def test_get_format_fs(self, mock_type): vfs = vfsimpl.VFSLocalFS(self.rawfile) vfs.setup = mock.MagicMock() vfs.teardown = mock.MagicMock() def fake_setup(): vfs.mount = mock.MagicMock() vfs.mount.device = None vfs.mount.get_dev.side_effect = fake_get_dev def fake_teardown(): vfs.mount.device = None def fake_get_dev(): vfs.mount.device = '/dev/xyz' return True vfs.setup.side_effect = fake_setup vfs.teardown.side_effect = fake_teardown vfs.setup() self.assertEqual('ext3', vfs.get_image_fs()) vfs.teardown() vfs.mount.get_dev.assert_called_once_with() mock_type.assert_called_once_with('/dev/xyz')
def test_replace_file(self): global files, commands files = {} commands = [] self.stubs.Set(utils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" vfs.replace_file("/some/file", "Goodbye") self.assertTrue("/scratch/dir/some/file" in files) self.assertEquals(files["/scratch/dir/some/file"]["content"], "Goodbye") self.assertEqual(commands, [{ 'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True } }, { 'args': ('tee', '/scratch/dir/some/file'), 'kwargs': { 'process_input': 'Goodbye', 'run_as_root': True } }])
def test_append_file(self): global files, commands files = {} commands = [] self.stubs.Set(processutils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" vfs.append_file("/some/file", " Goodbye") self.assertIn("/scratch/dir/some/file", files) self.assertEqual(files["/scratch/dir/some/file"]["content"], "Hello World Goodbye") root_helper = nova.utils._get_root_helper() self.assertEqual(commands, [{'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('tee', '-a', '/scratch/dir/some/file'), 'kwargs': {'process_input': ' Goodbye', 'run_as_root': True, 'root_helper': root_helper}}])
def test_check_safe_path(self): if tests_utils.is_osx(): self.skipTest("Unable to test on OSX") vfs = vfsimpl.VFSLocalFS("dummy.img") vfs.imgdir = "/foo" ret = vfs._canonical_path('etc/something.conf') self.assertEquals(ret, '/foo/etc/something.conf')
def test_check_unsafe_path(self): if tests_utils.is_osx(): self.skipTest("Unable to test on OSX") vfs = vfsimpl.VFSLocalFS("dummy.img") vfs.imgdir = "/foo" self.assertRaises(exception.Invalid, vfs._canonical_path, 'etc/../../../something.conf')
def test_replace_file(self): global files, commands files = {} commands = [] self.stub_out('oslo_concurrency.processutils.execute', fake_execute) vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = "/scratch/dir" vfs.replace_file("/some/file", "Goodbye") self.assertIn("/scratch/dir/some/file", files) self.assertEqual(files["/scratch/dir/some/file"]["content"], "Goodbye") root_helper = nova.utils.get_root_helper() self.assertEqual(commands, [{ 'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('tee', '/scratch/dir/some/file'), 'kwargs': { 'process_input': 'Goodbye', 'run_as_root': True, 'root_helper': root_helper } }])
def test_has_file(self): global commands, files files = {} commands = [] self.stubs.Set(utils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" vfs.read_file("/some/file") self.assertTrue(vfs.has_file("/some/file")) self.assertFalse(vfs.has_file("/other/file")) self.assertEqual(commands, [{'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True}}, {'args': ('cat', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True}}, {'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True}}, {'args': ('readlink', '-e', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True}}, {'args': ('readlink', '-nm', '/scratch/dir/other/file'), 'kwargs': {'run_as_root': True}}, {'args': ('readlink', '-e', '/scratch/dir/other/file'), 'kwargs': {'run_as_root': True}}, ])
def test_makepath(self): global dirs, commands dirs = [] commands = [] self.stubs.Set(processutils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" vfs.make_path("/some/dir") vfs.make_path("/other/dir") self.assertEqual(dirs, ["/scratch/dir/some/dir", "/scratch/dir/other/dir"]), root_helper = nova.utils._get_root_helper() self.assertEqual(commands, [{'args': ('readlink', '-nm', '/scratch/dir/some/dir'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('mkdir', '-p', '/scratch/dir/some/dir'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('readlink', '-nm', '/scratch/dir/other/dir'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('mkdir', '-p', '/scratch/dir/other/dir'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}])
def test_check_safe_path(self): if not tests_utils.coreutils_readlink_available(): self.skipTest("coreutils readlink(1) unavailable") vfs = vfsimpl.VFSLocalFS("dummy.img") vfs.imgdir = "/foo" ret = vfs._canonical_path('etc/something.conf') self.assertEqual(ret, '/foo/etc/something.conf')
def test_get_format_fs(self, execute): vfs = vfsimpl.VFSLocalFS(self.rawfile) vfs.setup = mock.MagicMock() vfs.teardown = mock.MagicMock() def fake_setup(): vfs.mount = mock.MagicMock() vfs.mount.device = None vfs.mount.get_dev.side_effect = fake_get_dev def fake_teardown(): vfs.mount.device = None def fake_get_dev(): vfs.mount.device = '/dev/xyz' return True vfs.setup.side_effect = fake_setup vfs.teardown.side_effect = fake_teardown execute.return_value = ('ext3\n', '') vfs.setup() self.assertEqual('ext3', vfs.get_image_fs()) vfs.teardown() vfs.mount.get_dev.assert_called_once_with() execute.assert_called_once_with('blkid', '-o', 'value', '-s', 'TYPE', '/dev/xyz', run_as_root=True, check_exit_code=[0, 2])
def test_check_unsafe_path(self): if not tests_utils.coreutils_readlink_available(): self.skipTest("coreutils readlink(1) unavailable") vfs = vfsimpl.VFSLocalFS(self.rawfile) vfs.imgdir = "/foo" self.assertRaises(exception.Invalid, vfs._canonical_path, 'etc/../../../something.conf')
def test_check_unsafe_path(self, read_link): vfs = vfsimpl.VFSLocalFS(self.rawfile) vfs.imgdir = '/foo' read_link.return_value = '/etc/something.conf' self.assertRaises(exception.Invalid, vfs._canonical_path, 'etc/../../../something.conf')
def test_check_safe_path(self, read_link): vfs = vfsimpl.VFSLocalFS(self.rawfile) vfs.imgdir = '/foo' read_link.return_value = '/foo/etc/something.conf' ret = vfs._canonical_path('etc/something.conf') self.assertEqual(ret, '/foo/etc/something.conf')
def test_replace_file(self, write_file, read_link): vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = '/scratch/dir' vfs.replace_file('/some/file', 'Goodbye') read_link.assert_called() write_file.assert_called_with(read_link.return_value, 'w', 'Goodbye')
def test_has_file(self): global commands, files files = {} commands = [] self.stubs.Set(processutils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = "/scratch/dir" vfs.read_file("/some/file") self.assertTrue(vfs.has_file("/some/file")) self.assertFalse(vfs.has_file("/other/file")) root_helper = nova.utils._get_root_helper() self.assertEqual(commands, [ { 'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('cat', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('readlink', '-e', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('readlink', '-nm', '/scratch/dir/other/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('readlink', '-e', '/scratch/dir/other/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, ])
def test_set_ownership(self): global commands, files commands = [] files = {} self.stubs.Set(processutils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" vfs.read_file("/some/file") self.assertEqual(files["/scratch/dir/some/file"]["uid"], 100) self.assertEqual(files["/scratch/dir/some/file"]["gid"], 100) vfs.set_ownership("/some/file", "fred", None) self.assertEqual(files["/scratch/dir/some/file"]["uid"], 105) self.assertEqual(files["/scratch/dir/some/file"]["gid"], 100) vfs.set_ownership("/some/file", None, "users") self.assertEqual(files["/scratch/dir/some/file"]["uid"], 105) self.assertEqual(files["/scratch/dir/some/file"]["gid"], 500) vfs.set_ownership("/some/file", "joe", "admins") self.assertEqual(files["/scratch/dir/some/file"]["uid"], 110) self.assertEqual(files["/scratch/dir/some/file"]["gid"], 600) root_helper = nova.utils._get_root_helper() self.assertEqual(commands, [{'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('cat', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('chown', 'fred', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('chgrp', 'users', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('chown', 'joe:admins', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}])
def test_setup_mount_false(self, NbdMount, mkdtemp): vfs = vfsimpl.VFSLocalFS(self.qcowfile) mounter = mock.MagicMock() mkdtemp.return_value = 'tmp/' NbdMount.return_value = mounter vfs.setup(mount=False) self.assertTrue(mkdtemp.called) NbdMount.assert_called_once_with(self.qcowfile, "tmp/", None) self.assertFalse(mounter.do_mount.called)
def test_setup_mount_false(self, NbdMount, mkdtemp): vfs = vfsimpl.VFSLocalFS("img.qcow2", imgfmt='qcow2') mounter = mock.MagicMock() mkdtemp.return_value = 'tmp/' NbdMount.return_value = mounter vfs.setup(mount=False) self.assertTrue(mkdtemp.called) NbdMount.assert_called_once_with('img.qcow2', 'tmp/', None) self.assertFalse(mounter.do_mount.called)
def test_makepath(self, mkdir, read_link): vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = '/scratch/dir' vfs.make_path('/some/dir') read_link.assert_called() mkdir.assert_called_with(read_link.return_value) read_link.reset_mock() mkdir.reset_mock() vfs.make_path('/other/dir') read_link.assert_called() mkdir.assert_called_with(read_link.return_value)
def test_read_file(self): global commands, files files = {} commands = [] self.stubs.Set(processutils, 'execute', fake_execute) vfs = vfsimpl.VFSLocalFS(imgfile="/dummy.qcow2", imgfmt="qcow2") vfs.imgdir = "/scratch/dir" self.assertEqual(vfs.read_file("/some/file"), "Hello World") root_helper = nova.utils._get_root_helper() self.assertEqual(commands, [{'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}, {'args': ('cat', '/scratch/dir/some/file'), 'kwargs': {'run_as_root': True, 'root_helper': root_helper}}])
def test_makepath(self): global dirs, commands dirs = [] commands = [] self.stub_out('oslo_concurrency.processutils.execute', fake_execute) vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = "/scratch/dir" vfs.make_path("/some/dir") vfs.make_path("/other/dir") self.assertEqual(dirs, ["/scratch/dir/some/dir", "/scratch/dir/other/dir"]), root_helper = nova.utils.get_root_helper() self.assertEqual(commands, [{ 'args': ('readlink', '-nm', '/scratch/dir/some/dir'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('mkdir', '-p', '/scratch/dir/some/dir'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('readlink', '-nm', '/scratch/dir/other/dir'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('mkdir', '-p', '/scratch/dir/other/dir'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }])
def test_read_file(self): global commands, files files = {} commands = [] self.stub_out('oslo_concurrency.processutils.execute', fake_execute) vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = "/scratch/dir" self.assertEqual(vfs.read_file("/some/file"), "Hello World") root_helper = nova.utils.get_root_helper() self.assertEqual(commands, [{ 'args': ('readlink', '-nm', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }, { 'args': ('cat', '/scratch/dir/some/file'), 'kwargs': { 'run_as_root': True, 'root_helper': root_helper } }])
def test_has_file(self, exists): vfs = vfsimpl.VFSLocalFS(self.qcowfile) vfs.imgdir = '/scratch/dir' has = vfs.has_file('/some/file') self.assertEqual(exists.return_value, has)