yandex-tank/Tests/MonitoringTest.py
2012-09-14 20:12:46 +04:00

116 lines
5.1 KiB
Python

from Tests.TankTests import TankTestCase
import tempfile
import time
from MonCollector.collector import MonitoringCollector, MonitoringDataListener
from Tank.Plugins.Monitoring import MonitoringPlugin, MonitoringWidget
from Tank.Core import TankCore
import logging
from Tank.Plugins.ConsoleOnline import Screen
from Tests.ConsoleOnlinePluginTest import FakeConsoleMarkup
class MonitoringCollectorTestCase(TankTestCase):
data = None
def test_collector(self):
mon = MonitoringCollector("config/mon1.conf")
listener = TestMonListener()
mon.add_listener(listener)
mon.prepare()
mon.start()
mon.poll()
self.assertEquals([], listener.data)
listener.data = []
time.sleep(1)
mon.poll()
self.assertNotEquals([], listener.data)
self.assertTrue(listener.data[0].startswith('start;'))
listener.data = []
time.sleep(2)
mon.poll()
self.assertNotEquals([], listener.data)
self.assertFalse(listener.data[0].startswith('start;'))
listener.data = []
time.sleep(3)
mon.poll()
self.assertNotEquals([], listener.data)
self.assertFalse(listener.data[0].startswith('start;'))
listener.data = []
mon.stop()
def test_plugin_disabled(self):
core = TankCore()
mon = MonitoringPlugin(core)
core.set_option(mon.SECTION, 'config', 'none')
mon.configure()
mon.prepare_test()
mon.start_test()
self.assertEquals(-1, mon.is_test_finished())
self.assertEquals(None, mon.monitoring)
time.sleep(1)
self.assertEquals(-1, mon.is_test_finished())
mon.end_test(0)
mon.post_process(0)
def test_plugin_default(self):
core = TankCore()
core.load_configs(['config/monitoring.conf'])
core.load_plugins()
core.plugins_configure()
core.plugins_prepare_test()
mon = MonitoringPlugin(core)
mon.configure()
mon.prepare_test()
mon.start_test()
self.assertEquals(-1, mon.is_test_finished())
self.assertNotEquals(None, mon.monitoring)
time.sleep(1)
self.assertEquals(-1, mon.is_test_finished())
mon.end_test(0)
mon.post_process(0)
def test_plugin_config(self):
core = TankCore()
core.load_configs(['config/monitoring.conf'])
core.load_plugins()
core.plugins_configure()
core.plugins_prepare_test()
mon = MonitoringPlugin(core)
core.set_option(mon.SECTION, 'config', "config/mon1.conf")
mon.configure()
mon.prepare_test()
mon.start_test()
self.assertEquals(-1, mon.is_test_finished())
self.assertNotEquals(None, mon.monitoring)
time.sleep(1)
self.assertEquals(-1, mon.is_test_finished())
mon.end_test(0)
mon.post_process(0)
def test_widget(self):
core = TankCore()
owner = MonitoringPlugin(core)
owner.monitoring=1
widget = MonitoringWidget(owner)
screen = Screen(50, FakeConsoleMarkup())
res = widget.render(screen)
self.assertEquals("Monitoring is <g>online<rst>:", res)
widget.monitoring_data("start;127.0.0.1;1347631472;Memory_total;Memory_used;Memory_free;Memory_shared;Memory_buff;Memory_cached;Net_recv;Net_send;Disk_read;Disk_write;System_csw;System_int;CPU_user;CPU_nice;CPU_system;CPU_idle;CPU_iowait;CPU_irq;CPU_softirq;System_numproc;System_numthreads")
res = widget.render(screen)
self.assertEquals("Monitoring is <g>online<rst>:\n 127.0.0.1:\n Memory used: n/a\n CPU iowait: n/a\n System numproc: n/a\n Memory free: n/a\n Memory shared: n/a\n Memory cached: n/a\n System numthreads: n/a\n CPU idle: n/a\n CPU user: n/a\n Memory total: n/a\n CPU system: n/a\n Disk write: n/a\n Disk read: n/a\n Memory buff: n/a\n Net send: n/a\n CPU irq: n/a\n Net recv: n/a\n CPU nice: n/a\n System int: n/a\n CPU softirq: n/a\n System csw: n/a", res)
widget.monitoring_data("127.0.0.1;1347631473;1507.65625;576.9609375;8055;1518;0;143360;34.9775784753;16.1434977578;0.0")
res = widget.render(screen)
self.assertNotEquals("Monitoring is <g>online<rst>:", res)
self.assertNotEquals("Monitoring is <g>online<rst>:\n 127.0.0.1:", res)
self.assertNotEquals("Monitoring is <g>online<rst>:\n 127.0.0.1:\n Memory used: n/a\n CPU iowait: n/a\n System numproc: n/a\n Memory free: n/a\n Memory shared: n/a\n Memory cached: n/a\n System numthreads: n/a\n CPU idle: n/a\n CPU user: n/a\n Memory total: n/a\n CPU system: n/a\n Disk write: n/a\n Disk read: n/a\n Memory buff: n/a\n Net send: n/a\n CPU irq: n/a\n Net recv: n/a\n CPU nice: n/a\n System int: n/a\n CPU softirq: n/a\n System csw: n/a", res)
class TestMonListener(MonitoringDataListener):
def __init__(self):
self.data = []
def monitoring_data(self, data_string):
logging.debug("MON DATA: %s", data_string)
self.data.append(data_string)