4 # Copyright 2016-17 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.
29 gi
.require_version('ProjectNsdYang', '1.0')
30 gi
.require_version('NsrYang', '1.0')
31 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
41 from rift
.mano
.utils
.project
import ManoProject
44 def prefix_project(xpath
):
45 return "/rw-project:project" + xpath
47 class TestGiXpath(unittest
.TestCase
):
51 def test_nsd_elements(self
):
53 Test that a particular element in a list is corerctly retrieved. In
54 this case, we are trying to retrieve an NSD from the NSD catalog.
57 # Create the initial NSD catalog
58 nsd_catalog
= ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog()
60 # Create an NSD, set its 'id', and add it to the catalog
61 nsd_id
= str(uuid
.uuid4())
62 nsd_catalog
.nsd
.append(
63 ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd(
68 # Retrieve the NSD using and xpath expression
69 xpath
= prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd[project-nsd:id={}]'.
71 nsd
= rwxpath
.getxattr(nsd_catalog
, xpath
)
73 self
.assertEqual(nsd_id
, nsd
.id)
75 # Modified the name of the NSD using an xpath expression
76 rwxpath
.setxattr(nsd_catalog
, xpath
+ "/project-nsd:name", "test-name")
78 name
= rwxpath
.getxattr(nsd_catalog
, xpath
+ "/project-nsd:name")
79 self
.assertEqual("test-name", name
)
81 def test_nsd_scalar_fields(self
):
83 Test that setxattr correctly sets the value specified by an xpath.
87 nsd
= ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd()
89 xpath
= prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd')
91 # Check that the unset fields are in fact set to None
92 self
.assertEqual(None, rwxpath
.getxattr(nsd
, xpath
+ "/project-nsd:name"))
93 self
.assertEqual(None, rwxpath
.getxattr(nsd
, xpath
+ "/project-nsd:short-name"))
95 # Set the values of the 'name' and 'short-name' fields
96 rwxpath
.setxattr(nsd
, xpath
+ "/project-nsd:name", "test-name")
97 rwxpath
.setxattr(nsd
, xpath
+ "/project-nsd:short-name", "test-short-name")
99 # Check that the 'name' and 'short-name' fields are correctly set
100 self
.assertEqual(nsd
.name
, rwxpath
.getxattr(nsd
, xpath
+ "/project-nsd:name"))
101 self
.assertEqual(nsd
.short_name
, rwxpath
.getxattr(nsd
, xpath
+ "/project-nsd:short-name"))
104 class TestInputParameterSubstitution(unittest
.TestCase
):
106 project
= ManoProject(logger
)
107 self
.substitute_input_parameters
= rwnsmtasklet
.InputParameterSubstitution(logger
, project
)
109 def test_null_arguments(self
):
111 If None is passed to the substitutor for either the NSD or the NSR
112 config, no exception should be raised.
115 nsd
= ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd()
116 nsr_config
= NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr()
118 self
.substitute_input_parameters(None, None)
119 self
.substitute_input_parameters(nsd
, None)
120 self
.substitute_input_parameters(None, nsr_config
)
122 def test_illegal_input_parameter(self
):
124 In the NSD there is a list of the parameters that are allowed to be
125 sbustituted by input parameters. This test checks that when an input
126 parameter is provided in the NSR config that is not in the NSD, it is
130 # Define the original NSD
131 nsd
= ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd()
133 nsd
.short_name
= "bob"
135 # Define which parameters may be modified
136 nsd
.input_parameter_xpath
.append(
137 ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
138 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
143 # Define the input parameters that are intended to be modified
144 nsr_config
= NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr()
145 nsr_config
.input_parameter
.extend([
146 NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
147 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
150 NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
151 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
156 self
.substitute_input_parameters(nsd
, nsr_config
)
158 # Verify that only the parameter in the input_parameter_xpath list is
159 # modified after the input parameters have been applied.
160 self
.assertEqual("alice", nsd
.name
)
161 self
.assertEqual("bob", nsd
.short_name
)
163 def test_substitution(self
):
165 Test that substitution of input parameters occurs as expected.
168 # Define the original NSD
169 nsd
= ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd()
170 # nsd.name = "robert"
171 # nsd.short_name = "bob"
173 # Define which parameters may be modified
174 nsd
.input_parameter_xpath
.extend([
175 ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
176 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
179 ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
180 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
181 label
="NSD Short Name",
185 # Define the input parameters that are intended to be modified
186 nsr_config
= NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr()
187 nsr_config
.input_parameter
.extend([
188 NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
189 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:name",
192 NsrYang
.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
193 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
198 self
.substitute_input_parameters(nsd
, nsr_config
)
200 # Verify that both the 'name' and 'short-name' fields are correctly
202 self
.assertEqual("robert", nsd
.name
)
203 self
.assertEqual("bob", nsd
.short_name
)
206 def main(argv
=sys
.argv
[1:]):
207 logging
.basicConfig(format
='TEST %(message)s')
209 parser
= argparse
.ArgumentParser()
210 parser
.add_argument('-v', '--verbose', action
='store_true')
212 args
= parser
.parse_args(argv
)
214 # Set the global logging level
215 logging
.getLogger().setLevel(logging
.DEBUG
if args
.verbose
else logging
.FATAL
)
217 # Make the test logger very quiet
218 logger
.addHandler(logging
.NullHandler())
220 # The unittest framework requires a program name, so use the name of this
221 # file instead (we do not want to have to pass a fake program name to main
222 # when this is called from the interpreter).
223 unittest
.main(argv
=[__file__
] + argv
,
224 testRunner
=xmlrunner
.XMLTestRunner(
225 output
=os
.environ
["RIFT_MODULE_TEST"]))
227 if __name__
== '__main__':