summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/Task/test.py.old
blob: 5a42e9a6c27658f9f93777fbd605835c879e3d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'''
Created on Sep 1, 2011

@author: steger
'''
import unittest
from Task import SubtaskManager, STRAT_FUNCTIONAL
from Example.Tools import sshping, sshtraceroute, sonomashortping, \
    sshmeminfo, sonomashortchirp, sshdf, sshhades
from Example.credentials import noviCredential, sonomaCredential,\
    novisaCredential, novihadesCredential
from Example.Metrics import RoundTripDelay, HopMeasurement, FreeMemory,\
    OnewayDelay, DiskUsage
from Example.Units import UM, unitless
from Resource.node import node
from Resource.path import path
from Example.Resources import PLdict, PLpaths
from time import sleep, time
from random import shuffle

TM = SubtaskManager(UM)

class Test(unittest.TestCase):
    cred_novi = noviCredential
    cred_siteadmin = novisaCredential
    cred_sonoma = sonomaCredential
    cred_hades = novihadesCredential

    def setUp(self):
        self.um = UM
        self.tm = TM

    def tearDown(self):
        pass
    
    def map_resource_to_parameter(self, tool, metric):
        pl = tool.parameters.copy()
        if issubclass(metric.resourcetype, node):
            resource = PLdict.values()[0]
            v, u = resource.get_ipaddress("eth0")
            pl.update("SourceAddress", v, u)
        elif issubclass(metric.resourcetype, path):
            resource = PLpaths[0]
            v, u = resource.source.get_ipaddress("eth0")
            pl.update("SourceAddress", v, u)
            v, u = resource.destination.get_ipaddress("eth0")
            pl.update("DestinationAddress", v, u)
        else:
            raise Exception("Unknown resource type")
        return pl

    def sshPingFg(self, tool):
        pl = self.map_resource_to_parameter(tool, RoundTripDelay)
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result")
        task.destroy()

    def test_sshPingFgNOVI(self):
        tool = sshping
        tool.parameters.update('Interface', "novi", unitless)
        self.sshPingFg(tool)

    def test_sshPingFgSubstrate(self):
        tool = sshping
        tool.parameters.update('Interface', "eth0", unitless)
        self.sshPingFg(tool)

    def test_sshTracerouteFg(self):
        tool = sshtraceroute
        pl = self.map_resource_to_parameter( tool, HopMeasurement )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
        task.destroy()

    def test_sshMeminfo(self):
        tool = sshmeminfo
        pl = self.map_resource_to_parameter( tool, FreeMemory )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result")
        task.destroy()

    def test_sshDiskinfo(self):
        tool = sshdf
        pl = self.map_resource_to_parameter( tool, DiskUsage )
        pl.update_by_list( DiskUsage.p_obligatory )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_siteadmin)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
        task.destroy()

    def test_sshMeminfoUserDefinedSampling(self):
        def sample5():
            for s in [.1, 1, 2, .5, -10]:
                yield s
        tool = sshmeminfo
        pl = self.map_resource_to_parameter( tool, FreeMemory )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi, samplingfunction = sample5)
        task.strategy = STRAT_FUNCTIONAL
        task.enable()
        while task.state == task.STATE_RUNNING:
            sleep(1)
        task.dataAdded.wait( 1 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
        task.destroy()

    def test_SONoMAShortPing(self):
        tool = sonomashortping
        pl = self.map_resource_to_parameter( tool, RoundTripDelay )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_sonoma, **tool.kwargs)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
        task.destroy()

    def test_SONoMAShortChirp(self):
        tool = sonomashortchirp
        pl = self.map_resource_to_parameter( tool, OnewayDelay )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_sonoma, **tool.kwargs)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
        task.destroy()

    def test_HADES(self):
        tool = sshhades
        pl = self.map_resource_to_parameter( tool, RoundTripDelay )
        pl.update('SourceAddress', '192.168.31.1', self.um.ipv4dotted)
        pl.update('DestinationAddress', '192.168.31.5', self.um.ipv4dotted)
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_hades, **tool.kwargs)
        task.enable()
        task.dataAdded.wait( 15 )
        self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
        task.destroy()

    def test_toggle(self):
        tool = sshmeminfo
        pl = self.map_resource_to_parameter( tool, FreeMemory )
        _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi)
        n = 10
        to = 1.5
        t = time()
        while n:
            task.enable()
            task.dataAdded.wait( to )
            task.disable()
            task.dataAdded.clear()
            n -= 1
        dt = time()-t
        self.assertGreater(len(task.data), 0, "measurement yielded empty result")
        self.assertGreater(dt, n*to, "measurement lasted longer than expected %f > %f" % (dt, n*to))
        task.destroy()

    def test_tm(self):
        tool = sshmeminfo
        pl = self.map_resource_to_parameter( tool, FreeMemory )
        N = len(self.tm)
        # deletion by task
        n= 10
        tasks = []
        while n:
            _, task =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi)
            n -= 1
            tasks.append(task)
        shuffle(tasks)
        while len(tasks):
            t = tasks.pop()
            tid = self.tm.getidentifier(t)
            self.tm.destroy(tid)
        # destroy by taskid
        n= 10
        taskids = []
        while n:
            taskid, _ =self.tm.generate(name = tool.name, driver = tool.driver, dataheader = tool.dataheaderdeclaration, 
                                     hookimplementations = tool.hooks, parameters = pl, credential = self.cred_novi)
            n -= 1
            taskids.append(taskid)
        shuffle(taskids)
        while len(taskids):
            tid = taskids.pop()
            self.tm.destroy(tid)
        self.assertEqual(N, len(self.tm), "some tasks were not removed from the SubtaskManager")


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_toggle']
    unittest.main()