def test_AddressStates(self): """ Test the AddressStates enumeration. """ from supvisors.ttypes import AddressStates self.assertEqual('UNKNOWN', AddressStates._to_string(AddressStates.UNKNOWN)) self.assertEqual('RUNNING', AddressStates._to_string(AddressStates.RUNNING)) self.assertEqual('SILENT', AddressStates._to_string(AddressStates.SILENT)) self.assertEqual('ISOLATING', AddressStates._to_string(AddressStates.ISOLATING)) self.assertEqual('ISOLATED', AddressStates._to_string(AddressStates.ISOLATED))
def state(self, newState): if self._state != newState: if self.check_transition(newState): self._state = newState self.logger.info('Address {} is {}'.format( self.address_name, self.state_string())) else: raise InvalidTransition('Address: transition rejected {} to {}'. format(self.state_string(), AddressStates._to_string(newState)))
def state(self, newState): if self._state != newState: if self.check_transition(newState): self._state = newState self.logger.info('Address {} is {}'.format( self.address_name, self.state_string())) else: raise InvalidTransition( 'Address: transition rejected {} to {}'.format( self.state_string(), AddressStates._to_string(newState)))
def test_timer_event(self): """ Test the handling of a timer event. """ from supvisors.context import Context from supvisors.ttypes import AddressStates context = Context(self.supvisors) with patch.object(self.supvisors.zmq.publisher, 'send_address_status') as mocked_send: # test address states excepting RUNNING: nothing happens for state in [x for x in AddressStates._values() if x != AddressStates.RUNNING]: context.on_timer_event() for address in context.addresses.values(): self.assertEqual(AddressStates.UNKNOWN, address.state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with recent local_time test_addresses = ['10.0.0.1', '10.0.0.3', '10.0.0.5'] for address_name in test_addresses: address = context.addresses[address_name] address._state = AddressStates.RUNNING address.local_time = time.time() context.on_timer_event() for address_name in test_addresses: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [x for x in context.addresses.keys() if x not in test_addresses]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with one recent local_time and with auto_fence activated address1 = context.addresses['10.0.0.3'] address1.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [x for x in test_addresses if x != '10.0.0.3']: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [x for x in context.addresses.keys() if x not in test_addresses]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(call(address1), mocked_send.call_args) # test with one other recent local_time and with auto_fence deactivated with patch.object(self.supvisors.options, 'auto_fence', False): mocked_send.reset_mock() address2 = context.addresses['10.0.0.5'] address2.local_time = time.time() - 100 address3 = context.addresses['10.0.0.1'] address3.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.SILENT, address2.state) self.assertEqual(AddressStates.SILENT, address3.state) self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [x for x in context.addresses.keys() if x not in test_addresses]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertItemsEqual([call(address2), call(address3)], mocked_send.call_args_list)
def test_transitions(self): """ Test the state transitions of AddressStatus. """ from supvisors.address import AddressStatus from supvisors.ttypes import AddressStates, InvalidTransition status = AddressStatus('10.0.0.1', self.supvisors.logger) for state1 in self.all_states: for state2 in self.all_states: # check all possible transitions from each state status._state = state1 if state2 in status._Transitions[state1]: status.state = state2 self.assertEqual(state2, status.state) self.assertEqual(AddressStates._to_string(state2), status.state_string()) elif state1 == state2: self.assertEqual(state1, status.state) else: with self.assertRaises(InvalidTransition): status.state = state2
def setUp(self): """ Create a logger that stores log traces. """ self.supvisors = DummySupvisors() from supvisors.ttypes import AddressStates self.all_states = AddressStates._values()
def setUp(self): """ Create a logger that stores log traces. """ self.supvisors = MockedSupvisors() from supvisors.ttypes import AddressStates self.all_states = AddressStates._values()
def state_string(self): """ Return the application state as a string. """ return AddressStates._to_string(self.state)
def test_timer_event(self): """ Test the handling of a timer event. """ from supvisors.context import Context from supvisors.ttypes import AddressStates context = Context(self.supvisors) with patch.object(self.supvisors.zmq.publisher, 'send_address_status') as mocked_send: # test address states excepting RUNNING: nothing happens for state in [ x for x in AddressStates._values() if x != AddressStates.RUNNING ]: context.on_timer_event() for address in context.addresses.values(): self.assertEqual(AddressStates.UNKNOWN, address.state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with recent local_time test_addresses = ['10.0.0.1', '10.0.0.3', '10.0.0.5'] for address_name in test_addresses: address = context.addresses[address_name] address._state = AddressStates.RUNNING address.local_time = time.time() context.on_timer_event() for address_name in test_addresses: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [ x for x in context.addresses.keys() if x not in test_addresses ]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(0, mocked_send.call_count) # test RUNNING address state with one recent local_time and with auto_fence activated address1 = context.addresses['10.0.0.3'] address1.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [x for x in test_addresses if x != '10.0.0.3']: self.assertEqual(AddressStates.RUNNING, context.addresses[address_name].state) for address_name in [ x for x in context.addresses.keys() if x not in test_addresses ]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertEqual(call(address1), mocked_send.call_args) # test with one other recent local_time and with auto_fence deactivated with patch.object(self.supvisors.options, 'auto_fence', False): mocked_send.reset_mock() address2 = context.addresses['10.0.0.5'] address2.local_time = time.time() - 100 address3 = context.addresses['10.0.0.1'] address3.local_time = time.time() - 100 context.on_timer_event() self.assertEqual(AddressStates.SILENT, address2.state) self.assertEqual(AddressStates.SILENT, address3.state) self.assertEqual(AddressStates.ISOLATING, address1.state) for address_name in [ x for x in context.addresses.keys() if x not in test_addresses ]: self.assertEqual(AddressStates.UNKNOWN, context.addresses[address_name].state) self.assertItemsEqual( [call(address2), call(address3)], mocked_send.call_args_list)