blob: c18aec7762ea914102ed20f1070f929134ef885e [file] [log] [blame]
Thangavelu K Sdcb04332017-01-27 22:57:56 +00001#
2# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import unittest
17import time
18import os, subprocess
19from nose.tools import *
20from nose.twistedtools import reactor, deferred
21from twisted.internet import defer
22from OnosCtrl import OnosCtrl
A R Karthick76a497a2017-04-12 10:59:39 -070023from CordTestUtils import log_test as log
Thangavelu K Sdcb04332017-01-27 22:57:56 +000024from CordContainer import *
25from docker import Client
26import json
27import requests
28log.setLevel('INFO')
29
30class monitoring_exchange(unittest.TestCase):
31
32 controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',')
33 onosLogLevel = 'INFO'
34 test_host_base = 'cord-tester1'#Hardcoded temporarily
35 collectd_app = 'org.onosproject.cpman'
36 testHostName = os.getenv('TEST_HOST', test_host_base)
37 testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
38 stat_optionList = os.getenv('USER_OPTIONS', '').split(',')
39 serverOptionsList = os.getenv('EXTERNAL_SERVER_OPTIONS', None)
40 CBENCH_TIMEOUT = 60
41
42 @classmethod
43 def setUpClass(cls):
44 onos_ctrl = OnosCtrl('org.onosproject.cpman')
45 status, _ = onos_ctrl.activate()
46
47 @classmethod
48 def tearDownClass(cls):
49 onos_ctrl = OnosCtrl('org.onosproject.cpman')
50 status, _ = onos_ctrl.deactivate()
51
52 @classmethod
53 def stat_option(cls, stats = None, serverDetails = None):
54 # each stats option we can do some specific functions
55 if stats is None:
56 stats = cls.stat_optionList
57 if serverDetails is None:
58 serverDetails = cls.serverOptionsList
59 stats_choice = 'COLLECTD'
60 test_name = cls.testHostName
A R Karthickf7a613b2017-02-24 09:36:44 -080061 test_image = 'cordtest/nose'
Thangavelu K Sdcb04332017-01-27 22:57:56 +000062 if stats_choice in stats:
63 onos_ctrl = OnosCtrl('org.onosproject.cpman')
64 status, _ = onos_ctrl.activate()
65 if serverDetails is '':
66 pass
67 elif serverDetails in 'NEW':
68 test_image = 'cord-test/exserver'
69 test_name ='cord-collectd'
70 else:
71 pass
72 ## TO-DO for already up and running server, install collectd agent etc...
73 cls.start_collectd_agent_in_server(name = test_name, image = test_image)
74 return
75
76
77 @classmethod
78 def collectd_agent_metrics(cls,controller=None, auth =None, url = None):
79 '''This function is getting a rules from ONOS with json formate'''
80 if url:
81 resp = requests.get(url, auth = auth)
82 log.info('CollectD agent has provided metrics via ONOS controller, \nurl = %s \nand stats = %s \nResponse = %s ' %(url,resp.json(),resp.ok))
83 assert_equal(resp.ok, True)
84 return resp
85
86
87 @classmethod
88 def start_collectd_agent_in_server(cls, name = None, image = None):
89 container_cmd_exec = Container(name = name, image = image)
90 tty = False
91 dckr = Client()
92 cmd = 'sudo /etc/init.d/collectd start'
93 i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True, shell = False)
94 return i
95
96 @deferred(CBENCH_TIMEOUT)
97 def test_stats_with_collectd_installation(self):
98 df = defer.Deferred()
99 def collectd_sample(df):
100 cmd = 'sudo /etc/init.d/collectd start'
101 output = subprocess.check_output(cmd,shell= True)
102 if 'Starting statistics collectio' in output:
103 log.info('Collectd is installed properly')
104 pass
105 else:
106 log.info('Collectd is not installed properly')
107 assert_equal(False, True)
108 df.callback(0)
109 reactor.callLater(0, collectd_sample, df)
110 return df
111
112 @deferred(CBENCH_TIMEOUT)
113 def test_stats_with_collectd_plugin_and_onos_installation(self):
114 df = defer.Deferred()
115 def collectd_sample(df):
116 cmd = 'ls'
117 output = subprocess.check_output(cmd,shell= True)
118 if 'write_onos' in output:
119 log.info('Collectd is installed properly and plugin happend to ONOS')
120 pass
121 else:
122 log.info('Collectd is not installed properly and no plugin happend to ONOS')
123 assert_equal(False, True)
124 df.callback(0)
125 reactor.callLater(0, collectd_sample, df)
126 return df
127
128 @deferred(CBENCH_TIMEOUT)
129 def test_stats_with_collectd_get_cpu_stats(self):
130 df = defer.Deferred()
131 def collectd_sample(df):
132 self.stat_option()
133 for controller in self.controllers:
134 if not controller:
135 continue
136 url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller)
137 auth = ('karaf', 'karaf')
138 self.collectd_agent_metrics(controller, auth, url = url_cpu_stats)
139 log.info('Successfully CPU metrics are retained by the stats')
140 df.callback(0)
141 reactor.callLater(0, collectd_sample, df)
142 return df
143
144 @deferred(CBENCH_TIMEOUT)
145 def test_stats_with_collectd_get_mem_stats(self):
146 df = defer.Deferred()
147 def collectd_sample(df):
148 self.stat_option()
149 for controller in self.controllers:
150 if not controller:
151 continue
152 url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller)
153 auth = ('karaf', 'karaf')
154 self.collectd_agent_metrics(controller, auth, url = url_mem_stats)
155 log.info('Successfully memory metrics are retained by the stats')
156 df.callback(0)
157 reactor.callLater(0, collectd_sample, df)
158 return df
159
160 @deferred(CBENCH_TIMEOUT)
161 def test_stats_with_collectd_get_control_metrics_messages(self):
162 df = defer.Deferred()
163 def collectd_sample(df):
164 self.stat_option()
165 for controller in self.controllers:
166 if not controller:
167 continue
168 url_messages_stats = 'http://%s:8181/onos/cpman/controlmetrics/messages'%(controller)
169 auth = ('karaf', 'karaf')
170 self.collectd_agent_metrics(controller, auth, url = url_messages_stats)
171 log.info('Successfully messages are retained by the stats')
172 df.callback(0)
173 reactor.callLater(0, collectd_sample, df)
174 return df
175
176 @deferred(CBENCH_TIMEOUT)
177 def test_stats_with_collectd_get_network_metrics_stats(self):
178 df = defer.Deferred()
179 def collectd_sample(df):
180 self.stat_option()
181 for controller in self.controllers:
182 if not controller:
183 continue
184 url_network_stats = 'http://%s:8181/onos/cpman/controlmetrics/network_metrics'%(controller)
185 auth = ('karaf', 'karaf')
186 self.collectd_agent_metrics(controller, auth, url = url_network_stats)
187 log.info('Successfully network metrics are retained by the stats')
188 df.callback(0)
189 reactor.callLater(0, collectd_sample, df)
190 return df
191
192 @deferred(CBENCH_TIMEOUT)
193 def test_stats_with_collectd_get_network_metrics_stats(self):
194 df = defer.Deferred()
195 def collectd_sample(df):
196 self.stat_option()
197 for controller in self.controllers:
198 if not controller:
199 continue
200 url_network_stats = 'http://%s:8181/onos/cpman/controlmetrics/disk_metrics'%(controller)
201 auth = ('karaf', 'karaf')
202 self.collectd_agent_metrics(controller, auth, url = url_network_stats)
203 log.info('Successfully disk metrics are retained by the stats')
204 df.callback(0)
205 reactor.callLater(0, collectd_sample, df)
206 return df
207
208 @deferred(CBENCH_TIMEOUT)
209 def test_stats_with_collectd_for_installing_new_container(self):
210 df = defer.Deferred()
211 def collectd_sample(df):
212 if 'NEW' in self.serverOptionsList:
213 test_image = 'cord-test/exserver'
214 test_name ='cord-collectd'
215 ## stopping collectd agent on test container if any
216 cmd = 'sudo /etc/init.d/collectd stop'
217 output = os.system(cmd)
218 ## starting collectd agent on new container
219 cmd = 'sudo /etc/init.d/collectd start'
220 output = self.start_collectd_agent_in_server(name = test_name, image = test_image)
221 if output == 0:
222 log.info('Collectd is installed properly on new container')
223 pass
224 else:
225 log.info('Collectd is not installed properly on new container')
226 assert_equal(False, True)
227 df.callback(0)
228 reactor.callLater(0, collectd_sample, df)
229 return df
230
231 @deferred(CBENCH_TIMEOUT)
232 def test_stats_with_collectd_for_cpu_metrics_on_new_container(self):
233 df = defer.Deferred()
234 def collectd_sample(df):
235 if 'NEW' in self.serverOptionsList:
236 ## stopping collectd agent on test container if any
237 cmd = 'sudo /etc/init.d/collectd stop'
238 output = os.system(cmd)
239 self.stat_option()
240 for controller in self.controllers:
241 if not controller:
242 continue
243 url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller)
244 auth = ('karaf', 'karaf')
245 self.collectd_agent_metrics(controller, auth, url = url_cpu_stats)
246 log.info('Successfully CPU metrics are retained by the stats')
247 df.callback(0)
248 reactor.callLater(0, collectd_sample, df)
249 return df
250
251 @deferred(CBENCH_TIMEOUT)
252 def test_stats_with_collectd_memory_metrics_on_new_container(self):
253 df = defer.Deferred()
254 def collectd_sample(df):
255 if 'NEW' in self.serverOptionsList:
256 ## stopping collectd agent on test container if any
257 cmd = 'sudo /etc/init.d/collectd stop'
258 output = os.system(cmd)
259 self.stat_option()
260 for controller in self.controllers:
261 if not controller:
262 continue
263 url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller)
264 auth = ('karaf', 'karaf')
265 self.collectd_agent_metrics(controller, auth, url = url_mem_stats)
266 log.info('Successfully memory metrics are retained by the stats')
267 df.callback(0)
268 reactor.callLater(0, collectd_sample, df)
269 return df
270
271 @deferred(CBENCH_TIMEOUT)
272 def test_stats_with_collectd_get_messages_on_new_container(self):
273 df = defer.Deferred()
274 def collectd_sample(df):
275 if 'NEW' in self.serverOptionsList:
276 ## stopping collectd agent on test container if any
277 cmd = 'sudo /etc/init.d/collectd stop'
278 output = os.system(cmd)
279 self.stat_option()
280 for controller in self.controllers:
281 if not controller:
282 continue
283 url_messages_stats = 'http://%s:8181/onos/cpman/controlmetrics/messages'%(controller)
284 auth = ('karaf', 'karaf')
285 self.collectd_agent_metrics(controller, auth, url = url_messages_stats)
286 log.info('Successfully messages metrics are retained by the stats')
287 df.callback(0)
288 reactor.callLater(0, collectd_sample, df)
289 return df
290
291 @deferred(CBENCH_TIMEOUT)
292 def test_stats_with_collectd_network_metrics_on_new_container(self):
293 df = defer.Deferred()
294 def collectd_sample(df):
295 if 'NEW' in self.serverOptionsList:
296 ## stopping collectd agent on test container if any
297 cmd = 'sudo /etc/init.d/collectd stop'
298 output = os.system(cmd)
299 self.stat_option()
300 for controller in self.controllers:
301 if not controller:
302 continue
303 url_network_stats = 'http://%s:8181/onos/cpman/controlmetrics/network_metrics'%(controller)
304 auth = ('karaf', 'karaf')
305 self.collectd_agent_metrics(controller, auth, url = url_network_stats)
306 log.info('Successfully network metrics are retained by the stats')
307 df.callback(0)
308 reactor.callLater(0, collectd_sample, df)
309 return df
310
311 @deferred(CBENCH_TIMEOUT)
312 def test_stats_with_collectd_disk_metrics_on_new_container(self):
313 df = defer.Deferred()
314 def collectd_sample(df):
315 if 'NEW' in self.serverOptionsList:
316 ## stopping collectd agent on test container if any
317 cmd = 'sudo /etc/init.d/collectd stop'
318 output = os.system(cmd)
319 self.stat_option()
320 for controller in self.controllers:
321 if not controller:
322 continue
323 url_disk_stats = 'http://%s:8181/onos/cpman/controlmetrics/disk_metrics'%(controller)
324 auth = ('karaf', 'karaf')
325 self.collectd_agent_metrics(controller, auth, url = url_disk_stats)
326 log.info('Successfully network metrics are retained by the stats')
327 df.callback(0)
328 reactor.callLater(0, collectd_sample, df)
329 return df