Statistics
| Branch: | Revision:

wcn_emulator / nepa_test.py @ 81bfe841

History | View | Annotate | Download (5.57 KB)

1
#!/usr/bin/env python
2
import sys
3
import inherit_config_parser
4
import ConfigParser
5
import inspect
6
import StringIO
7

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

    
10
from os import path
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
class conf(parameters):
19
    def checkCorrectness(self):
20
        self.checkNeededParams()
21
        return True
22

    
23

    
24
class ConfigurationFile():
25

    
26
    mandatoryOptions = {"testModule": None, "testClass": None, "times": 1}
27
    confParams = {}
28
    className = None
29

    
30
    def __init__(self, fileName, stanza, overrideOption=""):
31
        # check if filename esists
32
        if not path.isfile(fileName):
33
            error("Can not open the configuration file: " + fileName + "\n")
34
            sys.exit(1)
35
        self.parser = inherit_config_parser.InheritConfigParser()
36
        self.parser.optionxform = str
37
        self.parser.read(fileName)
38

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

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

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

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

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

    
71
        if overrideOption:
72
            options = overrideOption.replace(",", "\n")
73
            overrideConf = StringIO.StringIO("[DEFAULT]\n" + options + "\n")
74
            tmpParser = ConfigParser.ConfigParser()
75
            tmpParser.optionxform = str
76
            tmpParser.readfp(overrideConf)
77
            for name, value in tmpParser.defaults().items():
78
                print name, value
79
                self.confParams[name] = value
80

    
81
    def getConfigurations(self, name, raiseError=False):
82
        try:
83
            r = self.parser.get(self.testName, name)
84
        except ConfigParser.NoOptionError:
85
            if raiseError:
86
                error("No option \'" + name + "\' found!\n")
87
                sys.exit()
88
            else:
89
                return None
90
        return r
91

    
92

    
93
def link_conf(conf):
94
    link_opts = {}
95
    if conf.getConfigurations("link_bw"):
96
        link_opts["bw"] = int(conf.getConfigurations("link_bw"))
97
    if conf.getConfigurations("link_mean_delay"):
98
        link_opts["delay"] = (conf.getConfigurations("link_mean_delay"))
99
    if conf.getConfigurations("link_delay_sd"):
100
        link_opts["jitter"] = (conf.getConfigurations("link_delay_sd"))
101
    if conf.getConfigurations("link_delay_distribution"):
102
        link_opts["delay_distribution"] = \
103
            (conf.getConfigurations("link_delay_distribution"))
104
    if conf.getConfigurations("link_loss"):
105
        link_opts["loss"] = (conf.getConfigurations("link_loss"))
106
    return link_opts
107

    
108

    
109
def nepa_test():
110
    setLogLevel('info')
111
    need = [
112
        ("-f", ["configFile", True, "",
113
         "file with the available configurations", str]),
114
        ("-t", ["testName", True, "",
115
         "base name for test output", str])
116
        ]
117
    opt = [
118
        ("-d", ["drawGraph", False, False,
119
         "draw the graph before you run the test", int]),
120
        ("-g", ["graphFile", True, "",
121
         "file with the topology (overrides configuration)", str]),
122
        ("-o", ["overrideOption", True, "",
123
         "comma separated list of options to override in the ini file \
124
         (ex: a=10,b=100)", str]),
125
        ]
126

    
127
    P = conf(path.basename(__file__), need, opt)
128
    P.parseArgs()
129
    if not P.checkCorrectness():
130
        P.printUsage()
131
        sys.exit(1)
132

    
133
    configFile = P.getParam("configFile")
134
    testName = P.getParam("testName")
135
    C = ConfigurationFile(configFile, testName, P.getParam("overrideOption"))
136
    # parse the conf file
137
    networkGraph = P.getParam("graphFile")
138
    if networkGraph == "":
139
        networkGraph = C.getConfigurations("graphDefinition")
140
        if not networkGraph:
141
            error("No graph topology specified in conf file or command line!\n")
142
            sys.exit(1)
143
    drawGraph = P.getParam("drawGraph")
144

    
145
    link_opts = link_conf(C)
146

    
147
    net = GraphNet(networkGraph, draw=drawGraph, link_opts=link_opts)
148
    net.start()
149
    net.enableForwarding()
150
    enableShortestRoutes = C.getConfigurations("enableShortestRoutes")
151
    if not enableShortestRoutes or enableShortestRoutes.lower() == "true":
152
        net.setShortestRoutes()
153
    # CLI(net)
154
    graphname = networkGraph.split('/')[-1].split('.')[0]
155
    testPath = testName + "_" + graphname + "_" + str(int(time()))
156
    for i in range(int(C.getConfigurations("times"))):
157
        info("+++++++ Round: "+str(i+1) + '\n')
158
        test = C.className(net, testPath, C.confParams)
159
        test.runTest()
160
    net.stop()
161
    test.changePermissions()
162
    info("*** Done with experiment: " + testName + "\n")
163

    
164
if __name__ == "__main__":
165
    nepa_test()