def setUp(self): """Set things up for running tests.""" super(PosixTest, self).setUp() self.po = PosixOperations() # mock the sanity check, to ease testing self.po._sanity_check = lambda x: x
class PosixTest(TestCase): """Tests for vsc.filesystems.posix""" def setUp(self): """Set things up for running tests.""" super(PosixTest, self).setUp() self.po = PosixOperations() # mock the sanity check, to ease testing self.po._sanity_check = lambda x: x def test_is_dir(self): """Tests for is_dir method.""" self.assertEqual(self.po.is_dir(os.environ['HOME']), True) self.assertEqual(self.po.is_dir('/no/such/dir'), False)
def __init__(self, user_id, storage=None, pickle_storage=None, rest_client=None, account=None, pubkeys=None, host_institute=None, use_user_cache=False): """ Initialisation. @type vsc_user_id: string representing the user's VSC ID (vsc[0-9]{5}) """ super(VscTier2AccountpageUser, self).__init__(user_id, rest_client, account=account, pubkeys=pubkeys, use_user_cache=use_user_cache) # Move to vsc-config? default_pickle_storage = { GENT: VSC_SCRATCH_KYUKON, BRUSSEL: VSC_SCRATCH_THEIA, } if host_institute is None: host_institute = GENT self.host_institute = host_institute if pickle_storage is None: pickle_storage = default_pickle_storage[host_institute] self.pickle_storage = pickle_storage if storage is None: storage = VscStorage() self.institute_path_templates = storage.path_templates[ self.host_institute] self.institute_storage = storage[self.host_institute] self.vsc = VSC() self.gpfs = GpfsOperations() # Only used when needed self.posix = PosixOperations()
def __init__(self, user_id, storage=None, pickle_storage='VSC_SCRATCH_KYUKON', rest_client=None, account=None, pubkeys=None, host_institute=None, use_user_cache=False): """ Initialisation. @type vsc_user_id: string representing the user's VSC ID (vsc[0-9]{5}) """ super(VscTier2AccountpageUser, self).__init__(user_id, rest_client, account=account, pubkeys=pubkeys, use_user_cache=use_user_cache) self.pickle_storage = pickle_storage if not storage: self.storage = VscStorage() else: self.storage = storage self.vsc = VSC() self.gpfs = GpfsOperations() # Only used when needed self.posix = PosixOperations() self.host_institute = host_institute
class PosixTest(TestCase): """Tests for vsc.filesystem.posix""" def setUp(self): """Set things up for running tests.""" super(PosixTest, self).setUp() self.po = PosixOperations() # mock the sanity check, to ease testing self.po._sanity_check = lambda x: x def test_is_dir(self): """Tests for is_dir method.""" self.assertEqual(self.po.is_dir(os.environ['HOME']), True) self.assertEqual(self.po.is_dir('/no/such/dir'), False) @mock.patch('vsc.filesystem.open') @mock.patch('vsc.filesystem.posix.os.path.exists') @mock.patch('vsc.filesystem.posix.os.path.islink') @mock.patch('vsc.filesystem.posix.os.path.realpath') @mock.patch('vsc.filesystem.posix.os.unlink') def test__deploy_dot_file(self, mock_unlink, mock_realpath, mock_islink, mock_exists, mock_open): """Test for the _deploy_dot_file method""" (handle, path) = tempfile.mkstemp() mock_realpath.return_value = path # if branch mock_exists.return_value = True self.po._deploy_dot_file(path, "test_tempfile", "vsc40075", ["huppel"]) mock_islink.assert_not_called() mock_unlink.assert_not_called() mock_realpath.assert_not_called() # else + if branch mock_exists.reset_mock() mock_islink.reset_mock() mock_unlink.reset_mock() mock_open.reset_mock() mock_realpath.reset_mock() mock_exists.return_value = False mock_islink.return_value = True self.po._deploy_dot_file(path, "test_tempfile", "vsc40075", ["huppel"]) mock_unlink.assert_called_with(path) mock_realpath.assert_called_with(path) # else + else branch mock_exists.reset_mock() mock_islink.reset_mock() mock_unlink.reset_mock() mock_open.reset_mock() mock_realpath.reset_mock() mock_exists.return_value = False mock_islink.return_value = False self.po._deploy_dot_file(path, "test_tempfile", "vsc40075", ["huppel"]) mock_realpath.assert_not_called() mock_unlink.assert_not_called() os.close(handle)
class PosixTest(TestCase): """Tests for vsc.filesystem.posix""" def setUp(self): """Set things up for running tests.""" super(PosixTest, self).setUp() self.po = PosixOperations() # mock the sanity check, to ease testing self.po._sanity_check = lambda x: x def test_is_dir(self): """Tests for is_dir method.""" self.assertEqual(self.po.is_dir(os.environ['HOME']), True) self.assertEqual(self.po.is_dir('/no/such/dir'), False) @mock.patch('vsc.filesystem.open') @mock.patch('vsc.filesystem.posix.os.path.exists') @mock.patch('vsc.filesystem.posix.os.path.islink') @mock.patch('vsc.filesystem.posix.os.path.realpath') @mock.patch('vsc.filesystem.posix.os.unlink') def test__deploy_dot_file(self, mock_unlink, mock_realpath, mock_islink, mock_exists, mock_open): """Test for the _deploy_dot_file method""" (handle, path) = tempfile.mkstemp() mock_realpath.return_value = path # if branch mock_exists.return_value = True self.po._deploy_dot_file(path, "test_tempfile", "vsc40075", ["huppel"]) mock_islink.assert_not_called() mock_unlink.assert_not_called() mock_realpath.assert_not_called() # else + if branch mock_exists.reset_mock() mock_islink.reset_mock() mock_unlink.reset_mock() mock_open.reset_mock() mock_realpath.reset_mock() mock_exists.return_value = False mock_islink.return_value = True self.po._deploy_dot_file(path, "test_tempfile", "vsc40075", ["huppel"]) mock_unlink.assert_called_with(path) mock_realpath.assert_called_with(path) # else + else branch mock_exists.reset_mock() mock_islink.reset_mock() mock_unlink.reset_mock() mock_open.reset_mock() mock_realpath.reset_mock() mock_exists.return_value = False mock_islink.return_value = False self.po._deploy_dot_file(path, "test_tempfile", "vsc40075", ["huppel"]) mock_realpath.assert_not_called() mock_unlink.assert_not_called() os.close(handle) @mock.patch('vsc.filesystem.posix.os.stat') @mock.patch('vsc.filesystem.posix.os.makedirs') @mock.patch('vsc.filesystem.posix.os.chown') @mock.patch('vsc.filesystem.posix.os.chmod') def test_create_stat_dir_new(self, mock_chmod, mock_chown, mock_makedirs, mock_os_stat): """ Test to see what happens if the dir already exists """ mock_os_stat.side_effect = OSError('dir not found') mock_chmod.result_value = True mock_chown.result_value = True mock_makedirs.result_value = None test_uid = 2048 test_gid = 4096 test_path = '/tmp/test' test_permissions = 0o711 self.po.create_stat_directory(test_path, test_permissions, test_uid, test_gid, False) mock_os_stat.assert_called_with(test_path) mock_makedirs.assert_called_with(test_path) mock_chmod.assert_called_with(test_path, test_permissions) mock_chown.assert_called_with(test_path, test_uid, test_gid) @mock.patch('vsc.filesystem.posix.os.stat') @mock.patch('vsc.filesystem.posix.os.makedirs') @mock.patch('vsc.filesystem.posix.os.chown') @mock.patch('vsc.filesystem.posix.os.chmod') def test_create_stat_dir_existing_no_override_same_id(self, mock_chmod, mock_chown, mock_makedirs, mock_os_stat): """ Test to see what happens if the dir already exists """ test_uid = 2048 test_gid = 4096 test_path = '/tmp/test' test_permissions = 0o711 Statinfo = namedtuple("Statinfo", ["st_uid", "st_gid"]) mock_os_stat.result_value = Statinfo(test_uid, test_gid) self.po.create_stat_directory(test_path, test_permissions, test_uid, test_gid, False) mock_os_stat.assert_called_with(test_path) mock_makedirs.assert_not_called mock_chmod.assert_not_called @mock.patch('vsc.filesystem.posix.os.stat') @mock.patch('vsc.filesystem.posix.os.makedirs') @mock.patch('vsc.filesystem.posix.os.chown') @mock.patch('vsc.filesystem.posix.os.chmod') def test_create_stat_dir_existing_no_override_diff_uid(self, mock_chmod, mock_chown, mock_makedirs, mock_os_stat): """ Test to see what happens if the dir already exists """ test_uid = 2048 test_gid = 4096 test_path = '/tmp/test' test_permissions = 0o711 Statinfo = namedtuple("Statinfo", ["st_uid", "st_gid"]) mock_os_stat.result_value = Statinfo(test_uid + 1, test_gid) self.po.create_stat_directory(test_path, test_permissions, test_uid, test_gid, False) mock_os_stat.assert_called_with(test_path) mock_makedirs.assert_not_called mock_chown.assert_called_with(test_path, test_uid, test_gid) @mock.patch('vsc.filesystem.posix.os.stat') @mock.patch('vsc.filesystem.posix.os.makedirs') @mock.patch('vsc.filesystem.posix.os.chown') @mock.patch('vsc.filesystem.posix.os.chmod') def test_create_stat_dir_existing_no_override_diff_gid(self, mock_chmod, mock_chown, mock_makedirs, mock_os_stat): """ Test to see what happens if the dir already exists """ test_uid = 2048 test_gid = 4096 test_path = '/tmp/test' test_permissions = 0o711 Statinfo = namedtuple("Statinfo", ["st_uid", "st_gid"]) mock_os_stat.result_value = Statinfo(test_uid, test_gid+1) self.po.create_stat_directory(test_path, test_permissions, test_uid, test_gid, False) mock_os_stat.assert_called_with(test_path) mock_makedirs.assert_not_called mock_chown.assert_called_with(test_path, test_uid, test_gid) @mock.patch('vsc.filesystem.posix.os.stat') @mock.patch('stat.S_IMODE') @mock.patch('vsc.filesystem.posix.os.makedirs') @mock.patch('vsc.filesystem.posix.os.chown') @mock.patch('vsc.filesystem.posix.os.chmod') def test_create_stat_dir_existing_override(self, mock_chmod, mock_chown, mock_makedirs, mock_stat_s_imode, mock_os_stat): """ Test to see what happens if the dir already exists """ test_uid = 2048 test_gid = 4096 test_path = '/tmp/test' test_permissions = 0o711 Statinfo = namedtuple("Statinfo", ["st_uid", "st_gid"]) mock_os_stat.result_value = Statinfo(test_uid, test_gid) mock_stat_s_imode.return_value = 0o755 self.po.create_stat_directory(test_path, test_permissions, test_uid, test_gid, True) mock_os_stat.assert_called_with(test_path) mock_makedirs.assert_not_called mock_chmod.assert_called_with(test_path, test_permissions)