Initial commit
[ta/config-manager.git] / cmframework / test / cmdsshandler_test.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import unittest
16 import mock
17 from mock import call
18 import json
19 from collections import OrderedDict
20
21 from cmframework.utils.cmdsshandler import CMDSSHandler
22 from cmframework.apis.cmerror import CMError
23 from dss.api import dss_error
24
25
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')
31
32         mock_dss_client.assert_called_once_with('test_uri')
33
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')
38
39         mock_dss_client.return_value.get_domains.side_effect = dss_error.Error('no domains')
40
41         with self.assertRaises(CMError) as context:
42             handler.get_domains()
43
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')
48
49         expected_result = ['a domain', 'b domain', 'c domain']
50         mock_dss_client.return_value.get_domains.return_value = expected_result
51
52         domains = handler.get_domains()
53
54         assert domains == expected_result
55
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')
60
61         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
62
63         domain = handler.get_domain('not domain')
64
65         assert domain is None
66
67         mock_dss_client.return_value.get_domains.assert_called_once()
68         mock_dss_client.return_value.get_domain.assert_not_called()
69
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')
74
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')
77
78         with self.assertRaises(CMError) as context:
79             domain = handler.get_domain('a domain')
80
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')
83
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')
88
89         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
90
91         expected_result = OrderedDict([('name1', 'value1'), ('name2', 'value2')])
92         mock_dss_client.return_value.get_domain.return_value = expected_result
93
94         domain = handler.get_domain('a domain')
95
96         assert domain == expected_result
97
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')
102
103         mock_dss_client.return_value.set.side_effect = dss_error.Error('some error')
104
105         with self.assertRaises(CMError) as context:
106             handler.set('a domain', 'a name', 'a value')
107
108         mock_dss_client.return_value.set.assert_called_once_with('a domain', 'a name', 'a value')
109
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')
114
115         handler.set('a domain', 'a name', 'a value')
116
117         mock_dss_client.return_value.set.assert_called_once_with('a domain', 'a name', 'a value')
118
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')
123
124         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
125
126         mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
127
128         mock_dss_client.return_value.delete.side_effect = dss_error.Error('some error')
129
130         with self.assertRaises(CMError) as context:
131             handler.delete('a domain', 'name')
132
133         mock_dss_client.return_value.delete.assert_called_once_with('a domain', 'name')
134
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')
139
140         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
141
142         mock_dss_client.return_value.get_domain.return_value = OrderedDict([('name', 'value')])
143
144         handler.delete('a domain', 'a name')
145
146         mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
147         mock_dss_client.return_value.delete.assert_not_called()
148
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')
153
154         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
155
156         handler.delete('not domain', 'no name')
157
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()
161
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')
166
167         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
168
169         mock_dss_client.return_value.get_domain.side_effect = dss_error.Error('some error')
170
171         with self.assertRaises(CMError) as context:
172             handler.get('a domain', 'name')
173
174         mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
175
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')
180
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')])
183
184         value = handler.get('a domain', 'a name')
185
186         assert value is None
187
188         mock_dss_client.return_value.get_domain.assert_called_once_with('a domain')
189
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')
194
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')])
197
198         value = handler.get('some domain', 'a name')
199
200         assert value is None
201
202         mock_dss_client.return_value.get_domain.assert_not_called()
203         mock_dss_client.return_value.get_domains.assert_called_once()
204
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')
209
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')])
212
213         value = handler.get('a domain', 'name')
214
215         assert value == 'value'
216
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')
221
222         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
223
224         mock_dss_client.return_value.delete_domain.side_effect = dss_error.Error('some error')
225
226         with self.assertRaises(CMError) as context:
227             handler.delete_domain('a domain')
228
229         mock_dss_client.return_value.delete_domain.assert_called_once_with('a domain')
230
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')
235
236         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
237
238         handler.delete_domain('not domain')
239
240         mock_dss_client.return_value.get_domains.assert_called_once()
241         mock_dss_client.return_value.delete_domain.assert_not_called()
242
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')
247
248         mock_dss_client.return_value.get_domains.return_value = ['a domain', 'b domain', 'c domain']
249
250         handler.delete_domain('a domain')
251
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')
254
255
256 if __name__ == '__main__':
257     unittest.main()