def test_remove(self): test_file = "test_remove.txt" test_dir = "test_dir/" nested_dir = os.path.join(test_dir, "nested_file/") nested_file = os.path.join(nested_dir, test_file) with pfio.open(test_file, 'w') as fp: fp.write('foobar') # test remove on one file self.assertTrue(pfio.exists(test_file)) pfio.remove(test_file) self.assertFalse(pfio.exists(test_file)) # test remove on directory pfio.makedirs(nested_dir) with pfio.open(nested_file, 'w') as fp: fp.write('foobar') self.assertTrue(pfio.exists(test_dir)) self.assertTrue(pfio.exists(nested_dir)) self.assertTrue(pfio.exists(nested_file)) pfio.remove(test_dir, True) self.assertFalse(pfio.exists(test_dir)) self.assertFalse(pfio.exists(nested_dir)) self.assertFalse(pfio.exists(nested_file))
def test_rename(self): new_tmp_dir = tempfile.TemporaryDirectory() try: src = os.path.join("file://", new_tmp_dir.name, 'src') dst = os.path.join("file://", new_tmp_dir.name, 'dst') with pfio.open(src, 'w') as fp: fp.write('foobar') assert pfio.exists(src) assert not pfio.exists(dst) pfio.rename(src, dst) with pfio.open(dst, 'r') as fp: data = fp.read() assert data == 'foobar' assert not pfio.exists(src) assert pfio.exists(dst) finally: new_tmp_dir.cleanup()
def test_exists(self): non_exist_file = "non_exist_file" self.assertTrue(pfio.exists(self.tmpdir.name)) self.assertFalse(pfio.exists(non_exist_file))
def test_get_principal_name(self): ticket_cache_path = "/tmp/krb5cc_{}".format(os.getuid()) ticket_cache_backup_path = "/tmp/ccbackup_{}".format(getpass.getuser()) # remove the credential cache if pfio.exists(ticket_cache_path): pfio.rename(ticket_cache_path, ticket_cache_backup_path) original_ccname = os.environ.get("KRB5CCNAME") if original_ccname is not None: del os.environ['KRB5CCNAME'] # remove klist original_path = os.environ.get('PATH') if original_path is not None: del os.environ['PATH'] # remove keytab original_krb5_ktname = os.environ.get('KRB5_KTNAME') if original_krb5_ktname is not None: del os.environ['KRB5_KTNAME'] # priority 0: # getting login name when klist, cache and keytab are not available with HdfsFileSystem() as handler: self.assertEqual(getpass.getuser(), handler.username) # restore klist if original_path is not None: os.environ['PATH'] = original_path # priority 0: # getting login name when cache and keytab are not available with HdfsFileSystem() as handler: self.assertEqual(getpass.getuser(), handler.username) # priority 1: # getting keytab username when only cache is not available dummy_username = "******" with tempfile.TemporaryDirectory() as tmpd: # save the original KRB5_KTNAME try: keytab_path = create_dummy_keytab(tmpd, dummy_username) os.environ['KRB5_KTNAME'] = keytab_path with HdfsFileSystem() as handler: self.assertEqual(dummy_username, handler.username) finally: # put KRB5_KTNAME back if original_krb5_ktname is None: del os.environ['KRB5_KTNAME'] else: os.environ['KRB5_KTNAME'] = original_krb5_ktname # restore cache # remove the credential cache if pfio.exists(ticket_cache_backup_path): pfio.rename(ticket_cache_backup_path, ticket_cache_path) if original_ccname is not None: os.environ['KRB5CCNAME'] = original_ccname # priority 2: # getting principal name from cache with HdfsFileSystem() as handler: self.assertEqual(_get_principal_name_from_klist(), handler.username)