blob: 60e86e04ff97b771d14fab1a751723c6b1e3e4d2 [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 Scandolo485b7132017-06-30 11:46:47 -070017import unittest
18from mock import patch, MagicMock
19from grpc_client.models_accessor import GRPCModelsAccessor
20from grpc_client.resources import RESOURCES
Matteo Scandolo1bd10762017-10-18 09:53:14 +020021from grpc_client.KEYS import TOSCA_KEYS
Matteo Scandolo485b7132017-06-30 11:46:47 -070022
23class FakeObj:
24 new = None
25 filter = None
26
27class FakeResource:
28 objects = FakeObj
29
30class FakeModel:
31 pass
32
33class FakeExistingModel:
34 pass
35
36mock_resources = {
Matteo Scandolo21dde412017-07-11 18:54:12 -070037 'username~pass': {
Matteo Scandolo1bd10762017-10-18 09:53:14 +020038 'test-model': FakeResource,
39 'single-key': FakeResource,
Matteo Scandolo24197a12017-12-13 12:21:59 -080040 'double-key': FakeResource,
41 'one-of-key': FakeResource
Matteo Scandolo21dde412017-07-11 18:54:12 -070042 }
Matteo Scandolo485b7132017-06-30 11:46:47 -070043}
44
Matteo Scandolo1bd10762017-10-18 09:53:14 +020045mock_keys = {
46 'i-do-not-exists': ['name'],
47 'test-model': ['name'],
48 'empty-key': [],
49 'single-key': ['fake_key'],
50 'double-key': ['key_1', 'key_2'],
Matteo Scandolo24197a12017-12-13 12:21:59 -080051 'one-of-key': ['key_1', ['key_2', 'key_3']],
Matteo Scandolo1bd10762017-10-18 09:53:14 +020052}
53
Matteo Scandolo21dde412017-07-11 18:54:12 -070054USERNAME = 'username'
55PASSWORD = 'pass'
56
Matteo Scandolo485b7132017-06-30 11:46:47 -070057class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase):
58
Matteo Scandolo1bd10762017-10-18 09:53:14 +020059 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
Matteo Scandolo21dde412017-07-11 18:54:12 -070060 def test_unkown_user(self):
61 """
62 [GRPCModelsAccessor] get_model_from_classname: If a user does not have orm classes, raise
63 """
64 data = {
65 "name": "test"
66 }
67 with self.assertRaises(Exception) as e:
68 GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD)
69 self.assertEqual(e.exception.message, "[XOS-TOSCA] User 'username' does not have ready resources")
70
71 @patch.dict(RESOURCES, mock_resources, clear=True)
Matteo Scandolo1bd10762017-10-18 09:53:14 +020072 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
Matteo Scandolo485b7132017-06-30 11:46:47 -070073 def test_unkown_module(self):
74 """
75 [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise
76 """
77 data = {
78 "name": "test"
79 }
80 with self.assertRaises(Exception) as e:
Matteo Scandolo21dde412017-07-11 18:54:12 -070081 GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD)
Matteo Scandolo1bd10762017-10-18 09:53:14 +020082 self.assertEqual(e.exception.message, "[XOS-TOSCA] The model you are trying to create (class: i-do-not-exists, properties, {'name': 'test'}) is not know by xos-core")
83
84 def test_unkown_tosca_key(self):
85 """
86 [GRPCModelsAccessor] get_model_from_classname: If a model does not have a tosca_key, raise
87 """
88 data = {
89 "name": "test"
90 }
91 with self.assertRaises(Exception) as e:
92 GRPCModelsAccessor.get_model_from_classname('no-key', data, USERNAME, PASSWORD)
93 self.assertEqual(e.exception.message, "[XOS-TOSCA] Model no-key doesn't have a tosca_key specified")
94
95 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
96 def test_empty_tosca_key(self):
97 """
98 [GRPCModelsAccessor] get_model_from_classname: If a model does not have a tosca_key, raise
99 """
100 data = {
101 "name": "test"
102 }
103 with self.assertRaises(Exception) as e:
104 GRPCModelsAccessor.get_model_from_classname('empty-key', data, USERNAME, PASSWORD)
105 self.assertEqual(e.exception.message, "[XOS-TOSCA] Model empty-key doesn't have a tosca_key specified")
106
107 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
108 def test_tosca_key_are_defined(self):
109 """
110 [GRPCModelsAccessor] get_model_from_classname: a model should have a property for it's tosca_key
111 """
112 data = {
113 "name": "test",
114 }
115 with self.assertRaises(Exception) as e:
116 GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD)
117 self.assertEqual(e.exception.message, "[XOS-TOSCA] Model single-key doesn't have a property for the specified tosca_key ('fake_key')")
Matteo Scandolo485b7132017-06-30 11:46:47 -0700118
119 @patch.object(FakeResource.objects, "filter")
120 @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200121 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
122 def test_composite_key(self, mock_filter):
123 """
124 [GRPCModelsAccessor] get_model_from_classname: should use a composite key to lookup a model
125 """
126 data = {
127 "name": "test",
128 "key_1": "key1",
129 "key_2": "key2"
130 }
131 with patch.dict(RESOURCES, mock_resources, clear=True):
132 model = GRPCModelsAccessor.get_model_from_classname('double-key', data, USERNAME, PASSWORD)
133 mock_filter.assert_called_with(key_1="key1", key_2="key2")
134 self.assertEqual(model, FakeModel)
135
136 @patch.object(FakeResource.objects, "filter")
137 @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
138 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
Matteo Scandolo24197a12017-12-13 12:21:59 -0800139 def test_one_of_key(self, mock_filter):
140 """
141 [GRPCModelsAccessor] get_model_from_classname: should use a composite with one_of key to lookup a model
142 """
143 # NOTE it should be valid for items with either one of the keys
144 data2 = {
145 "name": "test",
146 "key_1": "key1",
147 "key_2": "key2"
148 }
149 with patch.dict(RESOURCES, mock_resources, clear=True):
150 model = GRPCModelsAccessor.get_model_from_classname('one-of-key', data2, USERNAME, PASSWORD)
151 mock_filter.assert_called_with(key_1="key1", key_2="key2")
152 self.assertEqual(model, FakeModel)
153
154 data3 = {
155 "name": "test",
156 "key_1": "key1",
157 "key_3": "key3"
158 }
159 with patch.dict(RESOURCES, mock_resources, clear=True):
160 model = GRPCModelsAccessor.get_model_from_classname('one-of-key', data3, USERNAME, PASSWORD)
161 mock_filter.assert_called_with(key_1="key1", key_3="key3")
162 self.assertEqual(model, FakeModel)
163
164 @patch.object(FakeResource.objects, "filter")
165 @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
166 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
167 def test_one_of_key_error(self, mock_filter):
168 data = {
169 "name": "test",
170 "key_1": "key1"
171 }
172 with self.assertRaises(Exception) as e:
173 GRPCModelsAccessor.get_model_from_classname('one-of-key', data, USERNAME, PASSWORD)
174 self.assertEqual(e.exception.message, "[XOS-TOSCA] Model one-of-key doesn't have a property for the specified tosca_key_one_of (['key_2', 'key_3'])")
175
176 @patch.object(FakeResource.objects, "filter")
177 @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
178 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700179 def test_new_model(self, mock_filter):
180 """
181 [GRPCModelsAccessor] get_model_from_classname: should create a new model
182 """
183 data = {
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200184 "name": "test",
185 "fake_key": "key"
Matteo Scandolo485b7132017-06-30 11:46:47 -0700186 }
187 with patch.dict(RESOURCES, mock_resources, clear=True):
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200188 model = GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD)
189 mock_filter.assert_called_with(fake_key="key")
Matteo Scandolo485b7132017-06-30 11:46:47 -0700190 self.assertEqual(model, FakeModel)
191
192 @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel]))
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200193 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700194 def test_existing_model(self):
195 """
196 [GRPCModelsAccessor] get_model_from_classname: should update an existing model
197 """
198 data = {
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200199 "name": "test",
200 "fake_key": "key"
Matteo Scandolo485b7132017-06-30 11:46:47 -0700201 }
202 with patch.dict(RESOURCES, mock_resources, clear=True):
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200203 model = GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700204 self.assertEqual(model, FakeExistingModel)
205
206 @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b']))
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200207 @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
Matteo Scandolo485b7132017-06-30 11:46:47 -0700208 def test_multiple_models(self):
209 """
210 [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found
211 """
212 data = {
213 "name": "test"
214 }
215 with patch.dict(RESOURCES, mock_resources, clear=True):
216 with self.assertRaises(Exception) as e:
Matteo Scandolo21dde412017-07-11 18:54:12 -0700217 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
Matteo Scandolo1bd10762017-10-18 09:53:14 +0200218 self.assertEqual(e.exception.message, "[XOS-Tosca] Model of class test-model and properties {'name': 'test'} has multiple instances, I can't handle it")
Matteo Scandolo1fedfae2017-10-09 13:57:00 -0700219
Matteo Scandolo485b7132017-06-30 11:46:47 -0700220if __name__ == '__main__':
221 unittest.main()