source: nscp/scripts/python/test_eventlog.py @ 58f0e80

0.4.00.4.10.4.2
Last change on this file since 58f0e80 was 58f0e80, checked in by Michael Medin <michael@…>, 19 months ago

2011-10-21 MickeM

  • Some more threading issues in Python (works perfectly(?) now)
  • Refactored the tests a bit making test suites runable from a central hub
  • Fixed soo everything builds and works on Linux

2011-10-19 MickeM

  • Fixed some threadding issues in PythonScript
  • Still working on refactoring the unittest helpers a bit

2011-10-16 MickeM

  • Fixed issue with loading zero-byte UTF-8 config files
  • Fixed some issues with settings subsystem (debug log as well as createing instances for --generate)
  • Added ability to set event log to real-time
  • Added support for multiple eventlogs in real-time
  • Property mode set to 100644
File size: 3.7 KB
Line 
1from NSCP import Settings, Registry, Core, log, status, sleep
2from test_helper import Callable, TestResult, get_test_manager, create_test_manager
3import plugin_pb2
4from types import *
5import socket
6import unicodedata
7
8class EventLogTest:
9        instance = None
10        key = ''
11        reg = None
12        got_response = False
13        last_source = None
14        last_command = None
15        last_status = None
16        last_message = None
17        last_perfdata = None
18        got_simple_response = None
19       
20        class SingletonHelper:
21                def __call__( self, *args, **kw ) :
22                        if EventLogTest.instance is None :
23                                object = EventLogTest()
24                                EventLogTest.instance = object
25                        return EventLogTest.instance
26
27        getInstance = SingletonHelper()
28
29        def desc(self):
30                return 'Testcase for eventlog'
31
32        def title(self):
33                return 'EventLog test'
34
35        def setup(self, plugin_id, prefix):
36                self.key = '_%stest_command'%prefix
37                self.reg = Registry.get(plugin_id)
38                self.reg.simple_subscription('pytest_evlog', EventLogTest.simple_inbox_handler)
39
40        def simple_inbox_handler(channel, source, command, code, message, perf):
41                instance = EventLogTest.getInstance()
42                return instance.simple_inbox_handler_wrapped(channel, source, command, code, message, perf)
43        simple_inbox_handler = Callable(simple_inbox_handler)
44
45        def simple_inbox_handler_wrapped(self, channel, source, command, status, message, perf):
46                message = unicodedata.normalize('NFKD', message).encode('ascii','ignore')
47                log('Got simple message %s on %s'%(command, channel))
48                self.got_simple_response = True
49                self.last_source = source
50                self.last_command = command
51                self.last_status = status
52                self.last_message = message
53                self.last_perfdata = perf
54                return True
55
56        def teardown(self):
57                None
58
59        def test_create(self, source, id, severity, category, arguments):
60                self.last_message = None
61                args = ['--source', source,
62                                        '--id', id,
63                                        '--severity', severity,
64                                        '--category', category
65                                        ]
66                for f in arguments:
67                        args.append('--argument')
68                        args.append(f)
69                (ret, msg) = Core.get().simple_exec('any', 'insert-eventlog', args)
70                return ret == 0
71
72        def run_test(self):
73                result = TestResult()
74                result.add_message(self.test_create('Application Error', 1000, '0', 'error', ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']), 'Testing to create a log message')
75                sleep(1)
76                log(self.last_message)
77                log('%s'%self.last_message)
78                result.assert_equals(self.last_message, 'error Application Error: ', 'Verify that message is sent through')
79                return result
80
81        def install(self, arguments):
82                conf = Settings.get()
83                conf.set_string('/modules', 'pytest_eventlog', 'CheckEventLog')
84                conf.set_string('/modules', 'pytest', 'PythonScript')
85
86                conf.set_string('/settings/pytest/scripts', 'test_eventlog', 'test_eventlog.py')
87               
88                conf.set_string('/settings/pytest_eventlog/real-time', 'enabled', 'true')
89                conf.set_string('/settings/pytest_eventlog/real-time', 'maximum age', '5s')
90                conf.set_string('/settings/pytest_eventlog/real-time', 'destination', 'pytest_evlog')
91                conf.set_string('/settings/pytest_eventlog/real-time', 'language', 'english')
92               
93                conf.save()
94       
95        def uninstall(self):
96                None
97
98        def help(self):
99                None
100
101        def init(self, plugin_id):
102                None
103                #reg = Registry.get(plugin_id)
104                #reg.simple_function('test_eventlog', test, 'Run python EventLog unit test suite')
105
106        def shutdown(self):
107                None
108
109all_tests = [EventLogTest]
110
111def __main__():
112        test_manager = create_test_manager()
113        test_manager.add(all_tests)
114        test_manager.install()
115       
116def init(plugin_id, plugin_alias, script_alias):
117        test_manager = create_test_manager(plugin_id, plugin_alias, script_alias)
118        test_manager.add(all_tests)
119
120        test_manager.init()
121
122def shutdown():
123        test_manager = get_test_manager()
124        test_manager.shutdown()
Note: See TracBrowser for help on using the repository browser.