blob: 74b83c4ab88208c130bd3cda44be4e3368c65001 [file] [log] [blame]
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04001#!/usr/bin/env python3
2
Philip Josephba63fbf2017-04-04 15:46:10 +05303#
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
Philip Josephba63fbf2017-04-04 15:46:10 +053028import gi
29gi.require_version('ProjectNsdYang', '1.0')
30gi.require_version('NsrYang', '1.0')
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040031from gi.repository import (
Philip Josephba63fbf2017-04-04 15:46:10 +053032 ProjectNsdYang,
33 NsrYang,
34)
35
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040036
37logger = logging.getLogger('test-rwnsmtasklet')
38
39import rift.tasklets.rwnsmtasklet.rwnsmtasklet as rwnsmtasklet
40import rift.tasklets.rwnsmtasklet.xpath as rwxpath
Philip Josephba63fbf2017-04-04 15:46:10 +053041from rift.mano.utils.project import ManoProject
42
43
44def prefix_project(xpath):
45 return "/rw-project:project" + xpath
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040046
47class 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
Philip Josephba63fbf2017-04-04 15:46:10 +053058 nsd_catalog = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040059
60 # Create an NSD, set its 'id', and add it to the catalog
61 nsd_id = str(uuid.uuid4())
62 nsd_catalog.nsd.append(
Philip Josephba63fbf2017-04-04 15:46:10 +053063 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040064 id=nsd_id,
65 )
66 )
67
68 # Retrieve the NSD using and xpath expression
Philip Josephba63fbf2017-04-04 15:46:10 +053069 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd[project-nsd:id={}]'.
70 format(nsd_id))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040071 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
Philip Joseph4f810f22017-03-07 23:09:10 +053076 rwxpath.setxattr(nsd_catalog, xpath + "/project-nsd:name", "test-name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040077
Philip Joseph4f810f22017-03-07 23:09:10 +053078 name = rwxpath.getxattr(nsd_catalog, xpath + "/project-nsd:name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040079 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
Philip Josephba63fbf2017-04-04 15:46:10 +053087 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
88
89 xpath = prefix_project('/project-nsd:nsd-catalog/project-nsd:nsd')
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040090
91 # Check that the unset fields are in fact set to None
Philip Josephba63fbf2017-04-04 15:46:10 +053092 self.assertEqual(None, rwxpath.getxattr(nsd, xpath + "/project-nsd:name"))
93 self.assertEqual(None, rwxpath.getxattr(nsd, xpath + "/project-nsd:short-name"))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040094
95 # Set the values of the 'name' and 'short-name' fields
Philip Josephba63fbf2017-04-04 15:46:10 +053096 rwxpath.setxattr(nsd, xpath + "/project-nsd:name", "test-name")
97 rwxpath.setxattr(nsd, xpath + "/project-nsd:short-name", "test-short-name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040098
99 # Check that the 'name' and 'short-name' fields are correctly set
Philip Josephba63fbf2017-04-04 15:46:10 +0530100 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"))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400102
103
104class TestInputParameterSubstitution(unittest.TestCase):
105 def setUp(self):
Philip Josephba63fbf2017-04-04 15:46:10 +0530106 project = ManoProject(logger)
107 self.substitute_input_parameters = rwnsmtasklet.InputParameterSubstitution(logger, project)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400108
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 """
Philip Josephba63fbf2017-04-04 15:46:10 +0530115 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
Philip Josephf4937572017-03-03 01:55:37 +0530116 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400117
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
Philip Josephba63fbf2017-04-04 15:46:10 +0530131 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400132 nsd.name = "robert"
133 nsd.short_name = "bob"
134
135 # Define which parameters may be modified
136 nsd.input_parameter_xpath.append(
Philip Josephba63fbf2017-04-04 15:46:10 +0530137 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
138 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400139 label="NSD Name",
140 )
141 )
142
143 # Define the input parameters that are intended to be modified
Philip Josephf4937572017-03-03 01:55:37 +0530144 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400145 nsr_config.input_parameter.extend([
Philip Josephf4937572017-03-03 01:55:37 +0530146 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Philip Josephba63fbf2017-04-04 15:46:10 +0530147 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400148 value="alice",
149 ),
Philip Josephf4937572017-03-03 01:55:37 +0530150 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Philip Josephba63fbf2017-04-04 15:46:10 +0530151 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400152 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
Philip Josephba63fbf2017-04-04 15:46:10 +0530169 nsd = ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
170 # nsd.name = "robert"
171 # nsd.short_name = "bob"
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400172
173 # Define which parameters may be modified
174 nsd.input_parameter_xpath.extend([
Philip Josephba63fbf2017-04-04 15:46:10 +0530175 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
176 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400177 label="NSD Name",
178 ),
Philip Josephba63fbf2017-04-04 15:46:10 +0530179 ProjectNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
180 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400181 label="NSD Short Name",
182 ),
183 ])
184
185 # Define the input parameters that are intended to be modified
Philip Josephf4937572017-03-03 01:55:37 +0530186 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400187 nsr_config.input_parameter.extend([
Philip Josephf4937572017-03-03 01:55:37 +0530188 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Philip Josephba63fbf2017-04-04 15:46:10 +0530189 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400190 value="robert",
191 ),
Philip Josephf4937572017-03-03 01:55:37 +0530192 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
Philip Josephba63fbf2017-04-04 15:46:10 +0530193 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400194 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
206def 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
227if __name__ == '__main__':
228 main()