def setUp(self):
     self.services_helper = ServicesHelper()
     self.mock_options = MagicMock()
     self.agent_config_dir = mkdtemp(delete=True)
     self.agent_config = AgentConfig(["--config-path",
                                     self.agent_config_dir,
                                     "--hypervisor", "esx"])
class TestHypervisor(unittest.TestCase):

    def setUp(self):
        self.services_helper = ServicesHelper()
        self.mock_options = MagicMock()
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "esx"])

    def tearDown(self):
        self.services_helper.teardown()

    @patch("pysdk.connect.Connect")
    @patch("pyVmomi.vim.ServiceInstance")
    @patch.object(VimClient, "update_cache")
    @patch.object(Credentials, "username", new_callable=PropertyMock)
    @patch.object(Credentials, "password", new_callable=PropertyMock)
    def test_hypervisor(self, password_mock, user_mock, update_mock,
                        si_mock, connect_mock):
        user_mock.return_value = "user"
        password_mock.return_value = "password"
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "fake"])
        Hypervisor(self.agent_config)

    def test_hypervisor_setter(self):
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "fake"])
        hypervisor = Hypervisor(self.agent_config)
        hypervisor.set_cpu_overcommit(2.0)
        assert_that(hypervisor.cpu_overcommit, equal_to(2.0))
        hypervisor.set_memory_overcommit(3.0)
        assert_that(hypervisor.memory_overcommit, equal_to(3.0))

    def test_unknown_hypervisor(self):
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "dummy"])
        self.assertRaises(ValueError, Hypervisor, self.agent_config)
class TestHypervisor(unittest.TestCase):

    def setUp(self):
        self.services_helper = ServicesHelper()
        self.mock_options = MagicMock()
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "esx"])

    def tearDown(self):
        self.services_helper.teardown()

    @patch("pysdk.connect.Connect")
    @patch("pyVmomi.vim.ServiceInstance")
    @patch.object(VimClient, "update_cache")
    @patch.object(Credentials, "username", new_callable=PropertyMock)
    @patch.object(Credentials, "password", new_callable=PropertyMock)
    def test_hypervisor(self, password_mock, user_mock, update_mock,
                        si_mock, connect_mock):
        user_mock.return_value = "user"
        password_mock.return_value = "password"
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "fake"])
        Hypervisor(self.agent_config)

    def test_hypervisor_setter(self):
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "fake"])
        hypervisor = Hypervisor(self.agent_config)
        hypervisor.set_cpu_overcommit(2.0)
        assert_that(hypervisor.cpu_overcommit, equal_to(2.0))
        hypervisor.set_memory_overcommit(3.0)
        assert_that(hypervisor.memory_overcommit, equal_to(3.0))

    def test_unknown_hypervisor(self):
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--hypervisor", "dummy"])
        self.assertRaises(ValueError, Hypervisor, self.agent_config)

    @patch("host.hypervisor.esx.vm_config.GetEnv")
    @patch(
        "host.hypervisor.esx.image_manager."
        "EsxImageManager.monitor_for_cleanup")
    @patch("host.hypervisor.esx.hypervisor.VimClient")
    def test_large_page_disable(self, vim_client_mock,
                                monitor_mock, get_env_mock):
        vim_client_mock.return_value = MagicMock()
        hypervisor = Hypervisor(self.agent_config)
        vim_client = hypervisor.hypervisor.vim_client
        assert_that(vim_client.set_large_page_support.called, is_(True))
        vim_client.set_large_page_support.assert_called_once_with(
            disable=False)
        vim_client.reset_mock()

        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir,
                                        "--memory-overcommit", "1.5"])
        hypervisor = Hypervisor(self.agent_config)
        vim_client = hypervisor.hypervisor.vim_client
        assert_that(vim_client.set_large_page_support.called, is_(True))
        vim_client.set_large_page_support.assert_called_once_with(disable=True)
        vim_client.reset_mock()
Esempio n. 4
0
 def setUp(self):
     self.services_helper = ServicesHelper()
     self.hv = None
     self.agent_config_dir = mkdtemp(delete=True)
     self.agent_config = AgentConfig(
         ["--config-path", self.agent_config_dir])
Esempio n. 5
0
class TestUnitEsxHypervisor(unittest.TestCase):
    def setUp(self):
        self.services_helper = ServicesHelper()
        self.hv = None
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(
            ["--config-path", self.agent_config_dir])

    def tearDown(self):
        if self.hv:
            # Need to explicitly stop the thread and join it. Otherwise if it
            # runs beyond its own context, other test case could mocks some
            # function the thread relies on, and it could lead to bad
            # behaviors.
            self.hv.vim_client.sync_thread.stop()
            self.hv.vim_client.sync_thread.join()
        self.services_helper.teardown()

    def _retrieve_content(self):
        about_mock = PropertyMock(name="about_mock")
        about_mock.version = "5.5.99"
        about_mock.build = "999"
        content_mock = MagicMock(name="content")
        content_mock.about = about_mock
        return content_mock

    @patch("host.hypervisor.esx.vm_config.GetEnv")
    @patch.object(VimClient, "initialize_host_counters")
    @patch("host.hypervisor.esx.image_manager.EsxImageManager."
           "monitor_for_cleanup")
    @patch.object(VimClient, "first_vmk_ip_address", new_callable=PropertyMock)
    @patch.object(VimClient, "acquire_credentials")
    @patch.object(VimClient, "get_hostd_ssl_thumbprint")
    @patch.object(VimClient, "update_cache")
    @patch("pysdk.connect.Connect")
    @patch.object(VimClient, "host_uuid")
    def test_config(self, bios_uuid, connect_mock, update_mock,
                    thumbprint_mock, creds_mock, first_ip_mock, monitor_mock,
                    init_host_mock, get_env_mock):

        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        bios_uuid.return_value = "0" * 36

        first_ip_mock.return_value = "11.11.11.11"
        thumbprint_mock.return_value = "aabb"
        creds_mock.return_value = ["user", "pass"]

        # Simulate exception thrown during construction of the
        # EsxHypervisor and verify that no keep alive thread is started
        creds_mock.side_effect = AcquireCredentialsException()
        self.assertRaises(AcquireCredentialsException, EsxHypervisor,
                          self.agent_config)
        assert_that(update_mock.called, is_(False))

        creds_mock.side_effect = None

        self.hv = EsxHypervisor(self.agent_config)

        assert_that(update_mock.called, is_(True))

    @patch("host.hypervisor.esx.vm_config.GetEnv")
    @patch.object(VimClient, "initialize_host_counters")
    @patch("host.hypervisor.esx.image_manager.EsxImageManager."
           "monitor_for_cleanup")
    @patch.object(VimClient, "first_vmk_ip_address", new_callable=PropertyMock)
    @patch.object(VimClient, "acquire_credentials")
    @patch.object(VimClient, "get_hostd_ssl_thumbprint")
    @patch.object(VimClient, "update_cache")
    @patch("pysdk.connect.Connect")
    @patch.object(VimClient, "host_uuid")
    def test_listener(self, bios_uuid, connect_mock, update_mock,
                      thumbprint_mock, creds_mock, first_ip_mock, monitor_mock,
                      init_host_mock, get_env_mock):
        """Test update listeners"""
        class MyUpdateListener(UpdateListener):
            def __init__(self):
                self.nw_updated = False
                self.vm_updated = False
                self.ds_updated = False

            def networks_updated(self):
                self.nw_updated = True

            def virtual_machines_updated(self):
                self.vm_updated = True

            def datastores_updated(self):
                self.ds_updated = True

        creds_mock.return_value = ["user", "pass"]
        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        # Create 10 listeners each.
        listeners = []
        for i in xrange(10):
            listeners.append(MyUpdateListener())

        # Add listeners to the hypervisor and verify that the listeners get
        # notified immediately.
        self.hv = EsxHypervisor(self.agent_config)
        for listener in listeners:
            self.hv.add_update_listener(listener)
            assert_that(listener.nw_updated, is_(True))
            assert_that(listener.vm_updated, is_(True))
            assert_that(listener.ds_updated, is_(True))

        # Remove listeners.
        for listener in listeners:
            self.hv.remove_update_listener(listener)
        # 1 for datastore manager
        assert_that(len(self.hv.vim_client.update_listeners), is_(1))
Esempio n. 6
0
class TestUnitHypervisor(unittest.TestCase):
    def setUp(self):
        self.services_helper = ServicesHelper()
        self.hv = None
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(
            ["--config-path", self.agent_config_dir])

    def tearDown(self):
        if self.hv:
            # Need to explicitly stop the thread and join it. Otherwise if it
            # runs beyond its own context, other test case could mocks some
            # function the thread relies on, and it could lead to bad
            # behaviors.
            self.hv.host_client._sync_thread.stop()
            self.hv.host_client._sync_thread.join()
        self.services_helper.teardown()

    def _retrieve_content(self):
        about_mock = PropertyMock(name="about_mock")
        about_mock.version = "5.5.99"
        about_mock.build = "999"
        content_mock = MagicMock(name="content")
        return content_mock

    @patch("host.image.image_manager.ImageManager.monitor_for_cleanup")
    @patch.object(VimClient, "_acquire_local_credentials")
    @patch.object(VimCache, "poll_updates")
    @patch("pysdk.connect.Connect")
    def test_config(self, connect_mock, update_mock, creds_mock, monitor_mock):

        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        creds_mock.return_value = ["user", "pass"]

        # Simulate exception thrown during construction of the
        # Hypervisor and verify that no keep alive thread is started
        creds_mock.side_effect = AcquireCredentialsException()
        self.assertRaises(AcquireCredentialsException, Hypervisor,
                          self.agent_config)
        assert_that(update_mock.called, is_(False))

        creds_mock.side_effect = None

        self.hv = Hypervisor(self.agent_config)

        assert_that(update_mock.called, is_(True))

    @patch("host.image.image_manager.ImageManager.monitor_for_cleanup")
    @patch.object(VimClient, "_acquire_local_credentials")
    @patch.object(VimCache, "poll_updates")
    @patch("pysdk.connect.Connect")
    def test_listener(self, connect_mock, update_mock, creds_mock,
                      monitor_mock):
        """Test update listeners"""
        class MyUpdateListener(UpdateListener):
            def __init__(self):
                self.ds_updated = False

            def datastores_updated(self):
                self.ds_updated = True

        creds_mock.return_value = ["user", "pass"]
        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        # Create 10 listeners each.
        listeners = []
        for i in xrange(10):
            listeners.append(MyUpdateListener())

        # Add listeners to the hypervisor and verify that the listeners get
        # notified immediately.
        self.hv = Hypervisor(self.agent_config)
        for listener in listeners:
            self.hv.add_update_listener(listener)
            assert_that(listener.ds_updated, is_(True))

        # Remove listeners.
        for listener in listeners:
            self.hv.remove_update_listener(listener)
        # 1 for datastore manager
        assert_that(len(self.hv.host_client.update_listeners), is_(1))
Esempio n. 7
0
 def setUp(self):
     self.services_helper = ServicesHelper()
     self.mock_options = MagicMock()
     self.agent_config_dir = mkdtemp(delete=True)
     self.agent_config = AgentConfig(
         ["--config-path", self.agent_config_dir, "--hypervisor", "esx"])
Esempio n. 8
0
class TestHypervisor(unittest.TestCase):
    def setUp(self):
        self.services_helper = ServicesHelper()
        self.mock_options = MagicMock()
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(
            ["--config-path", self.agent_config_dir, "--hypervisor", "esx"])

    def tearDown(self):
        self.services_helper.teardown()

    @patch("pysdk.connect.Connect")
    @patch("pyVmomi.vim.ServiceInstance")
    @patch.object(VimClient, "update_cache")
    @patch.object(Credentials, "username", new_callable=PropertyMock)
    @patch.object(Credentials, "password", new_callable=PropertyMock)
    def test_hypervisor(self, password_mock, user_mock, update_mock, si_mock,
                        connect_mock):
        user_mock.return_value = "user"
        password_mock.return_value = "password"
        self.agent_config = AgentConfig(
            ["--config-path", self.agent_config_dir, "--hypervisor", "fake"])
        Hypervisor(self.agent_config)

    def test_hypervisor_setter(self):
        self.agent_config = AgentConfig(
            ["--config-path", self.agent_config_dir, "--hypervisor", "fake"])
        hypervisor = Hypervisor(self.agent_config)
        hypervisor.set_cpu_overcommit(2.0)
        assert_that(hypervisor.cpu_overcommit, equal_to(2.0))
        hypervisor.set_memory_overcommit(3.0)
        assert_that(hypervisor.memory_overcommit, equal_to(3.0))

    def test_unknown_hypervisor(self):
        self.agent_config = AgentConfig(
            ["--config-path", self.agent_config_dir, "--hypervisor", "dummy"])
        self.assertRaises(ValueError, Hypervisor, self.agent_config)

    @patch("host.hypervisor.esx.vm_config.GetEnv")
    @patch("host.hypervisor.esx.image_manager."
           "EsxImageManager.monitor_for_cleanup")
    @patch("host.hypervisor.esx.hypervisor.VimClient")
    def test_large_page_disable(self, vim_client_mock, monitor_mock,
                                get_env_mock):
        vim_client_mock.return_value = MagicMock()
        hypervisor = Hypervisor(self.agent_config)
        vim_client = hypervisor.hypervisor.vim_client
        assert_that(vim_client.set_large_page_support.called, is_(True))
        vim_client.set_large_page_support.assert_called_once_with(
            disable=False)
        vim_client.reset_mock()

        self.agent_config = AgentConfig([
            "--config-path", self.agent_config_dir, "--memory-overcommit",
            "1.5"
        ])
        hypervisor = Hypervisor(self.agent_config)
        vim_client = hypervisor.hypervisor.vim_client
        assert_that(vim_client.set_large_page_support.called, is_(True))
        vim_client.set_large_page_support.assert_called_once_with(disable=True)
        vim_client.reset_mock()
 def setUp(self):
     self.services_helper = ServicesHelper()
     self.hv = None
     self.agent_config_dir = mkdtemp(delete=True)
     self.agent_config = AgentConfig(["--config-path",
                                     self.agent_config_dir])
Esempio n. 10
0
class TestUnitHypervisor(unittest.TestCase):

    def setUp(self):
        self.services_helper = ServicesHelper()
        self.hv = None
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir])

    def tearDown(self):
        if self.hv:
            # Need to explicitly stop the thread and join it. Otherwise if it
            # runs beyond its own context, other test case could mocks some
            # function the thread relies on, and it could lead to bad
            # behaviors.
            self.hv.host_client._sync_thread.stop()
            self.hv.host_client._sync_thread.join()
        self.services_helper.teardown()

    def _retrieve_content(self):
        about_mock = PropertyMock(name="about_mock")
        about_mock.version = "5.5.99"
        about_mock.build = "999"
        content_mock = MagicMock(name="content")
        return content_mock

    @patch.object(Hypervisor, "create_host_client")
    @patch("host.image.image_manager.ImageManager.monitor_for_cleanup")
    @patch.object(VimClient, "_acquire_local_credentials")
    @patch.object(VimCache, "poll_updates")
    @patch("pysdk.connect.Connect")
    def test_config(self, connect_mock, update_mock, creds_mock, monitor_mock, create_host_client_mock):

        create_host_client_mock.return_value = VimClient()
        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        creds_mock.return_value = ["user", "pass"]

        # Simulate exception thrown during construction of the
        # Hypervisor and verify that no keep alive thread is started
        creds_mock.side_effect = AcquireCredentialsException()
        self.assertRaises(AcquireCredentialsException,
                          Hypervisor, self.agent_config)
        assert_that(update_mock.called, is_(False))

        creds_mock.side_effect = None

        self.hv = Hypervisor(self.agent_config)

        assert_that(update_mock.called, is_(True))

    @patch.object(Hypervisor, "create_host_client")
    @patch("host.image.image_manager.ImageManager.monitor_for_cleanup")
    @patch.object(VimClient, "_acquire_local_credentials")
    @patch.object(VimCache, "poll_updates")
    @patch("pysdk.connect.Connect")
    def test_listener(self, connect_mock, update_mock, creds_mock, monitor_mock, create_host_client_mock):
        """Test update listeners"""
        class MyUpdateListener(UpdateListener):
            def __init__(self):
                self.ds_updated = False

            def datastores_updated(self):
                self.ds_updated = True

        create_host_client_mock.return_value = VimClient()
        creds_mock.return_value = ["user", "pass"]
        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        # Create 10 listeners each.
        listeners = []
        for i in xrange(10):
            listeners.append(MyUpdateListener())

        # Add listeners to the hypervisor and verify that the listeners get
        # notified immediately.
        self.hv = Hypervisor(self.agent_config)
        for listener in listeners:
            self.hv.add_update_listener(listener)
            assert_that(listener.ds_updated, is_(True))

        # Remove listeners.
        for listener in listeners:
            self.hv.remove_update_listener(listener)
        # 1 for datastore manager
        assert_that(len(self.hv.host_client.update_listeners), is_(1))
class TestUnitEsxHypervisor(unittest.TestCase):

    def setUp(self):
        self.services_helper = ServicesHelper()
        self.hv = None
        self.agent_config_dir = mkdtemp(delete=True)
        self.agent_config = AgentConfig(["--config-path",
                                        self.agent_config_dir])

    def tearDown(self):
        if self.hv:
            # Need to explicitly stop the thread and join it. Otherwise if it
            # runs beyond its own context, other test case could mocks some
            # function the thread relies on, and it could lead to bad
            # behaviors.
            self.hv.vim_client.sync_thread.stop()
            self.hv.vim_client.sync_thread.join()
        self.services_helper.teardown()

    def _retrieve_content(self):
        about_mock = PropertyMock(name="about_mock")
        about_mock.version = "5.5.99"
        about_mock.build = "999"
        content_mock = MagicMock(name="content")
        content_mock.about = about_mock
        return content_mock

    @patch("host.hypervisor.esx.vm_config.GetEnv")
    @patch.object(VimClient, "initialize_host_counters")
    @patch("host.hypervisor.esx.image_manager.EsxImageManager."
           "monitor_for_cleanup")
    @patch.object(VimClient, "first_vmk_ip_address", new_callable=PropertyMock)
    @patch.object(VimClient, "acquire_credentials")
    @patch.object(VimClient, "get_hostd_ssl_thumbprint")
    @patch.object(VimClient, "update_cache")
    @patch("pysdk.connect.Connect")
    @patch.object(VimClient, "host_uuid")
    def test_config(
            self, bios_uuid, connect_mock, update_mock,
            thumbprint_mock, creds_mock, first_ip_mock,
            monitor_mock, init_host_mock, get_env_mock):

        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        bios_uuid.return_value = "0" * 36

        first_ip_mock.return_value = "11.11.11.11"
        thumbprint_mock.return_value = "aabb"
        creds_mock.return_value = ["user", "pass"]

        # Simulate exception thrown during construction of the
        # EsxHypervisor and verify that no keep alive thread is started
        creds_mock.side_effect = AcquireCredentialsException()
        self.assertRaises(AcquireCredentialsException,
                          EsxHypervisor, self.agent_config)
        assert_that(update_mock.called, is_(False))

        creds_mock.side_effect = None

        self.hv = EsxHypervisor(self.agent_config)

        assert_that(update_mock.called, is_(True))

    @patch("host.hypervisor.esx.vm_config.GetEnv")
    @patch.object(VimClient, "initialize_host_counters")
    @patch("host.hypervisor.esx.image_manager.EsxImageManager."
           "monitor_for_cleanup")
    @patch.object(VimClient, "first_vmk_ip_address", new_callable=PropertyMock)
    @patch.object(VimClient, "acquire_credentials")
    @patch.object(VimClient, "get_hostd_ssl_thumbprint")
    @patch.object(VimClient, "update_cache")
    @patch("pysdk.connect.Connect")
    @patch.object(VimClient, "host_uuid")
    def test_listener(
            self, bios_uuid, connect_mock, update_mock,
            thumbprint_mock, creds_mock, first_ip_mock,
            monitor_mock, init_host_mock, get_env_mock):
        """Test update listeners"""
        class MyUpdateListener(UpdateListener):
            def __init__(self):
                self.nw_updated = False
                self.vm_updated = False
                self.ds_updated = False

            def networks_updated(self):
                self.nw_updated = True

            def virtual_machines_updated(self):
                self.vm_updated = True

            def datastores_updated(self):
                self.ds_updated = True

        creds_mock.return_value = ["user", "pass"]
        si_mock = MagicMock(name="si_mock")
        si_mock.RetrieveContent = self._retrieve_content
        connect_mock.return_value = si_mock

        # Create 10 listeners each.
        listeners = []
        for i in xrange(10):
            listeners.append(MyUpdateListener())

        # Add listeners to the hypervisor and verify that the listeners get
        # notified immediately.
        self.hv = EsxHypervisor(self.agent_config)
        for listener in listeners:
            self.hv.add_update_listener(listener)
            assert_that(listener.nw_updated, is_(True))
            assert_that(listener.vm_updated, is_(True))
            assert_that(listener.ds_updated, is_(True))

        # Remove listeners.
        for listener in listeners:
            self.hv.remove_update_listener(listener)
        # 1 for datastore manager
        assert_that(len(self.hv.vim_client.update_listeners), is_(1))