| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
import sys |
| 12 |
|
| 13 |
from ansible_collections.cisco.aci.tests.unit.compat import unittest |
| 14 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule |
| 15 |
from ansible.module_utils.six import PY2 |
| 16 |
from ansible.module_utils._text import to_native |
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
|
| 21 |
class AltModule: |
| 22 |
params = dict( |
| 23 |
hostname="dummy", |
| 24 |
port=123, |
| 25 |
protocol="https", |
| 26 |
state="present", |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
class AltACIModule(ACIModule): |
| 31 |
def __init__(self): |
| 32 |
self.result = dict(changed=False) |
| 33 |
self.module = AltModule |
| 34 |
self.params = self.module.params |
| 35 |
|
| 36 |
|
| 37 |
aci = AltACIModule() |
| 38 |
|
| 39 |
|
| 40 |
try: |
| 41 |
from lxml import etree |
| 42 |
|
| 43 |
except ImportError: |
| 44 |
pytestmark = pytest.mark.skip("ACI Ansible modules require the lxml and xmljson Python libraries") |
| 45 |
|
| 46 |
|
| 47 |
class AciRest(unittest.TestCase): |
| 48 |
def test_invalid_aci_login(self): |
| 49 |
self.maxDiff = None |
| 50 |
|
| 51 |
error = dict( |
| 52 |
code="401", |
| 53 |
text=("Username or password is incorrect - " "FAILED local authentication"), |
| 54 |
) |
| 55 |
|
| 56 |
imdata = [ |
| 57 |
{ |
| 58 |
"error": { |
| 59 |
"attributes": { |
| 60 |
"code": "401", |
| 61 |
"text": ("Username or password is incorrect - " "FAILED local authentication"), |
| 62 |
}, |
| 63 |
}, |
| 64 |
} |
| 65 |
] |
| 66 |
|
| 67 |
totalCount = 1 |
| 68 |
|
| 69 |
json_response = '{"totalCount":"1","imdata":[{"error":{"attributes":{"code":"401","text":"Username or password is incorrect - FAILED local authentication"}}}]}' # NOQA |
| 70 |
aci.response_json(json_response) |
| 71 |
self.assertEqual(aci.error, error) |
| 72 |
self.assertEqual(aci.imdata, imdata) |
| 73 |
self.assertEqual(aci.totalCount, totalCount) |
| 74 |
|
| 75 |
# Python 2.7+ is needed for xmljson |
| 76 |
if sys.version_info < (2, 7): |
| 77 |
return |
| 78 |
|
| 79 |
xml_response = """<?xml version="1.0" encoding="UTF-8"?> |
| 80 |
<imdata totalCount="1"> |
| 81 |
<error |
| 82 |
code="401" |
| 83 |
text="Username or password is incorrect - FAILED local |
| 84 |
authentication" |
| 85 |
/> |
| 86 |
</imdata> |
| 87 |
""" |
| 88 |
aci.response_xml(xml_response) |
| 89 |
self.assertEqual(aci.error, error) |
| 90 |
self.assertEqual(aci.imdata, imdata) |
| 91 |
self.assertEqual(aci.totalCount, totalCount) |
| 92 |
|
| 93 |
def test_valid_aci_login(self): |
| 94 |
self.maxDiff = None |
| 95 |
|
| 96 |
imdata = [ |
| 97 |
{ |
| 98 |
"aaaLogin": { |
| 99 |
"attributes": { |
| 100 |
"token": "ZldYAsoO9d0FfAQM8xaEVWvQPSOYwpnqzhwpIC1r4MaToknJjlIuAt9+TvXqrZ8lWYIGPj6VnZkWiS8nJfaiaX/AyrdD35jsSxiP3zydh+849xym7ALCw/fFNsc7b5ik1HaMuSUtdrN8fmCEUy7Pq/QNpGEqkE8m7HaxAuHpmvXgtdW1bA+KKJu2zY1c/tem", # NOQA |
| 101 |
"siteFingerprint": "NdxD72K/uXaUK0wn", |
| 102 |
"refreshTimeoutSeconds": "600", |
| 103 |
"maximumLifetimeSeconds": "86400", |
| 104 |
"guiIdleTimeoutSeconds": "1200", |
| 105 |
"restTimeoutSeconds": "90", |
| 106 |
"creationTime": "1500134817", |
| 107 |
"firstLoginTime": "1500134817", |
| 108 |
"userName": "admin", |
| 109 |
"remoteUser": "false", |
| 110 |
"unixUserId": "15374", |
| 111 |
"sessionId": "o7hObsqNTfCmDGcZI5c4ng==", |
| 112 |
"lastName": "", |
| 113 |
"firstName": "", |
| 114 |
"version": "2.0(2f)", |
| 115 |
"buildTime": "Sat Aug 20 23:07:07 PDT 2016", |
| 116 |
"node": "topology/pod-1/node-1", |
| 117 |
}, |
| 118 |
"children": [ |
| 119 |
{ |
| 120 |
"aaaUserDomain": { |
| 121 |
"attributes": { |
| 122 |
"name": "all", |
| 123 |
"rolesR": "admin", |
| 124 |
"rolesW": "admin", |
| 125 |
}, |
| 126 |
"children": [ |
| 127 |
{ |
| 128 |
"aaaReadRoles": { |
| 129 |
"attributes": {}, |
| 130 |
}, |
| 131 |
}, |
| 132 |
{ |
| 133 |
"aaaWriteRoles": { |
| 134 |
"attributes": {}, |
| 135 |
"children": [ |
| 136 |
{ |
| 137 |
"role": { |
| 138 |
"attributes": { |
| 139 |
"name": "admin", |
| 140 |
}, |
| 141 |
}, |
| 142 |
} |
| 143 |
], |
| 144 |
}, |
| 145 |
}, |
| 146 |
], |
| 147 |
}, |
| 148 |
}, |
| 149 |
{ |
| 150 |
"DnDomainMapEntry": { |
| 151 |
"attributes": { |
| 152 |
"dn": "uni/tn-common", |
| 153 |
"readPrivileges": "admin", |
| 154 |
"writePrivileges": "admin", |
| 155 |
}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
{ |
| 159 |
"DnDomainMapEntry": { |
| 160 |
"attributes": { |
| 161 |
"dn": "uni/tn-infra", |
| 162 |
"readPrivileges": "admin", |
| 163 |
"writePrivileges": "admin", |
| 164 |
}, |
| 165 |
}, |
| 166 |
}, |
| 167 |
{ |
| 168 |
"DnDomainMapEntry": { |
| 169 |
"attributes": { |
| 170 |
"dn": "uni/tn-mgmt", |
| 171 |
"readPrivileges": "admin", |
| 172 |
"writePrivileges": "admin", |
| 173 |
}, |
| 174 |
}, |
| 175 |
}, |
| 176 |
], |
| 177 |
}, |
| 178 |
} |
| 179 |
] |
| 180 |
|
| 181 |
totalCount = 1 |
| 182 |
|
| 183 |
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 |
| 184 |
aci.response_json(json_response) |
| 185 |
self.assertEqual(aci.imdata, imdata) |
| 186 |
self.assertEqual(aci.totalCount, totalCount) |
| 187 |
|
| 188 |
# Python 2.7+ is needed for xmljson |
| 189 |
if sys.version_info < (2, 7): |
| 190 |
return |
| 191 |
|
| 192 |
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 |
| 193 |
aci.response_xml(xml_response) |
| 194 |
self.assertEqual(aci.imdata, imdata) |
| 195 |
self.assertEqual(aci.totalCount, totalCount) |
| 196 |
|
| 197 |
def test_invalid_input(self): |
| 198 |
self.maxDiff = None |
| 199 |
|
| 200 |
error = dict( |
| 201 |
code="401", |
| 202 |
text=("Username or password is incorrect - " "FAILED local authentication"), |
| 203 |
) |
| 204 |
|
| 205 |
imdata = [ |
| 206 |
{ |
| 207 |
"error": { |
| 208 |
"attributes": { |
| 209 |
"code": "401", |
| 210 |
"text": ("Username or password is incorrect - " "FAILED local authentication"), |
| 211 |
}, |
| 212 |
}, |
| 213 |
} |
| 214 |
] |
| 215 |
|
| 216 |
totalCount = 1 |
| 217 |
|
| 218 |
json_response = '{"totalCount":"1","imdata":[{"error":{"attributes":{"code":"401","text":"Username or password is incorrect - FAILED local authentication"}}}]}' # NOQA |
| 219 |
aci.response_json(json_response) |
| 220 |
self.assertEqual(aci.error, error) |
| 221 |
self.assertEqual(aci.imdata, imdata) |
| 222 |
self.assertEqual(aci.totalCount, totalCount) |
| 223 |
|
| 224 |
# Python 2.7+ is needed for xmljson |
| 225 |
if sys.version_info < (2, 7): |
| 226 |
return |
| 227 |
|
| 228 |
xml_response = """<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"> |
| 229 |
<error |
| 230 |
code="401" |
| 231 |
text="Username or password is incorrect - FAILED local |
| 232 |
authentication" |
| 233 |
/> |
| 234 |
</imdata> |
| 235 |
""" |
| 236 |
aci.response_xml(xml_response) |
| 237 |
self.assertEqual(aci.error, error) |
| 238 |
self.assertEqual(aci.imdata, imdata) |
| 239 |
self.assertEqual(aci.totalCount, totalCount) |
| 240 |
|
| 241 |
def test_empty_response(self): |
| 242 |
self.maxDiffi = None |
| 243 |
|
| 244 |
if PY2: |
| 245 |
error_text = "Unable to parse output as JSON, see 'raw' output. No JSON object could be decoded" |
| 246 |
else: |
| 247 |
error_text = "Unable to parse output as JSON, see 'raw' output. Expecting value: line 1 column 1 (char 0)" |
| 248 |
|
| 249 |
error = dict( |
| 250 |
code=-1, |
| 251 |
text=error_text, |
| 252 |
) |
| 253 |
raw = "" |
| 254 |
|
| 255 |
json_response = "" |
| 256 |
aci.response_json(json_response) |
| 257 |
self.assertEqual(aci.error, error) |
| 258 |
self.assertEqual(aci.result["raw"], raw) |
| 259 |
|
| 260 |
# Python 2.7+ is needed for xmljson |
| 261 |
if sys.version_info < (2, 7): |
| 262 |
return |
| 263 |
|
| 264 |
elif etree.LXML_VERSION < (3, 3, 0, 0): |
| 265 |
error_text = "Unable to parse output as XML, see 'raw' output. None" |
| 266 |
elif etree.LXML_VERSION < (4, 0, 0, 0): |
| 267 |
error_text = to_native("Unable to parse output as XML, see 'raw' output. None (line 0)", errors="surrogate_or_strict") |
| 268 |
elif PY2: |
| 269 |
error_text = "Unable to parse output as XML, see 'raw' output. Document is empty, line 1, column 1 (line 1)" |
| 270 |
else: |
| 271 |
error_text = None |
| 272 |
|
| 273 |
xml_response = "" |
| 274 |
aci.response_xml(xml_response) |
| 275 |
|
| 276 |
if error_text is None: |
| 277 |
# errors vary on Python 3.8+ for unknown reasons |
| 278 |
# accept any of the following error messages |
| 279 |
errors = ( |
| 280 |
("Unable to parse output as XML, see 'raw' output. None (line 0)"), |
| 281 |
("Unable to parse output as XML, see 'raw' output. Document is empty, line 1, column 1 (<string>, line 1)"), |
| 282 |
) |
| 283 |
|
| 284 |
for error in errors: |
| 285 |
if error in aci.error["text"]: |
| 286 |
error_text = error |
| 287 |
break |
| 288 |
|
| 289 |
error = dict( |
| 290 |
code=-1, |
| 291 |
text=error_text, |
| 292 |
) |
| 293 |
|
| 294 |
raw = "" |
| 295 |
|
| 296 |
self.assertEqual(aci.error, error) |
| 297 |
self.assertEqual(aci.result["raw"], raw) |
| 298 |
|
| 299 |
def test_invalid_response(self): |
| 300 |
self.maxDiff = None |
| 301 |
|
| 302 |
if sys.version_info < (2, 7): |
| 303 |
error_text = "Unable to parse output as JSON, see 'raw' output. Expecting object: line 1 column 8 (char 8)" |
| 304 |
elif PY2: |
| 305 |
error_text = "Unable to parse output as JSON, see 'raw' output. No JSON object could be decoded" |
| 306 |
else: |
| 307 |
error_text = "Unable to parse output as JSON, see 'raw' output. Expecting value: line 1 column 9 (char 8)" |
| 308 |
|
| 309 |
error = dict( |
| 310 |
code=-1, |
| 311 |
text=error_text, |
| 312 |
) |
| 313 |
|
| 314 |
raw = '{ "aaa":' |
| 315 |
|
| 316 |
json_response = '{ "aaa":' |
| 317 |
aci.response_json(json_response) |
| 318 |
self.assertEqual(aci.error, error) |
| 319 |
self.assertEqual(aci.result["raw"], raw) |
| 320 |
|
| 321 |
# Python 2.7+ is needed for xmljson |
| 322 |
if sys.version_info < (2, 7): |
| 323 |
return |
| 324 |
|
| 325 |
elif etree.LXML_VERSION < (3, 3, 0, 0): |
| 326 |
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" |
| 327 |
elif PY2: |
| 328 |
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)" |
| 329 |
|
| 330 |
else: |
| 331 |
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)" |
| 332 |
|
| 333 |
error = dict( |
| 334 |
code=-1, |
| 335 |
text=error_text, |
| 336 |
) |
| 337 |
|
| 338 |
raw = "<aaa " |
| 339 |
|
| 340 |
xml_response = "<aaa " |
| 341 |
aci.response_xml(xml_response) |
| 342 |
self.assertEqual(aci.error, error) |
| 343 |
self.assertEqual(aci.result["raw"], raw) |