def test_apply_delta(self): from juju.model import Model from juju.application import Application model = Model() model._connector = mock.MagicMock() delta = _make_delta('application', 'add', dict(name='foo')) # test add prev, new = model.state.apply_delta(delta) self.assertEqual( len(model.state.state[delta.entity][delta.get_id()]), 1) self.assertIsNone(prev) self.assertIsInstance(new, Application) # test remove delta.type = 'remove' prev, new = model.state.apply_delta(delta) # length of the entity history deque is now 3: # - 1 for the first delta # - 1 for the second delta # - 1 for the None sentinel appended after the 'remove' self.assertEqual( len(model.state.state[delta.entity][delta.get_id()]), 3) self.assertIsInstance(new, Application) # new object is falsy because its data is None self.assertFalse(new) self.assertIsInstance(prev, Application) self.assertTrue(prev)
def test_relation_does_not_match_anything(self): model = Model() model._connector = mock.MagicMock() delta = _make_delta('relation', 'bar', dict(id="uuid-1234", name='foo', endpoints=[{"application-name": "foo"}])) model.state.apply_delta(delta) rel = Relation("uuid-1234", model) self.assertFalse(rel.matches(["xxx"]))
async def test_hostname(mock_cf): model = Model() model._connector = mock.MagicMock() model.state = mock.MagicMock() # Calling hostname() when no information is available (e.g. targeting # an older controller, agent not started yet etc.) should return None model.state.entity_data = mock.MagicMock(return_value={}) mach = Machine('test', model) assert mach.hostname is None model.state.entity_data = mock.MagicMock(return_value={ 'hostname': 'thundering-herds', }) assert mach.hostname == 'thundering-herds'
async def test_unit_is_leader(mock_cf): tests = [ { 'applications': { 'test': { 'units': { 'test/0': { 'subordinates': { 'test-sub/0': { 'leader': True } } } } }, 'test-sub': { 'subordinate-to': [ 'test' ] } }, 'description': "Tests that subordinate units reports is leader correctly", 'unit': 'test-sub/0', 'rval': True }, { 'applications': { 'test': { 'units': { 'test/0': { 'leader': True } } } }, 'description': "Tests that unit reports is leader correctly", 'unit': 'test/0', 'rval': True }, { 'applications': {}, 'description': "Tests that non existent apps return False as leader", 'unit': 'test/0', 'rval': False }, { 'applications': { 'test': { 'units': {} } }, 'description': "Tests that apps with no units report False as leader", 'unit': 'test/0', 'rval': False }, { 'applications': { 'test': { 'units': { 'test/0': { 'subordinates': { 'test-sub/0': {} } } } }, 'test1': { 'units': { 'test1/0': { 'subordinates': { 'test-sub/1': { 'leader': True } } } } }, 'test-sub': { 'subordinate-to': [ 'test', 'test1' ] } }, 'description': "Tests that subordinate units of multiple applications reports is leader correctly", 'unit': 'test-sub/1', 'rval': True }, { 'applications': { 'test': { 'units': { 'test/0': { 'subordinates': { 'test-sub/0': { 'leader': True } } } } }, 'test1': { 'units': { 'test1/0': { 'subordinates': { 'test-sub/1': {} } } } }, 'test-sub': { 'subordinate-to': [ 'test', 'test1' ] } }, 'description': "Tests that subordinate units of multiple applications reports is leader correctly", 'unit': 'test-sub/1', 'rval': False } ] model = Model() model._connector = mock.MagicMock() for test in tests: status = mock.Mock() status.applications = test['applications'] client_facade = mock_cf.from_connection() client_facade.FullStatus = base.AsyncMock(return_value=status) unit = Unit("test", model) unit.name = test['unit'] rval = await unit.is_leader_from_status() assert rval == test['rval']