本文整理汇总了Python中gns3.cloud.rackspace_ctrl.RackspaceCtrl.upload_file方法的典型用法代码示例。如果您正苦于以下问题:Python RackspaceCtrl.upload_file方法的具体用法?Python RackspaceCtrl.upload_file怎么用?Python RackspaceCtrl.upload_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gns3.cloud.rackspace_ctrl.RackspaceCtrl
的用法示例。
在下文中一共展示了RackspaceCtrl.upload_file方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestRackspaceCtrl
# 需要导入模块: from gns3.cloud.rackspace_ctrl import RackspaceCtrl [as 别名]
# 或者: from gns3.cloud.rackspace_ctrl.RackspaceCtrl import upload_file [as 别名]
#.........这里部分代码省略.........
time.sleep(2)
print("Image created.")
print("Getting shared images...")
r_images = self.ctrl._get_shared_images("user_foo", "ORD", "3.0")
self.assertTrue("image_id" in r_images[0])
self.assertTrue("image_id" in r_images[1])
self.assertTrue("member_id" in r_images[0])
self.assertTrue("member_id" in r_images[1])
self.assertTrue("status" in r_images[0])
self.assertTrue("status" in r_images[1])
self.assertEqual(r_images[0]["status"], "pending")
self.assertEqual(r_images[1]["status"], "pending")
print("Done.")
print("Getting shared images...")
r_images2 = self.ctrl._get_shared_images("user_foo", "ORD", "3.0")
self.assertTrue("image_id" in r_images2[0])
self.assertTrue("image_id" in r_images2[1])
self.assertEqual(r_images[0]["image_id"], r_images2[0]["image_id"])
self.assertEqual(r_images[1]["image_id"], r_images2[1]["image_id"])
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")
示例2: TestRackspaceCtrl
# 需要导入模块: from gns3.cloud.rackspace_ctrl import RackspaceCtrl [as 别名]
# 或者: from gns3.cloud.rackspace_ctrl.RackspaceCtrl import upload_file [as 别名]
class TestRackspaceCtrl(unittest.TestCase):
def setUp(self):
""" Set up the objects used by most of the tests. """
self.ctrl = RackspaceCtrl("valid_user", "valid_api_key", "http://foo.bar:8888")
self.ctrl.post_fn = stub_rackspace_identity_post
self.driver_cls = MockLibCloudDriver
def test_authenticate_valid_user(self):
""" Test authentication with a valid user and api key. """
auth_result = self.ctrl.authenticate()
self.assertEqual(auth_result, True)
self.assertIsNotNone(self.ctrl.token)
def test_authenticate_empty_user(self):
""" Ensure authentication with empty string as username fails. """
ctrl = RackspaceCtrl("", "valid_api_key", "http://foo.bar:8888")
ctrl.post_fn = stub_rackspace_identity_post
auth_result = ctrl.authenticate()
self.assertEqual(auth_result, False)
self.assertIsNone(ctrl.token)
def test_authenticate_empty_apikey(self):
""" Ensure authentication with empty string as api_key fails. """
ctrl = RackspaceCtrl("valid_user", "", "http://foo.bar:8888")
ctrl.post_fn = stub_rackspace_identity_post
auth_result = ctrl.authenticate()
self.assertEqual(auth_result, False)
self.assertIsNone(ctrl.token)
def test_authenticate_invalid_user(self):
""" Ensure authentication with invalid user credentials fails. """
ctrl = RackspaceCtrl("invalid_user", "invalid_api_key", "http://foo.bar:8888")
ctrl.post_fn = stub_rackspace_identity_post
auth_result = ctrl.authenticate()
self.assertEqual(auth_result, False)
self.assertIsNone(ctrl.token)
def test_list_regions(self):
""" Ensure that list_regions returns the correct result. """
self.ctrl.authenticate()
regions = self.ctrl.list_regions()
expected_regions = [{"IAD": "iad"}, {"DFW": "dfw"}, {"SYD": "syd"}, {"ORD": "ord"}]
self.assertCountEqual(regions, expected_regions)
def test_set_region(self):
""" Ensure that set_region sets 'region' and 'driver'. """
self.ctrl.authenticate()
result = self.ctrl.set_region("iad")
self.assertEqual(result, True)
self.assertEqual(self.ctrl.region, "iad")
self.assertIsNotNone(self.ctrl.driver)
def test_set_invalid_region(self):
""" Ensure that calling 'set_region' with an invalid param fails. """
self.ctrl.authenticate()
result = self.ctrl.set_region("invalid")
self.assertEqual(result, False)
self.assertIsNone(self.ctrl.region)
self.assertIsNone(self.ctrl.driver)
def test_token_parsed(self):
""" Ensure that the token is set. """
ctrl = RackspaceCtrl("valid_user", "valid_api_key", "http://foo.bar:8888")
ctrl.post_fn = stub_rackspace_identity_post
ctrl.authenticate()
self.assertEqual("abcdefgh0123456789", ctrl.token)
def test_upload_file(self):
self.ctrl.storage_driver = mock.MagicMock()
mock_container = mock.MagicMock()
mock_container.list_objects = mock.MagicMock(return_value=[])
self.ctrl.storage_driver.create_container = mock.MagicMock(return_value=mock_container)
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)
return_value = self.ctrl.upload_file(test_file.name, "test_folder/test.txt")
#.........这里部分代码省略.........