Skip to content

Commit

Permalink
[wmi] adding test for win32 event log.
Browse files Browse the repository at this point in the history
  • Loading branch information
truthbk committed Dec 10, 2015
1 parent 56a8758 commit 1cb7e85
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/checks/fixtures/wmi/win32_ntlogevent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
EventCode 0
EventIdentifier 0
EventType 0
InsertionStrings [insertionstring]
Logfile Application
Message SomeMessage
SourceName MSQLSERVER
TimeGenerated 20151224113047.000000-480

This comment has been minimized.

Copy link
@yannmh

yannmh Dec 14, 2015

Member

Will the test stop working after 🎅 🎄 ? 😛

User FooUser
Type Error
36 changes: 36 additions & 0 deletions tests/checks/mock/test_w32logevent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# project
from tests.core.test_wmi import TestCommonWMI
from tests.checks.common import AgentCheckTest


class W32LogEventTestCase(AgentCheckTest, TestCommonWMI):
CHECK_NAME = 'win32_event_log'

WIN_LOGEVENT_CONFIG = {
'host': ".",
'tags': ["mytag1", "mytag2"],
'sites': ["Default_Web_Site", "Failing site"],
'logfile': ["Application"],
'type': ["Error", "Warning"],
'source_name': ["MSSQLSERVER"]
}

def test_check(self):
"""
Returns the right metrics and service checks
"""
# Run check
config = {
'instances': [self.WIN_LOGEVENT_CONFIG]
}

self.run_check_twice(config)

# TODO: Test for events
self.assertEvent('SomeMessage', count=1,
tags=self.WIN_LOGEVENT_CONFIG['tags'],
msg_title='Application/MSQLSERVER',
event_type='win32_log_event', alert_type='error',
source_type_name='event viewer')

self.coverage_report()
12 changes: 12 additions & 0 deletions tests/core/test_wmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def ExecQuery(self, query, query_language, flags):
if query == ("Select Name,State from Win32_Service WHERE Name = 'WSService' OR Name = 'WinHttpAutoProxySvc'"):
results += load_fixture("win32_service_up", ("Name", "WinHttpAutoProxySvc"))
results += load_fixture("win32_service_down", ("Name", "WSService"))
if query == ("Select Message,SourceName,TimeGenerated,Type,User,InsertionStrings,EventCode from Win32_NTLogEvent WHERE ( SourceName = 'MSSQLSERVER' ) "
"AND ( Type = 'Warning' OR Type = 'Error' ) AND TimeGenerated >= '20151224113047.000000-480'"):
results += load_fixture("win32_ntlogevent")

return results

Expand Down Expand Up @@ -190,6 +193,14 @@ def ConnectServer(self, *args, **kwargs):

ConnectServer.call_count = _connect_call_count

def to_time(wmi_ts):
"Just return any time struct"
return (2015, 12, 24, 11, 30, 47, 0, 0)

def from_time(year=0, month=0, day=0, hours=0, minutes=0,
seconds=0, microseconds=0, timezone=0):
"Just return any WMI date"
return "20151224113047.000000-480"

class TestCommonWMI(unittest.TestCase):
"""
Expand All @@ -205,6 +216,7 @@ def setUp(self):
sys.modules['pywintypes'] = Mock()
sys.modules['win32com'] = Mock()
sys.modules['win32com.client'] = Mock(Dispatch=Dispatch)
sys.modules['wmi'] = Mock(from_time=from_time, to_time=to_time)

from checks.libs.wmi import sampler
WMISampler = partial(sampler.WMISampler, log)
Expand Down

0 comments on commit 1cb7e85

Please sign in to comment.