4 # Copyright 2016 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
28 #Setting RIFT_VAR_ROOT if not already set for unit test execution
29 if "RIFT_VAR_ROOT" not in os
.environ
:
30 os
.environ
['RIFT_VAR_ROOT'] = os
.path
.join(os
.environ
['RIFT_INSTALL'], 'var/rift/unittest')
32 from gi
.repository
import (
37 logger
= logging
.getLogger('test-rwnsmtasklet')
39 import rift
.tasklets
.rwnsmtasklet
.rwnsmtasklet
as rwnsmtasklet
40 import rift
.tasklets
.rwnsmtasklet
.xpath
as rwxpath
42 class TestGiXpath(unittest
.TestCase
):
46 def test_nsd_elements(self
):
48 Test that a particular element in a list is corerctly retrieved. In
49 this case, we are trying to retrieve an NSD from the NSD catalog.
52 # Create the initial NSD catalog
53 nsd_catalog
= NsdYang
.YangData_Nsd_NsdCatalog()
55 # Create an NSD, set its 'id', and add it to the catalog
56 nsd_id
= str(uuid
.uuid4())
57 nsd_catalog
.nsd
.append(
58 NsdYang
.YangData_Nsd_NsdCatalog_Nsd(
63 # Retrieve the NSD using and xpath expression
64 xpath
= '/nsd:nsd-catalog/nsd:nsd[nsd:id={}]'.format(nsd_id
)
65 nsd
= rwxpath
.getxattr(nsd_catalog
, xpath
)
67 self
.assertEqual(nsd_id
, nsd
.id)
69 # Modified the name of the NSD using an xpath expression
70 rwxpath
.setxattr(nsd_catalog
, xpath
+ "/nsd:name", "test-name")
72 name
= rwxpath
.getxattr(nsd_catalog
, xpath
+ "/nsd:name")
73 self
.assertEqual("test-name", name
)
75 def test_nsd_scalar_fields(self
):
77 Test that setxattr correctly sets the value specified by an xpath.
81 nsd
= NsdYang
.YangData_Nsd_NsdCatalog_Nsd()
83 # Check that the unset fields are in fact set to None
84 self
.assertEqual(None, rwxpath
.getxattr(nsd
, "/nsd:nsd-catalog/nsd:nsd/nsd:name"))
85 self
.assertEqual(None, rwxpath
.getxattr(nsd
, "/nsd:nsd-catalog/nsd:nsd/nsd:short-name"))
87 # Set the values of the 'name' and 'short-name' fields
88 rwxpath
.setxattr(nsd
, "/nsd:nsd-catalog/nsd:nsd/nsd:name", "test-name")
89 rwxpath
.setxattr(nsd
, "/nsd:nsd-catalog/nsd:nsd/nsd:short-name", "test-short-name")
91 # Check that the 'name' and 'short-name' fields are correctly set
92 self
.assertEqual(nsd
.name
, rwxpath
.getxattr(nsd
, "/nsd:nsd-catalog/nsd:nsd/nsd:name"))
93 self
.assertEqual(nsd
.short_name
, rwxpath
.getxattr(nsd
, "/nsd:nsd-catalog/nsd:nsd/nsd:short-name"))
96 class TestInputParameterSubstitution(unittest
.TestCase
):
98 self
.substitute_input_parameters
= rwnsmtasklet
.InputParameterSubstitution(logger
)
100 def test_null_arguments(self
):
102 If None is passed to the substitutor for either the NSD or the NSR
103 config, no exception should be raised.
106 nsd
= NsdYang
.YangData_Nsd_NsdCatalog_Nsd()
107 nsr_config
= NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr()
109 self
.substitute_input_parameters(None, None)
110 self
.substitute_input_parameters(nsd
, None)
111 self
.substitute_input_parameters(None, nsr_config
)
113 def test_illegal_input_parameter(self
):
115 In the NSD there is a list of the parameters that are allowed to be
116 sbustituted by input parameters. This test checks that when an input
117 parameter is provided in the NSR config that is not in the NSD, it is
121 # Define the original NSD
122 nsd
= NsdYang
.YangData_Nsd_NsdCatalog_Nsd()
124 nsd
.short_name
= "bob"
126 # Define which parameters may be modified
127 nsd
.input_parameter_xpath
.append(
128 NsdYang
.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
129 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
134 # Define the input parameters that are intended to be modified
135 nsr_config
= NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr()
136 nsr_config
.input_parameter
.extend([
137 NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
138 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
141 NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
142 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
147 self
.substitute_input_parameters(nsd
, nsr_config
)
149 # Verify that only the parameter in the input_parameter_xpath list is
150 # modified after the input parameters have been applied.
151 self
.assertEqual("alice", nsd
.name
)
152 self
.assertEqual("bob", nsd
.short_name
)
154 def test_substitution(self
):
156 Test that substitution of input parameters occurs as expected.
159 # Define the original NSD
160 nsd
= NsdYang
.YangData_Nsd_NsdCatalog_Nsd()
162 nsd
.short_name
= "bob"
164 # Define which parameters may be modified
165 nsd
.input_parameter_xpath
.extend([
166 NsdYang
.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
167 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
170 NsdYang
.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
171 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
172 label
="NSD Short Name",
176 # Define the input parameters that are intended to be modified
177 nsr_config
= NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr()
178 nsr_config
.input_parameter
.extend([
179 NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
180 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
183 NsrYang
.YangData_Nsr_NsInstanceConfig_Nsr_InputParameter(
184 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
189 self
.substitute_input_parameters(nsd
, nsr_config
)
191 # Verify that both the 'name' and 'short-name' fields are correctly
193 self
.assertEqual("robert", nsd
.name
)
194 self
.assertEqual("bob", nsd
.short_name
)
197 def main(argv
=sys
.argv
[1:]):
198 logging
.basicConfig(format
='TEST %(message)s')
200 parser
= argparse
.ArgumentParser()
201 parser
.add_argument('-v', '--verbose', action
='store_true')
203 args
= parser
.parse_args(argv
)
205 # Set the global logging level
206 logging
.getLogger().setLevel(logging
.DEBUG
if args
.verbose
else logging
.FATAL
)
208 # Make the test logger very quiet
209 logger
.addHandler(logging
.NullHandler())
211 # The unittest framework requires a program name, so use the name of this
212 # file instead (we do not want to have to pass a fake program name to main
213 # when this is called from the interpreter).
214 unittest
.main(argv
=[__file__
] + argv
,
215 testRunner
=xmlrunner
.XMLTestRunner(
216 output
=os
.environ
["RIFT_MODULE_TEST"]))
218 if __name__
== '__main__':