def test_single_datastore(self, mkdir_mock):
        """Test that datastore manager works with a single datastore."""
        hypervisor = MagicMock()
        host_client = MagicMock()

        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            # name, url, type, local
            ["datastore1", "id-1", "VMFS", True],
        ])
        hypervisor.host_client = host_client

        # No valid datastore. One image datastore for cloud VMs.
        ds_list = []
        image_ds = [{"name": "datastore1", "used_for_vms": True}]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(ds_manager.initialized, is_(True))
        assert_that(ds_manager.get_datastore_ids(), is_(["id-1"]))
        assert_that(ds_manager.vm_datastores(), is_([]))
        assert_that(ds_manager.image_datastores(), is_(["id-1"]))

        # No valid datastore. No image datastore for cloud VMs.
        ds_list = ["bad-ds"]
        image_ds = [{"name": "datastore1", "used_for_vms": False}]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(ds_manager.initialized, is_(False))
    def test_nonexistent_datastores(self, mkdir_mock):
        """Test that non-existent datastore get filtered out."""
        host_client = MagicMock()
        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            ["datastore1", "id-1", "VMFS", True],
            ["datastore2", "id-2", "VMFS", True],
            ["datastore3", "id-3", "VMFS", True],
        ])
        hypervisor = MagicMock()
        hypervisor.host_client = host_client

        ds_list = ["datastore1", "bad-datastore1"]
        image_ds = [
            {
                "name": "datastore2",
                "used_for_vms": True
            },
            {
                "name": "datastore3",
                "used_for_vms": False
            },
            {
                "name": "bad-datastores2",
                "used_for_vms": False
            },
        ]
        manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(manager.get_datastore_ids(),
                    contains_inanyorder("id-1", "id-2", "id-3"))
        assert_that(manager.vm_datastores(), is_(["id-1"]))
        assert_that(manager.image_datastores(),
                    contains_inanyorder("id-2", "id-3"))
        assert_that(manager.initialized, is_(True))
    def test_multiple_image_datastores(self, mkdir_mock):
        """Test that datastore manager works with multiple image datastores."""
        host_client = MagicMock()
        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            ["datastore1", "id-1", "VMFS", True],
            ["datastore2", "id-2", "VMFS", True],
            ["datastore3", "id-3", "VMFS", True],
        ])
        hypervisor = MagicMock()
        hypervisor.host_client = host_client

        ds_list = ["datastore1"]
        image_ds = [
            {
                "name": "datastore2",
                "used_for_vms": True
            },
            {
                "name": "datastore3",
                "used_for_vms": False
            },
        ]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(ds_manager.get_datastore_ids(),
                    contains_inanyorder("id-1", "id-2", "id-3"))
        assert_that(ds_manager.vm_datastores(), is_(["id-1"]))
        assert_that(ds_manager.image_datastores(),
                    contains_inanyorder("id-2", "id-3"))
        assert_that(ds_manager.initialized, is_(True))
    def test_single_datastore(self, mkdir_mock):
        """Test that datastore manager works with a single datastore."""
        hypervisor = MagicMock()
        host_client = MagicMock()

        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            # name, url, type, local
            ["datastore1", "id-1", "VMFS", True],
        ])
        hypervisor.host_client = host_client

        # No valid datastore. One image datastore for cloud VMs.
        ds_list = []
        image_ds = [{"name": "datastore1", "used_for_vms": True}]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(ds_manager.initialized, is_(True))
        assert_that(ds_manager.get_datastore_ids(), is_(["id-1"]))
        assert_that(ds_manager.vm_datastores(), is_([]))
        assert_that(ds_manager.image_datastores(), is_(["id-1"]))

        # No valid datastore. No image datastore for cloud VMs.
        ds_list = ["bad-ds"]
        image_ds = [{"name": "datastore1", "used_for_vms": False}]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(ds_manager.initialized, is_(False))
    def test_multiple_image_datastores(self, mkdir_mock):
        """Test that datastore manager works with multiple image datastores."""
        host_client = MagicMock()
        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            ["datastore1", "id-1", "VMFS", True],
            ["datastore2", "id-2", "VMFS", True],
            ["datastore3", "id-3", "VMFS", True],
        ])
        hypervisor = MagicMock()
        hypervisor.host_client = host_client

        ds_list = ["datastore1"]
        image_ds = [
            {"name": "datastore2", "used_for_vms": True},
            {"name": "datastore3", "used_for_vms": False},
        ]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(ds_manager.get_datastore_ids(),
                    contains_inanyorder("id-1", "id-2", "id-3"))
        assert_that(ds_manager.vm_datastores(), is_(["id-1"]))
        assert_that(ds_manager.image_datastores(),
                    contains_inanyorder("id-2", "id-3"))
        assert_that(ds_manager.initialized, is_(True))
    def test_nonexistent_datastores(self, mkdir_mock):
        """Test that non-existent datastore get filtered out."""
        host_client = MagicMock()
        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            ["datastore1", "id-1", "VMFS", True],
            ["datastore2", "id-2", "VMFS", True],
            ["datastore3", "id-3", "VMFS", True],
        ])
        hypervisor = MagicMock()
        hypervisor.host_client = host_client

        ds_list = ["datastore1", "bad-datastore1"]
        image_ds = [
            {"name": "datastore2", "used_for_vms": True},
            {"name": "datastore3", "used_for_vms": False},
            {"name": "bad-datastores2", "used_for_vms": False},
        ]
        manager = DatastoreManager(hypervisor, ds_list, image_ds)
        assert_that(manager.get_datastore_ids(),
                    contains_inanyorder("id-1", "id-2", "id-3"))
        assert_that(manager.vm_datastores(), is_(["id-1"]))
        assert_that(manager.image_datastores(),
                    contains_inanyorder("id-2", "id-3"))
        assert_that(manager.initialized, is_(True))
    def test_get_datastores(self):
        """ Test esx datastore manager with different datastore types.
        Verify the datastore types are correctly parsed and all the
        datastores are populated.
        """
        hypervisor = MagicMock()
        host_client = MagicMock()

        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            # name, url, type, local
            ["datastore1", "id-1", "VMFS", True],
            ["datastore2", "id-2", "VMFS", False],
            ["datastore3", "id-3", "NFS", None],
            ["datastore4", "id-4", "NFSV41", None],
            ["datastore5", "id-5", "vsan", None],
            ["datastore6", "id-6", "VFFS", None],
        ])

        hypervisor.host_client = host_client

        ds_list = ["datastore1", "datastore2", "datastore3",
                   "datastore4", "datastore5", "datastore6"]
        image_ds = [{"name": "datastore2", "used_for_vms": False}]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)

        assert_that(ds_manager.get_datastore_ids(),
                    contains_inanyorder("id-1", "id-2", "id-3", "id-4",
                                        "id-5", "id-6"))

        assert_that(ds_manager.vm_datastores(),
                    contains_inanyorder("id-1", "id-3", "id-4", "id-5",
                                        "id-6"))

        datastores = ds_manager.get_datastores()
        assert_that(datastores, contains_inanyorder(
            Datastore("id-1", "datastore1", type=DSType.LOCAL_VMFS,
                      tags=set([LOCAL_VMFS_TAG])),
            Datastore("id-2", "datastore2", type=DSType.SHARED_VMFS,
                      tags=set([SHARED_VMFS_TAG])),
            Datastore("id-3", "datastore3", type=DSType.NFS_3,
                      tags=set([NFS_TAG])),
            Datastore("id-4", "datastore4", type=DSType.NFS_41,
                      tags=set([NFS_TAG])),
            Datastore("id-5", "datastore5", type=DSType.VSAN, tags=set([VSAN_TAG])),
            Datastore("id-6", "datastore6", type=DSType.OTHER, tags=set())))

        assert_that(ds_manager.image_datastores(), is_(["id-2"]))
        assert_that(ds_manager.datastore_type("id-1"),
                    is_(DSType.LOCAL_VMFS))
        assert_that(ds_manager.datastore_type("id-2"),
                    is_(DSType.SHARED_VMFS))
        assert_that(ds_manager.datastore_type("id-3"),
                    is_(DSType.NFS_3))
        assert_that(ds_manager.datastore_type("id-4"),
                    is_(DSType.NFS_41))
        assert_that(ds_manager.datastore_type("id-5"),
                    is_(DSType.VSAN))
        assert_that(ds_manager.datastore_type("id-6"),
                    is_(DSType.OTHER))

        # test normalize
        assert_that(ds_manager.normalize("id-1"), is_("id-1"))
        assert_that(ds_manager.normalize("datastore1"), is_("id-1"))
    def test_get_datastores(self):
        """ Test esx datastore manager with different datastore types.
        Verify the datastore types are correctly parsed and all the
        datastores are populated.
        """
        hypervisor = MagicMock()
        host_client = MagicMock()

        host_client.get_all_datastores.return_value = self.get_datastore_mock([
            # name, url, type, local
            ["datastore1", "id-1", "VMFS", True],
            ["datastore2", "id-2", "VMFS", False],
            ["datastore3", "id-3", "NFS", None],
            ["datastore4", "id-4", "NFSV41", None],
            ["datastore5", "id-5", "vsan", None],
            ["datastore6", "id-6", "VFFS", None],
        ])

        hypervisor.host_client = host_client

        ds_list = [
            "datastore1", "datastore2", "datastore3", "datastore4",
            "datastore5", "datastore6"
        ]
        image_ds = [{"name": "datastore2", "used_for_vms": False}]
        ds_manager = DatastoreManager(hypervisor, ds_list, image_ds)

        assert_that(
            ds_manager.get_datastore_ids(),
            contains_inanyorder("id-1", "id-2", "id-3", "id-4", "id-5",
                                "id-6"))

        assert_that(
            ds_manager.vm_datastores(),
            contains_inanyorder("id-1", "id-3", "id-4", "id-5", "id-6"))

        datastores = ds_manager.get_datastores()
        assert_that(
            datastores,
            contains_inanyorder(
                Datastore("id-1",
                          "datastore1",
                          type=DSType.LOCAL_VMFS,
                          tags=set([LOCAL_VMFS_TAG])),
                Datastore("id-2",
                          "datastore2",
                          type=DSType.SHARED_VMFS,
                          tags=set([SHARED_VMFS_TAG])),
                Datastore("id-3",
                          "datastore3",
                          type=DSType.NFS_3,
                          tags=set([NFS_TAG])),
                Datastore("id-4",
                          "datastore4",
                          type=DSType.NFS_41,
                          tags=set([NFS_TAG])),
                Datastore("id-5",
                          "datastore5",
                          type=DSType.VSAN,
                          tags=set([VSAN_TAG])),
                Datastore("id-6", "datastore6", type=DSType.OTHER,
                          tags=set())))

        assert_that(ds_manager.image_datastores(), is_(["id-2"]))
        assert_that(ds_manager.datastore_type("id-1"), is_(DSType.LOCAL_VMFS))
        assert_that(ds_manager.datastore_type("id-2"), is_(DSType.SHARED_VMFS))
        assert_that(ds_manager.datastore_type("id-3"), is_(DSType.NFS_3))
        assert_that(ds_manager.datastore_type("id-4"), is_(DSType.NFS_41))
        assert_that(ds_manager.datastore_type("id-5"), is_(DSType.VSAN))
        assert_that(ds_manager.datastore_type("id-6"), is_(DSType.OTHER))

        # test normalize
        assert_that(ds_manager.normalize("id-1"), is_("id-1"))
        assert_that(ds_manager.normalize("datastore1"), is_("id-1"))