Example #1
0
    def test_add_unit(self):
        """
        'juju add-unit <service_name>' will add a new service
        unit of the given service.
        """
        unit_names = yield self.service_state1.get_unit_names()
        self.assertEqual(len(unit_names), 1)

        finished = self.setup_cli_reactor()
        self.setup_exit(0)
        self.mocker.replay()
        # trash environment to check syncing
        yield self.client.delete("/environment")
        main(["add-unit", "mysql"])
        yield finished

        # verify the env state was synced
        esm = EnvironmentStateManager(self.client)
        yield esm.get_config()

        # verify the unit and its machine assignment.
        unit_names = yield self.service_state1.get_unit_names()
        self.assertEqual(len(unit_names), 2)

        topology = yield self.get_topology()
        unit = yield self.service_state1.get_unit_state("mysql/1")
        machine_id = topology.get_service_unit_machine(self.service_state1.internal_id, unit.internal_id)
        self.assertNotEqual(machine_id, None)
        self.assertIn("Unit 'mysql/1' added to service 'mysql'", self.output.getvalue())
        yield self.assert_machine_assignments("mysql", [1, 2])
    def test_terminate_unused_machine(self):
        """Verify a typical allocation, unassignment, and then termination."""
        wait_on_reactor_stopped = self.setup_cli_reactor()
        self.setup_exit(0)
        self.mocker.replay()
        wordpress_service_state = \
            yield self.add_service_from_charm("wordpress")
        wordpress_unit_state = yield wordpress_service_state.add_unit_state()
        wordpress_machine_state = yield self.add_machine_state()
        yield wordpress_unit_state.assign_to_machine(wordpress_machine_state)
        riak_service_state = yield self.add_service_from_charm("riak")
        riak_unit_state = yield riak_service_state.add_unit_state()
        riak_machine_state = yield self.add_machine_state()
        yield riak_unit_state.assign_to_machine(riak_machine_state)
        mysql_service_state = yield self.add_service_from_charm("mysql")
        mysql_unit_state = yield mysql_service_state.add_unit_state()
        mysql_machine_state = yield self.add_machine_state()
        yield mysql_unit_state.assign_to_machine(mysql_machine_state)
        yield wordpress_unit_state.unassign_from_machine()
        yield mysql_unit_state.unassign_from_machine()
        yield self.assert_machine_states([0, 1, 2, 3], [])

        # trash environment to check syncing
        yield self.client.delete("/environment")
        main(["terminate-machine", "1", "3"])
        yield wait_on_reactor_stopped

        # check environment synced
        esm = EnvironmentStateManager(self.client)
        yield esm.get_config()

        self.assertIn(
            "Machines terminated: 1, 3", self.output.getvalue())
        yield self.assert_machine_states([0, 2], [1, 3])
Example #3
0
    def test_deploy_sends_environment(self):
        """Uses charm name as service name if possible."""
        environment = self.config.get("firstenv")
        yield deploy.deploy(
            self.config, environment, self.unbundled_repo_path, "local:sample",
            None, logging.getLogger("deploy"), [])

        env_state_manager = EnvironmentStateManager(self.client)
        env_config = yield env_state_manager.get_config()

        self.assertEquals(yaml.load(env_config.serialize("firstenv")),
                          yaml.load(self.config.serialize("firstenv")))
Example #4
0
    def test_deploy_sends_environment(self):
        """Uses charm name as service name if possible."""
        environment = self.config.get("firstenv")

        yield deploy.deploy(self.config, environment, self.unbundled_repo_path,
                            "local:sample", None, logging.getLogger("deploy"))

        env_state_manager = EnvironmentStateManager(self.client)
        env_config = yield env_state_manager.get_config()

        self.assertEquals(yaml.load(env_config.serialize("firstenv")),
                          yaml.load(self.config.serialize("firstenv")))
    def test_environment_constraint(self):
        yield self.client.delete("/environment")
        finished = self.setup_cli_reactor()
        self.setup_exit(0)
        self.mocker.replay()
        main(["set-constraints", "arch=arm", "cpu=any"])
        yield finished

        esm = EnvironmentStateManager(self.client)
        yield esm.get_config()
        constraints = yield esm.get_constraints()
        self.assertEquals(constraints, {
            "ubuntu-series": None,
            "provider-type": "dummy",
            "arch": "arm",
            "cpu": None,
            "mem": 512.0})
Example #6
0
class EnvironmentStateManagerTest(StateTestBase, EnvironmentsConfigTestBase):

    @inlineCallbacks
    def setUp(self):
        yield super(EnvironmentStateManagerTest, self).setUp()
        self.environment_state_manager = EnvironmentStateManager(self.client)
        self.write_config(SAMPLE_ENV)
        self.config.load()

    @inlineCallbacks
    def tearDown(self):
        yield super(EnvironmentStateManagerTest, self).tearDown()

    @inlineCallbacks
    def test_set_config_state(self):
        """
        The simplest thing the manager can do is serialize a given
        environment and save it in zookeeper.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")

        serialized = self.config.serialize("myfirstenv")
        content, stat = yield self.client.get("/environment")
        self.assertEquals(yaml.load(content), yaml.load(serialized))

    @inlineCallbacks
    def test_set_config_state_replaces_environment(self):
        """
        Setting the environment should also work with an existing
        environment.
        """
        yield self.client.create("/environment", "Replace me!")

        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")

        serialized = self.config.serialize("myfirstenv")
        content, stat = yield self.client.get("/environment")
        self.assertEquals(yaml.load(content), yaml.load(serialized))

    @inlineCallbacks
    def test_get_config(self):
        """
        We can also retrieve a loaded config from the environment.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.get_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    def test_get_config_when_missing(self):
        """
        get_config should blow up politely if the environment config
        is missing.
        """
        d = self.environment_state_manager.get_config()
        return self.assertFailure(d, EnvironmentStateNotFound)

    @inlineCallbacks
    def test_get_in_legacy_environment_no(self):
        yield self.push_default_config()
        esm = self.environment_state_manager
        legacy = yield esm.get_in_legacy_environment()
        self.assertEquals(legacy, False)

    @inlineCallbacks
    def test_get_in_legacy_environment_yes(self):
        yield self.push_default_config()
        self.client.delete("/constraints")
        esm = self.environment_state_manager
        legacy = yield esm.get_in_legacy_environment()
        self.assertEquals(legacy, True)

    def test_get_constraint_set_no_env(self):
        d = self.environment_state_manager.get_constraint_set()
        return self.assertFailure(d, EnvironmentStateNotFound)

    @inlineCallbacks
    def test_get_constraint_set(self):
        yield self.push_default_config()
        cs = yield self.environment_state_manager.get_constraint_set()
        constraints = cs.parse(["arch=any", "cpu=10"])
        self.assertEquals(constraints, {
            "ubuntu-series": None,
            "provider-type": "dummy",
            "arch": None,
            "cpu": 10.0,
            "mem": 512.0})

    def test_get_constraints_no_env(self):
        d = self.environment_state_manager.get_constraints()
        return self.assertFailure(d, EnvironmentStateNotFound)

    @inlineCallbacks
    def test_get_constraints_env_with_no_node(self):
        yield self.push_default_config()
        self.client.delete("/constraints")
        constraints = yield self.environment_state_manager.get_constraints()
        self.assertEquals(constraints.data, {})

    @inlineCallbacks
    def test_set_constraints(self):
        yield self.push_default_config()
        constraints = dummy_cs.parse(["cpu=any", "mem=32T"])
        yield self.environment_state_manager.set_constraints(constraints)
        roundtrip = yield self.environment_state_manager.get_constraints()
        self.assertEquals(roundtrip, constraints)
Example #7
0
class EnvironmentStateManagerTest(StateTestBase, EnvironmentsConfigTestBase):

    @inlineCallbacks
    def setUp(self):
        yield super(EnvironmentStateManagerTest, self).setUp()
        self.environment_state_manager = EnvironmentStateManager(self.client)
        self.write_config(SAMPLE_ENV)
        self.config.load()

    @inlineCallbacks
    def tearDown(self):
        yield super(EnvironmentStateManagerTest, self).tearDown()

    @inlineCallbacks
    def test_set_config_state(self):
        """
        The simplest thing the manager can do is serialize a given
        environment and save it in zookeeper.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")

        serialized = self.config.serialize("myfirstenv")
        content, stat = yield self.client.get("/environment")
        self.assertEquals(yaml.load(content), yaml.load(serialized))

    @inlineCallbacks
    def test_set_config_state_replaces_environment(self):
        """
        Setting the environment should also work with an existing
        environment.
        """
        yield self.client.create("/environment", "Replace me!")

        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")

        serialized = self.config.serialize("myfirstenv")
        content, stat = yield self.client.get("/environment")
        self.assertEquals(yaml.load(content), yaml.load(serialized))

    @inlineCallbacks
    def test_get_config(self):
        """
        We can also retrieve a loaded config from the environment.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.get_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    def test_get_config_when_missing(self):
        """
        get_config should blow up politely if the environment config
        is missing.
        """
        d = self.environment_state_manager.get_config()
        return self.assertFailure(d, EnvironmentStateNotFound)

    @inlineCallbacks
    def test_wait_for_config_pre_existing(self):
        """
        wait_for_config() should return the environment immediately
        if it already exists.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.wait_for_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    @inlineCallbacks
    def test_wait_for_config_pre_creation(self):
        """
        wait_for_config() should wait until the environment
        configuration is made available, and return it.
        """
        manager = self.environment_state_manager
        d = manager.wait_for_config()
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield d
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    @inlineCallbacks
    def test_wait_for_config_retries_on_race(self):
        """
        If the config seems to exist, but then goes away, try again.
        """
        mock_manager = self.mocker.patch(self.environment_state_manager)
        mock_manager.get_config()
        self.mocker.throw(EnvironmentStateNotFound())
        mock_manager.get_config()
        self.mocker.passthrough()
        self.mocker.replay()

        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.wait_for_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))
Example #8
0
class EnvironmentStateManagerTest(StateTestBase, EnvironmentsConfigTestBase):
    @inlineCallbacks
    def setUp(self):
        yield super(EnvironmentStateManagerTest, self).setUp()
        self.environment_state_manager = EnvironmentStateManager(self.client)
        self.write_config(SAMPLE_ENV)
        self.config.load()

    @inlineCallbacks
    def tearDown(self):
        yield super(EnvironmentStateManagerTest, self).tearDown()

    @inlineCallbacks
    def test_set_config_state(self):
        """
        The simplest thing the manager can do is serialize a given
        environment and save it in zookeeper.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")

        serialized = self.config.serialize("myfirstenv")
        content, stat = yield self.client.get("/environment")
        self.assertEquals(yaml.load(content), yaml.load(serialized))

    @inlineCallbacks
    def test_set_config_state_replaces_environment(self):
        """
        Setting the environment should also work with an existing
        environment.
        """
        yield self.client.create("/environment", "Replace me!")

        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")

        serialized = self.config.serialize("myfirstenv")
        content, stat = yield self.client.get("/environment")
        self.assertEquals(yaml.load(content), yaml.load(serialized))

    @inlineCallbacks
    def test_get_config(self):
        """
        We can also retrieve a loaded config from the environment.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.get_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    def test_get_config_when_missing(self):
        """
        get_config should blow up politely if the environment config
        is missing.
        """
        d = self.environment_state_manager.get_config()
        return self.assertFailure(d, EnvironmentStateNotFound)

    @inlineCallbacks
    def test_wait_for_config_pre_existing(self):
        """
        wait_for_config() should return the environment immediately
        if it already exists.
        """
        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.wait_for_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    @inlineCallbacks
    def test_wait_for_config_pre_creation(self):
        """
        wait_for_config() should wait until the environment
        configuration is made available, and return it.
        """
        manager = self.environment_state_manager
        d = manager.wait_for_config()
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield d
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))

    @inlineCallbacks
    def test_wait_for_config_retries_on_race(self):
        """
        If the config seems to exist, but then goes away, try again.
        """
        mock_manager = self.mocker.patch(self.environment_state_manager)
        mock_manager.get_config()
        self.mocker.throw(EnvironmentStateNotFound())
        mock_manager.get_config()
        self.mocker.passthrough()
        self.mocker.replay()

        manager = self.environment_state_manager
        yield manager.set_config_state(self.config, "myfirstenv")
        config = yield manager.wait_for_config()
        serialized1 = self.config.serialize("myfirstenv")
        serialized2 = config.serialize("myfirstenv")
        self.assertEquals(yaml.load(serialized1), yaml.load(serialized2))