From 08179c9d2e90d488208079b9f14462d1e6ce9b11 Mon Sep 17 00:00:00 2001 From: roopa Date: Thu, 27 Mar 2014 16:53:32 -0700 Subject: [PATCH] state manager test Ticket: Reviewed By: Testing Done: --- tests/ifstatetest | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 tests/ifstatetest 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) +