|
15 | 15 | # under the License. |
16 | 16 |
|
17 | 17 | from __future__ import absolute_import |
| 18 | +from splunklib.binding import HTTPError |
18 | 19 | from tests import testlib |
19 | 20 | import logging |
20 | 21 |
|
21 | 22 | import splunklib.client as client |
| 23 | +from splunklib import results |
22 | 24 |
|
23 | 25 | import pytest |
24 | 26 |
|
25 | 27 |
|
26 | 28 | @pytest.mark.smoke |
27 | 29 | class TestMacro(testlib.SDKTestCase): |
| 30 | + macro_name = "SDKTestMacro" |
| 31 | + |
28 | 32 | def setUp(self): |
29 | 33 | super(TestMacro, self).setUp() |
| 34 | + self.clean() |
30 | 35 | macros = self.service.macros |
31 | 36 | logging.debug("Macros namespace: %s", macros.service.namespace) |
32 | | - self.macro_name = testlib.tmpname() |
33 | 37 | definition = '| eval test="123"' |
34 | 38 | self.macro = macros.create(self.macro_name, definition) |
35 | 39 |
|
36 | 40 | def tearDown(self): |
37 | 41 | super(TestMacro, self).setUp() |
| 42 | + self.clean() |
| 43 | + |
| 44 | + def clean(self): |
38 | 45 | for macro in self.service.macros: |
39 | | - if macro.name.startswith("delete-me"): |
| 46 | + if macro.name == self.macro_name: |
40 | 47 | self.service.macros.delete(macro.name) |
41 | 48 |
|
42 | 49 | def check_macro(self, macro): |
@@ -153,6 +160,115 @@ def test_acl_fails_without_owner(self): |
153 | 160 | ) |
154 | 161 |
|
155 | 162 |
|
| 163 | +class TestMacroSPL(testlib.SDKTestCase): |
| 164 | + macro_name = "SDKTestMacro" |
| 165 | + |
| 166 | + def setUp(self): |
| 167 | + testlib.SDKTestCase.setUp(self) |
| 168 | + self.clean() |
| 169 | + |
| 170 | + def tearDown(self): |
| 171 | + testlib.SDKTestCase.setUp(self) |
| 172 | + self.clean() |
| 173 | + |
| 174 | + def clean(self): |
| 175 | + for macro in self.service.macros: |
| 176 | + if macro.name.startswith(self.macro_name): |
| 177 | + self.service.macros.delete(macro.name) |
| 178 | + |
| 179 | + def test_use_macro_in_search(self): |
| 180 | + self.service.macros.create(self.macro_name, 'eval test="123"') |
| 181 | + |
| 182 | + stream = self.service.jobs.oneshot( |
| 183 | + f"| makeresults count=1 | `{self.macro_name}`", |
| 184 | + output_mode="json", |
| 185 | + ) |
| 186 | + |
| 187 | + result = results.JSONResultsReader(stream) |
| 188 | + out = list(result) |
| 189 | + |
| 190 | + self.assertTrue(len(out) == 1) |
| 191 | + self.assertEqual(out[0]["test"], "123") |
| 192 | + |
| 193 | + def test_use_macro_in_search_with_single_arg(self): |
| 194 | + # Macros with arguments must contain the amount of arguments in parens, |
| 195 | + # otherwise a macro is not going to work. |
| 196 | + macro_name = self.macro_name + "(1)" |
| 197 | + |
| 198 | + self.service.macros.create(macro_name, 'eval test="$value$"', args="value") |
| 199 | + stream = self.service.jobs.oneshot( |
| 200 | + f"| makeresults count=1 | `{self.macro_name}(12)`", |
| 201 | + output_mode="json", |
| 202 | + ) |
| 203 | + |
| 204 | + result = results.JSONResultsReader(stream) |
| 205 | + out = list(result) |
| 206 | + |
| 207 | + self.assertTrue(len(out) == 1) |
| 208 | + self.assertEqual(out[0]["test"], "12") |
| 209 | + |
| 210 | + def test_use_macro_in_search_with_multiple_args(self): |
| 211 | + # Macros with arguments must contain the amount of arguments in parens, |
| 212 | + # otherwise a macro is not going to work. |
| 213 | + macro_name = self.macro_name + "(2)" |
| 214 | + |
| 215 | + self.service.macros.create( |
| 216 | + macro_name, 'eval test="$value$", test2="$value2$"', args="value,value2" |
| 217 | + ) |
| 218 | + stream = self.service.jobs.oneshot( |
| 219 | + f"| makeresults count=1 | `{self.macro_name}(12, 34)`", |
| 220 | + output_mode="json", |
| 221 | + ) |
| 222 | + |
| 223 | + result = results.JSONResultsReader(stream) |
| 224 | + out = list(result) |
| 225 | + |
| 226 | + self.assertTrue(len(out) == 1) |
| 227 | + self.assertEqual(out[0]["test"], "12") |
| 228 | + self.assertEqual(out[0]["test2"], "34") |
| 229 | + |
| 230 | + def test_use_macro_in_search_validation_success(self): |
| 231 | + macro_name = self.macro_name + "(2)" |
| 232 | + |
| 233 | + self.service.macros.create( |
| 234 | + macro_name, |
| 235 | + 'eval test="$value$", test2="$value2$"', |
| 236 | + args="value,value2", |
| 237 | + validation="value < value2", |
| 238 | + ) |
| 239 | + |
| 240 | + stream = self.service.jobs.oneshot( |
| 241 | + f"| makeresults count=1 | `{self.macro_name}(12, 34)`", |
| 242 | + output_mode="json", |
| 243 | + ) |
| 244 | + |
| 245 | + result = results.JSONResultsReader(stream) |
| 246 | + out = list(result) |
| 247 | + |
| 248 | + self.assertTrue(len(out) == 1) |
| 249 | + self.assertEqual(out[0]["test"], "12") |
| 250 | + self.assertEqual(out[0]["test2"], "34") |
| 251 | + |
| 252 | + def test_use_macro_in_search_validation_failure(self): |
| 253 | + macro_name = self.macro_name + "(2)" |
| 254 | + |
| 255 | + self.service.macros.create( |
| 256 | + macro_name, |
| 257 | + 'eval test="$value$", test2="$value2$"', |
| 258 | + args="value,value2", |
| 259 | + validation="value < value2", |
| 260 | + errormsg="value must be smaller that value2", |
| 261 | + ) |
| 262 | + |
| 263 | + def query(): |
| 264 | + self.service.jobs.oneshot( |
| 265 | + f"| makeresults count=1 | `{self.macro_name}(34, 12)`", |
| 266 | + output_mode="json", |
| 267 | + ) |
| 268 | + |
| 269 | + self.assertRaisesRegex(HTTPError, "value must be smaller that value2", query) |
| 270 | + |
| 271 | + |
156 | 272 | if __name__ == "__main__": |
157 | 273 | try: |
158 | 274 | import unittest2 as unittest |
|
0 commit comments