summaryrefslogtreecommitdiff
path: root/test/units/module_utils/network/aci/test_aci.py
blob: f930d75dc062bcc786d806eda390c1013b50541f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: utf-8 -*-

# Copyright 2017 Dag Wieers <dag@wieers.com>

# This file is part of Ansible by Red Hat
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.


import sys

from ansible.compat.tests import unittest
from ansible.module_utils.network.aci.aci import ACIModule
from ansible.module_utils.six import PY2, PY3
from ansible.module_utils._text import to_native

from nose.plugins.skip import SkipTest


class AltModule():
    params = dict(
        hostname='dummy',
        port=123,
        protocol='https',
        state='present',
    )


class AltACIModule(ACIModule):
    def __init__(self):
        self.result = dict(changed=False)
        self.module = AltModule
        self.params = self.module.params


aci = AltACIModule()


try:
    from lxml import etree
    if sys.version_info >= (2, 7):
        from xmljson import cobra
except ImportError:
    raise SkipTest("ACI Ansible modules require the lxml and xmljson Python libraries")


class AciRest(unittest.TestCase):

    def test_invalid_aci_login(self):
        self.maxDiff = None

        error = dict(
            code='401',
            text='Username or password is incorrect - FAILED local authentication',
        )

        imdata = [{
            'error': {
                'attributes': {
                    'code': '401',
                    'text': 'Username or password is incorrect - FAILED local authentication',
                },
            },
        }]

        totalCount = 1

        json_response = '{"totalCount":"1","imdata":[{"error":{"attributes":{"code":"401","text":"Username or password is incorrect - FAILED local authentication"}}}]}'  # NOQA
        json_result = dict()
        aci.response_json(json_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.imdata, imdata)
        self.assertEqual(aci.totalCount, totalCount)

        # Python 2.7+ is needed for xmljson
        if sys.version_info < (2, 7):
            return

        xml_response = '''<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1">
        <error code="401" text="Username or password is incorrect - FAILED local authentication"/>
        </imdata>
        '''
        xml_result = dict()
        aci.response_xml(xml_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.imdata, imdata)
        self.assertEqual(aci.totalCount, totalCount)

    def test_valid_aci_login(self):
        self.maxDiff = None

        imdata = [{
            'aaaLogin': {
                'attributes': {
                    'token': 'ZldYAsoO9d0FfAQM8xaEVWvQPSOYwpnqzhwpIC1r4MaToknJjlIuAt9+TvXqrZ8lWYIGPj6VnZkWiS8nJfaiaX/AyrdD35jsSxiP3zydh+849xym7ALCw/fFNsc7b5ik1HaMuSUtdrN8fmCEUy7Pq/QNpGEqkE8m7HaxAuHpmvXgtdW1bA+KKJu2zY1c/tem',  # NOQA
                    'siteFingerprint': 'NdxD72K/uXaUK0wn',
                    'refreshTimeoutSeconds': '600',
                    'maximumLifetimeSeconds': '86400',
                    'guiIdleTimeoutSeconds': '1200',
                    'restTimeoutSeconds': '90',
                    'creationTime': '1500134817',
                    'firstLoginTime': '1500134817',
                    'userName': 'admin',
                    'remoteUser': 'false',
                    'unixUserId': '15374',
                    'sessionId': 'o7hObsqNTfCmDGcZI5c4ng==',
                    'lastName': '',
                    'firstName': '',
                    'version': '2.0(2f)',
                    'buildTime': 'Sat Aug 20 23:07:07 PDT 2016',
                    'node': 'topology/pod-1/node-1',
                },
                'children': [{
                    'aaaUserDomain': {
                        'attributes': {
                            'name': 'all',
                            'rolesR': 'admin',
                            'rolesW': 'admin',
                        },
                        'children': [{
                            'aaaReadRoles': {
                                'attributes': {},
                            },
                        }, {
                            'aaaWriteRoles': {
                                'attributes': {},
                                'children': [{
                                    'role': {
                                        'attributes': {
                                            'name': 'admin',
                                        },
                                    },
                                }],
                            },
                        }],
                    },
                }, {
                    'DnDomainMapEntry': {
                        'attributes': {
                            'dn': 'uni/tn-common',
                            'readPrivileges': 'admin',
                            'writePrivileges': 'admin',
                        },
                    },
                }, {
                    'DnDomainMapEntry': {
                        'attributes': {
                            'dn': 'uni/tn-infra',
                            'readPrivileges': 'admin',
                             'writePrivileges': 'admin',
                        },
                    },
                }, {
                    'DnDomainMapEntry': {
                        'attributes': {
                            'dn': 'uni/tn-mgmt',
                            'readPrivileges': 'admin',
                            'writePrivileges': 'admin',
                        },
                    },
                }],
            },
        }]

        totalCount = 1

        json_response = '{"totalCount":"1","imdata":[{"aaaLogin":{"attributes":{"token":"ZldYAsoO9d0FfAQM8xaEVWvQPSOYwpnqzhwpIC1r4MaToknJjlIuAt9+TvXqrZ8lWYIGPj6VnZkWiS8nJfaiaX/AyrdD35jsSxiP3zydh+849xym7ALCw/fFNsc7b5ik1HaMuSUtdrN8fmCEUy7Pq/QNpGEqkE8m7HaxAuHpmvXgtdW1bA+KKJu2zY1c/tem","siteFingerprint":"NdxD72K/uXaUK0wn","refreshTimeoutSeconds":"600","maximumLifetimeSeconds":"86400","guiIdleTimeoutSeconds":"1200","restTimeoutSeconds":"90","creationTime":"1500134817","firstLoginTime":"1500134817","userName":"admin","remoteUser":"false","unixUserId":"15374","sessionId":"o7hObsqNTfCmDGcZI5c4ng==","lastName":"","firstName":"","version":"2.0(2f)","buildTime":"Sat Aug 20 23:07:07 PDT 2016","node":"topology/pod-1/node-1"},"children":[{"aaaUserDomain":{"attributes":{"name":"all","rolesR":"admin","rolesW":"admin"},"children":[{"aaaReadRoles":{"attributes":{}}},{"aaaWriteRoles":{"attributes":{},"children":[{"role":{"attributes":{"name":"admin"}}}]}}]}},{"DnDomainMapEntry":{"attributes":{"dn":"uni/tn-common","readPrivileges":"admin","writePrivileges":"admin"}}},{"DnDomainMapEntry":{"attributes":{"dn":"uni/tn-infra","readPrivileges":"admin","writePrivileges":"admin"}}},{"DnDomainMapEntry":{"attributes":{"dn":"uni/tn-mgmt","readPrivileges":"admin","writePrivileges":"admin"}}}]}}]}'  # NOQA
        json_result = dict()
        aci.response_json(json_response)
        self.assertEqual(aci.imdata, imdata)
        self.assertEqual(aci.totalCount, totalCount)

        # Python 2.7+ is needed for xmljson
        if sys.version_info < (2, 7):
            return

        xml_response = '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1">\n<aaaLogin token="ZldYAsoO9d0FfAQM8xaEVWvQPSOYwpnqzhwpIC1r4MaToknJjlIuAt9+TvXqrZ8lWYIGPj6VnZkWiS8nJfaiaX/AyrdD35jsSxiP3zydh+849xym7ALCw/fFNsc7b5ik1HaMuSUtdrN8fmCEUy7Pq/QNpGEqkE8m7HaxAuHpmvXgtdW1bA+KKJu2zY1c/tem" siteFingerprint="NdxD72K/uXaUK0wn" refreshTimeoutSeconds="600" maximumLifetimeSeconds="86400" guiIdleTimeoutSeconds="1200" restTimeoutSeconds="90" creationTime="1500134817" firstLoginTime="1500134817" userName="admin" remoteUser="false" unixUserId="15374" sessionId="o7hObsqNTfCmDGcZI5c4ng==" lastName="" firstName="" version="2.0(2f)" buildTime="Sat Aug 20 23:07:07 PDT 2016" node="topology/pod-1/node-1">\n<aaaUserDomain name="all" rolesR="admin" rolesW="admin">\n<aaaReadRoles/>\n<aaaWriteRoles>\n<role name="admin"/>\n</aaaWriteRoles>\n</aaaUserDomain>\n<DnDomainMapEntry dn="uni/tn-common" readPrivileges="admin" writePrivileges="admin"/>\n<DnDomainMapEntry dn="uni/tn-infra" readPrivileges="admin" writePrivileges="admin"/>\n<DnDomainMapEntry dn="uni/tn-mgmt" readPrivileges="admin" writePrivileges="admin"/>\n</aaaLogin></imdata>\n'''  # NOQA
        xml_result = dict()
        aci.response_xml(xml_response)
        self.assertEqual(aci.imdata, imdata)
        self.assertEqual(aci.totalCount, totalCount)

    def test_invalid_input(self):
        self.maxDiff = None

        error = dict(
            code='401',
            text='Username or password is incorrect - FAILED local authentication',
        )

        imdata = [{
            'error': {
                'attributes': {
                    'code': '401',
                    'text': 'Username or password is incorrect - FAILED local authentication',
                },
            },
        }]

        totalCount = 1

        json_response = '{"totalCount":"1","imdata":[{"error":{"attributes":{"code":"401","text":"Username or password is incorrect - FAILED local authentication"}}}]}'  # NOQA
        json_result = dict()
        aci.response_json(json_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.imdata, imdata)
        self.assertEqual(aci.totalCount, totalCount)

        # Python 2.7+ is needed for xmljson
        if sys.version_info < (2, 7):
            return

        xml_response = '''<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1">
        <error code="401" text="Username or password is incorrect - FAILED local authentication"/>
        </imdata>
        '''
        xml_result = dict()
        aci.response_xml(xml_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.imdata, imdata)
        self.assertEqual(aci.totalCount, totalCount)

    def test_empty_response(self):
        self.maxDiffi = None

        if PY2:
            error_text = "Unable to parse output as JSON, see 'raw' output. No JSON object could be decoded"
        else:
            error_text = "Unable to parse output as JSON, see 'raw' output. Expecting value: line 1 column 1 (char 0)"

        error = dict(
            code=-1,
            text=error_text,
        )
        raw = ''

        json_response = ''
        json_result = dict()
        aci.response_json(json_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.result['raw'], raw)

        # Python 2.7+ is needed for xmljson
        if sys.version_info < (2, 7):
            return

        elif etree.LXML_VERSION < (3, 3, 0, 0):
            error_text = "Unable to parse output as XML, see 'raw' output. None",
        elif etree.LXML_VERSION < (4, 0, 0, 0):
            error_text = to_native(u"Unable to parse output as XML, see 'raw' output. None (line 0)", errors='surrogate_or_strict')
        elif PY2:
            error_text = "Unable to parse output as XML, see 'raw' output. Document is empty, line 1, column 1 (line 1)"
        else:
            error_text = "Unable to parse output as XML, see 'raw' output. Document is empty, line 1, column 1 (<string>, line 1)"

        error = dict(
            code=-1,
            text=error_text,
        )

        raw = ''

        xml_response = ''
        xml_result = dict()
        aci.response_xml(xml_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.result['raw'], raw)

    def test_invalid_response(self):
        self.maxDiff = None

        if sys.version_info < (2, 7):
            error_text = "Unable to parse output as JSON, see 'raw' output. Expecting object: line 1 column 8 (char 8)"
        elif PY2:
            error_text = "Unable to parse output as JSON, see 'raw' output. No JSON object could be decoded"
        else:
            error_text = "Unable to parse output as JSON, see 'raw' output. Expecting value: line 1 column 9 (char 8)"

        error = dict(
            code=-1,
            text=error_text,
        )

        raw = '{ "aaa":'

        json_response = '{ "aaa":'
        json_result = dict()
        aci.response_json(json_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.result['raw'], raw)

        # Python 2.7+ is needed for xmljson
        if sys.version_info < (2, 7):
            return

        elif etree.LXML_VERSION < (3, 3, 0, 0):
            error_text = "Unable to parse output as XML, see 'raw' output. Couldn't find end of Start Tag aaa line 1, line 1, column 5"  # NOQA

        elif PY2:
            error_text = "Unable to parse output as XML, see 'raw' output. Couldn't find end of Start Tag aaa line 1, line 1, column 6 (line 1)"  # NOQA

        else:
            error_text = "Unable to parse output as XML, see 'raw' output. Couldn't find end of Start Tag aaa line 1, line 1, column 6 (<string>, line 1)"  # NOQA

        error = dict(
            code=-1,
            text=error_text,
        )

        raw = '<aaa '

        xml_response = '<aaa '
        xml_result = dict()
        aci.response_xml(xml_response)
        self.assertEqual(aci.error, error)
        self.assertEqual(aci.result['raw'], raw)