Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | from mock import patch, MagicMock |
| 19 | from grpc_client.models_accessor import GRPCModelsAccessor |
| 20 | from grpc_client.resources import RESOURCES |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 21 | from grpc_client.KEYS import TOSCA_KEYS |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 22 | |
| 23 | class FakeObj: |
| 24 | new = None |
| 25 | filter = None |
| 26 | |
| 27 | class FakeResource: |
| 28 | objects = FakeObj |
| 29 | |
| 30 | class FakeModel: |
| 31 | pass |
| 32 | |
| 33 | class FakeExistingModel: |
| 34 | pass |
| 35 | |
| 36 | mock_resources = { |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 37 | 'username~pass': { |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 38 | 'test-model': FakeResource, |
| 39 | 'single-key': FakeResource, |
| 40 | 'double-key': FakeResource |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 41 | } |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 44 | mock_keys = { |
| 45 | 'i-do-not-exists': ['name'], |
| 46 | 'test-model': ['name'], |
| 47 | 'empty-key': [], |
| 48 | 'single-key': ['fake_key'], |
| 49 | 'double-key': ['key_1', 'key_2'], |
| 50 | } |
| 51 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 52 | USERNAME = 'username' |
| 53 | PASSWORD = 'pass' |
| 54 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 55 | class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase): |
| 56 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 57 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 58 | def test_unkown_user(self): |
| 59 | """ |
| 60 | [GRPCModelsAccessor] get_model_from_classname: If a user does not have orm classes, raise |
| 61 | """ |
| 62 | data = { |
| 63 | "name": "test" |
| 64 | } |
| 65 | with self.assertRaises(Exception) as e: |
| 66 | GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD) |
| 67 | self.assertEqual(e.exception.message, "[XOS-TOSCA] User 'username' does not have ready resources") |
| 68 | |
| 69 | @patch.dict(RESOURCES, mock_resources, clear=True) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 70 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 71 | def test_unkown_module(self): |
| 72 | """ |
| 73 | [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise |
| 74 | """ |
| 75 | data = { |
| 76 | "name": "test" |
| 77 | } |
| 78 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 79 | GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 80 | 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") |
| 81 | |
| 82 | def test_unkown_tosca_key(self): |
| 83 | """ |
| 84 | [GRPCModelsAccessor] get_model_from_classname: If a model does not have a tosca_key, raise |
| 85 | """ |
| 86 | data = { |
| 87 | "name": "test" |
| 88 | } |
| 89 | with self.assertRaises(Exception) as e: |
| 90 | GRPCModelsAccessor.get_model_from_classname('no-key', data, USERNAME, PASSWORD) |
| 91 | self.assertEqual(e.exception.message, "[XOS-TOSCA] Model no-key doesn't have a tosca_key specified") |
| 92 | |
| 93 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
| 94 | def test_empty_tosca_key(self): |
| 95 | """ |
| 96 | [GRPCModelsAccessor] get_model_from_classname: If a model does not have a tosca_key, raise |
| 97 | """ |
| 98 | data = { |
| 99 | "name": "test" |
| 100 | } |
| 101 | with self.assertRaises(Exception) as e: |
| 102 | GRPCModelsAccessor.get_model_from_classname('empty-key', data, USERNAME, PASSWORD) |
| 103 | self.assertEqual(e.exception.message, "[XOS-TOSCA] Model empty-key doesn't have a tosca_key specified") |
| 104 | |
| 105 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
| 106 | def test_tosca_key_are_defined(self): |
| 107 | """ |
| 108 | [GRPCModelsAccessor] get_model_from_classname: a model should have a property for it's tosca_key |
| 109 | """ |
| 110 | data = { |
| 111 | "name": "test", |
| 112 | } |
| 113 | with self.assertRaises(Exception) as e: |
| 114 | GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD) |
| 115 | self.assertEqual(e.exception.message, "[XOS-TOSCA] Model single-key doesn't have a property for the specified tosca_key ('fake_key')") |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 116 | |
| 117 | @patch.object(FakeResource.objects, "filter") |
| 118 | @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel)) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 119 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
| 120 | def test_composite_key(self, mock_filter): |
| 121 | """ |
| 122 | [GRPCModelsAccessor] get_model_from_classname: should use a composite key to lookup a model |
| 123 | """ |
| 124 | data = { |
| 125 | "name": "test", |
| 126 | "key_1": "key1", |
| 127 | "key_2": "key2" |
| 128 | } |
| 129 | with patch.dict(RESOURCES, mock_resources, clear=True): |
| 130 | model = GRPCModelsAccessor.get_model_from_classname('double-key', data, USERNAME, PASSWORD) |
| 131 | mock_filter.assert_called_with(key_1="key1", key_2="key2") |
| 132 | self.assertEqual(model, FakeModel) |
| 133 | |
| 134 | @patch.object(FakeResource.objects, "filter") |
| 135 | @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel)) |
| 136 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 137 | def test_new_model(self, mock_filter): |
| 138 | """ |
| 139 | [GRPCModelsAccessor] get_model_from_classname: should create a new model |
| 140 | """ |
| 141 | data = { |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 142 | "name": "test", |
| 143 | "fake_key": "key" |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 144 | } |
| 145 | with patch.dict(RESOURCES, mock_resources, clear=True): |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 146 | model = GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD) |
| 147 | mock_filter.assert_called_with(fake_key="key") |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 148 | self.assertEqual(model, FakeModel) |
| 149 | |
| 150 | @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel])) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 151 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 152 | def test_existing_model(self): |
| 153 | """ |
| 154 | [GRPCModelsAccessor] get_model_from_classname: should update an existing model |
| 155 | """ |
| 156 | data = { |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 157 | "name": "test", |
| 158 | "fake_key": "key" |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 159 | } |
| 160 | with patch.dict(RESOURCES, mock_resources, clear=True): |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 161 | model = GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 162 | self.assertEqual(model, FakeExistingModel) |
| 163 | |
| 164 | @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b'])) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 165 | @patch.dict(TOSCA_KEYS, mock_keys, clear=True) |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 166 | def test_multiple_models(self): |
| 167 | """ |
| 168 | [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found |
| 169 | """ |
| 170 | data = { |
| 171 | "name": "test" |
| 172 | } |
| 173 | with patch.dict(RESOURCES, mock_resources, clear=True): |
| 174 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 175 | GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD) |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 176 | 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 Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 177 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 178 | if __name__ == '__main__': |
| 179 | unittest.main() |