summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/Service/test.py
blob: efc888dd9499a35e546f7918434d644f8500e73a (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from __future__ import with_statement
'''
Created on Aug 10, 2011

@author: steger
'''
import site
site.addsitedir('../site-packages')

import unittest2
from rdflib import Graph, Namespace, Literal
from Example.credentials import noviCredentialIARGS
from Example.Platforms import FRAMEWORK
from Util.MonitoringQueryImpl import MonitoringQueryImpl


class Test(unittest2.TestCase):
    
    def setUp(self):
        self.MSI_planetlab = FRAMEWORK.getService('PlanetLab')
        self.PL_O = self.MSI_planetlab.service.ontology
        NS = self.PL_O.ns
        self.S = NS('stat')['UnmodifiedExtractOfFeatureSamples']
        self.F = NS('query')['Formatter_JSON']

    def tearDown(self):
        pass

    def test_echo(self):
        p1 = "PlanetLab"
        p2 = "FEDERICA"
        response = FRAMEWORK.getService(p1).echo(p1)
        print response
        i, o = response.split("->")
        got = (i.split("@")[-1].strip(), o.split("@")[-1].strip())
        expect = (p1, p1)
        self.assertEquals(expect, got, "Echo reply differs from expected (%s): %s" % (expect, response))
        response = FRAMEWORK.getService(p2).echo(p1)
        i, o = response.split("->")
        got = (i.split("@")[-1].strip(), o.split("@")[-1].strip())
        expect = (p2, p1)
        self.assertEquals(expect, got, "Echo reply differs from expected (%s): %s" % (expect, response))

    def test_measure(self):
        doc = "../monitoringmodel/monitoringQuery_example.owl" # % self.PL_O.baseurl
        with open(doc) as fp:
            q = fp.read()
#        response = self.MS_planetlab.measure(credential = mykeyring, query = q)
        response = self.MSI_planetlab.measure(credential = [noviCredentialIARGS], query = q)
        print response
        self.assertTrue(response, "Got nothing due to former errors")
        self.assertGreater(len(response.splitlines()), 26, "got empty measurement response")

    def new_g(self):
        g = Graph()
        for k, (_, ns) in self.PL_O.ontology.iteritems():
            g.bind(k, Namespace(ns))
        return g

    def save(self, fn, q):
        try:
            with open(fn, 'w') as f:
                f.write(q)
        except:
            pass

    def test_genq_mem(self):
        g = self.new_g()
        mns = Namespace("http://foo.bar/req.owl#")
        g.bind('q', mns)
        NS = self.PL_O.ns
        TYPE = NS('rdf')['type']
        Q = mns['measureMemoryInformation']
#        Q2 = mns['measureMemoryInformation2']
        R = mns['smilax1']
        I1 = mns['ifin']
        I2 = mns['ifout']
        IPADDR = Literal('150.254.160.19')
        ADDR = mns['smilax_address']
        g.add((Q, TYPE, NS('owl')['NamedIndividual']))
        g.add((Q, TYPE, NS('query')['BundleQuery']))
#        g.add((Q, NS('feature')['hasFeature'], NS('feature')['FreeMemory']))
        g.add((Q, NS('feature')['hasFeature'], NS('feature')['MemoryUtilization']))
        g.add((Q, NS('query')['hasResource'], R))

#        g.add((Q2, TYPE, NS('owl')['NamedIndividual']))
#        g.add((Q2, TYPE, NS('query')['BundleQuery']))
#        g.add((Q2, NS('feature')['hasFeature'], NS('feature')['AvailableMemory']))
#        g.add((Q2, NS('query')['hasResource'], R))

        g.add((Q, NS('stat')['hasSample'], self.S))
        g.add((Q, NS('query')['hasFormatter'], self.F))

#        g.add((Q2, NS('stat')['hasSample'], self.S))
#        g.add((Q2, NS('query')['hasFormatter'], self.F))

        
        g.add((R, TYPE, NS('core')['Node']))
        g.add((R, TYPE, NS('core')['Resource']))
        g.add((R, TYPE, NS('owl')['NamedIndividual']))
 
        g.add((R, NS('core')['hasInboundInterface'], I1))
        g.add((R, NS('core')['hasOutboundInterface'], I1))
        g.add((I1, TYPE, NS('core')['Interface']))
        g.add((I2, TYPE, NS('core')['Interface']))
        g.add((I1, NS('core')['hasIPv4Address'], ADDR))
        g.add((I2, NS('core')['hasIPv4Address'], ADDR))
        g.add((ADDR, TYPE, NS('owl')['NamedIndividual']))
        g.add((ADDR, TYPE, NS('unit')['IPAddress']))
        g.add((ADDR, NS('unit')['hasValue'], IPADDR))
        query = g.serialize()
        self.save(fn = "/tmp/genq_mem.owl", q = query)
        response = self.MSI_planetlab.measure(credential = [noviCredentialIARGS], query = query)
        print response
        self.assertGreater(len(response.splitlines()), 20, "got empty measurement response")

    def test_genq_cpu(self):
        g = self.new_g()
        mns = Namespace("http://foo.bar/req.owl#")
        g.bind('q', mns)
        NS = self.PL_O.ns
        TYPE = NS('rdf')['type']
        Q = mns['measureCPUInformation']
        R = mns['smilax1']
        I1 = mns['ifin']
        I2 = mns['ifout']
        IPADDR = Literal('150.254.160.19')
        ADDR = mns['smilax_address']
        g.add((Q, TYPE, NS('owl')['NamedIndividual']))
        g.add((Q, TYPE, NS('query')['BundleQuery']))
        g.add((Q, NS('feature')['hasFeature'], NS('feature')['CPUUtilization']))
        g.add((Q, NS('query')['hasResource'], R))

        g.add((Q, NS('stat')['hasSample'], self.S))
        g.add((Q, NS('query')['hasFormatter'], self.F))
        
        g.add((R, TYPE, NS('core')['Node']))
        g.add((R, TYPE, NS('core')['Resource']))
        g.add((R, TYPE, NS('owl')['NamedIndividual']))

        g.add((R, NS('core')['hasInboundInterface'], I1))
        g.add((R, NS('core')['hasOutboundInterface'], I1))
        g.add((I1, TYPE, NS('core')['Interface']))
        g.add((I2, TYPE, NS('core')['Interface']))
        g.add((I1, NS('core')['hasIPv4Address'], ADDR))
        g.add((I2, NS('core')['hasIPv4Address'], ADDR))
        g.add((ADDR, TYPE, NS('owl')['NamedIndividual']))
        g.add((ADDR, TYPE, NS('unit')['IPAddress']))
        g.add((ADDR, NS('unit')['hasValue'], IPADDR))
        query = g.serialize()
        self.save(fn = "/tmp/genq_cpu.owl", q = query)
        response = self.MSI_planetlab.measure(credential = [noviCredentialIARGS], query = query)
        print response
        self.assertGreater(len(response.splitlines()), 16, "got empty measurement response")

    def test_genq_err(self):
        g = self.new_g()
        mns = Namespace("http://foo.bar/req.owl#")
        g.bind('q', mns)
        NS = self.PL_O.ns
        TYPE = NS('rdf')['type']
        Q = mns['cantmeasureright']
        R = mns['smilax1']
        I1 = mns['ifin']
        I2 = mns['ifout']
        IPADDR = Literal('150.254.160.19')
        ADDR = mns['smilax_address']
        g.add((Q, TYPE, NS('owl')['NamedIndividual']))
        g.add((Q, TYPE, NS('query')['BundleQuery']))
        g.add((Q, NS('feature')['hasFeature'], NS('feature')['UsedMemory']))
        g.add((Q, NS('query')['hasResource'], R))

        g.add((Q, NS('stat')['hasSample'], self.S))
        g.add((Q, NS('query')['hasFormatter'], self.F))
        
        g.add((R, TYPE, NS('core')['Node']))
        g.add((R, TYPE, NS('core')['Resource']))
        g.add((R, TYPE, NS('owl')['NamedIndividual']))

        g.add((R, NS('core')['hasInboundInterface'], I1))
        g.add((R, NS('core')['hasOutboundInterface'], I1))
        g.add((I1, TYPE, NS('core')['Interface']))
        g.add((I2, TYPE, NS('core')['Interface']))
        g.add((I1, NS('core')['hasIPv4Address'], ADDR))
        g.add((I2, NS('core')['hasIPv4Address'], ADDR))
        g.add((ADDR, TYPE, NS('owl')['NamedIndividual']))
        g.add((ADDR, TYPE, NS('unit')['IPAddress']))
        g.add((ADDR, NS('unit')['hasValue'], IPADDR))
        query = g.serialize()
        self.save(fn = "/tmp/genq_cpu.owl", q = query)
        response = self.MSI_planetlab.measure(credential = [noviCredentialIARGS], query = query)
        print response
        self.assertTrue("error" in response, "no error message! got %s" % response)

    def test_genq_helper(self):
        q = MonitoringQueryImpl(self.MSI_planetlab._ms)
        q.addFeature('measureMemoryInfo', 'MemoryUtilization')
        q.addResource('measureMemoryInfo', 'smilax1', 'Node')
        q.addInterface('smilax1', 'ifin', 'hasInboundInterface')
        q.addInterface('smilax1', 'ifout', 'hasOutboundInterface')
        q.defineInterface('ifin','150.254.160.19', 'hasIPv4Address')
        q.defineInterface('ifout','150.254.160.19', 'hasIPv4Address')
        query = q.serialize()
        self.save(fn = "/tmp/genq_mem2.owl", q = query)
        print query
        response = self.MSI_planetlab.measure(credential = [noviCredentialIARGS], query = query)
        print response
        self.assertGreater(len(response.splitlines()), 20, "got empty measurement response")


    def test_genq_complex(self):
        g = self.new_g()
        mns = Namespace("http://foo.bar/req.owl#")
        g.bind('q', mns)
        NS = self.PL_O.ns
        TYPE = NS('rdf')['type']
        Q1 = mns['measureMemoryInformation']
        Q2 = mns['measureCPUInformation']
        Q3 = mns['measureDiskInformation']
        Q4 = mns['measureUsedMemory'] # should generate error
        R = mns['smilax1']
        I1 = mns['ifin']
        I2 = mns['ifout']
        P = mns['part']
        IPADDR = Literal('150.254.160.19')
        ADDR = mns['smilax_address']
        for Q, feature in [(Q1, 'FreeMemory'), (Q2, 'CPULoad'), (Q3, 'FreeDiskSpace'), (Q4, 'UsedMemory')]:
            g.add((Q, TYPE, NS('owl')['NamedIndividual']))
            g.add((Q, TYPE, NS('query')['BundleQuery']))
            g.add((Q, NS('feature')['hasFeature'], NS('feature')[feature]))
            g.add((Q, NS('query')['hasResource'], R))

            g.add((Q, NS('stat')['hasSample'], self.S))
            g.add((Q, NS('query')['hasFormatter'], self.F))
        
        g.add((R, TYPE, NS('core')['Node']))
        g.add((R, TYPE, NS('core')['Resource']))
        g.add((R, TYPE, NS('owl')['NamedIndividual']))

        g.add((R, NS('core')['hasInboundInterface'], I1))
        g.add((R, NS('core')['hasOutboundInterface'], I1))
        g.add((I1, TYPE, NS('core')['Interface']))
        g.add((I2, TYPE, NS('core')['Interface']))
        g.add((I1, NS('core')['hasIPv4Address'], ADDR))
        g.add((I2, NS('core')['hasIPv4Address'], ADDR))
        g.add((ADDR, TYPE, NS('owl')['NamedIndividual']))
        g.add((ADDR, TYPE, NS('unit')['IPAddress']))
        g.add((ADDR, NS('unit')['hasValue'], IPADDR))
        g.add((Q3, NS('param')['hasParameter'], P))
        g.add((P, TYPE, NS('owl')['NamedIndividual']))
        g.add((P, TYPE, NS('query')['QueryParameter']))
        g.add((P, NS('param')['paramName'], Literal("partition")))
        g.add((P, NS('unit')['hasValue'], Literal("/")))
        g.add((P, NS('param')['hasType'], NS('param')['String']))
        g.add((P, TYPE, NS('unit')['NameOfSomething']))
        query = g.serialize()
        self.save(fn = "/tmp/genq_complex.owl", q = query)
        response = self.MSI_planetlab.measure(credential = [noviCredentialIARGS], query = query)
        print response
        self.assertTrue(response, "got nothing")
        self.assertGreater(len(response.splitlines()), 26, "got empty measurement response")
        self.assertTrue("error" in response, "no error message! got %s" % response)



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