Statistics
| Branch: | Revision:

wcn_emulator / test_generic.py @ c93edefb

History | View | Annotate | Download (2.34 KB)

1

    
2
import signal
3
import os 
4
from random import sample
5
from psutil import Process
6
from time import sleep
7

    
8
from mininet.log import info, error, debug, output 
9

    
10
class MininetTest(object):
11
    def __init__(self, mininet, path, duration):
12
        self.net = mininet
13
        self.pendingProc = {} 
14
        self.duration = duration
15
        self.prefix = ''
16
    
17
    def getHostSample(self, num):
18
        hosts = sample(self.net.values(), num)
19
        return hosts[:num]
20

    
21
    def getAllHosts(self):
22
        return self.net.values()
23

    
24
    def bgCmd(self, host, force_multiple_processes, *args):
25
        # here it's a little workaround for tracing the resulting pid
26
        # it launch the new process using the mininet interface
27
        # but it check the newly created process id using psutil
28
        host_proc = Process(host.pid)
29
        host_ps = set(host_proc.get_children())
30
        debug("Sending cmd: \n\t"+str(args)+"\n")
31
        if force_multiple_processes:
32
            host.waiting = False
33
        host.sendCmd(*(args+("&",)))
34
        sleep(0.5)
35
        try :
36
            pid = (set(host_proc.get_children()).difference(host_ps)).\
37
                    pop().pid
38
            info("BGProcess: "+str(pid)+"; ")
39
            self.pendingProc[pid] = host
40
        except:
41
            info("*** Unable to launch command:\n\t "+str(args))
42
            return None
43
        return pid
44

    
45
    def sendSig(self,pid,sig=signal.SIGTERM):
46
        try:
47
            info("Killing BGProcess: "+str(pid)+"; ")
48
            os.kill( pid, sig )
49
        except OSError:
50
            error("Error while killing process "+str(pid))
51
            pass
52

    
53
    def killAll(self, sig = signal.SIGKILL):
54
        for pid in self.pendingProc.keys():
55
            self.sendSig(pid, sig)
56
            #r = self.pendingProc[pid].monitor(100) # wait exiting
57

    
58
        self.pendingProc.clear()
59
        for host in self.net.values():
60
            host.waiting = False
61

    
62
    def setPrefix(self, name):
63
        self.prefix = str(name) + '_' + str(self.duration) + '/'
64
        if not os.path.exists(self.prefix):
65
                os.makedirs(self.prefix)
66

    
67
    def changePermissions(self):
68
        os.chmod(self.prefix, 0777)
69
        for root, dirs, files in os.walk(self.prefix):
70
            for dir in dirs:
71
                os.chmod(os.path.join(root, dir), 0777)
72
            for file in files:
73
                os.chmod(os.path.join(root, file), 0777)