def get_constraints(self): """Get the machine's hardware constraints""" # Note: machine constraints should not be settable; they're a snapshot # of the constraints of the unit state for which they were created. (It # makes no sense to arbitrarily declare that an m1.small is now a # cc2.8xlarge, anyway.) esm = EnvironmentStateManager(self._client) constraint_set = yield esm.get_constraint_set() data = yield self._get_node_value("constraints", {}) returnValue(constraint_set.load(data))
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)
def push_env_constraints(self, *constraint_strs): esm = EnvironmentStateManager(self.client) constraint_set = yield esm.get_constraint_set() yield esm.set_constraints(constraint_set.parse(constraint_strs))