def state_check(*args, **kwargs): """ Diskcache wrapper for checking nodeState before and after the update_runner() tries to grab new data. """ from node_tools import state_data as st get_state(cache) prev_state = AttrDict.from_nested_dict(st.fpnState) if not prev_state.online: logger.warning('nodeState not initialized (node not online)') else: logger.info('Node online with id: {}'.format(prev_state.fpn_id)) result = func(*args, **kwargs) get_state(cache) next_state = AttrDict.from_nested_dict(st.fpnState) if not next_state.online and not prev_state.online: logger.warning('nodeState still not initialized (node not online)') elif next_state.online and prev_state.online: get_state_values(prev_state, next_state) if st.changes: logger.info('NETSTATE: diff is {}'.format(st.changes)) put_state_msg('CONFIG') if next_state.fallback: logger.error('NETSTATE: fallback mode is True (network suspect)') return result
def test_do_cleanup(): from node_tools import state_data as stest get_state(cache) nodeState = AttrDict.from_nested_dict(stest.fpnState) # print(nodeState) with pytest.raises(FileNotFoundError): do_cleanup('./bin', '127.0.0.1') with pytest.raises(FileNotFoundError): do_cleanup(path='./bin')
def test_get_state(): from node_tools import state_data as stest get_state(cache) nodeState = AttrDict.from_nested_dict(stest.fpnState) assert isinstance(nodeState, dict) assert nodeState.online assert nodeState.fpn_id == 'beefea68e6' assert not nodeState.fallback assert nodeState.fpn0 assert nodeState.fpn1 assert nodeState.moon_id0 == 'deadd738e6' assert nodeState['fpn_id0'] == 'b6079f73c63cea29' assert nodeState['fpn_id1'] == 'b6079f73ca8129ad'
def test_get_state_values(): from node_tools import state_data as stest from node_tools.data_funcs import get_state_values assert isinstance(stest.changes, list) assert not stest.changes get_state(cache) prev_state = AttrDict.from_nested_dict(stest.fpnState) assert prev_state.online assert prev_state.fpn0 assert prev_state.fpn1 # induce a change stest.fpnState.update(fpn1=False, fpn_id1=None) next_state = AttrDict.from_nested_dict(stest.fpnState) assert not next_state.fpn1 assert not stest.changes # now we should see old/new values in the state diff get_state_values(prev_state, next_state, True) assert isinstance(stest.changes, tuple) assert len(stest.changes) == 2 assert len(stest.changes[0]) == 2 get_state_values(prev_state, next_state) assert len(stest.changes[0]) == 2 # reset shared state vars stest.changes = [] stest.fpnState.update(fpn1=True, fpn_id1='b6079f73ca8129ad') assert stest.fpnState == prev_state # induce two changes stest.fpnState.update(fpn0=False, fpn1=False, fpn_id0=None, fpn_id1=None) next_state = AttrDict.from_nested_dict(stest.fpnState) assert not next_state.fpn0 assert not next_state.fpn1 assert not stest.changes # now we should see only new values for both changes in the state diff get_state_values(prev_state, next_state) assert isinstance(stest.changes, tuple) assert len(stest.changes) == 4 assert len(stest.changes[0]) == 2 # reset shared state vars stest.changes = []