diff --git a/tests/ifstatetest b/tests/ifstatetest new file mode 100755 index 0000000..a761e75 --- /dev/null +++ b/tests/ifstatetest @@ -0,0 +1,56 @@ +#!/usr/bin/python + +""" test for testing and profiling state manager """ + +import cProfile + +from ifupdown.networkinterfaces import * +from ifupdown.iface import * +from ifupdown.statemanager import pickling +import os + +ifaceobjdict = {} +state_file = '/tmp/ifstatetest' + +def save_iface(ifaceobj): + ifaceobjdict[ifaceobj.get_name()] = ifaceobj + +def read_default_iface_config(): + """ Reads default network interface config /etc/network/interfaces. """ + nifaces = networkInterfaces() + nifaces.subscribe('iface_found', save_iface) + nifaces.load() + +def save_state(): + try: + with open(state_file, 'w') as f: + for ifaceobj in ifaceobjdict.values(): + pickling.save_obj(f, ifaceobj) + except: + raise + +def load_state(): + global ifaceobjdict + + if not os.path.exists(state_file): + return + + del ifaceobjdict + ifaceobjdict = {} + + # Read all ifaces from file + for ifaceobj in pickling.load(state_file): + save_iface(ifaceobj) + + +print 'Reading iface config files ..' +cProfile.run('read_default_iface_config()') +print 'number of objects: %d' %len(ifaceobjdict) + +print 'saving iface state ..' +cProfile.run('save_state()') + +print 'loading iface state ..' +cProfile.run('load_state()') +print 'number of objects: %d' %len(ifaceobjdict) +