Skip to content

Commit a611cd9

Browse files
committed
catch ro users
1 parent 1ae218e commit a611cd9

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

aci-preupgrade-validation-script.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5271,9 +5271,10 @@ def apic_database_size_check(cversion, **kwargs):
52715271
recommended_action = 'Contact Cisco TAC to investigate all flagged large DB sizes'
52725272
for id in apic_id_to_name:
52735273
collect_stats_cmd = "acidiag dbsize --topshard --apic " + id + " -f json"
5274-
collect_shard_stats_data = run_cmd(collect_stats_cmd, splitlines=False)
5275-
if collect_shard_stats_data is None:
5276-
return Result(result=NA, msg="acidiag command not available to current user")
5274+
try:
5275+
collect_shard_stats_data = run_cmd(collect_stats_cmd, splitlines=False)
5276+
except subprocess.CalledProcessError:
5277+
return Result(result=MANUAL, msg="acidiag command not available to current user")
52775278
top_db_stats = json.loads(collect_shard_stats_data)
52785279

52795280
for db_stats in top_db_stats['dbs']:

tests/apic_database_size_check/test_apic_database_size_check.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,37 @@ def test_logic(mock_icurl, mock_run_cmd, cversion, expected_result):
296296
cver = script.AciVersion(cversion) if cversion else None
297297
result = script.apic_database_size_check(1, 1, cver)
298298
assert result == expected_result
299+
300+
301+
@pytest.mark.parametrize(
302+
"icurl_outputs, cmd_outputs, cversion, expected_result",
303+
[
304+
# If user does not have permissions to run acidiag, flag MANUAL
305+
(
306+
{apic_node_api: read_data(dir, 'infraWiNode_4.json')},
307+
{
308+
apic1_acidiag: {"CalledProcessError": True},
309+
apic2_acidiag: {"CalledProcessError": True},
310+
apic3_acidiag: {"CalledProcessError": True},
311+
},
312+
"6.1(3f)",
313+
script.MANUAL,
314+
),
315+
# If user has permissions to run acidiag but command fails, flag ERROR
316+
(
317+
{apic_node_api: read_data(dir, 'infraWiNode_4.json')},
318+
{
319+
apic1_acidiag: {"CalledProcessError": True},
320+
apic2_acidiag: {"CalledProcessError": True},
321+
apic3_acidiag: {"CalledProcessError": True},
322+
apic4_acidiag: {"CalledProcessError": True},
323+
},
324+
"6.1(3f)",
325+
script.MANUAL,
326+
),
327+
],
328+
)
329+
def test_permission_logic(mock_icurl, mock_run_cmd, cversion, expected_result):
330+
cver = script.AciVersion(cversion) if cversion else None
331+
result = script.apic_database_size_check(1, 1, cver)
332+
assert result == expected_result

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
import logging
55
import importlib
6+
from subprocess import CalledProcessError
67

78
TEST_DIR = os.path.dirname(os.path.abspath(__file__))
89
PROJECT_DIR = os.path.dirname(TEST_DIR)
@@ -213,8 +214,10 @@ def _mock_run_cmd(cmd, splitlines=False):
213214
if details is None:
214215
log.error("Command `%s` not found in test data", cmd)
215216
return ""
216-
splitlines = details.get("splitlines", False)
217+
if details.get("CalledProcessError"):
218+
raise CalledProcessError(127, cmd)
217219

220+
splitlines = details.get("splitlines", False)
218221
output = details.get("output")
219222
if output is None:
220223
log.error("Output for cmd `%s` not found in test data", cmd)

0 commit comments

Comments
 (0)