Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/module_utils/test_response_helper.py

raw
1 # Copyright (c) 2022 NetApp
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit tests for module_utils rest_generic.py - REST features '''
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8
9 from ansible_collections.netapp.ontap.plugins.module_utils import rest_response_helpers
10
11 RECORD = {'key': 'value'}
12
13 RESPONSES = {
14 'empty': {},
15 'zero_record': {'num_records': 0},
16 'empty_records': {'records': []},
17 'one_record': {'records': [RECORD], 'num_records': 1},
18 'one_record_no_num_records': {'records': [RECORD]},
19 'one_record_no_num_records_no_records': RECORD,
20 'two_records': {'records': [RECORD, RECORD], 'num_records': 2},
21 }
22
23
24 def test_check_for_0_or_1_records():
25 # no records --> None
26 response_in, error_in, response_out, error_out = RESPONSES['zero_record'], None, None, None
27 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
28 response_in, error_in, response_out, error_out = RESPONSES['empty_records'], None, None, None
29 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
30
31 # one record
32 response_in, error_in, response_out, error_out = RESPONSES['one_record'], None, RECORD, None
33 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
34 response_in, error_in, response_out, error_out = RESPONSES['one_record_no_num_records'], None, RECORD, None
35 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
36 response_in, error_in, response_out, error_out = RESPONSES['one_record_no_num_records_no_records'], None, RECORD, None
37 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
38
39
40 def test_check_for_0_or_1_records_errors():
41 # bad input
42 response_in, error_in, response_out, error_out = None, None, None, 'calling: cluster: no response None.'
43 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
44 response_in, error_in, response_out, error_out = RESPONSES['empty'], None, None, 'calling: cluster: no response {}.'
45 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
46
47 # error in
48 response_in, error_in, response_out, error_out = None, 'some_error', None, 'calling: cluster: got some_error.'
49 assert rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in) == (response_out, error_out)
50
51 # more than 1 record
52 response_in, error_in, response_out, error_out = RESPONSES['two_records'], None, RESPONSES['two_records'], 'calling: cluster: unexpected response'
53 response, error = rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in)
54 assert response == response_out
55 assert error.startswith(error_out)
56 assert 'for query' not in error
57 response, error = rest_response_helpers.check_for_0_or_1_records('cluster', response_in, error_in, query=RECORD)
58 assert response == response_out
59 assert error.startswith(error_out)
60 expected = 'for query: %s' % RECORD
61 assert expected in error
62
63
64 def test_check_for_0_or_more_records():
65 # no records --> None
66 response_in, error_in, response_out, error_out = RESPONSES['zero_record'], None, None, None
67 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
68 response_in, error_in, response_out, error_out = RESPONSES['empty_records'], None, None, None
69 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
70
71 # one record
72 response_in, error_in, response_out, error_out = RESPONSES['one_record'], None, [RECORD], None
73 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
74 response_in, error_in, response_out, error_out = RESPONSES['one_record_no_num_records'], None, [RECORD], None
75 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
76
77 # more than 1 record
78 response_in, error_in, response_out, error_out = RESPONSES['two_records'], None, [RECORD, RECORD], None
79 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
80
81
82 def test_check_for_0_or_more_records_errors():
83 # bad input
84 response_in, error_in, response_out, error_out = None, None, None, 'calling: cluster: no response None.'
85 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
86 response_in, error_in, response_out, error_out = RESPONSES['empty'], None, None, 'calling: cluster: no response {}.'
87 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
88 error = "calling: cluster: got No \"records\" key in {'key': 'value'}."
89 response_in, error_in, response_out, error_out = RESPONSES['one_record_no_num_records_no_records'], None, None, error
90 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
91
92 # error in
93 response_in, error_in, response_out, error_out = None, 'some_error', None, 'calling: cluster: got some_error.'
94 assert rest_response_helpers.check_for_0_or_more_records('cluster', response_in, error_in) == (response_out, error_out)
95
96
97 class MockOntapRestAPI:
98 def __init__(self, job_response=None, error=None, raise_if_called=False):
99 self.job_response, self.error, self.raise_if_called = job_response, error, raise_if_called
100
101 def wait_on_job(self, job):
102 if self.raise_if_called:
103 raise AttributeError('wait_on_job should not be called in this test!')
104 return self.job_response, self.error
105
106
107 def test_check_for_error_and_job_results_no_job():
108 rest_api = MockOntapRestAPI(raise_if_called=True)
109 response_in, error_in, response_out, error_out = None, None, None, None
110 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
111
112 response_in, error_in, response_out, error_out = 'any', None, 'any', None
113 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
114
115 response = {'no_job': 'entry'}
116 response_in, error_in, response_out, error_out = response, None, response, None
117 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
118
119
120 def test_check_for_error_and_job_results_with_job():
121 rest_api = MockOntapRestAPI(job_response='job_response', error=None)
122 response = {'job': 'job_entry'}
123 expected_response = {'job': 'job_entry', 'job_response': 'job_response'}
124 response_in, error_in, response_out, error_out = response, None, expected_response, None
125 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
126
127 response = {'jobs': ['job_entry'], 'num_records': 1}
128 expected_response = {'jobs': ['job_entry'], 'num_records': 1, 'job_response': 'job_response'}
129 response_in, error_in, response_out, error_out = response, None, expected_response, None
130 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
131
132
133 def test_negative_check_for_error_and_job_results_error_in():
134 rest_api = MockOntapRestAPI(raise_if_called=True)
135 response_in, error_in, response_out, error_out = None, 'forced_error', None, 'calling: cluster: got forced_error.'
136 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
137 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api, raw_error=False) == (response_out, error_out)
138 error_out = 'forced_error'
139 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api, raw_error=True) == (response_out, error_out)
140
141
142 def test_negative_check_for_error_and_job_results_job_error():
143 rest_api = MockOntapRestAPI(job_response='job_response', error='job_error')
144 response = {'job': 'job_entry'}
145 response_in, error_in, response_out, error_out = response, None, response, "job reported error: job_error, received {'job': 'job_entry'}."
146 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)
147 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api, raw_error=False) == (response_out, error_out)
148 error_out = 'job_error'
149 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api, raw_error=True) == (response_out, error_out)
150
151
152 def test_negative_check_for_error_and_job_results_multiple_jobs_error():
153 rest_api = MockOntapRestAPI(raise_if_called=True)
154 response = {'jobs': 'job_entry', 'num_records': 3}
155 response_in, error_in, response_out, error_out = response, None, response, "multiple jobs in progress, can't check status"
156 assert rest_response_helpers.check_for_error_and_job_results('cluster', response_in, error_in, rest_api) == (response_out, error_out)