Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 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 | |
| 16 | |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 17 | # |
| 18 | # Copyright 2016-present Ciena Corporation |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | # you may not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
| 23 | # |
| 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | # |
| 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
| 31 | # |
| 32 | import unittest |
| 33 | import time |
| 34 | import os, subprocess |
| 35 | from nose.tools import * |
| 36 | from nose.twistedtools import reactor, deferred |
| 37 | from twisted.internet import defer |
| 38 | from OnosCtrl import OnosCtrl |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 39 | from CordTestUtils import log_test as log |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 40 | from CordContainer import * |
| 41 | from docker import Client |
| 42 | import json |
| 43 | import requests |
| 44 | log.setLevel('INFO') |
| 45 | |
| 46 | class monitoring_exchange(unittest.TestCase): |
| 47 | |
| 48 | controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',') |
| 49 | onosLogLevel = 'INFO' |
| 50 | test_host_base = 'cord-tester1'#Hardcoded temporarily |
| 51 | collectd_app = 'org.onosproject.cpman' |
| 52 | testHostName = os.getenv('TEST_HOST', test_host_base) |
| 53 | testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel) |
| 54 | stat_optionList = os.getenv('USER_OPTIONS', '').split(',') |
| 55 | serverOptionsList = os.getenv('EXTERNAL_SERVER_OPTIONS', None) |
| 56 | CBENCH_TIMEOUT = 60 |
| 57 | |
| 58 | @classmethod |
| 59 | def setUpClass(cls): |
| 60 | onos_ctrl = OnosCtrl('org.onosproject.cpman') |
| 61 | status, _ = onos_ctrl.activate() |
| 62 | |
| 63 | @classmethod |
| 64 | def tearDownClass(cls): |
| 65 | onos_ctrl = OnosCtrl('org.onosproject.cpman') |
| 66 | status, _ = onos_ctrl.deactivate() |
| 67 | |
| 68 | @classmethod |
| 69 | def stat_option(cls, stats = None, serverDetails = None): |
| 70 | # each stats option we can do some specific functions |
| 71 | if stats is None: |
| 72 | stats = cls.stat_optionList |
| 73 | if serverDetails is None: |
| 74 | serverDetails = cls.serverOptionsList |
| 75 | stats_choice = 'COLLECTD' |
| 76 | test_name = cls.testHostName |
A R Karthick | f7a613b | 2017-02-24 09:36:44 -0800 | [diff] [blame] | 77 | test_image = 'cordtest/nose' |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 78 | if stats_choice in stats: |
| 79 | onos_ctrl = OnosCtrl('org.onosproject.cpman') |
| 80 | status, _ = onos_ctrl.activate() |
| 81 | if serverDetails is '': |
| 82 | pass |
| 83 | elif serverDetails in 'NEW': |
| 84 | test_image = 'cord-test/exserver' |
| 85 | test_name ='cord-collectd' |
| 86 | else: |
| 87 | pass |
| 88 | ## TO-DO for already up and running server, install collectd agent etc... |
| 89 | cls.start_collectd_agent_in_server(name = test_name, image = test_image) |
| 90 | return |
| 91 | |
| 92 | |
| 93 | @classmethod |
| 94 | def collectd_agent_metrics(cls,controller=None, auth =None, url = None): |
| 95 | '''This function is getting a rules from ONOS with json formate''' |
| 96 | if url: |
| 97 | resp = requests.get(url, auth = auth) |
| 98 | log.info('CollectD agent has provided metrics via ONOS controller, \nurl = %s \nand stats = %s \nResponse = %s ' %(url,resp.json(),resp.ok)) |
| 99 | assert_equal(resp.ok, True) |
| 100 | return resp |
| 101 | |
| 102 | |
| 103 | @classmethod |
| 104 | def start_collectd_agent_in_server(cls, name = None, image = None): |
| 105 | container_cmd_exec = Container(name = name, image = image) |
| 106 | tty = False |
| 107 | dckr = Client() |
| 108 | cmd = 'sudo /etc/init.d/collectd start' |
| 109 | i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True, shell = False) |
| 110 | return i |
| 111 | |
| 112 | @deferred(CBENCH_TIMEOUT) |
| 113 | def test_stats_with_collectd_installation(self): |
| 114 | df = defer.Deferred() |
| 115 | def collectd_sample(df): |
| 116 | cmd = 'sudo /etc/init.d/collectd start' |
| 117 | output = subprocess.check_output(cmd,shell= True) |
| 118 | if 'Starting statistics collectio' in output: |
| 119 | log.info('Collectd is installed properly') |
| 120 | pass |
| 121 | else: |
| 122 | log.info('Collectd is not installed properly') |
| 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_plugin_and_onos_installation(self): |
| 130 | df = defer.Deferred() |
| 131 | def collectd_sample(df): |
| 132 | cmd = 'ls' |
| 133 | output = subprocess.check_output(cmd,shell= True) |
| 134 | if 'write_onos' in output: |
| 135 | log.info('Collectd is installed properly and plugin happend to ONOS') |
| 136 | pass |
| 137 | else: |
| 138 | log.info('Collectd is not installed properly and no plugin happend to ONOS') |
| 139 | assert_equal(False, True) |
| 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_cpu_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_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller) |
| 153 | auth = ('karaf', 'karaf') |
| 154 | self.collectd_agent_metrics(controller, auth, url = url_cpu_stats) |
| 155 | log.info('Successfully CPU 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_mem_stats(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_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller) |
| 169 | auth = ('karaf', 'karaf') |
| 170 | self.collectd_agent_metrics(controller, auth, url = url_mem_stats) |
| 171 | log.info('Successfully memory metrics 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_control_metrics_messages(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_messages_stats = 'http://%s:8181/onos/cpman/controlmetrics/messages'%(controller) |
| 185 | auth = ('karaf', 'karaf') |
| 186 | self.collectd_agent_metrics(controller, auth, url = url_messages_stats) |
| 187 | log.info('Successfully messages 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/network_metrics'%(controller) |
| 201 | auth = ('karaf', 'karaf') |
| 202 | self.collectd_agent_metrics(controller, auth, url = url_network_stats) |
| 203 | log.info('Successfully network 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_get_network_metrics_stats(self): |
| 210 | df = defer.Deferred() |
| 211 | def collectd_sample(df): |
| 212 | self.stat_option() |
| 213 | for controller in self.controllers: |
| 214 | if not controller: |
| 215 | continue |
| 216 | url_network_stats = 'http://%s:8181/onos/cpman/controlmetrics/disk_metrics'%(controller) |
| 217 | auth = ('karaf', 'karaf') |
| 218 | self.collectd_agent_metrics(controller, auth, url = url_network_stats) |
| 219 | log.info('Successfully disk metrics are retained by the stats') |
| 220 | df.callback(0) |
| 221 | reactor.callLater(0, collectd_sample, df) |
| 222 | return df |
| 223 | |
| 224 | @deferred(CBENCH_TIMEOUT) |
| 225 | def test_stats_with_collectd_for_installing_new_container(self): |
| 226 | df = defer.Deferred() |
| 227 | def collectd_sample(df): |
| 228 | if 'NEW' in self.serverOptionsList: |
| 229 | test_image = 'cord-test/exserver' |
| 230 | test_name ='cord-collectd' |
| 231 | ## stopping collectd agent on test container if any |
| 232 | cmd = 'sudo /etc/init.d/collectd stop' |
| 233 | output = os.system(cmd) |
| 234 | ## starting collectd agent on new container |
| 235 | cmd = 'sudo /etc/init.d/collectd start' |
| 236 | output = self.start_collectd_agent_in_server(name = test_name, image = test_image) |
| 237 | if output == 0: |
| 238 | log.info('Collectd is installed properly on new container') |
| 239 | pass |
| 240 | else: |
| 241 | log.info('Collectd is not installed properly on new container') |
| 242 | assert_equal(False, True) |
| 243 | df.callback(0) |
| 244 | reactor.callLater(0, collectd_sample, df) |
| 245 | return df |
| 246 | |
| 247 | @deferred(CBENCH_TIMEOUT) |
| 248 | def test_stats_with_collectd_for_cpu_metrics_on_new_container(self): |
| 249 | df = defer.Deferred() |
| 250 | def collectd_sample(df): |
| 251 | if 'NEW' in self.serverOptionsList: |
| 252 | ## stopping collectd agent on test container if any |
| 253 | cmd = 'sudo /etc/init.d/collectd stop' |
| 254 | output = os.system(cmd) |
| 255 | self.stat_option() |
| 256 | for controller in self.controllers: |
| 257 | if not controller: |
| 258 | continue |
| 259 | url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller) |
| 260 | auth = ('karaf', 'karaf') |
| 261 | self.collectd_agent_metrics(controller, auth, url = url_cpu_stats) |
| 262 | log.info('Successfully CPU metrics are retained by the stats') |
| 263 | df.callback(0) |
| 264 | reactor.callLater(0, collectd_sample, df) |
| 265 | return df |
| 266 | |
| 267 | @deferred(CBENCH_TIMEOUT) |
| 268 | def test_stats_with_collectd_memory_metrics_on_new_container(self): |
| 269 | df = defer.Deferred() |
| 270 | def collectd_sample(df): |
| 271 | if 'NEW' in self.serverOptionsList: |
| 272 | ## stopping collectd agent on test container if any |
| 273 | cmd = 'sudo /etc/init.d/collectd stop' |
| 274 | output = os.system(cmd) |
| 275 | self.stat_option() |
| 276 | for controller in self.controllers: |
| 277 | if not controller: |
| 278 | continue |
| 279 | url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller) |
| 280 | auth = ('karaf', 'karaf') |
| 281 | self.collectd_agent_metrics(controller, auth, url = url_mem_stats) |
| 282 | log.info('Successfully memory metrics are retained by the stats') |
| 283 | df.callback(0) |
| 284 | reactor.callLater(0, collectd_sample, df) |
| 285 | return df |
| 286 | |
| 287 | @deferred(CBENCH_TIMEOUT) |
| 288 | def test_stats_with_collectd_get_messages_on_new_container(self): |
| 289 | df = defer.Deferred() |
| 290 | def collectd_sample(df): |
| 291 | if 'NEW' in self.serverOptionsList: |
| 292 | ## stopping collectd agent on test container if any |
| 293 | cmd = 'sudo /etc/init.d/collectd stop' |
| 294 | output = os.system(cmd) |
| 295 | self.stat_option() |
| 296 | for controller in self.controllers: |
| 297 | if not controller: |
| 298 | continue |
| 299 | url_messages_stats = 'http://%s:8181/onos/cpman/controlmetrics/messages'%(controller) |
| 300 | auth = ('karaf', 'karaf') |
| 301 | self.collectd_agent_metrics(controller, auth, url = url_messages_stats) |
| 302 | log.info('Successfully messages metrics are retained by the stats') |
| 303 | df.callback(0) |
| 304 | reactor.callLater(0, collectd_sample, df) |
| 305 | return df |
| 306 | |
| 307 | @deferred(CBENCH_TIMEOUT) |
| 308 | def test_stats_with_collectd_network_metrics_on_new_container(self): |
| 309 | df = defer.Deferred() |
| 310 | def collectd_sample(df): |
| 311 | if 'NEW' in self.serverOptionsList: |
| 312 | ## stopping collectd agent on test container if any |
| 313 | cmd = 'sudo /etc/init.d/collectd stop' |
| 314 | output = os.system(cmd) |
| 315 | self.stat_option() |
| 316 | for controller in self.controllers: |
| 317 | if not controller: |
| 318 | continue |
| 319 | url_network_stats = 'http://%s:8181/onos/cpman/controlmetrics/network_metrics'%(controller) |
| 320 | auth = ('karaf', 'karaf') |
| 321 | self.collectd_agent_metrics(controller, auth, url = url_network_stats) |
| 322 | log.info('Successfully network metrics are retained by the stats') |
| 323 | df.callback(0) |
| 324 | reactor.callLater(0, collectd_sample, df) |
| 325 | return df |
| 326 | |
| 327 | @deferred(CBENCH_TIMEOUT) |
| 328 | def test_stats_with_collectd_disk_metrics_on_new_container(self): |
| 329 | df = defer.Deferred() |
| 330 | def collectd_sample(df): |
| 331 | if 'NEW' in self.serverOptionsList: |
| 332 | ## stopping collectd agent on test container if any |
| 333 | cmd = 'sudo /etc/init.d/collectd stop' |
| 334 | output = os.system(cmd) |
| 335 | self.stat_option() |
| 336 | for controller in self.controllers: |
| 337 | if not controller: |
| 338 | continue |
| 339 | url_disk_stats = 'http://%s:8181/onos/cpman/controlmetrics/disk_metrics'%(controller) |
| 340 | auth = ('karaf', 'karaf') |
| 341 | self.collectd_agent_metrics(controller, auth, url = url_disk_stats) |
| 342 | log.info('Successfully network metrics are retained by the stats') |
| 343 | df.callback(0) |
| 344 | reactor.callLater(0, collectd_sample, df) |
| 345 | return df |