3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from collections import OrderedDict
21 from cmframework.utils.cmdsshandler import CMDSSHandler
22 from cmframework.apis.cmerror import CMError
23 from dss.api import dss_error
26 class CMDSSHandlerTest(unittest.TestCase):
27 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
28 @mock.patch('cmframework.utils.cmdsshandler.logging')
29 def test_init(self, mock_logging, mock_dss_client):
30 handler = CMDSSHandler(uri='test_uri')
32 mock_dss_client.assert_called_once_with('test_uri')
34 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
35 @mock.patch('cmframework.utils.cmdsshandler.logging')
36 def test_get_domains_exception(self, mock_logging, mock_dss_client):
37 handler = CMDSSHandler(uri='test_uri')
39 mock_dss_client.return_value.get_domains.side_effect = dss_error.Error('no domains')
41 with self.assertRaises(CMError) as context:
44 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
45 @mock.patch('cmframework.utils.cmdsshandler.logging')
46 def test_get_domains(self, mock_logging, mock_dss_client):
47 handler = CMDSSHandler(uri='test_uri')
49 expected_result = ['a domain', 'b domain', 'c domain']
50 mock_dss_client.return_value.get_domains.return_value = expected_result
52 domains = handler.get_domains()
54 assert domains == expected_result
56 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
57 @mock.patch('cmframework.utils.cmdsshandler.logging')
58 def test_get_domain_not_existing(self, mock_logging, mock_dss_client):
59 handler = CMDSSHandler(uri='test_uri')
61 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
63 domain = handler.get_domain('not domain')
67 mock_dss_client.return_value.get_domains.assert_called_once()
68 mock_dss_client.return_value.get_domain.assert_not_called()
70 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
71 @mock.patch('cmframework.utils.cmdsshandler.logging')
72 def test_get_domain_dss_fails(self, mock_logging, mock_dss_client):
73 handler = CMDSSHandler(uri='test_uri')
75 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
76 mock_dss_client.return_value.get_domain.side_effect = dss_error.Error('some error')
78 with self.assertRaises(CMError) as context:
79 domain = handler.get_domain('a domain')
81 mock_dss_client.return_value.get_domains.assert_called_once()
82 mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
84 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
85 @mock.patch('cmframework.utils.cmdsshandler.logging')
86 def test_get_domain(self, mock_logging, mock_dss_client):
87 handler = CMDSSHandler(uri='test_uri')
89 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
91 expected_result = OrderedDict([('name1', 'value1'), ('name2', 'value2')])
92 mock_dss_client.return_value.get_domain.return_value = expected_result
94 domain = handler.get_domain('a domain')
96 assert domain == expected_result
98 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
99 @mock.patch('cmframework.utils.cmdsshandler.logging')
100 def test_set_dss_fails(self, mock_logging, mock_dss_client):
101 handler = CMDSSHandler(uri='test_uri')
103 mock_dss_client.return_value.set.side_effect = dss_error.Error('some error')
105 with self.assertRaises(CMError) as context:
106 handler.set('a domain', 'a name', 'a value')
108 mock_dss_client.return_value.set.assert_called_once_with('a domain', 'a name', 'a value')
110 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
111 @mock.patch('cmframework.utils.cmdsshandler.logging')
112 def test_set(self, mock_logging, mock_dss_client):
113 handler = CMDSSHandler(uri='test_uri')
115 handler.set('a domain', 'a name', 'a value')
117 mock_dss_client.return_value.set.assert_called_once_with('a domain', 'a name', 'a value')
119 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
120 @mock.patch('cmframework.utils.cmdsshandler.logging')
121 def test_delete_dss_fails(self, mock_logging, mock_dss_client):
122 handler = CMDSSHandler(uri='test_uri')
124 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
126 mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
128 mock_dss_client.return_value.delete.side_effect = dss_error.Error('some error')
130 with self.assertRaises(CMError) as context:
131 handler.delete('a domain', 'name')
133 mock_dss_client.return_value.delete.assert_called_once_with('a domain', 'name')
135 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
136 @mock.patch('cmframework.utils.cmdsshandler.logging')
137 def test_delete_non_existing_name(self, mock_logging, mock_dss_client):
138 handler = CMDSSHandler(uri='test_uri')
140 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
142 mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
144 handler.delete('a domain', 'a name')
146 mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
147 mock_dss_client.return_value.delete.assert_not_called()
149 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
150 @mock.patch('cmframework.utils.cmdsshandler.logging')
151 def test_delete_non_existing_domain(self, mock_logging, mock_dss_client):
152 handler = CMDSSHandler(uri='test_uri')
154 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
156 handler.delete('not domain', 'no name')
158 mock_dss_client.return_value.get_domains.assert_called_once()
159 mock_dss_client.return_value.get_domain.assert_not_called()
160 mock_dss_client.return_value.delete.assert_not_called()
162 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
163 @mock.patch('cmframework.utils.cmdsshandler.logging')
164 def test_get_dss_fails(self, mock_logging, mock_dss_client):
165 handler = CMDSSHandler(uri='test_uri')
167 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
169 mock_dss_client.return_value.get_domain.side_effect = dss_error.Error('some error')
171 with self.assertRaises(CMError) as context:
172 handler.get('a domain', 'name')
174 mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
176 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
177 @mock.patch('cmframework.utils.cmdsshandler.logging')
178 def test_get_non_existing_name(self, mock_logging, mock_dss_client):
179 handler = CMDSSHandler(uri='test_uri')
181 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
182 mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
184 value = handler.get('a domain', 'a name')
188 mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
190 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
191 @mock.patch('cmframework.utils.cmdsshandler.logging')
192 def test_get_non_existing_domain(self, mock_logging, mock_dss_client):
193 handler = CMDSSHandler(uri='test_uri')
195 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
196 mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
198 value = handler.get('some domain', 'a name')
202 mock_dss_client.return_value.get_domain.assert_not_called()
203 mock_dss_client.return_value.get_domains.assert_called_once()
205 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
206 @mock.patch('cmframework.utils.cmdsshandler.logging')
207 def test_get(self, mock_logging, mock_dss_client):
208 handler = CMDSSHandler(uri='test_uri')
210 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
211 mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
213 value = handler.get('a domain', 'name')
215 assert value == 'value'
217 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
218 @mock.patch('cmframework.utils.cmdsshandler.logging')
219 def test_delete_domain_dss_fails(self, mock_logging, mock_dss_client):
220 handler = CMDSSHandler(uri='test_uri')
222 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
224 mock_dss_client.return_value.delete_domain.side_effect = dss_error.Error('some error')
226 with self.assertRaises(CMError) as context:
227 handler.delete_domain('a domain')
229 mock_dss_client.return_value.delete_domain.assert_called_once_with('a domain')
231 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
232 @mock.patch('cmframework.utils.cmdsshandler.logging')
233 def test_delete_domain_non_existent(self, mock_logging, mock_dss_client):
234 handler = CMDSSHandler(uri='test_uri')
236 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
238 handler.delete_domain('not domain')
240 mock_dss_client.return_value.get_domains.assert_called_once()
241 mock_dss_client.return_value.delete_domain.assert_not_called()
243 @mock.patch('cmframework.utils.cmdsshandler.dss_client.Client')
244 @mock.patch('cmframework.utils.cmdsshandler.logging')
245 def test_delete_domain(self, mock_logging, mock_dss_client):
246 handler = CMDSSHandler(uri='test_uri')
248 mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
250 handler.delete_domain('a domain')
252 mock_dss_client.return_value.get_domains.assert_called_once()
253 mock_dss_client.return_value.delete_domain.assert_called_once_with('a domain')
256 if __name__ == '__main__':