Update project maturity status
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / test_extension.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 import extension
19
20 # #pylint: disable=no-member,no-method-argument,unused-variable
21
22
23 class TestRegistrar(object):
24
25     def test_list_based_registrar_with_single_element_registration(self):
26         class ExtensionRegistration(extension._ExtensionRegistration):
27             @extension._registrar
28             def list_based_registrar(*_):
29                 return []
30         extension_registration = ExtensionRegistration()
31
32         @extension_registration
33         class Extension(object):
34             def list_based_registrar(self):
35                 return True
36
37         assert extension_registration.list_based_registrar() == []
38         extension_registration.init()
39         assert extension_registration.list_based_registrar() == [True]
40
41     def test_list_based_registrar_with_sequence_element_registration(self):
42         class ExtensionRegistration(extension._ExtensionRegistration):
43             @extension._registrar
44             def list_based_registrar1(*_):
45                 return []
46
47             @extension._registrar
48             def list_based_registrar2(*_):
49                 return []
50
51             @extension._registrar
52             def list_based_registrar3(*_):
53                 return []
54         extension_registration = ExtensionRegistration()
55
56         @extension_registration
57         class Extension(object):
58             def list_based_registrar1(*_):
59                 return [True, True]
60
61             def list_based_registrar2(*_):
62                 return True, True
63
64             def list_based_registrar3(*_):
65                 return set([True])
66
67         extension_registration.init()
68         assert extension_registration.list_based_registrar1() == [True, True]
69         assert extension_registration.list_based_registrar2() == [True, True]
70         assert extension_registration.list_based_registrar3() == [True]
71
72     def test_dict_based_registrar(self):
73         class ExtensionRegistration(extension._ExtensionRegistration):
74             @extension._registrar
75             def dict_based_registrar(*_):
76                 return {}
77         extension_registration = ExtensionRegistration()
78
79         @extension_registration
80         class Extension1(object):
81             def dict_based_registrar(self):
82                 return {
83                     'a': 'a',
84                     'b': 'b'
85                 }
86
87         @extension_registration
88         class Extension2(object):
89             def dict_based_registrar(self):
90                 return {
91                     'c': 'c',
92                     'd': 'd'
93                 }
94
95         assert extension_registration.dict_based_registrar() == {}
96         extension_registration.init()
97         assert extension_registration.dict_based_registrar() == {
98             'a': 'a',
99             'b': 'b',
100             'c': 'c',
101             'd': 'd'
102         }
103
104     def test_invalid_duplicate_key_dict_based_registrar(self):
105         class ExtensionRegistration(extension._ExtensionRegistration):
106             @extension._registrar
107             def dict_based_registrar(*_):
108                 return {}
109         extension_registration = ExtensionRegistration()
110
111         @extension_registration
112         class Extension1(object):
113             def dict_based_registrar(self):
114                 return {
115                     'a': 'val1',
116                 }
117
118         @extension_registration
119         class Extension2(object):
120             def dict_based_registrar(self):
121                 return {
122                     'a': 'val2',
123                 }
124
125         with pytest.raises(RuntimeError):
126             extension_registration.init()
127
128     def test_unsupported_registrar(self):
129         with pytest.raises(RuntimeError):
130             class ExtensionRegistration(extension._ExtensionRegistration):
131                 @extension._registrar
132                 def unsupported_registrar(*_):
133                     return set()
134             extension_registration = ExtensionRegistration()
135
136             @extension_registration
137             class Extension(object):
138                 def unsupported_registrar(self):
139                     return True
140
141             extension_registration.init()
142
143     def test_unimplemented_registration(self):
144         class ExtensionRegistration(extension._ExtensionRegistration):
145             @extension._registrar
146             def list_based_registrar(*_):
147                 return []
148         extension_registration = ExtensionRegistration()
149
150         @extension_registration
151         class Extension(object):
152             pass
153
154         assert extension_registration.list_based_registrar() == []
155         extension_registration.init()
156         assert extension_registration.list_based_registrar() == []