blob: fa9975187bc2ce2753198e336b1c0f72fdcacc22 [file] [log] [blame]
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04001#!/usr/bin/env python3
2
3#
4# Copyright 2016 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
20import argparse
21import logging
22import os
23import sys
24import unittest
25import uuid
26import xmlrunner
27
28from gi.repository import (
29 NsdYang,
30 NsrYang,
31 )
32
33logger = logging.getLogger('test-rwnsmtasklet')
34
35import rift.tasklets.rwnsmtasklet.rwnsmtasklet as rwnsmtasklet
36import rift.tasklets.rwnsmtasklet.xpath as rwxpath
37
38class TestGiXpath(unittest.TestCase):
39 def setUp(self):
40 rwxpath.reset_cache()
41
42 def test_nsd_elements(self):
43 """
44 Test that a particular element in a list is corerctly retrieved. In
45 this case, we are trying to retrieve an NSD from the NSD catalog.
46
47 """
48 # Create the initial NSD catalog
Philip Josephf4937572017-03-03 01:55:37 +053049 nsd_catalog = NsdYang.YangData_RwProject_Project_NsdCatalog()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040050
51 # Create an NSD, set its 'id', and add it to the catalog
52 nsd_id = str(uuid.uuid4())
53 nsd_catalog.nsd.append(
Philip Josephf4937572017-03-03 01:55:37 +053054 NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd(
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040055 id=nsd_id,
56 )
57 )
58
59 # Retrieve the NSD using and xpath expression
Philip Josephf4937572017-03-03 01:55:37 +053060 xpath = '/rw-project:project/nsd:nsd-catalog/nsd:nsd[nsd:id={}]'.format(nsd_id)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040061 nsd = rwxpath.getxattr(nsd_catalog, xpath)
62
63 self.assertEqual(nsd_id, nsd.id)
64
65 # Modified the name of the NSD using an xpath expression
66 rwxpath.setxattr(nsd_catalog, xpath + "/nsd:name", "test-name")
67
68 name = rwxpath.getxattr(nsd_catalog, xpath + "/nsd:name")
69 self.assertEqual("test-name", name)
70
71 def test_nsd_scalar_fields(self):
72 """
73 Test that setxattr correctly sets the value specified by an xpath.
74
75 """
76 # Define a simple NSD
Philip Josephf4937572017-03-03 01:55:37 +053077 nsd = NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040078
79 # Check that the unset fields are in fact set to None
Philip Josephf4937572017-03-03 01:55:37 +053080 self.assertEqual(None, rwxpath.getxattr(nsd, "/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name"))
81 self.assertEqual(None, rwxpath.getxattr(nsd, "/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:short-name"))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040082
83 # Set the values of the 'name' and 'short-name' fields
Philip Josephf4937572017-03-03 01:55:37 +053084 rwxpath.setxattr(nsd, "/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name", "test-name")
85 rwxpath.setxattr(nsd, "/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:short-name", "test-short-name")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040086
87 # Check that the 'name' and 'short-name' fields are correctly set
Philip Josephf4937572017-03-03 01:55:37 +053088 self.assertEqual(nsd.name, rwxpath.getxattr(nsd, "/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name"))
89 self.assertEqual(nsd.short_name, rwxpath.getxattr(nsd, "/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:short-name"))
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040090
91
92class TestInputParameterSubstitution(unittest.TestCase):
93 def setUp(self):
94 self.substitute_input_parameters = rwnsmtasklet.InputParameterSubstitution(logger)
95
96 def test_null_arguments(self):
97 """
98 If None is passed to the substitutor for either the NSD or the NSR
99 config, no exception should be raised.
100
101 """
Philip Josephf4937572017-03-03 01:55:37 +0530102 nsd = NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
103 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400104
105 self.substitute_input_parameters(None, None)
106 self.substitute_input_parameters(nsd, None)
107 self.substitute_input_parameters(None, nsr_config)
108
109 def test_illegal_input_parameter(self):
110 """
111 In the NSD there is a list of the parameters that are allowed to be
112 sbustituted by input parameters. This test checks that when an input
113 parameter is provided in the NSR config that is not in the NSD, it is
114 not applied.
115
116 """
117 # Define the original NSD
Philip Josephf4937572017-03-03 01:55:37 +0530118 nsd = NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400119 nsd.name = "robert"
120 nsd.short_name = "bob"
121
122 # Define which parameters may be modified
123 nsd.input_parameter_xpath.append(
Philip Josephf4937572017-03-03 01:55:37 +0530124 NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
125 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400126 label="NSD Name",
127 )
128 )
129
130 # Define the input parameters that are intended to be modified
Philip Josephf4937572017-03-03 01:55:37 +0530131 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400132 nsr_config.input_parameter.extend([
Philip Josephf4937572017-03-03 01:55:37 +0530133 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
134 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400135 value="alice",
136 ),
Philip Josephf4937572017-03-03 01:55:37 +0530137 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
138 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400139 value="alice",
140 ),
141 ])
142
143 self.substitute_input_parameters(nsd, nsr_config)
144
145 # Verify that only the parameter in the input_parameter_xpath list is
146 # modified after the input parameters have been applied.
147 self.assertEqual("alice", nsd.name)
148 self.assertEqual("bob", nsd.short_name)
149
150 def test_substitution(self):
151 """
152 Test that substitution of input parameters occurs as expected.
153
154 """
155 # Define the original NSD
Philip Josephf4937572017-03-03 01:55:37 +0530156 nsd = NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400157 nsd.name = "robert"
158 nsd.short_name = "bob"
159
160 # Define which parameters may be modified
161 nsd.input_parameter_xpath.extend([
Philip Josephf4937572017-03-03 01:55:37 +0530162 NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
163 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400164 label="NSD Name",
165 ),
Philip Josephf4937572017-03-03 01:55:37 +0530166 NsdYang.YangData_RwProject_Project_NsdCatalog_Nsd_InputParameterXpath(
167 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400168 label="NSD Short Name",
169 ),
170 ])
171
172 # Define the input parameters that are intended to be modified
Philip Josephf4937572017-03-03 01:55:37 +0530173 nsr_config = NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr()
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400174 nsr_config.input_parameter.extend([
Philip Josephf4937572017-03-03 01:55:37 +0530175 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
176 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400177 value="robert",
178 ),
Philip Josephf4937572017-03-03 01:55:37 +0530179 NsrYang.YangData_RwProject_Project_NsInstanceConfig_Nsr_InputParameter(
180 xpath="/rw-project:project/nsd:nsd-catalog/nsd:nsd/nsd:short-name",
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400181 value="bob",
182 ),
183 ])
184
185 self.substitute_input_parameters(nsd, nsr_config)
186
187 # Verify that both the 'name' and 'short-name' fields are correctly
188 # replaced.
189 self.assertEqual("robert", nsd.name)
190 self.assertEqual("bob", nsd.short_name)
191
192
193def main(argv=sys.argv[1:]):
194 logging.basicConfig(format='TEST %(message)s')
195
196 parser = argparse.ArgumentParser()
197 parser.add_argument('-v', '--verbose', action='store_true')
198
199 args = parser.parse_args(argv)
200
201 # Set the global logging level
202 logging.getLogger().setLevel(logging.DEBUG if args.verbose else logging.FATAL)
203
204 # Make the test logger very quiet
205 logger.addHandler(logging.NullHandler())
206
207 # The unittest framework requires a program name, so use the name of this
208 # file instead (we do not want to have to pass a fake program name to main
209 # when this is called from the interpreter).
210 unittest.main(argv=[__file__] + argv,
211 testRunner=xmlrunner.XMLTestRunner(
212 output=os.environ["RIFT_MODULE_TEST"]))
213
214if __name__ == '__main__':
215 main()