Statistics
| Branch: | Revision:

wcn_emulator / nepa_test.py @ b3454700

History | View | Annotate | Download (5.11 KB)

1
#!/usr/bin/env python
2
import sys
3
from os import path
4
# add the nepa_test directory to sys path
5
sys.path.append(path.dirname(sys.argv[0]))
6
import ConfigParser
7
import inspect
8

    
9
sys.path.append('test_code')
10

    
11
from time import time
12

    
13
from parameters_parser import parameters
14
from network_builder import *
15
from test_code import *
16

    
17

    
18

    
19
class conf(parameters):
20
    def checkCorrectness(self):
21
        self.checkNeededParams()
22
        return True
23

    
24
class configurationFile():
25

    
26
    mandatoryOptions = {"testModule":None
27
            , "testClass":None, "times":1}
28
    confParams = {}
29
    className = None
30
    def __init__(self, fileName, stanza):
31
        # check if filename esists
32
        try:
33
            fd =  open(fileName, "r")
34
        except IOError:
35
            error("Can not open the configuration file: " + fileName\
36
                + "\n")
37
            sys.exit(1)
38
        self.parser = ConfigParser.SafeConfigParser()
39
        self.parser.read(fileName)
40

    
41
        self.testName = stanza 
42
        if stanza not in self.parser.sections():
43
            error("Can not find configuration " + stanza \
44
                    + " in file " + fileName + "\n")
45
            sys.exit(1)
46
        for o in self.mandatoryOptions:
47
            self.mandatoryOptions[o] = \
48
                self.getConfigurations(o, raiseError=True)
49

    
50
        moduleName = "test_code." + self.mandatoryOptions['testModule']
51
        if moduleName not in sys.modules:
52
            errorString = "ERROR: no " \
53
                + self.mandatoryOptions['testModule'] \
54
                + " module  has been loaded!\n"
55
            error(errorString)
56
            sys.exit(1)
57

    
58
        if self.mandatoryOptions['testClass'] not in \
59
                zip(*inspect.getmembers(sys.modules[moduleName]))[0]:
60
            errorString = "ERROR: no " \
61
                + self.mandatoryOptions['testClass'] \
62
                + " simulation class is present in "\
63
                + moduleName + "\n"
64
            error(errorString)
65
            sys.exit(1)
66

    
67
        self.className = getattr(sys.modules[moduleName],
68
            self.mandatoryOptions['testClass'])
69

    
70
        for name, value in self.parser.items(self.testName):
71
            self.confParams[name] = value
72

    
73
    def getConfigurations(self, name, raiseError=False):
74
        try:
75
            r = self.parser.get(self.testName, name)
76
        except ConfigParser.NoOptionError:
77
            if raiseError:
78
                error("No option \'" + name + "\' found!\n")
79
                sys.exit()
80
            else:
81
                return None
82
        return r
83

    
84

    
85
def link_conf(conf):
86
    link_opts = {}
87
    if conf.getConfigurations("link_bw"):
88
        link_opts["bw"] = int(conf.getConfigurations("link_bw"))
89
    if conf.getConfigurations("link_mean_delay"):
90
        link_opts["delay"] = (conf.getConfigurations("link_mean_delay"))
91
    if conf.getConfigurations("link_delay_sd"):
92
        link_opts["jitter"] = (conf.getConfigurations("link_delay_sd"))
93
    if conf.getConfigurations("link_delay_distribution"):
94
        link_opts["delay_distribution"] = (conf.getConfigurations("link_delay_distribution"))
95
    if conf.getConfigurations("link_loss"):
96
        link_opts["loss"] = (conf.getConfigurations("link_loss"))
97
    return link_opts
98

    
99
def nepa_test():
100
    setLogLevel('info')
101
    need = [
102
            ("-f", ["configFile", True, "",
103
                "file with the available configurations", str]),
104
            ("-t", ["testName", True, "",
105
                "base name for test output", str])
106
           ]
107
    opt = [
108
            ("-d", ["drawGraph", False, False,
109
                "draw the graph before you run the test", int]),
110
            ("-g", ["graphFile", True, "",
111
                "file with the topology (overrides configuration)", str])
112
          ]
113

    
114
    P = conf(path.basename(__file__),need, opt)
115
    P.parseArgs()
116
    if P.checkCorrectness() == False:
117
        P.printUsage()
118
        sys.exit(1)
119

    
120
    configFile = P.getParam("configFile")
121
    testName = P.getParam("testName")
122
    C = configurationFile(configFile, testName)
123
    #import code
124
    #code.interact(local=locals())
125
    # parse the conf file
126
    networkGraph = P.getParam("graphFile")
127
    if networkGraph == "":
128
        networkGraph = C.getConfigurations("graphDefinition")
129
        if networkGraph == None:
130
            error("No graph topology specified in config file or command " + \
131
                "line!\n")
132
            sys.exit(1)
133
    drawGraph = P.getParam("drawGraph")
134

    
135
    link_opts = link_conf(C)
136

    
137
    net = GraphNet(networkGraph, draw = drawGraph, link_opts = link_opts)
138
    net.start()
139
    net.enableForwarding()
140
    enableShortestRoutes = C.getConfigurations("enableShortestRoutes")
141
    if enableShortestRoutes == None or enableShortestRoutes.lower() == "true":
142
        net.setShortestRoutes()
143
    #CLI(net)
144
    graphname = networkGraph.split('/')[-1].split('.')[0]
145
    testPath = testName + "_" + graphname + "_" + str(int(time())) 
146
    for i in range(int(C.getConfigurations("times"))):
147
        info("+++++++ Round: "+str(i+1) + '\n')
148
        test = C.className(net, testPath, C.confParams)
149
        test.runTest()
150
    net.stop()
151
    test.changePermissions()
152
    info("*** Done with experiment: " + testName + "\n")
153

    
154
if __name__ == "__main__":
155
    nepa_test()