def test_wait(self, _status, juju_agent): status = _status.return_value = deepcopy(mock_status) def set_state(which, key, value): status['services']['meteor']['units']['meteor/0'][which][key] = value t = Talisman(['meteor'], timeout=self.timeout) t.wait(self.timeout) set_state('workload-status', 'current', 'unknown') t.wait(self.timeout) set_state('workload-status', 'current', 'blocked') t.wait(self.timeout) set_state('workload-status', 'current', 'active') set_state('agent-status', 'current', 'executing') set_state('juju-status', 'current', 'executing') self.assertRaises(TimeoutError, t.wait, self.timeout) set_state('agent-status', 'current', 'idle') set_state('agent-status', 'since', datetime.now().strftime('%d %b %Y %H:%M:%S')) set_state('juju-status', 'current', 'idle') set_state('juju-status', 'since', datetime.now().strftime('%d %b %Y %H:%M:%S')) self.assertRaises(TimeoutError, t.wait, self.timeout) t = Talisman(['old'], timeout=self.timeout) juju_agent.return_value = None self.assertRaises(TimeoutError, t.wait, self.timeout) juju_agent.return_value = {'hook': 'foo'} self.assertRaises(TimeoutError, t.wait, self.timeout) juju_agent.return_value = {} t.wait(self.timeout)
def test_wait_for_status_containers(self, status): status.return_value = { 'machines': { '0': { 'containers': { '0/lxc/1': {'agent-state': 'started'}, '0/lxc/2': {'agent-state': 'pending'}, }, }, }, 'services': { 'foo': { 'units': { 'foo/0': { 'machine': '0/lxc/1', 'public-address': 'something', }, }, }, 'bar': { 'units': { 'bar/0': { 'machine': '0/lxc/2', 'public-address': 'something', }, }, }, }, } talisman = Talisman([], timeout=self.timeout) talisman.wait_for_status('env', ['foo'], self.timeout) self.assertRaises(TimeoutError, talisman.wait_for_status, 'env', ['bar'], self.timeout)
def test_subordinate(self, default_env, upload_scripts, wait_for_status): default_env.return_value = 'local' wait_for_status.return_value = mock_status sentry = Talisman(['meteor', 'rsyslog-forwarder']) self.assertTrue('rsyslog-forwarder/0' in sentry.unit)
def test_getitem(self, default_env, upload_scripts, wait_for_status): default_env.return_value = 'local' wait_for_status.return_value = mock_status sentry = Talisman(['meteor']) self.assertEqual(sentry['meteor/0'], sentry.unit['meteor/0']) self.assertEqual(sentry['meteor'], list(sentry.unit.values()))
def test_init(self, default_env, upload_scripts, wait_for_status): default_env.return_value = 'local' wait_for_status.return_value = mock_status sentry = Talisman(['meteor']) self.assertTrue('meteor/0' in sentry.unit) self.assertTrue('meteor/1' in sentry.unit)
def test_wait_for_status_containers(self, status): status.return_value = { 'machines': { '0': { 'containers': { '0/lxc/1': { 'agent-state': 'started', 'juju-status': { 'current': 'started' } }, '0/lxc/2': { 'agent-state': 'pending', 'juju-status': { 'current': 'pending' } }, }, }, }, 'services': { 'foo': { 'units': { 'foo/0': { 'machine': '0/lxc/1', 'public-address': 'something', }, }, }, 'bar': { 'units': { 'bar/0': { 'machine': '0/lxc/2', 'public-address': 'something', }, }, }, }, } talisman = Talisman([], timeout=self.timeout) talisman.wait_for_status('env', ['foo'], self.timeout) self.assertRaises(TimeoutError, talisman.wait_for_status, 'env', ['bar'], self.timeout)
def test_getitem(self, default_env, upload_scripts, wait_for_status): default_env.return_value = 'local' wait_for_status.return_value = mock_status sentry = Talisman(['meteor'], timeout=self.timeout) self.assertEqual(sentry['meteor/0'], sentry.unit['meteor/0']) self.assertSetEqual(set(sentry['meteor']), set(sentry.unit.values())) with self.assertRaises(KeyError): sentry['meteor/99'] # Non-existent unit. # Non-existent service, or service with no units. self.assertEqual(sentry['invalid'], []) with self.assertRaises(KeyError): sentry['invalid/99'] # Non-existent unit in non-existent service.
def test_wait_for_status(self, status): status.return_value = mock_status talisman = Talisman([], timeout=self.timeout) talisman.wait_for_status('env', ['meteor'], self.timeout) talisman.wait_for_status('env', ['old'], self.timeout) talisman.wait_for_status('env', ['sub'], self.timeout) talisman.wait_for_status('env', ['unsub'], self.timeout) self.assertRaises(TimeoutError, talisman.wait_for_status, 'env', ['pending'], self.timeout) self.assertRaises(TimeoutError, talisman.wait_for_status, 'env', ['nopublic'], self.timeout) self.assertRaisesRegexp(Exception, r'Error on unit.*hook failed', talisman.wait_for_status, 'env', ['errord'], self.timeout) self.assertRaisesRegexp(Exception, r'Error on unit.*hook failed', talisman.wait_for_status, 'env', ['olderrord'], self.timeout) self.assertRaises(TimeoutError, talisman.wait_for_status, 'env', ['subpend'], self.timeout)
def test_wait_for_messages(self, _status): status = _status.return_value = deepcopy(mock_status) t = Talisman([], timeout=self.timeout) def set_status(unit, message): service = unit.split('/')[0] status['services'][service]['units'][unit]['workload-status']['message'] = message t.wait_for_messages({'meteor': 'ready'}, self.timeout) t.wait_for_messages({'meteor': re.compile('r..dy')}, self.timeout) t.wait_for_messages({'meteor': {'ready'}}, self.timeout) t.wait_for_messages({'meteor': re.compile('ready|ok')}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': {'ready', 'ok'}}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok']}, self.timeout) set_status('meteor/0', 'ok') self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': 'ready'}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': re.compile('r..dy')}, self.timeout) t.wait_for_messages({'meteor': {'ready'}}, self.timeout) t.wait_for_messages({'meteor': re.compile('ready|ok')}, self.timeout) t.wait_for_messages({'meteor': {'ready', 'ok'}}, self.timeout) t.wait_for_messages({'meteor': ['ready', 'ok']}, self.timeout) set_status('meteor/1', 'ok') self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': 'ready'}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': re.compile('r..dy')}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': {'ready'}}, self.timeout) t.wait_for_messages({'meteor': re.compile('ready|ok')}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': {'ready', 'ok'}}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok']}, self.timeout) set_status('meteor/0', 'ready') status['services']['meteor']['units']['meteor/2'] = { 'workload-status': { 'current': 'active', 'message': 'working', }, } t.wait_for_messages({'meteor': {'ready', 'ok'}}) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok']}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok', 'ready']}, self.timeout) t.wait_for_messages({'rsyslog-forwarder': 'rsyslog'}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'rsyslog-forwarder': 'ready'}, self.timeout) self.assertRaises(UnsupportedError, t.wait_for_messages, {'old': 'ready'}, self.timeout)
def test_wait_for_messages(self, _status): status = _status.return_value = deepcopy(mock_status) t = Talisman([], timeout=self.timeout) def set_status(unit, message): service = unit.split('/')[0] status['services'][service]['units'][unit]['workload-status'][ 'message'] = message t.wait_for_messages({'meteor': 'ready'}, self.timeout) t.wait_for_messages({'meteor': re.compile('r..dy')}, self.timeout) t.wait_for_messages({'meteor': {'ready'}}, self.timeout) t.wait_for_messages({'meteor': re.compile('ready|ok')}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': {'ready', 'ok'}}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok']}, self.timeout) set_status('meteor/0', 'ok') self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': 'ready'}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': re.compile('r..dy')}, self.timeout) t.wait_for_messages({'meteor': {'ready'}}, self.timeout) t.wait_for_messages({'meteor': re.compile('ready|ok')}, self.timeout) t.wait_for_messages({'meteor': {'ready', 'ok'}}, self.timeout) t.wait_for_messages({'meteor': ['ready', 'ok']}, self.timeout) set_status('meteor/1', 'ok') self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': 'ready'}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': re.compile('r..dy')}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': {'ready'}}, self.timeout) t.wait_for_messages({'meteor': re.compile('ready|ok')}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': {'ready', 'ok'}}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok']}, self.timeout) set_status('meteor/0', 'ready') status['services']['meteor']['units']['meteor/2'] = { 'workload-status': { 'current': 'active', 'message': 'working', }, } t.wait_for_messages({'meteor': {'ready', 'ok'}}) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok']}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'meteor': ['ready', 'ok', 'ready']}, self.timeout) t.wait_for_messages({'rsyslog-forwarder': 'rsyslog'}, self.timeout) self.assertRaises(TimeoutError, t.wait_for_messages, {'rsyslog-forwarder': 'ready'}, self.timeout) self.assertRaises(UnsupportedError, t.wait_for_messages, {'old': 'ready'}, self.timeout)
def test_wait(self, _status, juju_agent): status = _status.return_value = deepcopy(mock_status) def set_state(which, key, value): status['services']['meteor']['units']['meteor/0'][which][ key] = value t = Talisman(['meteor'], timeout=self.timeout) t.wait(self.timeout) set_state('workload-status', 'current', 'unknown') t.wait(self.timeout) set_state('workload-status', 'current', 'blocked') t.wait(self.timeout) set_state('workload-status', 'current', 'active') set_state('agent-status', 'current', 'executing') set_state('juju-status', 'current', 'executing') self.assertRaises(TimeoutError, t.wait, self.timeout) set_state('agent-status', 'current', 'idle') set_state('agent-status', 'since', datetime.now().strftime('%d %b %Y %H:%M:%S')) set_state('juju-status', 'current', 'idle') set_state('juju-status', 'since', datetime.now().strftime('%d %b %Y %H:%M:%S')) self.assertRaises(TimeoutError, t.wait, self.timeout) t = Talisman(['old'], timeout=self.timeout) juju_agent.return_value = None self.assertRaises(TimeoutError, t.wait, self.timeout) juju_agent.return_value = {'hook': 'foo'} self.assertRaises(TimeoutError, t.wait, self.timeout) juju_agent.return_value = {} t.wait(self.timeout)