48b4ff2edcddc12b0b1fc17ce600373b0886a1b1
[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
32 #Setting RIFT_VAR_ROOT if not already set for unit test execution
33 if "RIFT_VAR_ROOT" not in os.environ:
34 os.environ['RIFT_VAR_ROOT'] = os.path.join(os.environ['RIFT_INSTALL'], 'var/rift/unittest')
35
36 from gi.repository import (
37 ProjectNsdYang,
38 NsrYang,
39 )
40
41
42 logger = logging.getLogger('test-rwnsmtasklet')
43
44 import rift.tasklets.rwnsmtasklet.rwnsmtasklet as rwnsmtasklet
45 import rift.tasklets.rwnsmtasklet.xpath as rwxpath
46 from rift.mano.utils.project import ManoProject
47
48
49 def prefix_project(xpath):
50 return "/rw-project:project" + xpath
51
52 class TestGiXpath(unittest.TestCase):
53 def setUp(self):
54 rwxpath.reset_cache()
55
56 def test_nsd_elements(self):
57 """
58 Test that a particular element in a list is corerctly retrieved. In
59 this case, we are trying to retrieve an NSD from the NSD catalog.
60
61 """
62 # Create the initial NSD catalog
63 nsd_catalog = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog()
64
65 # Create an NSD, set its 'id', and add it to the catalog
66 nsd_id = str(uuid.uuid4())
67 nsd_catalog.nsd.append(
68 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd(
69 id=nsd_id,
70 )
71 )
72
73 # Retrieve the NSD using and xpath expression
74 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd[project-nsd:id={}]'.
75 format(nsd_id))
76 nsd = rwxpath.getxattr(nsd_catalog, xpath)
77
78 self.assertEqual(nsd_id, nsd.id)
79
80 # Modified the name of the NSD using an xpath expression
81 rwxpath.setxattr(nsd_catalog, xpath + "/project-nsd:name", "test-name")
82
83 name = rwxpath.getxattr(nsd_catalog, xpath + "/project-nsd:name")
84 self.assertEqual("test-name", name)
85
86 def test_nsd_scalar_fields(self):
87 """
88 Test that setxattr correctly sets the value specified by an xpath.
89
90 """
91 # Define a simple NSD
92 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
93
94 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd')
95
96 # Check that the unset fields are in fact set to None
97 self.assertEqual(None, rwxpath.getxattr(nsd, xpath + "/project-nsd:name"))
98 self.assertEqual(None, rwxpath.getxattr(nsd, xpath + "/project-nsd:short-name"))
99
100 # Set the values of the 'name' and 'short-name' fields
101 rwxpath.setxattr(nsd, xpath + "/project-nsd:name", "test-name")
102 rwxpath.setxattr(nsd, xpath + "/project-nsd:short-name", "test-short-name")
103
104 # Check that the 'name' and 'short-name' fields are correctly set
105 self.assertEqual(nsd.name, rwxpath.getxattr(nsd, xpath + "/project-nsd:name"))
106 self.assertEqual(nsd.short_name, rwxpath.getxattr(nsd, xpath + "/project-nsd:short-name"))
107
108
109 class TestInputParameterSubstitution(unittest.TestCase):
110 def setUp(self):
111 project = ManoProject(logger)
112 self.substitute_input_parameters = rwnsmtasklet.InputParameterSubstitution(logger, project)
113
114 def test_null_arguments(self):
115 """
116 If None is passed to the substitutor for either the NSD or the NSR
117 config, no exception should be raised.
118
119 """
120 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
121 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
122
123 self.substitute_input_parameters(None, None)
124 self.substitute_input_parameters(nsd, None)
125 self.substitute_input_parameters(None, nsr_config)
126
127 def test_illegal_input_parameter(self):
128 """
129 In the NSD there is a list of the parameters that are allowed to be
130 sbustituted by input parameters. This test checks that when an input
131 parameter is provided in the NSR config that is not in the NSD, it is
132 not applied.
133
134 """
135 # Define the original NSD
136 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
137 nsd.name = "robert"
138 nsd.short_name = "bob"
139
140 # Define which parameters may be modified
141 nsd.input_parameter_xpath.append(
142 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
143 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
144 label="NSD Name",
145 )
146 )
147
148 # Define the input parameters that are intended to be modified
149 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
150 nsr_config.input_parameter.extend([
151 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
152 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
153 value="alice",
154 ),
155 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
156 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
157 value="alice",
158 ),
159 ])
160
161 self.substitute_input_parameters(nsd, nsr_config)
162
163 # Verify that only the parameter in the input_parameter_xpath list is
164 # modified after the input parameters have been applied.
165 self.assertEqual("alice", nsd.name)
166 self.assertEqual("bob", nsd.short_name)
167
168 def test_substitution(self):
169 """
170 Test that substitution of input parameters occurs as expected.
171
172 """
173 # Define the original NSD
174 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
175 # nsd.name = "robert"
176 # nsd.short_name = "bob"
177
178 # Define which parameters may be modified
179 nsd.input_parameter_xpath.extend([
180 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
181 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
182 label="NSD Name",
183 ),
184 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
185 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
186 label="NSD Short Name",
187 ),
188 ])
189
190 # Define the input parameters that are intended to be modified
191 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
192 nsr_config.input_parameter.extend([
193 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
194 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
195 value="robert",
196 ),
197 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
198 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
199 value="bob",
200 ),
201 ])
202
203 self.substitute_input_parameters(nsd, nsr_config)
204
205 # Verify that both the 'name' and 'short-name' fields are correctly
206 # replaced.
207 self.assertEqual("robert", nsd.name)
208 self.assertEqual("bob", nsd.short_name)
209
210
211 def main(argv=sys.argv[1:]):
212 logging.basicConfig(format='TEST %(message)s')
213
214 parser = argparse.ArgumentParser()
215 parser.add_argument('-v', '--verbose', action='store_true')
216
217 args = parser.parse_args(argv)
218
219 # Set the global logging level
220 logging.getLogger().setLevel(logging.DEBUG if args.verbose else logging.FATAL)
221
222 # Make the test logger very quiet
223 logger.addHandler(logging.NullHandler())
224
225 # The unittest framework requires a program name, so use the name of this
226 # file instead (we do not want to have to pass a fake program name to main
227 # when this is called from the interpreter).
228 unittest.main(argv=[__file__] + argv,
229 testRunner=xmlrunner.XMLTestRunner(
230 output=os.environ["RIFT_MODULE_TEST"]))
231
232 if __name__ == '__main__':
233 main()