|
14 | 14 | # License for the specific language governing permissions and limitations |
15 | 15 | # under the License. |
16 | 16 |
|
17 | | -f = open('log', 'w') |
18 | | - |
19 | | -def log(s): |
20 | | - print >>f, "FJR:" + time.asctime() + ": " + s |
21 | | - f.flush() |
22 | | - |
23 | 17 | import testlib |
24 | | -import time |
| 18 | +import logging |
| 19 | + |
25 | 20 | import splunklib.client as client |
26 | 21 |
|
27 | 22 | def event_count(index): |
28 | 23 | return int(index.content.totalEventCount) |
29 | 24 |
|
30 | 25 | def alert_count(search): |
31 | | - n = int(search.content.get('triggered_alert_count', 0)) |
32 | | - print "# alerts: %d" % n |
33 | | - return n |
| 26 | + return int(search.content.get('triggered_alert_count', 0)) |
34 | 27 |
|
35 | 28 | class TestCase(testlib.TestCase): |
36 | | - def test_crud(self): |
37 | | - service = client.connect(**self.opts.kwargs) |
38 | | - |
39 | | - searches = service.saved_searches |
40 | | - fired_alerts = service.fired_alerts |
41 | | - |
42 | | - if 'sdk-tests' not in service.indexes: |
43 | | - service.indexes.create("sdk-tests") |
44 | | - |
45 | | - # Clean out the test index |
46 | | - index = service.indexes['sdk-tests'] |
47 | | - |
48 | | - # Delete any leftover test search |
49 | | - search_name = "sdk-test-search" + str(time.time()) |
50 | | - if search_name in searches: searches.delete(search_name) |
51 | | - self.assertFalse(search_name in searches) |
52 | | - |
53 | | - # Create a saved search that will register an alert for any event |
54 | | - # submitted to the `sdk-tests` index. Note that the search is not |
55 | | - # yet scheduled - we wil schedule after doing a little more cleanup. |
56 | | - query = "index=sdk-tests" |
57 | | - kwargs = { |
58 | | - 'actions': "rss", |
59 | | - 'alert_type': "always", |
60 | | - 'alert_comparator': "greater than", |
61 | | - 'alert_threshold': "0", |
62 | | - 'alert.severity': "5", |
63 | | - 'alert.suppress': "0", |
64 | | - 'alert.track': "1", |
65 | | - 'dispatch.earliest_time': "-1m", |
66 | | - 'dispatch.latest_time': "now", |
67 | | - 'is_scheduled': "0", |
68 | | - 'realtime_schedule': "1", |
69 | | - 'cron_schedule': "* * * * *" |
70 | | - } |
71 | | - search = searches.create(search_name, query, **kwargs) |
72 | | - self.assertEqual(search.name, search_name) |
73 | | - self.assertEqual(search.content.is_scheduled, "0") |
74 | | - |
75 | | - # Clear out any search history that may have matched due to reuse of |
76 | | - # the saved search name. |
77 | | - for job in search.history(): job.cancel() |
78 | | - testlib.wait(search, lambda search: len(search.history()) == 0) |
79 | | - self.assertEqual(len(search.history()), 0) |
80 | | - |
81 | | - # Now schedule the saved search |
82 | | - search.update(is_scheduled=1) |
83 | | - search.refresh() |
84 | | - |
85 | | - self.assertEqual(search.content.is_scheduled, "1") |
86 | | - self.assertEqual(alert_count(search), 0) |
87 | | - |
88 | | - # Wait for the saved search to run. When it runs we will see a new job |
89 | | - # show up in the search's history. |
90 | | - def f(search): |
91 | | - search.refresh() |
92 | | - n = len(search.history()) |
93 | | - return n==1 |
94 | | - testlib.wait(search, f, timeout=120) |
95 | | - self.assertEqual(len(search.history()), 1) |
96 | | - |
97 | | - # There should be no alerts if the search job hasn't been |
98 | | - # created yet. |
99 | | - search.refresh() |
100 | | - self.assertTrue((alert_count(search) == 0) == (search_name not in fired_alerts)) |
101 | | - |
102 | | - # Submit events and verify that they each trigger the expected |
103 | | - # alert |
104 | | - base_count = event_count(index) |
105 | | - |
106 | | - for count in xrange(1, 6): |
107 | | - # Submit an event that the search is expected to match, and wait |
108 | | - # for the indexer to process. |
109 | | - self.assertTrue(event_count(index) <= base_count+count) |
110 | | - index.submit("Hello #%d!!!" % count) |
111 | | - log("Submitted %d" % count) |
112 | | - testlib.wait(index, lambda index: event_count(index) == base_count+count) |
113 | | - time.sleep(2) |
114 | | - log("Finished sleep") |
115 | | - # return |
| 29 | + def setUp(self): |
| 30 | + testlib.TestCase.setUp(self) |
| 31 | + saved_searches = self.service.saved_searches |
| 32 | + self.saved_search_name = testlib.tmpname() |
| 33 | + self.assertFalse(self.saved_search_name in saved_searches) |
| 34 | + query = "search index=_internal GET | head 10" |
| 35 | + kwargs = {'alert_type': "number of events", |
| 36 | + 'alert_comparator': "greater than", |
| 37 | + 'alert_threshold': "0", |
| 38 | + 'alert.severity': "3", |
| 39 | + 'alert.suppress': "0", |
| 40 | + 'alert.track': "1", |
| 41 | + 'dispatch.earliest_time': "rt-1m", |
| 42 | + 'dispatch.latest_time': "rt-0m", |
| 43 | + 'is_scheduled': "1", |
| 44 | + 'cron_schedule': "* * * * *"} |
| 45 | + self.saved_search = saved_searches.create( |
| 46 | + self.saved_search_name, |
| 47 | + query, **kwargs) |
| 48 | + |
| 49 | + def test_new_search_is_empty(self): |
| 50 | + self.assertEqual(alert_count(self.saved_search), 0) |
| 51 | + self.assertEqual(len(self.saved_search.history()), 0) |
| 52 | + |
| 53 | + def test_alert_on_event(self): |
| 54 | + N = alert_count(self.saved_search) |
| 55 | + self.saved_search.refresh() |
| 56 | + self.assertEqual(alert_count(self.saved_search), N+1) |
| 57 | + |
| 58 | + |
| 59 | + # # Wait for the saved search to run. When it runs we will see a new job |
| 60 | + # # show up in the search's history. |
| 61 | + # def f(search): |
| 62 | + # search.refresh() |
| 63 | + # n = len(search.history()) |
| 64 | + # return n==1 |
| 65 | + # testlib.wait(search, f, timeout=120) |
| 66 | + # self.assertEqual(len(search.history()), 1) |
| 67 | + |
| 68 | + # # There should be no alerts if the search job hasn't been |
| 69 | + # # created yet. |
| 70 | + # search.refresh() |
| 71 | + # self.assertTrue((alert_count(search) == 0) == (search_name not in fired_alerts)) |
| 72 | + |
| 73 | + # # Submit events and verify that they each trigger the expected |
| 74 | + # # alert |
| 75 | + # base_count = event_count(index) |
| 76 | + |
| 77 | + # for count in xrange(1, 6): |
| 78 | + # # Submit an event that the search is expected to match, and wait |
| 79 | + # # for the indexer to process. |
| 80 | + # self.assertTrue(event_count(index) <= base_count+count) |
| 81 | + # index.submit("Hello #%d!!!" % count) |
| 82 | + # log("Submitted %d" % count) |
| 83 | + # testlib.wait(index, lambda index: event_count(index) == base_count+count) |
| 84 | + # time.sleep(2) |
| 85 | + # log("Finished sleep") |
| 86 | + # # return |
116 | 87 |
|
117 | | - # # Wait for the saved search to register the triggered alert |
118 | | - # self.assertTrue(alert_count(search) <= count) |
119 | | - # testlib.wait( |
120 | | - # search, |
121 | | - # lambda search: alert_count(search) == count, |
122 | | - # timeout=10) |
123 | | - # self.assertEqual(alert_count(search), count) |
124 | | - |
125 | | - # # And now .. after all that trouble, verify that we see the alerts! |
126 | | - # self.assertTrue(search_name in fired_alerts) |
127 | | - # alert_group = fired_alerts[search_name] |
128 | | - # import pprint; pprint.pprint(alert_group._state) |
129 | | - # self.assertEqual(alert_group.savedsearch_name, search_name) |
130 | | - # import pprint; pprint.pprint(alert_group._state) |
131 | | - # self.assertEqual(alert_group.count, count) |
132 | | - return |
133 | | - # Cleanup |
134 | | - searches.delete(search_name) |
135 | | - self.assertFalse(search_name in searches) |
136 | | - self.assertFalse(search_name in fired_alerts) |
137 | | - |
138 | | - def test_read(self): |
139 | | - service = client.connect(**self.opts.kwargs) |
140 | | - |
141 | | - for alert_group in service.fired_alerts: |
142 | | - alert_group.count |
143 | | - for alert in alert_group.alerts: |
144 | | - alert.content |
| 88 | + # # # Wait for the saved search to register the triggered alert |
| 89 | + # # self.assertTrue(alert_count(search) <= count) |
| 90 | + # # testlib.wait( |
| 91 | + # # search, |
| 92 | + # # lambda search: alert_count(search) == count, |
| 93 | + # # timeout=10) |
| 94 | + # # self.assertEqual(alert_count(search), count) |
| 95 | + |
| 96 | + # # # And now .. after all that trouble, verify that we see the alerts! |
| 97 | + # # self.assertTrue(search_name in fired_alerts) |
| 98 | + # # alert_group = fired_alerts[search_name] |
| 99 | + # # import pprint; pprint.pprint(alert_group._state) |
| 100 | + # # self.assertEqual(alert_group.savedsearch_name, search_name) |
| 101 | + # # import pprint; pprint.pprint(alert_group._state) |
| 102 | + # # self.assertEqual(alert_group.count, count) |
| 103 | + # return |
| 104 | + # # Cleanup |
| 105 | + # searches.delete(search_name) |
| 106 | + # self.assertFalse(search_name in searches) |
| 107 | + # self.assertFalse(search_name in fired_alerts) |
| 108 | + |
| 109 | + # def test_read(self): |
| 110 | + # service = client.connect(**self.opts.kwargs) |
| 111 | + |
| 112 | + # for alert_group in service.fired_alerts: |
| 113 | + # alert_group.count |
| 114 | + # for alert in alert_group.alerts: |
| 115 | + # alert.content |
145 | 116 |
|
146 | 117 | if __name__ == "__main__": |
147 | 118 | testlib.main() |
0 commit comments