當前位置: 首頁>>代碼示例>>Python>>正文


Python RackspaceCtrl.find_storage_image_names方法代碼示例

本文整理匯總了Python中gns3.cloud.rackspace_ctrl.RackspaceCtrl.find_storage_image_names方法的典型用法代碼示例。如果您正苦於以下問題:Python RackspaceCtrl.find_storage_image_names方法的具體用法?Python RackspaceCtrl.find_storage_image_names怎麽用?Python RackspaceCtrl.find_storage_image_names使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在gns3.cloud.rackspace_ctrl.RackspaceCtrl的用法示例。


在下文中一共展示了RackspaceCtrl.find_storage_image_names方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestRackspaceCtrl

# 需要導入模塊: from gns3.cloud.rackspace_ctrl import RackspaceCtrl [as 別名]
# 或者: from gns3.cloud.rackspace_ctrl.RackspaceCtrl import find_storage_image_names [as 別名]

#.........這裏部分代碼省略.........
            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)

        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)

        file_object.download.assert_called_once_with(test_file.name)

    def test_find_storage_image_names(self):
        self.ctrl.storage_driver = mock.MagicMock()
        mock_container = mock.MagicMock()
        self.ctrl.storage_driver.get_container = mock.MagicMock(return_value=mock_container)
        mock_container.list_objects = mock.MagicMock(
            return_value=[
                MockStorageObject("images/IOS/test_image_1.image"),
                MockStorageObject("images/IOS/test_image_1.image.md5"),
                MockStorageObject("images/IOS/test_image_2.img"),
                MockStorageObject("images/IOS/test_image_2.img.md5"),
                MockStorageObject("images/IOS/test_image_3.img"),
                MockStorageObject("projects/project1.zip"),
                MockStorageObject("test_image_1.image"),
            ]
        )

        image_names = self.ctrl.find_storage_image_names(["test_image_1.image", "test_image_2.img"])

        self.assertDictEqual(
            image_names,
            {"test_image_1.image": "images/IOS/test_image_1.image", "test_image_2.img": "images/IOS/test_image_2.img"},
        )

    def test_find_storage_image_names__missing(self):
        self.ctrl.storage_driver = mock.MagicMock()
        mock_container = mock.MagicMock()
        self.ctrl.storage_driver.get_container = mock.MagicMock(return_value=mock_container)
        mock_container.list_objects = mock.MagicMock(
            return_value=[
                MockStorageObject("images/IOS/test_image_1.image"),
                MockStorageObject("images/IOS/test_image_1.image.md5"),
                MockStorageObject("images/IOS/test_image_2.img.md5"),
                MockStorageObject("projects/project1.zip"),
                MockStorageObject("test_image_1.image"),
            ]
        )

        self.assertRaises(Exception, self.ctrl.find_storage_image_names, ["test_image_1.image", "test_image_2.img"])
開發者ID:planctechnologies,項目名稱:gns3-gui,代碼行數:104,代碼來源:test_cloud_unit.py


注:本文中的gns3.cloud.rackspace_ctrl.RackspaceCtrl.find_storage_image_names方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。