Skip to content

Commit c744b8f

Browse files
committed
Adding urlhandling test script. Removed unecessesary UrlEncoded check. Formatting
1 parent 6f1df19 commit c744b8f

File tree

3 files changed

+135
-9
lines changed

3 files changed

+135
-9
lines changed

splunklib/binding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def __new__(self, val='', skip_encode=False, encode_slash=False):
122122
elif skip_encode:
123123
return str.__new__(self, val)
124124
elif encode_slash:
125-
return str.__new__(self,urllib.quote(val, ''))
125+
return str.__new__(self, urllib.quote(val, ''))
126126
else:
127127
# When subclassing str, just call str's __new__ method
128128
# with your class and the value you want to have in the

splunklib/client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
676676
# self.path to the Endpoint is relative in the SDK, so passing
677677
# owner, app, sharing, etc. along will produce the correct
678678
# namespace in the final request.
679-
if not isinstance(path_segment, UrlEncoded): path_segment=UrlEncoded(path_segment, encode_slash=True)
679+
path_segment = UrlEncoded(path_segment, encode_slash=True)
680680
if path_segment.startswith('/'):
681681
path = path_segment
682682
else:
@@ -1161,7 +1161,6 @@ def __getitem__(self, key):
11611161
response = self.get(key, owner=ns.owner, app=ns.app)
11621162
else:
11631163
response = self.get(key)
1164-
11651164
entries = self._load_list(response)
11661165
if len(entries) > 1:
11671166
raise AmbiguousReferenceException("Found multiple entities named '%s'; please specify a namespace." % key)
@@ -1520,7 +1519,8 @@ def delete(self, name, **params):
15201519
saved_searches.delete('my_saved_search')
15211520
assert 'my_saved_search' not in saved_searches
15221521
"""
1523-
if not isinstance(name, UrlEncoded): name = UrlEncoded(name, encode_slash=True)
1522+
1523+
name = UrlEncoded(name, encode_slash=True)
15241524
if 'namespace' in params:
15251525
namespace = params.pop('namespace')
15261526
params['owner'] = namespace.owner
@@ -1964,7 +1964,7 @@ def __getitem__(self, key):
19641964
if isinstance(key, tuple) and len(key) == 2:
19651965
# Fetch a single kind
19661966
key, kind = key
1967-
if not isinstance(key, UrlEncoded): key = UrlEncoded(key, encode_slash=True)
1967+
key = UrlEncoded(key, encode_slash=True)
19681968
try:
19691969
response = self.get(self.kindpath(kind) + "/" + key)
19701970
entries = self._load_list(response)
@@ -1981,7 +1981,7 @@ def __getitem__(self, key):
19811981
raise
19821982
else:
19831983
# Iterate over all the kinds looking for matches.
1984-
if not isinstance(key, UrlEncoded): key = UrlEncoded(key, encode_slash=True)
1984+
key = UrlEncoded(key, encode_slash=True)
19851985
kind = None
19861986
candidate = None
19871987
for kind in self.kinds:
@@ -2002,7 +2002,7 @@ def __getitem__(self, key):
20022002
else:
20032003
raise
20042004
if candidate is None:
2005-
raise KeyError(key) # Never found a match. Refer to name matching <http://docs.splunk.com/Documentation/Splunk/6.1.1/RESTAPI/RESTinput#data.2Finputs.2Fmonitor.2F.7Bname.7D>
2005+
raise KeyError(key) # Never found a match.
20062006
else:
20072007
return candidate
20082008

@@ -2015,6 +2015,7 @@ def __contains__(self, key):
20152015
except KeyError:
20162016
return False
20172017
else:
2018+
key = UrlEncoded(key, encode_slash=True)
20182019
# Without a kind, we want to minimize the number of round trips to the server, so we
20192020
# reimplement some of the behavior of __getitem__ in order to be able to stop searching
20202021
# on the first hit.
@@ -2077,8 +2078,7 @@ def create(self, name, kind, **kwargs):
20772078
# and we have to adjust accordingly.
20782079

20792080
# Must check if the name passed in is UrlEncoded and change it accordingly.
2080-
if not isinstance(name, UrlEncoded): name = UrlEncoded(name, encode_slash=True)
2081-
2081+
name = UrlEncoded(name, encode_slash=True)
20822082
path = _path(
20832083
self.path + kindpath,
20842084
'%s:%s' % (kwargs['restrictToHost'], name) \

tests/test_urlhandling.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import os
2+
from subprocess import PIPE, Popen
3+
from os.path import expanduser
4+
import time
5+
import sys
6+
import urllib
7+
import testlib
8+
9+
import splunklib.client as client
10+
from splunklib.binding import UrlEncoded
11+
12+
try:
13+
from utils import *
14+
except ImportError:
15+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
16+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
17+
18+
# Creates a file in the default Home directory
19+
# Probably won't work with windows
20+
def make_file(filename):
21+
Popen(("touch '%s/%s'"%(expanduser('~'),filename)), shell=True)
22+
23+
24+
def delete_file(filename):
25+
Popen(("rm '%s/%s'"%(expanduser('~'),filename)), shell=True)
26+
27+
28+
class ExamplesTestCase(testlib.SDKTestCase):
29+
def setUp(self):
30+
if os.name == "nt":
31+
sys.exit("Windows not supported")
32+
33+
opts = parse(sys.argv[1:], config=".splunkrc")
34+
kwargs = dslice(opts.kwargs, FLAGS_SPLUNK)
35+
self.service = client.connect(**kwargs)
36+
super(ExamplesTestCase, self).setUp()
37+
38+
def test_creating_monitor_inputs(self):
39+
input_names = [
40+
"myfile#",
41+
"myfile!",
42+
"myfile$",
43+
"myfile(abc).txt"
44+
]
45+
for name in input_names:
46+
make_file(name)
47+
pathname = '/'.join([expanduser('~'), name])
48+
if self.service.inputs.__contains__(pathname):
49+
self.service.inputs.delete(name=pathname, kind="monitor")
50+
time.sleep(1) # Need to wait for delete to be processed or else there is a failure.
51+
inp = self.service.inputs.create(name=pathname, kind="monitor")
52+
else:
53+
inp = self.service.inputs.create(name=pathname, kind="monitor")
54+
test = self.service.inputs[pathname]
55+
test.refresh()
56+
inp.refresh()
57+
self.assertTrue(inp.name == pathname)
58+
self.assertTrue(inp.access == test.access)
59+
self.assertTrue(inp.content == test.content)
60+
self.assertTrue(inp.fields == test.fields)
61+
self.assertTrue(inp.links == test.links)
62+
self.assertTrue(inp.state == test.state)
63+
self.service.inputs.delete(name=pathname)
64+
delete_file(name)
65+
66+
def test_saved_searches(self):
67+
search_name = [
68+
"mysearch#",
69+
"my/search@",
70+
"my/search/test(abc)"
71+
]
72+
search_param = "search *"
73+
for search in search_name:
74+
if self.service.saved_searches.__contains__(search):
75+
service.saved_searches.delete(search)
76+
sear = self.service.saved_searches.create(name=search, search=search_param)
77+
else:
78+
sear = self.service.saved_searches.create(name=search, search=search_param)
79+
test = self.service.saved_searches[search]
80+
test.refresh()
81+
sear.refresh()
82+
self.assertTrue(sear.name == search)
83+
self.assertTrue(sear.name == test.name)
84+
self.assertTrue(sear.access == test.access)
85+
self.assertTrue(sear.content == test.content)
86+
self.service.saved_searches.delete(search)
87+
88+
def test_urlhandling_inputs(self):
89+
for inp in self.service.inputs:
90+
raw = self.service.inputs.get(name=inp.name)
91+
url_name = UrlEncoded(inp.name, encode_slash=True)
92+
encoded = self.service.inputs.get(name=url_name)
93+
self.assertTrue(raw.status == encoded.status)
94+
self.assertTrue(raw.headers == encoded.headers)
95+
self.assertTrue(raw.reason == encoded.reason)
96+
self.assertTrue(str(raw.body)== str(encoded.body))
97+
98+
def test_urlhandling_saved_searchs(self):
99+
for ss in self.service.saved_searches:
100+
raw = self.service.saved_searches.get(ss.name)
101+
encoded = self.service.saved_searches.get(UrlEncoded(ss.name, encode_slash=True))
102+
self.assertTrue(raw.status == encoded.status)
103+
self.assertTrue(raw.headers == encoded.headers)
104+
self.assertTrue(raw.reason == encoded.reason)
105+
self.assertTrue(str(raw.body)== str(encoded.body))
106+
107+
def test_urlhandling(self):
108+
for inp in self.service.inputs:
109+
name = inp.name
110+
self.assertTrue(urllib.quote(name) == UrlEncoded(name))
111+
self.assertTrue(urllib.quote(name, '') == UrlEncoded(name, encode_slash=True))
112+
self.assertTrue(name == UrlEncoded(name, skip_encode=True))
113+
114+
115+
if __name__=="__main__":
116+
os.chdir("../examples")
117+
try:
118+
import unittest2 as unittest2
119+
except ImportError:
120+
import unittest
121+
unittest.main()
122+
123+
124+
125+
126+

0 commit comments

Comments
 (0)