blob: aceec43af55460fd56e2b42dd0c979b421f894c3 [file] [log] [blame]
Matteo Scandolodf2600b2017-07-05 17:01:29 -07001import unittest
2from mock import patch, MagicMock
3from tosca.parser import TOSCA_Parser
4from grpc_client.resources import RESOURCES
5
6class FakeObj:
7 new = None
8 filter = None
9
10class FakeModel:
11 save = None
12 id = 1
13
14class FakeGuiExt:
15 objects = FakeObj
16
17class FakeSite:
18 objects = FakeObj
19
20class FakeUser:
21 objects = FakeObj
22
Matteo Scandolo21dde412017-07-11 18:54:12 -070023USERNAME = "username"
24PASSWORD = "pass"
25
26mock_resources = {}
27mock_resources["%s~%s" % (USERNAME, PASSWORD)] = {
Matteo Scandolodf2600b2017-07-05 17:01:29 -070028 'XOSGuiExtension': FakeGuiExt,
29 'Site': FakeSite,
30 'User': FakeUser
31}
32
33class TOSCA_Parser_E2E(unittest.TestCase):
34
35 @patch.dict(RESOURCES, mock_resources, clear=True)
36 @patch.object(FakeGuiExt.objects, 'filter', MagicMock(return_value=[FakeModel]))
37 @patch.object(FakeModel, 'save')
38 def test_basic_creation(self, mock_save):
39 """
40 [TOSCA_Parser] Should save models defined in a TOSCA recipe
41 """
42 recipe = """
43tosca_definitions_version: tosca_simple_yaml_1_0
44
45description: Persist xos-sample-gui-extension
46
47imports:
48 - custom_types/xosguiextension.yaml
49
50topology_template:
51 node_templates:
52
53 # UI Extension
54 test:
55 type: tosca.nodes.XOSGuiExtension
56 properties:
57 name: test
58 files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
59"""
60
Matteo Scandolo21dde412017-07-11 18:54:12 -070061 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
Matteo Scandolodf2600b2017-07-05 17:01:29 -070062
63 parser.execute()
64
65 # checking that the model has been saved
66 mock_save.assert_called()
67
68 self.assertIsNotNone(parser.templates_by_model_name['test'])
69 self.assertEqual(parser.ordered_models_name, ['test'])
70
71 # check that the model was saved with the expected values
72 saved_model = parser.saved_model_by_name['test']
73 self.assertEqual(saved_model.name, 'test')
74 self.assertEqual(saved_model.files, '/spa/extensions/test/vendor.js, /spa/extensions/test/app.js')
75
76 @patch.dict(RESOURCES, mock_resources, clear=True)
77 @patch.object(FakeSite.objects, 'filter', MagicMock(return_value=[FakeModel]))
78 @patch.object(FakeUser.objects, 'filter', MagicMock(return_value=[FakeModel]))
79 @patch.object(FakeModel, 'save')
80 def test_related_models_creation(self, mock_save):
81 """
82 [TOSCA_Parser] Should save related models defined in a TOSCA recipe
83 """
84
85 recipe = """
86tosca_definitions_version: tosca_simple_yaml_1_0
87
88description: Create a new site with one user
89
90imports:
91 - custom_types/user.yaml
92 - custom_types/site.yaml
93
94topology_template:
95 node_templates:
96
97 # Site
98 site_onlab:
99 type: tosca.nodes.Site
100 properties:
101 name: Open Networking Lab
102 site_url: http://onlab.us/
103 hosts_nodes: True
104
105 # User
106 user_test:
107 type: tosca.nodes.User
108 properties:
109 username: test@opencord.org
110 email: test@opencord.org
111 password: mypwd
112 firstname: User
113 lastname: Test
114 is_admin: True
115 requirements:
116 - site:
117 node: site_onlab
118 relationship: tosca.relationships.BelongsToOne
119"""
120
Matteo Scandolo21dde412017-07-11 18:54:12 -0700121 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
Matteo Scandolodf2600b2017-07-05 17:01:29 -0700122
123 parser.execute()
124
125 self.assertEqual(mock_save.call_count, 2)
126
127 self.assertIsNotNone(parser.templates_by_model_name['site_onlab'])
128 self.assertIsNotNone(parser.templates_by_model_name['user_test'])
129 self.assertEqual(parser.ordered_models_name, ['site_onlab', 'user_test'])
130
131 # check that the model was saved with the expected values
132 saved_site = parser.saved_model_by_name['site_onlab']
133 self.assertEqual(saved_site.name, 'Open Networking Lab')
134
135 saved_user = parser.saved_model_by_name['user_test']
136 self.assertEqual(saved_user.firstname, 'User')
Matteo Scandolod12be212017-07-07 10:44:34 -0700137 self.assertEqual(saved_user.site_id, 1)
138
139 @patch.dict(RESOURCES, mock_resources, clear=True)
140 @patch.object(FakeSite.objects, 'filter', MagicMock(return_value=[]))
141 def test_must_exist_fail(self):
142 """
143 [TOSCA_Parser] Should throw an error if an object with 'must_exist' does not exist
144 """
145 recipe = """
146 tosca_definitions_version: tosca_simple_yaml_1_0
147
148 description: Create a new site with one user
149
150 imports:
151 - custom_types/user.yaml
152 - custom_types/site.yaml
153
154 topology_template:
155 node_templates:
156
157 # Site
158 site_onlab:
159 type: tosca.nodes.Site
160 properties:
161 name: Open Networking Lab
162 must-exist: True
163 """
164
Matteo Scandolo21dde412017-07-11 18:54:12 -0700165 parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
Matteo Scandolod12be212017-07-07 10:44:34 -0700166
167 with self.assertRaises(Exception) as e:
168 parser.execute()
169
170 self.assertEqual(e.exception.message, "[XOS-TOSCA] Model Site:Open Networking Lab has property 'must-exist' but cannot be found")