1+ # -*- coding: utf-8 -*-
2+ # Copyright 2011-2013 Splunk, Inc.
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License"): you may
5+ # not use this file except in compliance with the License. You may obtain
6+ # a copy of the License at
7+ #
8+ # http://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+ # License for the specific language governing permissions and limitations
14+ # under the License.
15+
16+ from splunklib .modularinput .modularinput_testlib import unittest , xml_compare
17+ from splunklib .modularinput .scheme import Scheme
18+ from splunklib .modularinput .argument import Argument
19+
20+ try :
21+ import xml .etree .cElementTree as ET
22+ except ImportError :
23+ import xml .etree .ElementTree as ET
24+
25+ class SchemeTest (unittest .TestCase ):
26+ def test_generate_xml_from_scheme_with_default_values (self ):
27+ """Checks the Scheme generated by creating a Scheme object and setting no fields on it.
28+ This test checks for sane defaults in the class."""
29+
30+ scheme = Scheme ("abcd" )
31+
32+ constructed = scheme .toXML ()
33+ expected = ET .parse (open ("data/scheme_with_defaults.xml" )).getroot ()
34+
35+ self .assertTrue (xml_compare (expected , constructed ))
36+
37+ def test_generate_xml_from_scheme (self ):
38+ """Checks that the XML generated by a Scheme object with all its fields set and
39+ some arguments added matches what we expect."""
40+
41+ scheme = Scheme ("abcd" )
42+ scheme .description = u"쎼 and 쎶 and <&> für"
43+ scheme .streamingMode = scheme .streamingModeSimple
44+ scheme .useExternalValidation = "false"
45+ scheme .useSingleInstance = "true"
46+
47+ arg1 = Argument ("arg1" )
48+ scheme .addArgument (arg1 )
49+
50+ arg2 = Argument ("arg2" )
51+ arg2 .description = u"쎼 and 쎶 and <&> für"
52+ arg2 .dataType = arg2 .dataTypeNumber
53+ arg2 .requiredOnCreate = "true"
54+ arg2 .requiredOnEdit = "true"
55+ arg2 .validation = "is_pos_int('some_name')"
56+ scheme .addArgument (arg2 )
57+
58+ constructed = scheme .toXML ()
59+ expected = ET .parse (open ("data/scheme_without_defaults.xml" )).getroot ()
60+
61+ self .assertTrue (xml_compare (expected , constructed ))
62+
63+ def test_generate_xml_from_argument_with_default_values (self ):
64+ """Checks that the XML produced from an Argument class that is initialized but has no additional manipulations
65+ made to it is what we expect. This is mostly a check of the default values."""
66+
67+ argument = Argument ("some_name" )
68+
69+ root = ET .Element ("" )
70+ constructed = argument .addToDocument (root )
71+
72+ expected = ET .parse (open ("data/argument_with_defaults.xml" )).getroot ()
73+
74+ self .assertTrue (xml_compare (expected , constructed ))
75+
76+ def test_generate_xml_from_argument (self ):
77+ """Checks that the XML generated by an Argument class with all its possible values set is what we expect."""
78+ argument = Argument ("some_name" )
79+ argument .description = u"쎼 and 쎶 and <&> für"
80+ argument .dataType = argument .dataTypeBoolean
81+ argument .validation = "is_pos_int('some_name')"
82+ argument .requiredOnEdit = "true"
83+ argument .requiredOnCreate = "true"
84+
85+ root = ET .Element ("" )
86+ constructed = argument .addToDocument (root )
87+
88+ expected = ET .parse (open ("data/argument_without_defaults.xml" )).getroot ()
89+
90+ self .assertTrue (xml_compare (expected , constructed ))
91+
92+ if __name__ == "__main__" :
93+ unittest .main ()
0 commit comments