Statistics
| Branch: | Revision:

wcn_emulator / test_generic.py @ 0597ba9b

History | View | Annotate | Download (2.22 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)).pop().pid
37
            info("BGProcess: "+str(pid)+"; ")
38
            self.pendingProc[pid] = host
39
        except:
40
            info("*** Unable to launch command:\n\t "+str(args))
41
            return None
42
        return pid
43

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

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

    
58
    def setPrefix(self, name):
59
        self.prefix = str(name) + '_' + str(self.duration) + '/'
60
        if not os.path.exists(self.prefix):
61
                os.makedirs(self.prefix)
62

    
63
    def changePermissions(self):
64
        os.chmod(self.prefix, 0777)
65
        for root, dirs, files in os.walk(self.prefix):
66
            for dir in dirs:
67
                os.chmod(os.path.join(root, dir), 0777)
68
            for file in files:
69
                os.chmod(os.path.join(root, file), 0777)
70