Exemple #1
0
 def test_load_image_datastores(self):
     """
     Verify that the image_datastores field gets loaded from config.json.
     """
     self.agent._parse_options(["--config-path", self.agent_conf_dir])
     expected_image_ds = [
         {
             "name": "ds1",
             "used_for_vms": True
         },
         {
             "name": "ds2",
             "used_for_vms": False
         },
     ]
     req = ProvisionRequest()
     req.datastores = ["ds1", "ds2", "ds3"]
     req.image_datastores = set(
         [ImageDatastore("ds1", True),
          ImageDatastore("ds2", False)])
     self.agent.update_config(req)
     self.agent._persist_config()
     self.agent._load_config()
     assert_that(self.agent.datastores, equal_to(["ds1", "ds2", "ds3"]))
     assert_that(self.agent.image_datastores,
                 contains_inanyorder(*expected_image_ds))
    def test_reboot_required(self):
        """
        Test that reboot required flag is set when all the required agent
        parameters are set.
        """
        self.agent._parse_options(["--config-path", self.agent_conf_dir])
        # Check that reboot required is false until we set all the params
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr
        self.agent.update_config(req)
        # Verify that the bootstrap is still false as zk config is not
        # specified.
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr
        self.agent.update_config(req)
        self.assertTrue(self.agent.reboot_required)
    def test_config_change(self):
        # Test chairman config change
        chairman_callback1 = mock.MagicMock()
        chairman_callback2 = mock.MagicMock()

        chairman_server = [
            ServerAddress("192.168.0.1", 8835),
            ServerAddress("192.168.0.2", 8835),
        ]
        self.agent.on_config_change(self.agent.CHAIRMAN, chairman_callback1)
        self.agent.on_config_change(self.agent.CHAIRMAN, chairman_callback2)
        provision = ProvisionRequest(chairman_server=chairman_server)
        self.agent.update_config(provision)
        chairman_callback1.assert_called_once_with(chairman_server)
        chairman_callback2.assert_called_once_with(chairman_server)
        self.assertFalse(self.agent.reboot_required)

        # Test cpu_overcommit and memory_overcommit config change
        cpu_callback = mock.MagicMock()
        mem_callback = mock.MagicMock()

        provision.cpu_overcommit = 5.0
        provision.memory_overcommit = 6.0
        self.agent.on_config_change(self.agent.CPU_OVERCOMMIT, cpu_callback)
        self.agent.on_config_change(self.agent.MEMORY_OVERCOMMIT, mem_callback)
        self.agent.update_config(provision)
        cpu_callback.assert_called_once_with(5.0)
        mem_callback.assert_called_once_with(6.0)
    def test_config_change(self):
        # Test chairman config change
        chairman_callback1 = mock.MagicMock()
        chairman_callback2 = mock.MagicMock()

        chairman_server = [
            ServerAddress("192.168.0.1", 8835),
            ServerAddress("192.168.0.2", 8835),
        ]
        self.agent.on_config_change(self.agent.CHAIRMAN, chairman_callback1)
        self.agent.on_config_change(self.agent.CHAIRMAN, chairman_callback2)
        provision = ProvisionRequest(chairman_server=chairman_server)
        self.agent.update_config(provision)
        chairman_callback1.assert_called_once_with(chairman_server)
        chairman_callback2.assert_called_once_with(chairman_server)
        self.assertFalse(self.agent.reboot_required)

        # Test cpu_overcommit and memory_overcommit config change
        cpu_callback = mock.MagicMock()
        mem_callback = mock.MagicMock()

        provision.cpu_overcommit = 5.0
        provision.memory_overcommit = 6.0
        self.agent.on_config_change(self.agent.CPU_OVERCOMMIT, cpu_callback)
        self.agent.on_config_change(self.agent.MEMORY_OVERCOMMIT, mem_callback)
        self.agent.update_config(provision)
        cpu_callback.assert_called_once_with(5.0)
        mem_callback.assert_called_once_with(6.0)
Exemple #5
0
    def _provision_host(self, datastore):
        """ Provisions the agents on the remote hosts """
        req = ProvisionRequest()
        req.availability_zone = "test"
        req.datastores = [datastore]
        req.networks = ["VM Network"]
        req.address = ServerAddress(host="localhost", port=8835)
        req.chairman_server = [ServerAddress("localhost", 13000)]
        req.memory_overcommit = 2.0
        req.image_datastores = set(
            [ImageDatastore(name=datastore, used_for_vms=True)])
        res = self.agent_client.provision(req)

        # This will trigger a restart if the agent config changes, which
        # will happen the first time provision_hosts is called.
        self.assertEqual(res.result, ProvisionResultCode.OK)

        # If a restart is required subsequent calls will fail.
        # This is an indirect way to determine if a restart is required.
        host_config_request = Host.GetConfigRequest()
        res = self.host_client.get_host_config(host_config_request)

        if (res.result != GetConfigResultCode.OK):
            # Sleep for 15 s for the agent to reboot and come back up
            time.sleep(15)
    def test_reboot_required(self):
        """
        Test that reboot required flag is set when all the required agent
        parameters are set.
        """
        self.agent._parse_options(["--config-path", self.agent_conf_dir])
        # Check that reboot required is false until we set all the params
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.datastores = ["ds3", "ds4"]
        req.stats_plugin_config = StatsPluginConfig()
        req.stats_plugin_config.enabled = False
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr
        self.agent.provision(req)
        # Verify that the bootstrap is still false as zk config is not
        # specified.
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        req = ProvisionRequest()
        req.datastores = ["ds3", "ds4"]
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr
        self.agent.provision(req)
        self.assertTrue(self.agent.reboot_required)
 def _update_agent_invalid_config(self):
     """
     Negative test case to update the agent with an invalid config
     """
     req = ProvisionRequest()
     req.datastores = ["bootstrap_datastore"]
     addr = ServerAddress(host="foobar", port=8835)
     req.address = addr
     req.memory_overcommit = 0.5
     res = self.control_client.provision(req)
     self.assertEqual(res.result, ProvisionResultCode.INVALID_CONFIG)
    def test_config_change(self):
        # Test cpu_overcommit and memory_overcommit config change
        cpu_callback = mock.MagicMock()
        mem_callback = mock.MagicMock()

        provision = ProvisionRequest()
        provision.cpu_overcommit = 5.0
        provision.memory_overcommit = 6.0
        self.agent.on_config_change(self.agent.CPU_OVERCOMMIT, cpu_callback)
        self.agent.on_config_change(self.agent.MEMORY_OVERCOMMIT, mem_callback)
        self.agent.provision(provision)
        cpu_callback.assert_called_once_with(5.0)
        mem_callback.assert_called_once_with(6.0)
 def test_load_image_datastores(self):
     """
     Verify that the image_datastores field gets loaded from config.json.
     """
     self.agent._parse_options(["--config-path", self.agent_conf_dir])
     expected_image_ds = [
         {"name": "ds1", "used_for_vms": True},
         {"name": "ds2", "used_for_vms": False},
     ]
     req = ProvisionRequest()
     req.datastores = ["ds1", "ds2", "ds3"]
     req.image_datastores = set([ImageDatastore("ds1", True),
                                 ImageDatastore("ds2", False)])
     self.agent.update_config(req)
     self.agent._persist_config()
     self.agent._load_config()
     assert_that(self.agent.datastores, equal_to(["ds1", "ds2", "ds3"]))
     assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
    def test_load_image_datastores(self):
        """
        Verify that the image_datastores field gets loaded from config.json.
        """
        self.agent._parse_options(["--config-path", self.agent_conf_dir])
        expected_image_ds = [
            {"name": "ds1", "used_for_vms": True},
            {"name": "ds2", "used_for_vms": False},
        ]
        req = ProvisionRequest()
        req.datastores = ["ds1", "ds2", "ds3", "ds4"]
        req.image_datastores = {ImageDatastore("ds1", True), ImageDatastore("ds2", False)}
        self.agent.provision(req)
        self.agent._load_config()
        assert_that(self.agent.datastores, equal_to(["ds1", "ds2", "ds3", "ds4"]))
        assert_that(self.agent.image_datastores, contains_inanyorder(*expected_image_ds))

        imageds_callback = mock.MagicMock()
        self.agent.on_config_change(self.agent.IMAGE_DATASTORES, imageds_callback)
        req = UpdateConfigRequest()
        req.image_datastores = {ImageDatastore("ds1", True), ImageDatastore("ds2", False), ImageDatastore("ds3", True)}
        self.agent.update(req)
        expected_image_ds = [
            {"name": "ds1", "used_for_vms": True},
            {"name": "ds2", "used_for_vms": False},
            {"name": "ds3", "used_for_vms": True},
        ]
        assert_that(self.agent.image_datastores, contains_inanyorder(*expected_image_ds))
        imageds_callback.assert_called_once_with(self.agent.image_datastores)
        self.agent._load_config()
        assert_that(self.agent.image_datastores, contains_inanyorder(*expected_image_ds))
    def _update_agent_config(self):
        """
        Update the agent config using the bootstrap interface.
        Verifies that thrift requests after a config update return back
        SYSTEM_ERROR and the agent is stopped.
        :return the request we used to configure the host with.
        """
        req = ProvisionRequest()
        req.datastores = ["bootstrap_datastore"]
        req.networks = ["Bootstrap Network"]
        addr = ServerAddress(host="foobar", port=8835)
        req.address = addr
        req.memory_overcommit = 2.0
        res = self.control_client.provision(req)
        self.assertEqual(res.result, ProvisionResultCode.OK)

        # Now check that the agent went down and all requests fail until the
        # agent restarts.
        # Use a time greater than the restart poll thread.
        remaining_sleep_time = 15
        sleep_time = 0.5
        agent_restarted = False
        while 0 < remaining_sleep_time:
            try:
                res = self.control_client.get_agent_status()
                # Verify that the response is restarting
                self.assertEqual(res.status, AgentStatusCode.RESTARTING)
                time.sleep(sleep_time)
                remaining_sleep_time -= sleep_time
            except TTransport.TTransportException:
                agent_restarted = True
                break
            except:
                logger.debug("Caught exception, assuming restart: ",
                             exc_info=True)
                agent_restarted = True
                break
        self.assertTrue(agent_restarted)
        return req
Exemple #12
0
 def _update_agent_invalid_config(self):
     """
     Negative test case to update the agent with an invalid config
     """
     req = ProvisionRequest()
     req.availability_zone = "bootstrap_availability_zone_id"
     req.datastores = ["bootstrap_datastore"]
     req.networks = ["Bootstrap Network"]
     addr = ServerAddress(host="foobar", port=8835)
     req.address = addr
     req.chairman_server = [ServerAddress("h1", 13000),
                            ServerAddress("h2", 13000)]
     req.memory_overcommit = 0.5
     res = self.control_client.provision(req)
     self.assertEqual(res.result, ProvisionResultCode.INVALID_CONFIG)
    def _update_agent_config(self):
        """
        Update the agent config using the bootstrap interface.
        Verifies that thrift requests after a config update return back
        SYSTEM_ERROR and the agent is stopped.
        :return the request we used to configure the host with.
        """
        req = ProvisionRequest()
        req.datastores = ["bootstrap_datastore"]
        addr = ServerAddress(host="foobar", port=8835)
        req.address = addr
        req.memory_overcommit = 2.0
        res = self.control_client.provision(req)
        self.assertEqual(res.result, ProvisionResultCode.OK)

        # Now check that the agent went down and all requests fail until the
        # agent restarts.
        # Use a time greater than the restart poll thread.
        remaining_sleep_time = 15
        sleep_time = 0.5
        agent_restarted = False
        while 0 < remaining_sleep_time:
            try:
                res = self.control_client.get_agent_status()
                # Verify that the response is restarting
                self.assertEqual(res.status, AgentStatusCode.RESTARTING)
                time.sleep(sleep_time)
                remaining_sleep_time -= sleep_time
            except TTransport.TTransportException:
                agent_restarted = True
                break
            except:
                logger.debug("Caught exception, assuming restart: ",
                             exc_info=True)
                agent_restarted = True
                break
        self.assertTrue(agent_restarted)
        return req
    def _provision_host(self, datastore):
        """ Provisions the agents on the remote hosts """
        req = ProvisionRequest()
        req.availability_zone = "test"
        req.datastores = [datastore]
        req.networks = ["VM Network"]
        req.address = ServerAddress(host="localhost", port=8835)
        req.chairman_server = [ServerAddress("localhost", 13000)]
        req.memory_overcommit = 2.0
        req.image_datastores = set([ImageDatastore(name=datastore, used_for_vms=True)])
        res = self.agent_client.provision(req)

        # This will trigger a restart if the agent config changes, which
        # will happen the first time provision_hosts is called.
        self.assertEqual(res.result, ProvisionResultCode.OK)

        # If a restart is required subsequent calls will fail.
        # This is an indirect way to determine if a restart is required.
        host_config_request = Host.GetConfigRequest()
        res = self.host_client.get_host_config(host_config_request)

        if res.result != GetConfigResultCode.OK:
            # Sleep for 15 s for the agent to reboot and come back up
            time.sleep(15)
    def provision_hosts(self,
                        mem_overcommit=2.0,
                        datastores=None,
                        used_for_vms=True,
                        image_ds=None,
                        host_id=None,
                        deployment_id="test-deployment"):
        """ Provisions the agents on the remote hosts """
        if datastores is None:
            datastores = self.get_all_datastores()
            image_datastore = self.get_image_datastore()
        elif image_ds:
            image_datastore = image_ds
        else:
            image_datastore = datastores[0]

        req = ProvisionRequest()
        req.datastores = datastores
        req.address = ServerAddress(host=self.server, port=8835)
        req.memory_overcommit = mem_overcommit
        req.image_datastore_info = ImageDatastore(name=image_datastore,
                                                  used_for_vms=used_for_vms)
        req.image_datastores = set([req.image_datastore_info])
        req.management_only = True
        req.auth_enabled = False
        if host_id:
            req.host_id = host_id
        else:
            req.host_id = self.host_id

        if deployment_id:
            req.deployment_id = deployment_id
        else:
            req.deployment_id = self.deployment_id

        res = self.control_client.provision(req)

        # This will trigger a restart if the agent config changes, which
        # will happen the first time provision_hosts is called.
        self.assertEqual(res.result, ProvisionResultCode.OK)

        # Wait for up to 60 seconds for the agent to reboot.
        count = 0
        while count < 60:
            try:
                res = self.control_client.get_agent_status()
                if res.status == AgentStatusCode.OK:
                    # Agent is up
                    return
            except:
                logger.exception("Can't connect to agent")
            count += 1
            time.sleep(1)
            # Reconnect the clients
            self._close_agent_connections()
            self.client_connections()
        self.fail("Cannot connect to agent %s after provisioning" %
                  self.server)
        return host_id
    def test_agent_config_update(self):
        """ Test that updating the config using the RPC struct works """
        self.agent._parse_options(["--config-path", self.agent_conf_dir,
                                   "--hostname", "localhost",
                                   "--port", "1234",
                                   "--datastores", "ds1, ds2"])
        expected_image_ds = [{"name": "ds3", "used_for_vms": True}]

        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.provision_ready)
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.datastores = ["ds3", "ds4"]
        req.memory_overcommit = 1.5
        req.image_datastores = set([ImageDatastore("ds3", True)])
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr

        stats_plugin_config = StatsPluginConfig(
            stats_store_endpoint="10.0.0.100", stats_store_port=8081, stats_enabled=True)
        req.stats_plugin_config = stats_plugin_config

        req.host_id = "host1"
        req.deployment_id = "deployment1"
        self.agent.provision(req)

        assert_that(self.agent.stats_store_endpoint, equal_to("10.0.0.100"))
        assert_that(self.agent.stats_store_port, equal_to(8081))
        assert_that(self.agent.stats_enabled, equal_to(True))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.memory_overcommit,
                    equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))
        assert_that(self.agent.deployment_id, equal_to("deployment1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        # Verify we are able to unset all the configuration.
        req = ProvisionRequest()

        self.agent.provision(req)
        assert_that(self.agent.hostname, equal_to(None))
        assert_that(self.agent.host_port, equal_to(8835))
        assert_that(self.agent.datastores, equal_to([]))
        # Unsetting memory overcommit should set it to the default value.
        self.assertEqual(self.agent.memory_overcommit, 1.0)

        self.assertFalse(self.agent.bootstrap_ready)
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to(None))

        # Test an invalid update and verify the update doesn't have any side
        # effects.
        req = ProvisionRequest()
        req.datastores = ["ds3", "ds4"]
        req.memory_overcommit = 0.5
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr

        # Verify an exception is raised.
        self.assertRaises(InvalidConfig, self.agent.provision, req)
        assert_that(self.agent.hostname, equal_to(None))
        assert_that(self.agent.host_port, equal_to(8835))
        assert_that(self.agent.datastores, equal_to([]))
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertEqual(self.agent.memory_overcommit, 1.0)
    def test_agent_config_update(self):
        """ Test that updating the config using the RPC struct works """
        self.agent._parse_options(["--config-path", self.agent_conf_dir,
                                   "--availability-zone", "test",
                                   "--hostname", "localhost",
                                   "--port", "1234",
                                   "--datastores", "ds1, ds2"])
        expected_image_ds = [{"name": "ds3", "used_for_vms": True}]

        # Without chairman config we can't be provision ready
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.provision_ready)
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        req.memory_overcommit = 1.5
        req.image_datastores = set([ImageDatastore("ds3", True)])
        addr = ServerAddress(host="localhost", port=2345)
        req.chairman_server = [ServerAddress("h1", 13000),
                               ServerAddress("h2", 13000)]
        req.address = addr
        req.environment = {}
        req.environment["hypervisor"] = "fake"
        req.host_id = "host1"
        self.agent.update_config(req)

        assert_that(self.agent.availability_zone, equal_to("test1"))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.networks, equal_to(["Public"]))
        assert_that(self.agent.options.hypervisor, equal_to("fake"))
        assert_that(self.agent.chairman_list,
                    equal_to([ServerAddress("h1", 13000),
                              ServerAddress("h2", 13000)]))
        assert_that(self.agent.memory_overcommit,
                    equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        # Verify we are able to unset all the configuration.
        req = ProvisionRequest()

        self.agent.update_config(req)
        assert_that(self.agent.availability_zone, equal_to(None))
        assert_that(self.agent.hostname, equal_to(None))
        assert_that(self.agent.host_port, equal_to(8835))
        assert_that(self.agent.datastores, equal_to([]))
        assert_that(self.agent.networks, equal_to([]))
        assert_that(self.agent.chairman_list, equal_to([]))
        # Unsetting memory overcommit should set it to the default value.
        self.assertEqual(self.agent.memory_overcommit, 1.0)

        self.assertFalse(self.agent.bootstrap_ready)
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to(None))

        # Test an invalid update and verify the update doesn't have any side
        # effects.
        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        req.memory_overcommit = 0.5
        addr = ServerAddress(host="localhost", port=2345)
        req.chairman_server = [ServerAddress("h1", 13000),
                               ServerAddress("h2", 13000)]
        req.address = addr
        req.environment = {}
        req.environment["hypervisor"] = "fake"

        # Verify an exception is raised.
        self.assertRaises(InvalidConfig, self.agent.update_config, req)
        assert_that(self.agent.availability_zone, equal_to(None))
        assert_that(self.agent.hostname, equal_to(None))
        assert_that(self.agent.host_port, equal_to(8835))
        assert_that(self.agent.datastores, equal_to([]))
        assert_that(self.agent.networks, equal_to([]))
        assert_that(self.agent.chairman_list, equal_to([]))
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertEqual(self.agent.memory_overcommit, 1.0)

        # input an invalid datastore for image.
        req.image_datastores = set([ImageDatastore("ds5", False)])
        req.memory_overcommit = 2.0
        self.assertRaises(InvalidConfig, self.agent.update_config, req)
    def test_agent_config_update(self):
        """ Test that updating the config using the RPC struct works """
        self.agent._parse_options([
            "--config-path", self.agent_conf_dir, "--availability-zone",
            "test", "--hostname", "localhost", "--port", "1234",
            "--datastores", "ds1, ds2"
        ])
        expected_image_ds = [{"name": "ds3", "used_for_vms": True}]

        # Without chairman config we can't be provision ready
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.provision_ready)
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        req.memory_overcommit = 1.5
        req.image_datastores = set([ImageDatastore("ds3", True)])
        addr = ServerAddress(host="localhost", port=2345)
        req.chairman_server = [
            ServerAddress("h1", 13000),
            ServerAddress("h2", 13000)
        ]
        req.address = addr
        req.host_id = "host1"
        self.agent.update_config(req)

        assert_that(self.agent.availability_zone, equal_to("test1"))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.networks, equal_to(["Public"]))
        assert_that(
            self.agent.chairman_list,
            equal_to([ServerAddress("h1", 13000),
                      ServerAddress("h2", 13000)]))
        assert_that(self.agent.memory_overcommit, equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        # Verify we are able to unset all the configuration.
        req = ProvisionRequest()

        self.agent.update_config(req)
        assert_that(self.agent.availability_zone, equal_to(None))
        assert_that(self.agent.hostname, equal_to(None))
        assert_that(self.agent.host_port, equal_to(8835))
        assert_that(self.agent.datastores, equal_to([]))
        assert_that(self.agent.networks, equal_to([]))
        assert_that(self.agent.chairman_list, equal_to([]))
        # Unsetting memory overcommit should set it to the default value.
        self.assertEqual(self.agent.memory_overcommit, 1.0)

        self.assertFalse(self.agent.bootstrap_ready)
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to(None))

        # Test an invalid update and verify the update doesn't have any side
        # effects.
        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        req.memory_overcommit = 0.5
        addr = ServerAddress(host="localhost", port=2345)
        req.chairman_server = [
            ServerAddress("h1", 13000),
            ServerAddress("h2", 13000)
        ]
        req.address = addr

        # Verify an exception is raised.
        self.assertRaises(InvalidConfig, self.agent.update_config, req)
        assert_that(self.agent.availability_zone, equal_to(None))
        assert_that(self.agent.hostname, equal_to(None))
        assert_that(self.agent.host_port, equal_to(8835))
        assert_that(self.agent.datastores, equal_to([]))
        assert_that(self.agent.networks, equal_to([]))
        assert_that(self.agent.chairman_list, equal_to([]))
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertEqual(self.agent.memory_overcommit, 1.0)
    def test_agent_config_set_availability_zone(self):
        """ Test that updating the config using the RPC struct works """
        self.agent._parse_options([
            "--config-path", self.agent_conf_dir, "--availability-zone",
            "test", "--hostname", "localhost", "--port", "1234",
            "--datastores", "ds1, ds2"
        ])
        expected_image_ds = [{"name": "ds3", "used_for_vms": True}]

        # Without chairman config we can't be ready
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.provision_ready)
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        req.memory_overcommit = 1.5
        req.image_datastores = set([ImageDatastore("ds3", True)])
        addr = ServerAddress(host="localhost", port=2345)
        req.chairman_server = [
            ServerAddress("h1", 13000),
            ServerAddress("h2", 13000)
        ]
        req.address = addr
        req.host_id = "host1"
        self.agent.update_config(req)

        assert_that(self.agent.availability_zone, equal_to("test1"))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.networks, equal_to(["Public"]))
        assert_that(
            self.agent.chairman_list,
            equal_to([ServerAddress("h1", 13000),
                      ServerAddress("h2", 13000)]))
        assert_that(self.agent.memory_overcommit, equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        # Verify we are able to update availability zone only.
        setZone = SetAvailabilityZoneRequest()
        setZone.availability_zone = "test2"

        self.agent.set_availability_zone(setZone)
        assert_that(self.agent.availability_zone, equal_to("test2"))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.networks, equal_to(["Public"]))
        assert_that(
            self.agent.chairman_list,
            equal_to([ServerAddress("h1", 13000),
                      ServerAddress("h2", 13000)]))
        assert_that(self.agent.memory_overcommit, equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)
    def provision_hosts(self, mem_overcommit=2.0,
                        datastores=None, used_for_vms=True,
                        image_ds=None, host_id=None,
                        deployment_id="test-deployment"):
        """ Provisions the agents on the remote hosts """
        if datastores is None:
            datastores = self.get_all_datastores()
            image_datastore = self.get_image_datastore()
        elif image_ds:
            image_datastore = image_ds
        else:
            image_datastore = datastores[0]

        req = ProvisionRequest()
        req.datastores = datastores
        req.address = ServerAddress(host=self.server, port=8835)
        req.memory_overcommit = mem_overcommit
        req.image_datastore_info = ImageDatastore(
            name=image_datastore,
            used_for_vms=used_for_vms)
        req.image_datastores = set([req.image_datastore_info])
        req.management_only = True
        req.auth_enabled = False
        if host_id:
            req.host_id = host_id
        else:
            req.host_id = self.host_id

        if deployment_id:
            req.deployment_id = deployment_id
        else:
            req.deployment_id = self.deployment_id

        res = self.control_client.provision(req)

        # This will trigger a restart if the agent config changes, which
        # will happen the first time provision_hosts is called.
        self.assertEqual(res.result, ProvisionResultCode.OK)

        # Wait for up to 60 seconds for the agent to reboot.
        count = 0
        while count < 60:
            try:
                res = self.control_client.get_agent_status()
                if res.status == AgentStatusCode.OK:
                    # Agent is up
                    return
            except:
                logger.exception("Can't connect to agent")
            count += 1
            time.sleep(1)
            # Reconnect the clients
            self._close_agent_connections()
            self.client_connections()
        self.fail("Cannot connect to agent %s after provisioning" % self.server)
        return host_id
    def test_agent_config_set_availability_zone(self):
        """ Test that updating the config using the RPC struct works """
        self.agent._parse_options(["--config-path", self.agent_conf_dir,
                                   "--availability-zone", "test",
                                   "--hostname", "localhost",
                                   "--port", "1234",
                                   "--datastores", "ds1, ds2"])
        expected_image_ds = [{"name": "ds3", "used_for_vms": True}]

        # Without chairman config we can't be ready
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.provision_ready)
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        req.memory_overcommit = 1.5
        req.image_datastores = set([ImageDatastore("ds3", True)])
        addr = ServerAddress(host="localhost", port=2345)
        req.chairman_server = [ServerAddress("h1", 13000),
                               ServerAddress("h2", 13000)]
        req.address = addr
        req.host_id = "host1"
        self.agent.update_config(req)

        assert_that(self.agent.availability_zone, equal_to("test1"))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.networks, equal_to(["Public"]))
        assert_that(self.agent.chairman_list,
                    equal_to([ServerAddress("h1", 13000),
                              ServerAddress("h2", 13000)]))
        assert_that(self.agent.memory_overcommit,
                    equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        # Verify we are able to update availability zone only.
        setZone = SetAvailabilityZoneRequest()
        setZone.availability_zone = "test2"

        self.agent.set_availability_zone(setZone)
        assert_that(self.agent.availability_zone, equal_to("test2"))
        assert_that(self.agent.hostname, equal_to("localhost"))
        assert_that(self.agent.host_port, equal_to(2345))
        assert_that(self.agent.datastores, equal_to(["ds3", "ds4"]))
        assert_that(self.agent.networks, equal_to(["Public"]))
        assert_that(self.agent.chairman_list,
                    equal_to([ServerAddress("h1", 13000),
                              ServerAddress("h2", 13000)]))
        assert_that(self.agent.memory_overcommit,
                    equal_to(1.5))
        assert_that(self.agent.image_datastores, equal_to(expected_image_ds))
        assert_that(self.agent.host_id, equal_to("host1"))

        self.assertTrue(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)
    def test_reboot_required(self):
        """
        Test that reboot required flag is set when all the required agent
        parameters are set.
        """
        self.agent._parse_options(["--config-path", self.agent_conf_dir])
        # Check that reboot required is false until we set all the params
        self.assertFalse(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr
        self.agent.update_config(req)
        # Verify that the bootstrap is still false as zk config is not
        # specified.
        self.assertFalse(self.agent.bootstrap_ready)
        self.assertTrue(self.agent.reboot_required)

        req = ProvisionRequest()
        req.availability_zone = "test1"
        req.datastores = ["ds3", "ds4"]
        req.networks = ["Public"]
        addr = ServerAddress(host="localhost", port=2345)
        req.address = addr
        self.agent.update_config(req)
        self.assertTrue(self.agent.reboot_required)