本文整理汇总了Python中gns3.cloud.rackspace_ctrl.RackspaceCtrl.download_file方法的典型用法代码示例。如果您正苦于以下问题:Python RackspaceCtrl.download_file方法的具体用法?Python RackspaceCtrl.download_file怎么用?Python RackspaceCtrl.download_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gns3.cloud.rackspace_ctrl.RackspaceCtrl
的用法示例。
在下文中一共展示了RackspaceCtrl.download_file方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestRackspaceCtrl
# 需要导入模块: from gns3.cloud.rackspace_ctrl import RackspaceCtrl [as 别名]
# 或者: from gns3.cloud.rackspace_ctrl.RackspaceCtrl import download_file [as 别名]
#.........这里部分代码省略.........
self.assertTrue("member_id" in r_images2[0])
self.assertTrue("member_id" in r_images2[1])
self.assertEqual(r_images[0]["member_id"], r_images2[0]["member_id"])
self.assertEqual(r_images[1]["member_id"], r_images2[1]["member_id"])
self.assertTrue("status" in r_images2[0])
self.assertTrue("status" in r_images2[1])
self.assertEqual(r_images2[0]["status"], "ALREADYREQUESTED")
self.assertEqual(r_images2[1]["status"], "ALREADYREQUESTED")
print("Done.")
def test_upload_file(self):
try:
test_data = "abcdefg"
test_file = tempfile.NamedTemporaryFile(mode="w")
with test_file.file as f:
f.write(test_data)
return_value = self.ctrl.upload_file(test_file.name, "test_folder/test_file.txt")
container = self.ctrl.storage_driver.get_container(self.ctrl.GNS3_CONTAINER_NAME)
file_object = container.get_object("test_folder/test_file.txt")
hash_object = container.get_object("test_folder/test_file.txt.md5")
cloud_file_hash = ""
for chunk in hash_object.as_stream():
cloud_file_hash += chunk.decode("utf8")
cloud_file_contents = ""
for chunk in file_object.as_stream():
cloud_file_contents += chunk.decode("utf8")
self.assertEqual(cloud_file_hash, hashlib.md5(test_data.encode("utf8")).hexdigest())
self.assertEqual(cloud_file_contents, test_data)
self.assertEqual(return_value, True)
finally:
self._delete_container()
def test_list_projects(self):
container = self.ctrl.storage_driver.create_container(self.ctrl.GNS3_CONTAINER_NAME)
try:
container.upload_object_via_stream(StringIO("abcd"), "projects/project1.gns3.zip")
container.upload_object_via_stream(StringIO("abcd"), "projects/project1.gns3.zip.md5")
container.upload_object_via_stream(StringIO("abcd"), "projects/project2.gns3.zip")
container.upload_object_via_stream(StringIO("abcd"), "some_file.txt")
container.upload_object_via_stream(StringIO("abcd"), "some_file2.zip")
projects = self.ctrl.list_projects()
self.assertDictEqual(
projects, {"project1.gns3": "projects/project1.gns3.zip", "project2.gns3": "projects/project2.gns3.zip"}
)
finally:
self._delete_container()
def test_download_file(self):
container = self.ctrl.storage_driver.create_container(self.ctrl.GNS3_CONTAINER_NAME)
try:
test_data = b"abcdef"
container.upload_object_via_stream(BytesIO(test_data), "projects/project1.gns3.zip")
downloaded_data = self.ctrl.download_file("projects/project1.gns3.zip")
self.assertEqual(downloaded_data.read(), test_data)
finally:
self._delete_container()
def test_download_file__to_filesystem(self):
container = self.ctrl.storage_driver.create_container(self.ctrl.GNS3_CONTAINER_NAME)
try:
test_data = b"abcd"
container.upload_object_via_stream(BytesIO(test_data), "projects/project1.gns3.zip")
self.ctrl.download_file("projects/project1.gns3.zip", "downloaded_file.zip")
with open("downloaded_file.zip", "rb") as f:
self.assertEqual(f.read(), test_data)
finally:
self._delete_container()
os.remove("downloaded_file.zip")
def test_delete_file(self):
container = self.ctrl.storage_driver.create_container(self.ctrl.GNS3_CONTAINER_NAME)
try:
container.upload_object_via_stream(StringIO("abcde"), "projects/project1.gns3.zip")
container.upload_object_via_stream(StringIO("1234"), "projects/project1.gns3.zip.md5")
self.ctrl.delete_file("projects/project1.gns3.zip")
self.assertRaises(ObjectDoesNotExistError, container.get_object, "projects/project1.gns3.zip")
self.assertRaises(ObjectDoesNotExistError, container.get_object, "projects/project1.gns3.zip.md5")
finally:
self._delete_container()
示例2: TestRackspaceCtrl
# 需要导入模块: from gns3.cloud.rackspace_ctrl import RackspaceCtrl [as 别名]
# 或者: from gns3.cloud.rackspace_ctrl.RackspaceCtrl import download_file [as 别名]
#.........这里部分代码省略.........
return_value = self.ctrl.upload_file(test_file.name, "test_folder/test.txt")
upload_file_args = self.ctrl.storage_driver.upload_object_via_stream.call_args_list[0]
upload_hash_args = self.ctrl.storage_driver.upload_object_via_stream.call_args_list[1]
self.assertEqual(upload_file_args[0][2], "test_folder/test.txt")
self.assertEqual(upload_hash_args[0][2], "test_folder/test.txt.md5")
self.assertEqual(upload_file_args[0][0].name, test_file.name)
self.assertEqual(upload_hash_args[0][0].read(), test_data_hash)
self.assertEqual(return_value, True)
def test_upload_file__exists(self):
test_data = b"abcdef"
test_data_hash = hashlib.md5(test_data).hexdigest()
test_file = tempfile.NamedTemporaryFile()
with test_file.file as f:
f.write(test_data)
self.ctrl.storage_driver = mock.MagicMock()
mock_container = mock.MagicMock()
mock_container.list_objects = mock.MagicMock(
return_value=[MockStorageObject("test_folder/test.txt"), MockStorageObject("test_folder/test.txt.md5")]
)
self.ctrl.storage_driver.create_container = mock.MagicMock(return_value=mock_container)
mock_container.get_object = mock.MagicMock(return_value=MockStorageObject("", bytes(test_data_hash, "utf8")))
return_value = self.ctrl.upload_file(test_file.name, "test_folder/test.txt")
self.assertFalse(self.ctrl.storage_driver.upload_object_via_stream.called)
self.assertEqual(return_value, False)
def test_download_file__exists__same_hash(self):
test_data = b"abcdefghi"
test_data_hash = hashlib.md5(test_data).hexdigest()
test_file = tempfile.NamedTemporaryFile()
with test_file.file as f:
f.write(test_data)
self.ctrl.storage_driver = mock.MagicMock()
mock_container = mock.MagicMock()
self.ctrl.storage_driver.get_container = mock.MagicMock(return_value=mock_container)
file_object = MockStorageObject("test_file.txt")
file_object.download = mock.MagicMock()
file_hash_object = MockStorageObject("test_file.txt", bytes(test_data_hash, "utf8"))
mock_container.get_object = lambda name: {"test_file.txt": file_object, "test_file.txt.md5": file_hash_object}[
name
]
self.ctrl.download_file("test_file.txt", test_file.name)
self.assertFalse(file_object.download.called)
def test_download_file__exists__different_hash(self):
test_data = b"abcdefghij"
test_data_hash = "some_garbage_hash"
test_file = tempfile.NamedTemporaryFile()
with test_file.file as f:
f.write(test_data)
self.ctrl.storage_driver = mock.MagicMock()
mock_container = mock.MagicMock()
self.ctrl.storage_driver.get_container = mock.MagicMock(return_value=mock_container)