def test_upload_folder_with_files(self): client = self.client up = client.upload_file client.upload_file = Mock() client.connection.head_container = Mock() client.connection.put_container = Mock() cont_name = utils.random_name() cont = client.create_container(cont_name) gobj = client.get_object client.get_object = Mock(return_value=self.fake_object) safu = client._should_abort_folder_upload client._should_abort_folder_upload = Mock(return_value=False) upprog = client._update_progress client._update_progress = Mock() num_files = 10 fake_upload_key = "abcd" with utils.SelfDeletingTempDirectory() as tmpdir: for idx in xrange(num_files): nm = "file%s" % idx pth = os.path.join(tmpdir, nm) open(pth, "w").write("test") uploader = FakeFolderUploader(tmpdir, cont, "", fake_upload_key, client) # Note that the fake moved the actual run() code to a # different method uploader.actual_run() self.assertEqual(client.upload_file.call_count, num_files) client.get_object = gobj client.upload_file = up client._should_abort_folder_upload = safu client._update_progress = upprog
def test_sync_folder_to_container_nested(self): clt = self.client up = clt.upload_file clt.upload_file = Mock() clt.connection.head_container = Mock() clt.connection.put_container = Mock() clt.get_container_objects = Mock(return_value=[]) cont_name = utils.random_name(8) cont = clt.create_container(cont_name) num_files = 3 num_nested_files = 6 num_all_files = num_files + num_nested_files with utils.SelfDeletingTempDirectory() as tmpdir: for idx in xrange(num_files): nm = "file%s" % idx pth = os.path.join(tmpdir, nm) open(pth, "w").write("test") nested_folder = os.path.join(tmpdir, "nested") os.mkdir(nested_folder) for idx in xrange(num_nested_files): nm = "file%s" % idx pth = os.path.join(nested_folder, nm) open(pth, "w").write("test") clt.sync_folder_to_container(tmpdir, cont) self.assertEqual(clt.upload_file.call_count, num_all_files) clt.upload_file = up
def test_sync_folder_to_container_hidden(self): clt = self.client up = clt.upload_file clt.upload_file = Mock() clt.connection.head_container = Mock() clt.connection.put_container = Mock() clt.connection.head_object = Mock(return_value=fake_attdict) clt.get_container_objects = Mock(return_value=[]) cont_name = utils.random_name(8) cont = clt.create_container(cont_name) num_vis_files = 4 num_hid_files = 4 num_all_files = num_vis_files + num_hid_files with utils.SelfDeletingTempDirectory() as tmpdir: for idx in xrange(num_vis_files): nm = "file%s" % idx pth = os.path.join(tmpdir, nm) open(pth, "w").write("test") for idx in xrange(num_hid_files): nm = ".file%s" % idx pth = os.path.join(tmpdir, nm) open(pth, "w").write("test") clt.sync_folder_to_container(tmpdir, cont, include_hidden=True) self.assertEqual(clt.upload_file.call_count, num_all_files) clt.upload_file = up
def test_self_deleting_temp_directory(self): with utils.SelfDeletingTempDirectory() as tmp: self.assert_(isinstance(tmp, basestring)) self.assert_(os.path.exists(tmp)) self.assert_(os.path.isdir(tmp)) # Directory shoud be deleted after exiting the block self.assertFalse(os.path.exists(tmp))
def test_folder_size_no_ignore(self): with utils.SelfDeletingTempDirectory() as tmpdir: # write 5 files of 100 bytes each for idx in xrange(5): pth = os.path.join(tmpdir, "test%s" % idx) with open(pth, "w") as ff: ff.write(FAKE_CONTENT) fsize = utils.folder_size(tmpdir) self.assertEqual(fsize, 500)
def test_download_object(self): client = self.client sav_fetch = client.fetch_object client.fetch_object = Mock(return_value=utils.random_name( ascii_only=True)) sav_isdir = os.path.isdir os.path.isdir = Mock(return_value=True) nm = "one/two/three/four.txt" with utils.SelfDeletingTempDirectory() as tmpdir: fullpath = os.path.join(tmpdir, nm) client.download_object("fake", nm, tmpdir, structure=True) self.assertTrue(os.path.exists(fullpath)) with utils.SelfDeletingTempDirectory() as tmpdir: fullpath = os.path.join(tmpdir, os.path.basename(nm)) client.download_object("fake", nm, tmpdir, structure=False) self.assertTrue(os.path.exists(fullpath)) client.fetch_object = sav_fetch os.path.isdir = sav_isdir
def test_folder_size_no_ignore(self): with utils.SelfDeletingTempDirectory() as tmpdir: # write 10 files of 100 bytes each content = "x" * 100 for idx in xrange(10): pth = os.path.join(tmpdir, "test%s" % idx) with open(pth, "w") as ff: ff.write(content) fsize = utils.folder_size(tmpdir) self.assertEqual(fsize, 1000)
def test_folder_size_ignore_string(self): with utils.SelfDeletingTempDirectory() as tmpdir: # write 5 files of 100 bytes each for idx in six.moves.range(5): pth = os.path.join(tmpdir, "test%s" % idx) with open(pth, "w") as ff: ff.write(FAKE_CONTENT) # ignore one file fsize = utils.folder_size(tmpdir, ignore="*2") self.assertEqual(fsize, 400)
def test_folder_size_ignore_list(self): with utils.SelfDeletingTempDirectory() as tmpdir: # write 5 files of 100 bytes each for idx in xrange(5): pth = os.path.join(tmpdir, "test%s" % idx) with open(pth, "w") as ff: ff.write(FAKE_CONTENT) # ignore odd files ignore = ["*1", "*3"] fsize = utils.folder_size(tmpdir, ignore=ignore) self.assertEqual(fsize, 300)
def test_folder_size_ignore_list(self): with utils.SelfDeletingTempDirectory() as tmpdir: # write 10 files of 100 bytes each content = "x" * 100 for idx in xrange(10): pth = os.path.join(tmpdir, "test%s" % idx) with open(pth, "w") as ff: ff.write(content) # ignore odd files ignore = ["*1", "*3", "*5", "*7", "*9"] fsize = utils.folder_size(tmpdir, ignore=ignore) self.assertEqual(fsize, 500)
import pyrax import pyrax.exceptions as exc import pyrax.utils as utils pyrax.set_setting("identity_type", "rackspace") creds_file = os.path.expanduser("~/.rackspace_cloud_credentials") pyrax.set_credential_file(creds_file) cf = pyrax.cloudfiles cont_name = pyrax.utils.random_name(8) cont = cf.create_container(cont_name) # pyrax has a utility for creating temporary local directories that clean # themselves up. with utils.SelfDeletingTempDirectory() as tmpfolder: # Create a bunch of files for idx in xrange(13): fname = "file_%s" % idx pth = os.path.join(tmpfolder, fname) with open(pth, "w") as tmp: tmp.write("This is some text") # Create a subfolder. It will be deleted automatically as part of # the cleanup of SelfDeletingTempDirectory. subfolder_path = os.path.join(tmpfolder, "subfolder") os.mkdir(subfolder_path) # Create some files in the subfolder, too. for idx in xrange(7): fname = "subfile_%s" % idx pth = os.path.join(subfolder_path, fname) with open(pth, "w") as tmp: