]>
Commit | Line | Data |
---|---|---|
2c8c4ce7 RP |
1 | #!/usr/bin/python |
2 | ||
3 | """ test for testing and profiling state manager """ | |
4 | ||
5 | import cProfile | |
6 | ||
7 | from ifupdown.networkinterfaces import * | |
8 | from ifupdown.iface import * | |
9 | from ifupdown.statemanager import pickling | |
10 | import os | |
11 | ||
12 | ifaceobjdict = {} | |
13 | state_file = '/tmp/ifstatetest' | |
14 | ||
15 | def save_iface(ifaceobj): | |
16 | ifaceobjdict[ifaceobj.get_name()] = ifaceobj | |
17 | ||
18 | def read_default_iface_config(): | |
19 | """ Reads default network interface config /etc/network/interfaces. """ | |
20 | nifaces = networkInterfaces() | |
21 | nifaces.subscribe('iface_found', save_iface) | |
22 | nifaces.load() | |
23 | ||
24 | def save_state(): | |
25 | try: | |
26 | with open(state_file, 'w') as f: | |
27 | for ifaceobj in ifaceobjdict.values(): | |
28 | pickling.save_obj(f, ifaceobj) | |
29 | except: | |
30 | raise | |
31 | ||
32 | def load_state(): | |
33 | global ifaceobjdict | |
34 | ||
35 | if not os.path.exists(state_file): | |
36 | return | |
37 | ||
38 | del ifaceobjdict | |
39 | ifaceobjdict = {} | |
40 | ||
41 | # Read all ifaces from file | |
42 | for ifaceobj in pickling.load(state_file): | |
43 | save_iface(ifaceobj) | |
44 | ||
45 | ||
46 | print 'Reading iface config files ..' | |
47 | cProfile.run('read_default_iface_config()') | |
48 | print 'number of objects: %d' %len(ifaceobjdict) | |
49 | ||
50 | print 'saving iface state ..' | |
51 | cProfile.run('save_state()') | |
52 | ||
53 | print 'loading iface state ..' | |
54 | cProfile.run('load_state()') | |
55 | print 'number of objects: %d' %len(ifaceobjdict) | |
56 |