blob: 4b6d05d3a05f6ce0ad10d303d7bcc7ff9aebee89 [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolodf2600b2017-07-05 17:01:29 -070017import unittest
18from mock import patch, MagicMock
19from tosca.parser import TOSCA_Parser
20from grpc_client.resources import RESOURCES
21
22class FakeObj:
23 new = None
24 filter = None
25
26class FakeModel:
27 save = None
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070028 delete = None
29 is_new = False
Matteo Scandolodf2600b2017-07-05 17:01:29 -070030 id = 1
31
32class FakeGuiExt:
33 objects = FakeObj
34
35class FakeSite:
36 objects = FakeObj
37
38class FakeUser:
39 objects = FakeObj
40
Matteo Scandolo21dde412017-07-11 18:54:12 -070041USERNAME = "username"
42PASSWORD = "pass"
43
44mock_resources = {}
45mock_resources["%s~%s" % (USERNAME, PASSWORD)] = {
Matteo Scandolodf2600b2017-07-05 17:01:29 -070046 'XOSGuiExtension': FakeGuiExt,
47 'Site': FakeSite,
48 'User': FakeUser
49}
50
51class TOSCA_Parser_E2E(unittest.TestCase):
52
53 @patch.dict(RESOURCES, mock_resources, clear=True)
54 @patch.object(FakeGuiExt.objects, 'filter', MagicMock(return_value=[FakeModel]))
55 @patch.object(FakeModel, 'save')
56 def test_basic_creation(self, mock_save):
57 """
58 [TOSCA_Parser] Should save models defined in a TOSCA recipe
59 """
60 recipe = """
61tosca_definitions_version: tosca_simple_yaml_1_0
62
63description: Persist xos-sample-gui-extension
64
65imports:
66 - custom_types/xosguiextension.yaml
67
68topology_template:
69 node_templates:
70
71 # UI Extension
72 test:
73 type: tosca.nodes.XOSGuiExtension
74 properties:
75 name: test
76 files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
77"""
78
Matteo Scandolo21dde412017-07-11 18:54:12 -070079 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
Matteo Scandolodf2600b2017-07-05 17:01:29 -070080
81 parser.execute()
82
83 # checking that the model has been saved
84 mock_save.assert_called()
85
86 self.assertIsNotNone(parser.templates_by_model_name['test'])
87 self.assertEqual(parser.ordered_models_name, ['test'])
88
89 # check that the model was saved with the expected values
90 saved_model = parser.saved_model_by_name['test']
91 self.assertEqual(saved_model.name, 'test')
92 self.assertEqual(saved_model.files, '/spa/extensions/test/vendor.js, /spa/extensions/test/app.js')
93
94 @patch.dict(RESOURCES, mock_resources, clear=True)
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070095 @patch.object(FakeGuiExt.objects, 'filter', MagicMock(return_value=[FakeModel]))
96 @patch.object(FakeModel, 'delete')
97 def test_basic_deletion(self, mock_delete):
98 """
99 [TOSCA_Parser] Should delete models defined in a TOSCA recipe
100 """
101 recipe = """
102 tosca_definitions_version: tosca_simple_yaml_1_0
103
104 description: Persist xos-sample-gui-extension
105
106 imports:
107 - custom_types/xosguiextension.yaml
108
109 topology_template:
110 node_templates:
111
112 # UI Extension
113 test:
114 type: tosca.nodes.XOSGuiExtension
115 properties:
116 name: test
117 files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
118 """
119
120 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD, delete=True)
121
122 parser.execute()
123
124 # checking that the model has been saved
125 mock_delete.assert_called()
126
127 self.assertIsNotNone(parser.templates_by_model_name['test'])
128 self.assertEqual(parser.ordered_models_name, ['test'])
129
130 @patch.dict(RESOURCES, mock_resources, clear=True)
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700131 @patch.object(FakeSite.objects, 'filter', MagicMock(return_value=[FakeModel]))
132 @patch.object(FakeUser.objects, 'filter', MagicMock(return_value=[FakeModel]))
133 @patch.object(FakeModel, 'save')
134 def test_related_models_creation(self, mock_save):
135 """
136 [TOSCA_Parser] Should save related models defined in a TOSCA recipe
137 """
138
139 recipe = """
140tosca_definitions_version: tosca_simple_yaml_1_0
141
142description: Create a new site with one user
143
144imports:
145 - custom_types/user.yaml
146 - custom_types/site.yaml
147
148topology_template:
149 node_templates:
150
151 # Site
152 site_onlab:
153 type: tosca.nodes.Site
154 properties:
155 name: Open Networking Lab
156 site_url: http://onlab.us/
157 hosts_nodes: True
158
159 # User
160 user_test:
161 type: tosca.nodes.User
162 properties:
163 username: test@opencord.org
164 email: test@opencord.org
165 password: mypwd
166 firstname: User
167 lastname: Test
168 is_admin: True
169 requirements:
170 - site:
171 node: site_onlab
172 relationship: tosca.relationships.BelongsToOne
173"""
174
Matteo Scandolo21dde412017-07-11 18:54:12 -0700175 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700176
177 parser.execute()
178
179 self.assertEqual(mock_save.call_count, 2)
180
181 self.assertIsNotNone(parser.templates_by_model_name['site_onlab'])
182 self.assertIsNotNone(parser.templates_by_model_name['user_test'])
183 self.assertEqual(parser.ordered_models_name, ['site_onlab', 'user_test'])
184
185 # check that the model was saved with the expected values
186 saved_site = parser.saved_model_by_name['site_onlab']
187 self.assertEqual(saved_site.name, 'Open Networking Lab')
188
189 saved_user = parser.saved_model_by_name['user_test']
190 self.assertEqual(saved_user.firstname, 'User')
Matteo Scandolod12be212017-07-07 10:44:34 -0700191 self.assertEqual(saved_user.site_id, 1)
192
193 @patch.dict(RESOURCES, mock_resources, clear=True)
194 @patch.object(FakeSite.objects, 'filter', MagicMock(return_value=[]))
195 def test_must_exist_fail(self):
196 """
197 [TOSCA_Parser] Should throw an error if an object with 'must_exist' does not exist
198 """
199 recipe = """
200 tosca_definitions_version: tosca_simple_yaml_1_0
201
202 description: Create a new site with one user
203
204 imports:
205 - custom_types/user.yaml
206 - custom_types/site.yaml
207
208 topology_template:
209 node_templates:
210
211 # Site
212 site_onlab:
213 type: tosca.nodes.Site
214 properties:
215 name: Open Networking Lab
216 must-exist: True
217 """
218
Matteo Scandolo21dde412017-07-11 18:54:12 -0700219 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
Matteo Scandolod12be212017-07-07 10:44:34 -0700220
221 with self.assertRaises(Exception) as e:
222 parser.execute()
223
224 self.assertEqual(e.exception.message, "[XOS-TOSCA] Model Site:Open Networking Lab has property 'must-exist' but cannot be found")