vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / storage / test_collection_instrumentation.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  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 import pytest
17
18 from aria.modeling import models
19 from aria.storage import collection_instrumentation
20
21
22 class MockActor(object):
23     def __init__(self):
24         self.dict_ = {}
25         self.list_ = []
26
27
28 class MockMAPI(object):
29
30     def __init__(self):
31         pass
32
33     def put(self, *args, **kwargs):
34         pass
35
36     def update(self, *args, **kwargs):
37         pass
38
39
40 class CollectionInstrumentation(object):
41
42     @pytest.fixture
43     def actor(self):
44         return MockActor()
45
46     @pytest.fixture
47     def model(self):
48         return MockMAPI()
49
50     @pytest.fixture
51     def dict_(self, actor, model):
52         return collection_instrumentation._InstrumentedDict(model, actor, 'dict_', models.Attribute)
53
54     @pytest.fixture
55     def list_(self, actor, model):
56         return collection_instrumentation._InstrumentedList(model, actor, 'list_', models.Attribute)
57
58
59 class TestDict(CollectionInstrumentation):
60
61     def test_keys(self, actor, dict_):
62         dict_.update(
63             {
64                 'key1': models.Attribute.wrap('key1', 'value1'),
65                 'key2': models.Attribute.wrap('key2', 'value2')
66             }
67         )
68         assert sorted(dict_.keys()) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys())
69
70     def test_values(self, actor, dict_):
71         dict_.update({
72             'key1': models.Attribute.wrap('key1', 'value1'),
73             'key2': models.Attribute.wrap('key1', 'value2')
74         })
75         assert (sorted(dict_.values()) ==
76                 sorted(['value1', 'value2']) ==
77                 sorted(v.value for v in actor.dict_.values()))
78
79     def test_items(self, dict_):
80         dict_.update({
81             'key1': models.Attribute.wrap('key1', 'value1'),
82             'key2': models.Attribute.wrap('key1', 'value2')
83         })
84         assert sorted(dict_.items()) == sorted([('key1', 'value1'), ('key2', 'value2')])
85
86     def test_iter(self, actor, dict_):
87         dict_.update({
88             'key1': models.Attribute.wrap('key1', 'value1'),
89             'key2': models.Attribute.wrap('key1', 'value2')
90         })
91         assert sorted(list(dict_)) == sorted(['key1', 'key2']) == sorted(actor.dict_.keys())
92
93     def test_bool(self, dict_):
94         assert not dict_
95         dict_.update({
96             'key1': models.Attribute.wrap('key1', 'value1'),
97             'key2': models.Attribute.wrap('key1', 'value2')
98         })
99         assert dict_
100
101     def test_set_item(self, actor, dict_):
102         dict_['key1'] = models.Attribute.wrap('key1', 'value1')
103         assert dict_['key1'] == 'value1' == actor.dict_['key1'].value
104         assert isinstance(actor.dict_['key1'], models.Attribute)
105
106     def test_nested(self, actor, dict_):
107         dict_['key'] = {}
108         assert isinstance(actor.dict_['key'], models.Attribute)
109         assert dict_['key'] == actor.dict_['key'].value == {}
110
111         dict_['key']['inner_key'] = 'value'
112
113         assert len(dict_) == 1
114         assert 'inner_key' in dict_['key']
115         assert dict_['key']['inner_key'] == 'value'
116         assert dict_['key'].keys() == ['inner_key']
117         assert dict_['key'].values() == ['value']
118         assert dict_['key'].items() == [('inner_key', 'value')]
119         assert isinstance(actor.dict_['key'], models.Attribute)
120         assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict)
121
122         dict_['key'].update({'updated_key': 'updated_value'})
123         assert len(dict_) == 1
124         assert 'updated_key' in dict_['key']
125         assert dict_['key']['updated_key'] == 'updated_value'
126         assert sorted(dict_['key'].keys()) == sorted(['inner_key', 'updated_key'])
127         assert sorted(dict_['key'].values()) == sorted(['value', 'updated_value'])
128         assert sorted(dict_['key'].items()) == sorted([('inner_key', 'value'),
129                                                        ('updated_key', 'updated_value')])
130         assert isinstance(actor.dict_['key'], models.Attribute)
131         assert isinstance(dict_['key'], collection_instrumentation._InstrumentedDict)
132
133         dict_.update({'key': 'override_value'})
134         assert len(dict_) == 1
135         assert 'key' in dict_
136         assert dict_['key'] == 'override_value'
137         assert len(actor.dict_) == 1
138         assert isinstance(actor.dict_['key'], models.Attribute)
139         assert actor.dict_['key'].value == 'override_value'
140
141     def test_get_item(self, actor, dict_):
142         dict_['key1'] = models.Attribute.wrap('key1', 'value1')
143         assert isinstance(actor.dict_['key1'], models.Attribute)
144
145     def test_update(self, actor, dict_):
146         dict_['key1'] = 'value1'
147
148         new_dict = {'key2': 'value2'}
149         dict_.update(new_dict)
150         assert len(dict_) == 2
151         assert dict_['key2'] == 'value2'
152         assert isinstance(actor.dict_['key2'], models.Attribute)
153
154         new_dict = {}
155         new_dict.update(dict_)
156         assert new_dict['key1'] == dict_['key1']
157
158     def test_copy(self, dict_):
159         dict_['key1'] = 'value1'
160
161         new_dict = dict_.copy()
162         assert new_dict is not dict_
163         assert new_dict == dict_
164
165         dict_['key1'] = 'value2'
166         assert new_dict['key1'] == 'value1'
167         assert dict_['key1'] == 'value2'
168
169     def test_clear(self, dict_):
170         dict_['key1'] = 'value1'
171         dict_.clear()
172
173         assert len(dict_) == 0
174
175
176 class TestList(CollectionInstrumentation):
177
178     def test_append(self, actor, list_):
179         list_.append(models.Attribute.wrap('name', 'value1'))
180         list_.append('value2')
181         assert len(actor.list_) == 2
182         assert len(list_) == 2
183         assert isinstance(actor.list_[0], models.Attribute)
184         assert list_[0] == 'value1'
185
186         assert isinstance(actor.list_[1], models.Attribute)
187         assert list_[1] == 'value2'
188
189         list_[0] = 'new_value1'
190         list_[1] = 'new_value2'
191         assert isinstance(actor.list_[1], models.Attribute)
192         assert isinstance(actor.list_[1], models.Attribute)
193         assert list_[0] == 'new_value1'
194         assert list_[1] == 'new_value2'
195
196     def test_iter(self, list_):
197         list_.append('value1')
198         list_.append('value2')
199         assert sorted(list_) == sorted(['value1', 'value2'])
200
201     def test_insert(self, actor, list_):
202         list_.append('value1')
203         list_.insert(0, 'value2')
204         list_.insert(2, 'value3')
205         list_.insert(10, 'value4')
206         assert sorted(list_) == sorted(['value1', 'value2', 'value3', 'value4'])
207         assert len(actor.list_) == 4
208
209     def test_set(self, list_):
210         list_.append('value1')
211         list_.append('value2')
212
213         list_[1] = 'value3'
214         assert len(list_) == 2
215         assert sorted(list_) == sorted(['value1', 'value3'])
216
217     def test_insert_into_nested(self, actor, list_):
218         list_.append([])
219
220         list_[0].append('inner_item')
221         assert isinstance(actor.list_[0], models.Attribute)
222         assert len(list_) == 1
223         assert list_[0][0] == 'inner_item'
224
225         list_[0].append('new_item')
226         assert isinstance(actor.list_[0], models.Attribute)
227         assert len(list_) == 1
228         assert list_[0][1] == 'new_item'
229
230         assert list_[0] == ['inner_item', 'new_item']
231         assert ['inner_item', 'new_item'] == list_[0]
232
233
234 class TestDictList(CollectionInstrumentation):
235     def test_dict_in_list(self, actor, list_):
236         list_.append({})
237         assert len(list_) == 1
238         assert isinstance(actor.list_[0], models.Attribute)
239         assert actor.list_[0].value == {}
240
241         list_[0]['key'] = 'value'
242         assert list_[0]['key'] == 'value'
243         assert len(actor.list_) == 1
244         assert isinstance(actor.list_[0], models.Attribute)
245         assert actor.list_[0].value['key'] == 'value'
246
247     def test_list_in_dict(self, actor, dict_):
248         dict_['key'] = []
249         assert len(dict_) == 1
250         assert isinstance(actor.dict_['key'], models.Attribute)
251         assert actor.dict_['key'].value == []
252
253         dict_['key'].append('value')
254         assert dict_['key'][0] == 'value'
255         assert len(actor.dict_) == 1
256         assert isinstance(actor.dict_['key'], models.Attribute)
257         assert actor.dict_['key'].value[0] == 'value'