Skip to content

Commit c4f663d

Browse files
author
Shakeel Mohamed
committed
Updated modular input tests so they work when run with all tests
There was an issue with relative paths, which has now been resolved. See modularinput_testlib for the new data_open() function.
1 parent b0da92b commit c4f663d

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

tests/modularinput/modularinput_testlib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323

2424
sys.path.insert(0, os.path.join('../../splunklib', '..'))
2525

26-
from splunklib.modularinput.utils import xml_compare, parse_xml_data, parse_parameters
26+
from splunklib.modularinput.utils import xml_compare, parse_xml_data, parse_parameters
27+
28+
def data_open(filepath):
29+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), filepath)

tests/modularinput/test_event.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from tests.modularinput.modularinput_testlib import unittest, xml_compare
17+
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
1818
from splunklib.modularinput.event import Event, ET
1919
from splunklib.modularinput.event_writer import EventWriter
2020

@@ -46,7 +46,7 @@ def test_xml_of_event_with_minimal_configuration(self):
4646
event.write_to(stream)
4747

4848
constructed = ET.fromstring(stream.getvalue())
49-
expected = ET.parse(open("data/event_minimal.xml")).getroot()
49+
expected = ET.parse(data_open("data/event_minimal.xml")).getroot()
5050

5151
self.assertTrue(xml_compare(expected, constructed))
5252

@@ -69,7 +69,7 @@ def test_xml_of_event_with_more_configuration(self):
6969
event.write_to(stream)
7070

7171
constructed = ET.fromstring(stream.getvalue())
72-
expected = ET.parse(open("data/event_maximal.xml")).getroot()
72+
expected = ET.parse(data_open("data/event_maximal.xml")).getroot()
7373

7474
self.assertTrue(xml_compare(expected, constructed))
7575

@@ -95,7 +95,7 @@ def test_writing_events_on_event_writer(self):
9595
ew.write_event(e)
9696

9797
found = ET.fromstring("%s</stream>" % out.getvalue())
98-
expected = ET.parse(open("data/stream_with_one_event.xml")).getroot()
98+
expected = ET.parse(data_open("data/stream_with_one_event.xml")).getroot()
9999

100100
self.assertTrue(xml_compare(expected, found))
101101
self.assertEqual(err.getvalue(), "")
@@ -104,7 +104,7 @@ def test_writing_events_on_event_writer(self):
104104
ew.close()
105105

106106
found = ET.fromstring(out.getvalue())
107-
expected = ET.parse(open("data/stream_with_two_events.xml")).getroot()
107+
expected = ET.parse(data_open("data/stream_with_two_events.xml")).getroot()
108108

109109
self.assertTrue(xml_compare(expected, found))
110110

@@ -142,7 +142,7 @@ def test_write_xml_is_sane(self):
142142

143143
ew = EventWriter(out, err)
144144

145-
expected_xml = ET.parse(open("data/event_maximal.xml")).getroot()
145+
expected_xml = ET.parse(data_open("data/event_maximal.xml")).getroot()
146146

147147
ew.write_xml_document(expected_xml)
148148
found_xml = ET.fromstring(out.getvalue())

tests/modularinput/test_input_definition.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from tests.modularinput.modularinput_testlib import unittest
17+
from tests.modularinput.modularinput_testlib import unittest, data_open
1818
from splunklib.modularinput.input_definition import InputDefinition
1919

2020
class InputDefinitionTestCase(unittest.TestCase):
2121

2222
def test_parse_inputdef_with_zero_inputs(self):
2323
"""Check parsing of XML that contains only metadata"""
2424

25-
found = InputDefinition.parse(open("data/conf_with_0_inputs.xml"))
25+
found = InputDefinition.parse(data_open("data/conf_with_0_inputs.xml"))
2626

2727
expectedDefinition = InputDefinition()
2828
expectedDefinition.metadata = {
@@ -37,7 +37,7 @@ def test_parse_inputdef_with_zero_inputs(self):
3737
def test_parse_inputdef_with_two_inputs(self):
3838
"""Check parsing of XML that contains 2 inputs"""
3939

40-
found = InputDefinition.parse(open("data/conf_with_2_inputs.xml"))
40+
found = InputDefinition.parse(data_open("data/conf_with_2_inputs.xml"))
4141

4242
expectedDefinition = InputDefinition()
4343
expectedDefinition.metadata = {
@@ -67,7 +67,7 @@ def test_attempt_to_parse_malformed_input_definition_will_throw_exception(self):
6767
"""Does malformed XML cause the expected exception."""
6868

6969
with self.assertRaises(ValueError):
70-
found = InputDefinition.parse(open("data/conf_with_invalid_inputs.xml"))
70+
found = InputDefinition.parse(data_open("data/conf_with_invalid_inputs.xml"))
7171

7272
if __name__ == "__main__":
7373
unittest.main()

tests/modularinput/test_scheme.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16-
from tests.modularinput.modularinput_testlib import unittest, xml_compare
16+
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
1717
from splunklib.modularinput.scheme import Scheme
1818
from splunklib.modularinput.argument import Argument
1919

@@ -30,7 +30,7 @@ def test_generate_xml_from_scheme_with_default_values(self):
3030
scheme = Scheme("abcd")
3131

3232
constructed = scheme.to_xml()
33-
expected = ET.parse(open("data/scheme_with_defaults.xml")).getroot()
33+
expected = ET.parse(data_open("data/scheme_with_defaults.xml")).getroot()
3434

3535
self.assertTrue(xml_compare(expected, constructed))
3636

@@ -58,7 +58,7 @@ def test_generate_xml_from_scheme(self):
5858
scheme.add_argument(arg2)
5959

6060
constructed = scheme.to_xml()
61-
expected = ET.parse(open("data/scheme_without_defaults.xml")).getroot()
61+
expected = ET.parse(data_open("data/scheme_without_defaults.xml")).getroot()
6262

6363
self.assertTrue(xml_compare(expected, constructed))
6464

@@ -71,7 +71,7 @@ def test_generate_xml_from_argument_with_default_values(self):
7171
root = ET.Element("")
7272
constructed = argument.add_to_document(root)
7373

74-
expected = ET.parse(open("data/argument_with_defaults.xml")).getroot()
74+
expected = ET.parse(data_open("data/argument_with_defaults.xml")).getroot()
7575

7676
self.assertTrue(xml_compare(expected, constructed))
7777

@@ -90,7 +90,7 @@ def test_generate_xml_from_argument(self):
9090
root = ET.Element("")
9191
constructed = argument.add_to_document(root)
9292

93-
expected = ET.parse(open("data/argument_without_defaults.xml")).getroot()
93+
expected = ET.parse(data_open("data/argument_without_defaults.xml")).getroot()
9494

9595
self.assertTrue(xml_compare(expected, constructed))
9696

tests/modularinput/test_script.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from tests.modularinput.modularinput_testlib import unittest, xml_compare
15+
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
1616
from splunklib.modularinput.argument import Argument
1717
from splunklib.modularinput.event import Event
1818
from splunklib.modularinput.event_writer import EventWriter
@@ -103,7 +103,7 @@ def stream_events(self, inputs, ew):
103103
self.assertEqual(0, return_value)
104104

105105
found = ET.fromstring(out.getvalue())
106-
expected = ET.parse(open("data/scheme_without_defaults.xml")).getroot()
106+
expected = ET.parse(data_open("data/scheme_without_defaults.xml")).getroot()
107107

108108
self.assertTrue(xml_compare(expected, found))
109109

@@ -131,7 +131,7 @@ def stream_events(self, inputs, ew):
131131

132132
args = [TEST_SCRIPT_PATH, "--validate-arguments"]
133133

134-
return_value = script.run_script(args, ew, open("data/validation.xml"))
134+
return_value = script.run_script(args, ew, data_open("data/validation.xml"))
135135

136136
self.assertEqual("", err.getvalue())
137137
self.assertEqual("", out.getvalue())
@@ -160,9 +160,9 @@ def stream_events(self, inputs, ew):
160160

161161
args = [TEST_SCRIPT_PATH, "--validate-arguments"]
162162

163-
return_value = script.run_script(args, ew, open("data/validation.xml"))
163+
return_value = script.run_script(args, ew, data_open("data/validation.xml"))
164164

165-
expected = ET.parse(open("data/validation_error.xml")).getroot()
165+
expected = ET.parse(data_open("data/validation_error.xml")).getroot()
166166
found = ET.fromstring(out.getvalue())
167167

168168
self.assertEqual("", err.getvalue())
@@ -194,7 +194,7 @@ def stream_events(self, inputs, ew):
194194
ew.write_event(event)
195195

196196
script = NewScript()
197-
input_configuration = open("data/conf_with_2_inputs.xml")
197+
input_configuration = data_open("data/conf_with_2_inputs.xml")
198198

199199
out = StringIO()
200200
err = StringIO()
@@ -205,7 +205,7 @@ def stream_events(self, inputs, ew):
205205
self.assertEqual(0, return_value)
206206
self.assertEqual("", err.getvalue())
207207

208-
expected = ET.parse(open("data/stream_with_two_events.xml")).getroot()
208+
expected = ET.parse(data_open("data/stream_with_two_events.xml")).getroot()
209209
found = ET.fromstring(out.getvalue())
210210

211211
self.assertTrue(xml_compare(expected, found))

tests/modularinput/test_validation_definition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from tests.modularinput.modularinput_testlib import unittest
17+
from tests.modularinput.modularinput_testlib import unittest, data_open
1818
from splunklib.modularinput.validation_definition import ValidationDefinition
1919

2020
class ValidationDefinitionTestCase(unittest.TestCase):
2121
def test_validation_definition_parse(self):
2222
"""Check that parsing produces expected result"""
23-
found = ValidationDefinition.parse(open("data/validation.xml"))
23+
found = ValidationDefinition.parse(data_open("data/validation.xml"))
2424

2525
expected = ValidationDefinition()
2626
expected.metadata = {

0 commit comments

Comments
 (0)