blob: 48b4ff2edcddc12b0b1fc17ce600373b0886a1b1 [file] [log] [blame]
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04001#!/usr/bin/env python3
2
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -04003#
4# Copyright 2016-17 RIFT.IO Inc
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04005#
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
20import argparse
21import logging
22import os
23import sys
24import unittest
25import uuid
26import xmlrunner
27
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040028import gi
29gi.require_version('ProjectNsdYang', '1.0')
30gi.require_version('NsrYang', '1.0')
31
32#Setting RIFT_VAR_ROOT if not already set for unit test execution
33if "RIFT_VAR_ROOT" not in os.environ:
34 os.environ['RIFT_VAR_ROOT'] = os.path.join(os.environ['RIFT_INSTALL'], 'var/rift/unittest')
35
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040036from gi.repository import (
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040037 ProjectNsdYang,
38 NsrYang,
39)
40
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040041
42logger = logging.getLogger('test-rwnsmtasklet')
43
44import rift.tasklets.rwnsmtasklet.rwnsmtasklet as rwnsmtasklet
45import rift.tasklets.rwnsmtasklet.xpath as rwxpath
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040046from rift.mano.utils.project import ManoProject
47
48
49def prefix_project(xpath):
50 return "/rw-project:project" + xpath
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040051
52class 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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040063 nsd_catalog = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040064
65 # Create an NSD, set its 'id', and add it to the catalog
66 nsd_id = str(uuid.uuid4())
67 nsd_catalog.nsd.append(
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040068 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040069 id=nsd_id,
70 )
71 )
72
73 # Retrieve the NSD using and xpath expression
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040074 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd[project-nsd:id={}]'.
75 format(nsd_id))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040076 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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040081 rwxpath.setxattr(nsd_catalog, xpath + "/project-nsd:name", "test-name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040082
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040083 name = rwxpath.getxattr(nsd_catalog, xpath + "/project-nsd:name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040084 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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040092 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
93
94 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd')
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040095
96 # Check that the unset fields are in fact set to None
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -040097 self.assertEqual(None, rwxpath.getxattr(nsd, xpath + "/project-nsd:name"))
98 self.assertEqual(None, rwxpath.getxattr(nsd, xpath + "/project-nsd:short-name"))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040099
100 # Set the values of the 'name' and 'short-name' fields
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400101 rwxpath.setxattr(nsd, xpath + "/project-nsd:name", "test-name")
102 rwxpath.setxattr(nsd, xpath + "/project-nsd:short-name", "test-short-name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400103
104 # Check that the 'name' and 'short-name' fields are correctly set
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400105 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"))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400107
108
109class TestInputParameterSubstitution(unittest.TestCase):
110 def setUp(self):
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400111 project = ManoProject(logger)
112 self.substitute_input_parameters = rwnsmtasklet.InputParameterSubstitution(logger, project)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400113
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 """
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400120 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
121 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400122
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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400136 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400137 nsd.name = "robert"
138 nsd.short_name = "bob"
139
140 # Define which parameters may be modified
141 nsd.input_parameter_xpath.append(
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400142 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400143 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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400149 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400150 nsr_config.input_parameter.extend([
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400151 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400152 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
153 value="alice",
154 ),
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400155 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400156 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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400174 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
175 # nsd.name = "robert"
176 # nsd.short_name = "bob"
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400177
178 # Define which parameters may be modified
179 nsd.input_parameter_xpath.extend([
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400180 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400181 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
182 label="NSD Name",
183 ),
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400184 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400185 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
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400191 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400192 nsr_config.input_parameter.extend([
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400193 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400194 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
195 value="robert",
196 ),
Jeremy Mordkoff4870d0e2017-09-30 20:28:33 -0400197 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400198 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
211def 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
232if __name__ == '__main__':
233 main()