Merge from OSM SO master
[osm/SO.git] / rwlaunchpad / test / utest_rwnsm.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016-17 RIFT.IO Inc
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 #
18
19
20 import argparse
21 import logging
22 import os
23 import sys
24 import unittest
25 import uuid
26 import xmlrunner
27
28 import gi
29 gi.require_version('ProjectNsdYang', '1.0')
30 gi.require_version('NsrYang', '1.0')
31 from gi.repository import (
32 ProjectNsdYang,
33 NsrYang,
34 )
35
36
37 logger = logging.getLogger('test-rwnsmtasklet')
38
39 import rift.tasklets.rwnsmtasklet.rwnsmtasklet as rwnsmtasklet
40 import rift.tasklets.rwnsmtasklet.xpath as rwxpath
41 from rift.mano.utils.project import ManoProject
42
43
44 def prefix_project(xpath):
45 return "/rw-project:project" + xpath
46
47 class TestGiXpath(unittest.TestCase):
48 def setUp(self):
49 rwxpath.reset_cache()
50
51 def test_nsd_elements(self):
52 """
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.
55
56 """
57 # Create the initial NSD catalog
58 nsd_catalog = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog()
59
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(
64 id=nsd_id,
65 )
66 )
67
68 # Retrieve the NSD using and xpath expression
69 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd[project-nsd:id={}]'.
70 format(nsd_id))
71 nsd = rwxpath.getxattr(nsd_catalog, xpath)
72
73 self.assertEqual(nsd_id, nsd.id)
74
75 # Modified the name of the NSD using an xpath expression
76 rwxpath.setxattr(nsd_catalog, xpath + "/project-nsd:name", "test-name")
77
78 name = rwxpath.getxattr(nsd_catalog, xpath + "/project-nsd:name")
79 self.assertEqual("test-name", name)
80
81 def test_nsd_scalar_fields(self):
82 """
83 Test that setxattr correctly sets the value specified by an xpath.
84
85 """
86 # Define a simple NSD
87 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
88
89 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd')
90
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"))
94
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")
98
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"))
102
103
104 class TestInputParameterSubstitution(unittest.TestCase):
105 def setUp(self):
106 project = ManoProject(logger)
107 self.substitute_input_parameters = rwnsmtasklet.InputParameterSubstitution(logger, project)
108
109 def test_null_arguments(self):
110 """
111 If None is passed to the substitutor for either the NSD or the NSR
112 config, no exception should be raised.
113
114 """
115 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
116 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
117
118 self.substitute_input_parameters(None, None)
119 self.substitute_input_parameters(nsd, None)
120 self.substitute_input_parameters(None, nsr_config)
121
122 def test_illegal_input_parameter(self):
123 """
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
127 not applied.
128
129 """
130 # Define the original NSD
131 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
132 nsd.name = "robert"
133 nsd.short_name = "bob"
134
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",
139 label="NSD Name",
140 )
141 )
142
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",
148 value="alice",
149 ),
150 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
151 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
152 value="alice",
153 ),
154 ])
155
156 self.substitute_input_parameters(nsd, nsr_config)
157
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)
162
163 def test_substitution(self):
164 """
165 Test that substitution of input parameters occurs as expected.
166
167 """
168 # Define the original NSD
169 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
170 # nsd.name = "robert"
171 # nsd.short_name = "bob"
172
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",
177 label="NSD Name",
178 ),
179 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
180 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
181 label="NSD Short Name",
182 ),
183 ])
184
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",
190 value="robert",
191 ),
192 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
193 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
194 value="bob",
195 ),
196 ])
197
198 self.substitute_input_parameters(nsd, nsr_config)
199
200 # Verify that both the 'name' and 'short-name' fields are correctly
201 # replaced.
202 self.assertEqual("robert", nsd.name)
203 self.assertEqual("bob", nsd.short_name)
204
205
206 def main(argv=sys.argv[1:]):
207 logging.basicConfig(format='TEST %(message)s')
208
209 parser = argparse.ArgumentParser()
210 parser.add_argument('-v', '--verbose', action='store_true')
211
212 args = parser.parse_args(argv)
213
214 # Set the global logging level
215 logging.getLogger().setLevel(logging.DEBUG if args.verbose else logging.FATAL)
216
217 # Make the test logger very quiet
218 logger.addHandler(logging.NullHandler())
219
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"]))
226
227 if __name__ == '__main__':
228 main()