|
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 |
|
@@ -153,6 +155,115 @@ def test_acl_fails_without_owner(self): |
153 | 155 | ) |
154 | 156 |
|
155 | 157 |
|
| 158 | +class TestMacroSPL(testlib.SDKTestCase): |
| 159 | + macro_name = "SDKTestMacro" |
| 160 | + |
| 161 | + def setUp(self): |
| 162 | + testlib.SDKTestCase.setUp(self) |
| 163 | + self.clean() |
| 164 | + |
| 165 | + def tearDown(self): |
| 166 | + testlib.SDKTestCase.setUp(self) |
| 167 | + self.clean() |
| 168 | + |
| 169 | + def clean(self): |
| 170 | + for macro in self.service.macros: |
| 171 | + if macro.name.startswith(self.macro_name): |
| 172 | + self.service.macros.delete(macro.name) |
| 173 | + |
| 174 | + def test_use_macro_in_search(self): |
| 175 | + self.service.macros.create(self.macro_name, 'eval test="123"') |
| 176 | + |
| 177 | + stream = self.service.jobs.oneshot( |
| 178 | + f"| makeresults count=1 | `{self.macro_name}`", |
| 179 | + output_mode="json", |
| 180 | + ) |
| 181 | + |
| 182 | + result = results.JSONResultsReader(stream) |
| 183 | + out = list(result) |
| 184 | + |
| 185 | + self.assertTrue(len(out) == 1) |
| 186 | + self.assertEqual(out[0]["test"], "123") |
| 187 | + |
| 188 | + def test_use_macro_in_search_with_single_arg(self): |
| 189 | + # Macros with arguments must contain the amount of arguments in parens, |
| 190 | + # otherwise a macro is not going to work. |
| 191 | + macro_name = self.macro_name + "(1)" |
| 192 | + |
| 193 | + self.service.macros.create(macro_name, 'eval test="$value$"', args="value") |
| 194 | + stream = self.service.jobs.oneshot( |
| 195 | + f"| makeresults count=1 | `{self.macro_name}(12)`", |
| 196 | + output_mode="json", |
| 197 | + ) |
| 198 | + |
| 199 | + result = results.JSONResultsReader(stream) |
| 200 | + out = list(result) |
| 201 | + |
| 202 | + self.assertTrue(len(out) == 1) |
| 203 | + self.assertEqual(out[0]["test"], "12") |
| 204 | + |
| 205 | + def test_use_macro_in_search_with_multiple_args(self): |
| 206 | + # Macros with arguments must contain the amount of arguments in parens, |
| 207 | + # otherwise a macro is not going to work. |
| 208 | + macro_name = self.macro_name + "(2)" |
| 209 | + |
| 210 | + self.service.macros.create( |
| 211 | + macro_name, 'eval test="$value$", test2="$value2$"', args="value,value2" |
| 212 | + ) |
| 213 | + stream = self.service.jobs.oneshot( |
| 214 | + f"| makeresults count=1 | `{self.macro_name}(12, 34)`", |
| 215 | + output_mode="json", |
| 216 | + ) |
| 217 | + |
| 218 | + result = results.JSONResultsReader(stream) |
| 219 | + out = list(result) |
| 220 | + |
| 221 | + self.assertTrue(len(out) == 1) |
| 222 | + self.assertEqual(out[0]["test"], "12") |
| 223 | + self.assertEqual(out[0]["test2"], "34") |
| 224 | + |
| 225 | + def test_use_macro_in_search_validation_success(self): |
| 226 | + macro_name = self.macro_name + "(2)" |
| 227 | + |
| 228 | + self.service.macros.create( |
| 229 | + macro_name, |
| 230 | + 'eval test="$value$", test2="$value2$"', |
| 231 | + args="value,value2", |
| 232 | + validation="value < value2", |
| 233 | + ) |
| 234 | + |
| 235 | + stream = self.service.jobs.oneshot( |
| 236 | + f"| makeresults count=1 | `{self.macro_name}(12, 34)`", |
| 237 | + output_mode="json", |
| 238 | + ) |
| 239 | + |
| 240 | + result = results.JSONResultsReader(stream) |
| 241 | + out = list(result) |
| 242 | + |
| 243 | + self.assertTrue(len(out) == 1) |
| 244 | + self.assertEqual(out[0]["test"], "12") |
| 245 | + self.assertEqual(out[0]["test2"], "34") |
| 246 | + |
| 247 | + def test_use_macro_in_search_validation_failure(self): |
| 248 | + macro_name = self.macro_name + "(2)" |
| 249 | + |
| 250 | + self.service.macros.create( |
| 251 | + macro_name, |
| 252 | + 'eval test="$value$", test2="$value2$"', |
| 253 | + args="value,value2", |
| 254 | + validation="value < value2", |
| 255 | + errormsg="value must be smaller that value2", |
| 256 | + ) |
| 257 | + |
| 258 | + def query(): |
| 259 | + self.service.jobs.oneshot( |
| 260 | + f"| makeresults count=1 | `{self.macro_name}(34, 12)`", |
| 261 | + output_mode="json", |
| 262 | + ) |
| 263 | + |
| 264 | + self.assertRaisesRegex(HTTPError, "value must be smaller that value2", query) |
| 265 | + |
| 266 | + |
156 | 267 | if __name__ == "__main__": |
157 | 268 | try: |
158 | 269 | import unittest2 as unittest |
|
0 commit comments