| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2021, Laurent Nicolas <[email protected]> |
| 8 |
# All rights reserved. |
| 9 |
# |
| 10 |
# Redistribution and use in source and binary forms, with or without modification, |
| 11 |
# are permitted provided that the following conditions are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer. |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 27 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
""" unit tests for module_utils netapp.py |
| 30 |
|
| 31 |
Provides wrappers for cloudmanager REST APIs |
| 32 |
""" |
| 33 |
|
| 34 |
from __future__ import (absolute_import, division, print_function) |
| 35 |
__metaclass__ = type |
| 36 |
|
| 37 |
# import copy # for deepcopy |
| 38 |
import json |
| 39 |
import sys |
| 40 |
import pytest |
| 41 |
try: |
| 42 |
HAS_REQUESTS_EXC = True |
| 43 |
except ImportError: |
| 44 |
HAS_REQUESTS_EXC = False |
| 45 |
|
| 46 |
from ansible.module_utils import basic |
| 47 |
from ansible.module_utils._text import to_bytes |
| 48 |
from ansible_collections.netapp.cloudmanager.tests.unit.compat.mock import patch |
| 49 |
import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils |
| 50 |
from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module import cmp as nm_cmp, NetAppModule |
| 51 |
if (not netapp_utils.HAS_REQUESTS or not HAS_REQUESTS_EXC) and sys.version_info < (3, 5): |
| 52 |
pytestmark = pytest.mark.skip('skipping as missing required imports on 2.6 and 2.7') |
| 53 |
|
| 54 |
|
| 55 |
def set_module_args(args): |
| 56 |
"""prepare arguments so that they will be picked up during module creation""" |
| 57 |
args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) |
| 58 |
basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access |
| 59 |
|
| 60 |
|
| 61 |
class mockResponse: |
| 62 |
def __init__(self, json_data, status_code, headers=None): |
| 63 |
self.json_data = json_data |
| 64 |
self.status_code = status_code |
| 65 |
self.content = json_data |
| 66 |
self.headers = headers or {} |
| 67 |
|
| 68 |
def json(self): |
| 69 |
return self.json_data |
| 70 |
|
| 71 |
|
| 72 |
def create_module(args): |
| 73 |
argument_spec = netapp_utils.cloudmanager_host_argument_spec() |
| 74 |
set_module_args(args) |
| 75 |
module = basic.AnsibleModule(argument_spec) |
| 76 |
return module |
| 77 |
|
| 78 |
|
| 79 |
def create_restapi_object(args): |
| 80 |
module = create_module(args) |
| 81 |
return netapp_utils.CloudManagerRestAPI(module) |
| 82 |
|
| 83 |
|
| 84 |
def mock_args(feature_flags=None, client_id=None): |
| 85 |
args = { |
| 86 |
'refresh_token': 'ABCDEFGS' |
| 87 |
} |
| 88 |
return args |
| 89 |
|
| 90 |
|
| 91 |
TOKEN_DICT = { |
| 92 |
'access_token': 'access_token', |
| 93 |
'token_type': 'token_type' |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
def test_cmp(): |
| 98 |
assert nm_cmp(None, 'x') == -1 |
| 99 |
assert nm_cmp('y', 'x') == 1 |
| 100 |
assert nm_cmp('y', 'X') == 1 |
| 101 |
assert nm_cmp(['x', 'y'], ['x', 'X']) == 1 |
| 102 |
assert nm_cmp(['x', 'x'], ['x', 'X']) == 0 |
| 103 |
|
| 104 |
|
| 105 |
def test_set_parameters(): |
| 106 |
helper = NetAppModule() |
| 107 |
helper.set_parameters({'a': None, 'b': 'b'}) |
| 108 |
assert 'a' not in helper.parameters |
| 109 |
assert 'b' in helper.parameters |
| 110 |
|
| 111 |
|
| 112 |
def test_cd_action(): |
| 113 |
desired = {} |
| 114 |
helper = NetAppModule() |
| 115 |
assert helper.get_cd_action(None, desired) == 'create' |
| 116 |
desired['state'] = 'present' |
| 117 |
assert helper.get_cd_action(None, desired) == 'create' |
| 118 |
assert helper.get_cd_action({}, desired) is None |
| 119 |
desired['state'] = 'absent' |
| 120 |
assert helper.get_cd_action(None, desired) is None |
| 121 |
assert helper.get_cd_action({}, desired) == 'delete' |
| 122 |
|
| 123 |
|
| 124 |
def test_compare_and_update_values(): |
| 125 |
current = {'a': 'a', 'b': 'b'} |
| 126 |
desired = {} |
| 127 |
desired_key = [] |
| 128 |
helper = NetAppModule() |
| 129 |
assert helper.compare_and_update_values(current, desired, desired_key) == ({}, False) |
| 130 |
desired_key = ['a'] |
| 131 |
assert helper.compare_and_update_values(current, desired, desired_key) == ({'a': 'a'}, False) |
| 132 |
desired = {'a': 'a'} |
| 133 |
assert helper.compare_and_update_values(current, desired, desired_key) == ({'a': 'a'}, False) |
| 134 |
desired = {'a': 'c'} |
| 135 |
assert helper.compare_and_update_values(current, desired, desired_key) == ({'a': 'c'}, True) |
| 136 |
|
| 137 |
|
| 138 |
@patch('requests.request') |
| 139 |
def test_get_working_environments_info(mock_request): |
| 140 |
mock_request.side_effect = [ |
| 141 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 142 |
mockResponse(json_data={'a': 'b'}, status_code=200), |
| 143 |
mockResponse(json_data={'c': 'd'}, status_code=500) |
| 144 |
] |
| 145 |
helper = NetAppModule() |
| 146 |
rest_api = create_restapi_object(mock_args()) |
| 147 |
assert helper.get_working_environments_info(rest_api, '') == ({'a': 'b'}, None) |
| 148 |
assert helper.get_working_environments_info(rest_api, '') == ({'c': 'd'}, '500') |
| 149 |
|
| 150 |
|
| 151 |
def test_look_up_working_environment_by_name_in_list(): |
| 152 |
we_list = [{'name': 'bob', 'b': 'b'}, {'name': 'chuck', 'c': 'c'}] |
| 153 |
helper = NetAppModule() |
| 154 |
assert helper.look_up_working_environment_by_name_in_list(we_list, 'bob') == (we_list[0], None) |
| 155 |
error = "look_up_working_environment_by_name_in_list: Working environment not found" |
| 156 |
assert helper.look_up_working_environment_by_name_in_list(we_list, 'alice') == (None, error) |
| 157 |
|
| 158 |
|
| 159 |
@patch('requests.request') |
| 160 |
def test_get_working_environment_details_by_name(mock_request): |
| 161 |
we_list = [{'name': 'bob', 'b': 'b'}, {'name': 'chuck', 'c': 'c'}] |
| 162 |
json_data = {'onPremWorkingEnvironments': [], |
| 163 |
'gcpVsaWorkingEnvironments': [], |
| 164 |
'azureVsaWorkingEnvironments': [], |
| 165 |
'vsaWorkingEnvironments': [] |
| 166 |
} |
| 167 |
json_data_onprem = dict(json_data) |
| 168 |
json_data_onprem['onPremWorkingEnvironments'] = we_list |
| 169 |
json_data_gcp = dict(json_data) |
| 170 |
json_data_gcp['gcpVsaWorkingEnvironments'] = we_list |
| 171 |
json_data_azure = dict(json_data) |
| 172 |
json_data_azure['azureVsaWorkingEnvironments'] = we_list |
| 173 |
json_data_aws = dict(json_data) |
| 174 |
json_data_aws['vsaWorkingEnvironments'] = we_list |
| 175 |
mock_request.side_effect = [ |
| 176 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 177 |
mockResponse(json_data={'a': 'b'}, status_code=500), # exists |
| 178 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists |
| 179 |
mockResponse(json_data={'c': 'd'}, status_code=400), # get all |
| 180 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists |
| 181 |
mockResponse(json_data=json_data_onprem, status_code=200), # get all |
| 182 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists |
| 183 |
mockResponse(json_data=json_data_gcp, status_code=200), # get all |
| 184 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists |
| 185 |
mockResponse(json_data=json_data_azure, status_code=200), # get all |
| 186 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists |
| 187 |
mockResponse(json_data=json_data_aws, status_code=200), # get all |
| 188 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists |
| 189 |
mockResponse(json_data=json_data, status_code=200), # get all |
| 190 |
] |
| 191 |
helper = NetAppModule() |
| 192 |
rest_api = create_restapi_object(mock_args()) |
| 193 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'name') == (None, '500') |
| 194 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'name') == (None, '400') |
| 195 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'bob') == (we_list[0], None) |
| 196 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'bob') == (we_list[0], None) |
| 197 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'bob') == (we_list[0], None) |
| 198 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'bob') == (we_list[0], None) |
| 199 |
error = "get_working_environment_details_by_name: Working environment not found" |
| 200 |
assert helper.get_working_environment_details_by_name(rest_api, '', 'bob') == (None, error) |
| 201 |
|
| 202 |
|
| 203 |
@patch('requests.request') |
| 204 |
def test_get_working_environment_details(mock_request): |
| 205 |
mock_request.side_effect = [ |
| 206 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 207 |
mockResponse(json_data={'key': [{'a': 'b'}]}, status_code=200), |
| 208 |
mockResponse(json_data={'key': [{'c': 'd'}]}, status_code=500) |
| 209 |
] |
| 210 |
helper = NetAppModule() |
| 211 |
args = dict(mock_args()) |
| 212 |
rest_api = create_restapi_object(args) |
| 213 |
helper.parameters['working_environment_id'] = 'test_we' |
| 214 |
assert helper.get_working_environment_details(rest_api, '') == ({'key': [{'a': 'b'}]}, None) |
| 215 |
error = "Error: get_working_environment_details 500" |
| 216 |
assert helper.get_working_environment_details(rest_api, '') == (None, error) |
| 217 |
|
| 218 |
|
| 219 |
@patch('requests.request') |
| 220 |
def test_get_working_environment_detail_for_snapmirror(mock_request): |
| 221 |
json_data = {'onPremWorkingEnvironments': [], |
| 222 |
'gcpVsaWorkingEnvironments': [], |
| 223 |
'azureVsaWorkingEnvironments': [], |
| 224 |
'vsaWorkingEnvironments': [] |
| 225 |
} |
| 226 |
json_data_source = dict(json_data) |
| 227 |
json_data_source['onPremWorkingEnvironments'] = [{'name': 'test_we_s'}] |
| 228 |
json_data_destination = dict(json_data) |
| 229 |
json_data_destination['onPremWorkingEnvironments'] = [{'name': 'test_we_d'}] |
| 230 |
mock_request.side_effect = [ |
| 231 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 232 |
# by id, first test |
| 233 |
mockResponse(json_data={'key': [{'publicId': 'test_we_s'}]}, status_code=200), # env details source |
| 234 |
mockResponse(json_data={'key': [{'publicId': 'test_we_d'}]}, status_code=200), # env details dest |
| 235 |
# by id, second test |
| 236 |
mockResponse(json_data={'key': [{'c': 'd'}]}, status_code=500), # error source |
| 237 |
# by id, third test |
| 238 |
mockResponse(json_data={'key': [{'publicId': 'test_we_s'}]}, status_code=200), # env details source |
| 239 |
mockResponse(json_data={'key': [{'e': 'f'}]}, status_code=500), # error source |
| 240 |
# by name, first test |
| 241 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists source |
| 242 |
mockResponse(json_data=json_data_source, status_code=200), # env details source |
| 243 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists dest |
| 244 |
mockResponse(json_data=json_data_destination, status_code=200), # env details dest |
| 245 |
# by name, second test |
| 246 |
mockResponse(json_data={'key': {'c': 'd'}}, status_code=500), # error source |
| 247 |
# by name, third test |
| 248 |
mockResponse(json_data={'a': 'b'}, status_code=200), # exists source |
| 249 |
mockResponse(json_data=json_data_source, status_code=200), # env details source |
| 250 |
mockResponse(json_data={'key': {'e': 'f'}}, status_code=500), # error source |
| 251 |
] |
| 252 |
helper = NetAppModule() |
| 253 |
args = dict(mock_args()) |
| 254 |
rest_api = create_restapi_object(args) |
| 255 |
# search by id |
| 256 |
helper.parameters['source_working_environment_id'] = 'test_we_s' |
| 257 |
helper.parameters['destination_working_environment_id'] = 'test_we_d' |
| 258 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == ({'publicId': 'test_we_s'}, {'publicId': 'test_we_d'}, None) |
| 259 |
error = "Error getting WE info: 500: {'key': [{'c': 'd'}]}" |
| 260 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == (None, None, error) |
| 261 |
error = "Error getting WE info: 500: {'key': [{'e': 'f'}]}" |
| 262 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == (None, None, error) |
| 263 |
# search by name |
| 264 |
del helper.parameters['source_working_environment_id'] |
| 265 |
del helper.parameters['destination_working_environment_id'] |
| 266 |
helper.parameters['source_working_environment_name'] = 'test_we_s' |
| 267 |
helper.parameters['destination_working_environment_name'] = 'test_we_d' |
| 268 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == ({'name': 'test_we_s'}, {'name': 'test_we_d'}, None) |
| 269 |
error = '500' |
| 270 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == (None, None, error) |
| 271 |
error = '500' |
| 272 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == (None, None, error) |
| 273 |
# no destination id nor name |
| 274 |
del helper.parameters['destination_working_environment_name'] |
| 275 |
error = 'Cannot find working environment by destination_working_environment_id or destination_working_environment_name' |
| 276 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == (None, None, error) |
| 277 |
# no source id nor name |
| 278 |
del helper.parameters['source_working_environment_name'] |
| 279 |
error = 'Cannot find working environment by source_working_environment_id or source_working_environment_name' |
| 280 |
assert helper.get_working_environment_detail_for_snapmirror(rest_api, '') == (None, None, error) |
| 281 |
|
| 282 |
|
| 283 |
def test_create_account(): |
| 284 |
helper = NetAppModule() |
| 285 |
error = "Error: creating an account is not supported." |
| 286 |
assert helper.create_account("rest_api") == (None, error) |
| 287 |
|
| 288 |
|
| 289 |
@patch('requests.request') |
| 290 |
def test_get_or_create_account(mock_request): |
| 291 |
mock_request.side_effect = [ |
| 292 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 293 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), |
| 294 |
mockResponse(json_data=[], status_code=200), |
| 295 |
mockResponse(json_data={'c': 'd'}, status_code=500) |
| 296 |
] |
| 297 |
helper = NetAppModule() |
| 298 |
rest_api = create_restapi_object(mock_args()) |
| 299 |
assert helper.get_or_create_account(rest_api) == ('account_id', None) |
| 300 |
error = 'Error: account cannot be located - check credentials or provide account_id.' |
| 301 |
assert helper.get_or_create_account(rest_api) == (None, error) |
| 302 |
error = '500' |
| 303 |
assert helper.get_or_create_account(rest_api) == (None, error) |
| 304 |
|
| 305 |
|
| 306 |
@patch('requests.request') |
| 307 |
def test_get_account_info(mock_request): |
| 308 |
mock_request.side_effect = [ |
| 309 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 310 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), |
| 311 |
mockResponse(json_data=[], status_code=200), |
| 312 |
mockResponse(json_data={'c': 'd'}, status_code=500) |
| 313 |
] |
| 314 |
helper = NetAppModule() |
| 315 |
rest_api = create_restapi_object(mock_args()) |
| 316 |
assert helper.get_account_info(rest_api, '') == ([{'accountPublicId': 'account_id'}], None) |
| 317 |
assert helper.get_account_info(rest_api, '') == ([], None) |
| 318 |
assert helper.get_account_info(rest_api, '') == (None, '500') |
| 319 |
|
| 320 |
|
| 321 |
@patch('requests.request') |
| 322 |
def test_get_account_id(mock_request): |
| 323 |
mock_request.side_effect = [ |
| 324 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 325 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), |
| 326 |
mockResponse(json_data=[], status_code=200), |
| 327 |
mockResponse(json_data={'c': 'd'}, status_code=500) |
| 328 |
] |
| 329 |
helper = NetAppModule() |
| 330 |
rest_api = create_restapi_object(mock_args()) |
| 331 |
assert helper.get_account_id(rest_api) == ('account_id', None) |
| 332 |
error = 'Error: no account found - check credentials or provide account_id.' |
| 333 |
assert helper.get_account_id(rest_api) == (None, error) |
| 334 |
error = '500' |
| 335 |
assert helper.get_account_id(rest_api) == (None, error) |
| 336 |
|
| 337 |
|
| 338 |
@patch('requests.request') |
| 339 |
def test_get_accounts_info(mock_request): |
| 340 |
mock_request.side_effect = [ |
| 341 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 342 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), |
| 343 |
mockResponse(json_data={'c': 'd'}, status_code=500) |
| 344 |
] |
| 345 |
helper = NetAppModule() |
| 346 |
rest_api = create_restapi_object(mock_args()) |
| 347 |
assert helper.get_accounts_info(rest_api, '') == ([{'accountPublicId': 'account_id'}], None) |
| 348 |
error = '500' |
| 349 |
assert helper.get_accounts_info(rest_api, '') == (None, error) |
| 350 |
|
| 351 |
|
| 352 |
def test_set_api_root_path(): |
| 353 |
helper = NetAppModule() |
| 354 |
helper.parameters['working_environment_id'] = 'abc' |
| 355 |
working_environment_details = {'cloudProviderName': 'Amazon', 'isHA': False} |
| 356 |
helper.set_api_root_path(working_environment_details, helper) |
| 357 |
assert helper.api_root_path == '/occm/api/vsa' |
| 358 |
working_environment_details = {'cloudProviderName': 'Other', 'isHA': False} |
| 359 |
helper.set_api_root_path(working_environment_details, helper) |
| 360 |
assert helper.api_root_path == '/occm/api/other/vsa' |
| 361 |
working_environment_details = {'cloudProviderName': 'Other', 'isHA': True} |
| 362 |
helper.set_api_root_path(working_environment_details, helper) |
| 363 |
assert helper.api_root_path == '/occm/api/other/ha' |
| 364 |
|
| 365 |
|
| 366 |
@patch('requests.request') |
| 367 |
def test_get_occm_agents_by_account(mock_request): |
| 368 |
mock_request.side_effect = [ |
| 369 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 370 |
mockResponse(json_data=[{'a': 'b'}], status_code=200), |
| 371 |
mockResponse(json_data=[{'c': 'd'}], status_code=500) |
| 372 |
] |
| 373 |
helper = NetAppModule() |
| 374 |
rest_api = create_restapi_object(mock_args()) |
| 375 |
assert helper.get_occm_agents_by_account(rest_api, '') == ([{'a': 'b'}], None) |
| 376 |
error = '500' |
| 377 |
assert helper.get_occm_agents_by_account(rest_api, '') == ([{'c': 'd'}], error) |
| 378 |
|
| 379 |
|
| 380 |
@patch('requests.request') |
| 381 |
def test_get_occm_agents_by_name(mock_request): |
| 382 |
json_data = {'agents': |
| 383 |
[{'name': '', 'provider': ''}, |
| 384 |
{'name': 'a1', 'provider': 'p1'}, |
| 385 |
{'name': 'a1', 'provider': 'p1'}, |
| 386 |
{'name': 'a1', 'provider': 'p2'}, |
| 387 |
{'name': 'a2', 'provider': 'p1'}, |
| 388 |
]} |
| 389 |
mock_request.side_effect = [ |
| 390 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 391 |
mockResponse(json_data=json_data, status_code=200), |
| 392 |
mockResponse(json_data=json_data, status_code=500) |
| 393 |
] |
| 394 |
helper = NetAppModule() |
| 395 |
rest_api = create_restapi_object(mock_args()) |
| 396 |
expected = [agent for agent in json_data['agents'] if agent['name'] == 'a1' and agent['provider'] == 'p1'] |
| 397 |
assert helper.get_occm_agents_by_name(rest_api, 'account', 'a1', 'p1') == (expected, None) |
| 398 |
error = '500' |
| 399 |
assert helper.get_occm_agents_by_name(rest_api, 'account', 'a1', 'p1') == (expected, error) |
| 400 |
|
| 401 |
|
| 402 |
@patch('requests.request') |
| 403 |
def test_get_agents_info(mock_request): |
| 404 |
mock_request.side_effect = [ |
| 405 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 406 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), # get account_id |
| 407 |
mockResponse(json_data=[{'a': 'b'}], status_code=200), |
| 408 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), # get account_id |
| 409 |
mockResponse(json_data=[{'c': 'd'}], status_code=500), |
| 410 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=400), # get account_id |
| 411 |
] |
| 412 |
helper = NetAppModule() |
| 413 |
rest_api = create_restapi_object(mock_args()) |
| 414 |
assert helper.get_agents_info(rest_api, '') == ([{'a': 'b'}], None) |
| 415 |
error = '500' |
| 416 |
assert helper.get_agents_info(rest_api, '') == ([{'c': 'd'}], error) |
| 417 |
error = '400' |
| 418 |
assert helper.get_agents_info(rest_api, '') == (None, error) |
| 419 |
|
| 420 |
|
| 421 |
@patch('requests.request') |
| 422 |
def test_get_active_agents_info(mock_request): |
| 423 |
json_data = {'agents': |
| 424 |
[{'name': '', 'provider': '', 'agentId': 1, 'status': ''}, |
| 425 |
{'name': 'a1', 'provider': 'p1', 'agentId': 1, 'status': 'active'}, |
| 426 |
{'name': 'a1', 'provider': 'p1', 'agentId': 1, 'status': ''}, |
| 427 |
{'name': 'a1', 'provider': 'p2', 'agentId': 1, 'status': 'active'}, |
| 428 |
{'name': 'a2', 'provider': 'p1', 'agentId': 1, 'status': 'active'}, |
| 429 |
]} |
| 430 |
mock_request.side_effect = [ |
| 431 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 432 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), # get account_id |
| 433 |
mockResponse(json_data=json_data, status_code=200), |
| 434 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=200), # get account_id |
| 435 |
mockResponse(json_data=json_data, status_code=500), |
| 436 |
mockResponse(json_data=[{'accountPublicId': 'account_id'}], status_code=400), # get account_id |
| 437 |
] |
| 438 |
helper = NetAppModule() |
| 439 |
rest_api = create_restapi_object(mock_args()) |
| 440 |
active = [agent for agent in json_data['agents'] if agent['status'] == 'active'] |
| 441 |
expected = [{'name': agent['name'], 'client_id': agent['agentId'], 'provider': agent['provider']} for agent in active] |
| 442 |
assert helper.get_active_agents_info(rest_api, '') == (expected, None) |
| 443 |
error = '500' |
| 444 |
assert helper.get_active_agents_info(rest_api, '') == (expected, error) |
| 445 |
error = '400' |
| 446 |
assert helper.get_active_agents_info(rest_api, '') == (None, error) |
| 447 |
|
| 448 |
|
| 449 |
@patch('requests.request') |
| 450 |
def test_get_occm_agent_by_id(mock_request): |
| 451 |
json_data = {'agent': |
| 452 |
{'name': 'a1', 'provider': 'p1', 'agentId': 1, 'status': 'active'} |
| 453 |
} |
| 454 |
mock_request.side_effect = [ |
| 455 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 456 |
mockResponse(json_data=json_data, status_code=200), |
| 457 |
mockResponse(json_data=json_data, status_code=500), |
| 458 |
mockResponse(json_data={'a': 'b'}, status_code=500), |
| 459 |
] |
| 460 |
helper = NetAppModule() |
| 461 |
rest_api = create_restapi_object(mock_args()) |
| 462 |
expected = json_data['agent'] |
| 463 |
assert helper.get_occm_agent_by_id(rest_api, '') == (expected, None) |
| 464 |
error = '500' |
| 465 |
assert helper.get_occm_agent_by_id(rest_api, '') == (expected, error) |
| 466 |
assert helper.get_occm_agent_by_id(rest_api, '') == ({'a': 'b'}, error) |
| 467 |
|
| 468 |
|
| 469 |
@patch('requests.request') |
| 470 |
def test_check_occm_status(mock_request): |
| 471 |
json_data = {'agent': |
| 472 |
{'name': 'a1', 'provider': 'p1', 'agentId': 1, 'status': 'active'} |
| 473 |
} |
| 474 |
mock_request.side_effect = [ |
| 475 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 476 |
mockResponse(json_data=json_data, status_code=200), |
| 477 |
mockResponse(json_data=json_data, status_code=500) |
| 478 |
] |
| 479 |
helper = NetAppModule() |
| 480 |
rest_api = create_restapi_object(mock_args()) |
| 481 |
expected = json_data |
| 482 |
assert helper.check_occm_status(rest_api, '') == (expected, None) |
| 483 |
error = '500' |
| 484 |
assert helper.check_occm_status(rest_api, '') == (expected, error) |
| 485 |
|
| 486 |
|
| 487 |
@patch('requests.request') |
| 488 |
def test_register_agent_to_service(mock_request): |
| 489 |
mock_request.side_effect = [ |
| 490 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 491 |
mockResponse(json_data={}, status_code=200), |
| 492 |
mockResponse(json_data={}, status_code=200), |
| 493 |
mockResponse(json_data={}, status_code=500) |
| 494 |
] |
| 495 |
helper = NetAppModule() |
| 496 |
rest_api = create_restapi_object(mock_args()) |
| 497 |
helper.parameters['account_id'] = 'account_id' |
| 498 |
helper.parameters['company'] = 'company' |
| 499 |
helper.parameters['region'] = 'region' |
| 500 |
helper.parameters['subnet_id'] = 'subnet_id' |
| 501 |
expected = {} |
| 502 |
assert helper.register_agent_to_service(rest_api, 'provider', 'vpc') == (expected, None) |
| 503 |
args, kwargs = mock_request.call_args |
| 504 |
body = kwargs['json'] |
| 505 |
assert 'placement' in body |
| 506 |
assert 'network' in body['placement'] |
| 507 |
assert body['placement']['network'] == 'vpc' |
| 508 |
body_other = body |
| 509 |
assert helper.register_agent_to_service(rest_api, 'AWS', 'vpc') == (expected, None) |
| 510 |
args, kwargs = mock_request.call_args |
| 511 |
body = kwargs['json'] |
| 512 |
assert 'placement' in body |
| 513 |
assert 'network' in body['placement'] |
| 514 |
assert body['placement']['network'] == 'vpc' |
| 515 |
assert body_other != body |
| 516 |
body['placement']['provider'] = 'provider' |
| 517 |
assert body_other == body |
| 518 |
error = '500' |
| 519 |
assert helper.register_agent_to_service(rest_api, 'provider', 'vpc') == (expected, error) |
| 520 |
|
| 521 |
|
| 522 |
@patch('requests.request') |
| 523 |
def test_delete_occm(mock_request): |
| 524 |
mock_request.side_effect = [ |
| 525 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 526 |
mockResponse(json_data={'result': 'any'}, status_code=200), |
| 527 |
mockResponse(json_data={'result': 'any'}, status_code=500), |
| 528 |
] |
| 529 |
helper = NetAppModule() |
| 530 |
helper.parameters['account_id'] = 'account_id' |
| 531 |
rest_api = create_restapi_object(mock_args()) |
| 532 |
assert helper.delete_occm(rest_api, '') == ({'result': 'any'}, None) |
| 533 |
error = '500' |
| 534 |
assert helper.delete_occm(rest_api, '') == ({'result': 'any'}, error) |
| 535 |
|
| 536 |
|
| 537 |
@patch('requests.request') |
| 538 |
def test_delete_occm_agents(mock_request): |
| 539 |
agents = [{'agentId': 'a1'}, |
| 540 |
{'agentId': 'a2'}] |
| 541 |
mock_request.side_effect = [ |
| 542 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 543 |
mockResponse(json_data={'result': 'any'}, status_code=200), # a1 |
| 544 |
mockResponse(json_data={'result': 'any'}, status_code=200), # a2 |
| 545 |
mockResponse(json_data={'result': 'any'}, status_code=500), # a1 |
| 546 |
mockResponse(json_data={'result': 'any'}, status_code=200), # a2 |
| 547 |
mockResponse(json_data={'result': 'any'}, status_code=200), # a1 |
| 548 |
mockResponse(json_data={'result': 'any'}, status_code=200), # a2 |
| 549 |
] |
| 550 |
helper = NetAppModule() |
| 551 |
helper.parameters['account_id'] = 'account_id' |
| 552 |
rest_api = create_restapi_object(mock_args()) |
| 553 |
assert helper.delete_occm_agents(rest_api, agents) == [] |
| 554 |
error = '500' |
| 555 |
assert helper.delete_occm_agents(rest_api, agents) == [({'result': 'any'}, error)] |
| 556 |
agents.append({'a': 'b'}) |
| 557 |
error = "unexpected agent contents: {'a': 'b'}" |
| 558 |
assert helper.delete_occm_agents(rest_api, agents) == [(None, error)] |
| 559 |
|
| 560 |
|
| 561 |
@patch('requests.request') |
| 562 |
def test_get_tenant(mock_request): |
| 563 |
tenants = [{'publicId': 'a1'}, |
| 564 |
{'publicId': 'a2'}] |
| 565 |
mock_request.side_effect = [ |
| 566 |
mockResponse(json_data=TOKEN_DICT, status_code=200), # OAUTH |
| 567 |
mockResponse(json_data=tenants, status_code=200), # get success |
| 568 |
mockResponse(json_data={'result': 'any'}, status_code=500), # get error |
| 569 |
] |
| 570 |
helper = NetAppModule() |
| 571 |
# helper.parameters['account_id'] = 'account_id' |
| 572 |
rest_api = create_restapi_object(mock_args()) |
| 573 |
assert helper.get_tenant(rest_api, '') == ('a1', None) |
| 574 |
error = "Error: unexpected response on getting tenant for cvo: 500, {'result': 'any'}" |
| 575 |
assert helper.get_tenant(rest_api, '') == (None, error) |