Skip to content

Commit 6de12b1

Browse files
Synsemaszyk99
authored andcommitted
Add tests for macros
1 parent 93f51c4 commit 6de12b1

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

tests/test_macro.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2015 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from __future__ import absolute_import
18+
from tests import testlib
19+
import logging
20+
21+
import splunklib.client as client
22+
23+
import pytest
24+
25+
@pytest.mark.smoke
26+
class TestMacro(testlib.SDKTestCase):
27+
def setUp(self):
28+
super(TestMacro, self).setUp()
29+
macros = self.service.macros
30+
logging.debug("Macros namespace: %s", macros.service.namespace)
31+
self.macro_name = testlib.tmpname()
32+
definition = '| eval test="123"'
33+
self.macro = macros.create(self.macro_name, definition)
34+
35+
def tearDown(self):
36+
super(TestMacro, self).setUp()
37+
for macro in self.service.macros:
38+
if macro.name.startswith('delete-me'):
39+
self.service.macros.delete(macro.name)
40+
41+
def check_macro(self, macro):
42+
self.check_entity(macro)
43+
expected_fields = ['definition',
44+
'iseval',
45+
'args',
46+
'validation',
47+
'errormsg']
48+
for f in expected_fields:
49+
macro[f]
50+
is_eval = macro.iseval
51+
self.assertTrue(is_eval == '1' or is_eval == '0')
52+
53+
def test_create(self):
54+
self.assertTrue(self.macro_name in self.service.macros)
55+
self.check_macro(self.macro)
56+
57+
def test_create_with_args(self):
58+
macro_name = testlib.tmpname() + '(1)'
59+
definition = '| eval value="$value$"'
60+
kwargs = {
61+
'args': 'value',
62+
'validation': '$value$ > 10',
63+
'errormsg': 'value must be greater than 10'
64+
}
65+
macro = self.service.macros.create(macro_name, definition=definition, **kwargs)
66+
self.assertTrue(macro_name in self.service.macros)
67+
self.check_macro(macro)
68+
self.assertEqual(macro.iseval, '0')
69+
self.assertEqual(macro.args, kwargs.get('args'))
70+
self.assertEqual(macro.validation, kwargs.get('validation'))
71+
self.assertEqual(macro.errormsg, kwargs.get('errormsg'))
72+
self.service.macros.delete(macro_name)
73+
74+
def test_delete(self):
75+
self.assertTrue(self.macro_name in self.service.macros)
76+
self.service.macros.delete(self.macro_name)
77+
self.assertFalse(self.macro_name in self.service.macros)
78+
self.assertRaises(client.HTTPError,
79+
self.macro.refresh)
80+
81+
def test_update(self):
82+
new_definition = '| eval updated="true"'
83+
self.macro.update(definition=new_definition)
84+
self.macro.refresh()
85+
self.assertEqual(self.macro['definition'], new_definition)
86+
87+
is_eval = testlib.to_bool(self.macro['iseval'])
88+
self.macro.update(iseval=not is_eval)
89+
self.macro.refresh()
90+
self.assertEqual(testlib.to_bool(self.macro['iseval']), not is_eval)
91+
92+
def test_cannot_update_name(self):
93+
new_name = self.macro_name + '-alteration'
94+
self.assertRaises(client.IllegalOperationException,
95+
self.macro.update, name=new_name)
96+
97+
def test_name_collision(self):
98+
opts = self.opts.kwargs.copy()
99+
opts['owner'] = '-'
100+
opts['app'] = '-'
101+
opts['sharing'] = 'user'
102+
service = client.connect(**opts)
103+
logging.debug("Namespace for collision testing: %s", service.namespace)
104+
macros = service.macros
105+
name = testlib.tmpname()
106+
107+
dispatch1 = '| eval macro_one="1"'
108+
dispatch2 = '| eval macro_two="2"'
109+
namespace1 = client.namespace(app='search', sharing='app')
110+
namespace2 = client.namespace(owner='admin', app='search', sharing='user')
111+
new_macro2 = macros.create(
112+
name, dispatch2,
113+
namespace=namespace1)
114+
new_macro1 = macros.create(
115+
name, dispatch1,
116+
namespace=namespace2)
117+
118+
self.assertRaises(client.AmbiguousReferenceException,
119+
macros.__getitem__, name)
120+
macro1 = macros[name, namespace1]
121+
self.check_macro(macro1)
122+
macro1.update(**{'definition': '| eval number=1'})
123+
macro1.refresh()
124+
self.assertEqual(macro1['definition'], '| eval number=1')
125+
macro2 = macros[name, namespace2]
126+
macro2.update(**{'definition': '| eval number=2'})
127+
macro2.refresh()
128+
self.assertEqual(macro2['definition'], '| eval number=2')
129+
self.check_macro(macro2)
130+
131+
def test_no_equality(self):
132+
self.assertRaises(client.IncomparableException,
133+
self.macro.__eq__, self.macro)
134+
135+
def test_acl(self):
136+
self.assertEqual(self.macro.access["perms"], None)
137+
self.macro.acl_update(sharing="app", owner="admin", **{"perms.read": "admin, nobody"})
138+
self.assertEqual(self.macro.access["owner"], "admin")
139+
self.assertEqual(self.macro.access["sharing"], "app")
140+
self.assertEqual(self.macro.access["perms"]["read"], ['admin', 'nobody'])
141+
142+
def test_acl_fails_without_sharing(self):
143+
self.assertRaisesRegex(
144+
ValueError,
145+
"Required argument 'sharing' is missing.",
146+
self.macro.acl_update,
147+
owner="admin", app="search", **{"perms.read": "admin, nobody"}
148+
)
149+
150+
def test_acl_fails_without_owner(self):
151+
self.assertRaisesRegex(
152+
ValueError,
153+
"Required argument 'owner' is missing.",
154+
self.macro.acl_update,
155+
sharing="app", app="search", **{"perms.read": "admin, nobody"}
156+
)
157+
158+
159+
if __name__ == "__main__":
160+
try:
161+
import unittest2 as unittest
162+
except ImportError:
163+
import unittest
164+
unittest.main()

0 commit comments

Comments
 (0)