Skip to content

Commit 07ea2db

Browse files
ThatleftDetlef Sass
andauthored
adding of cli parameters for user and password (#335)
* added cli parameter for user (-u) and password (-p) to be used in an eased way for fully automated execution of 'aci-preupgrade-validation-script.py' * test: Add pytest --------- Co-authored-by: Detlef Sass <detlef.sass@de.bosch.com>
1 parent d118464 commit 07ea2db

4 files changed

Lines changed: 96 additions & 15 deletions

File tree

aci-preupgrade-validation-script.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,9 +1812,11 @@ def get_vpc_nodes():
18121812
return vpc_nodes
18131813

18141814

1815-
def query_common_data(api_only=False, arg_cversion=None, arg_tversion=None):
1816-
username = password = None
1817-
if not api_only:
1815+
def query_common_data(api_only=False, arg_cversion=None, arg_tversion=None, username=None, password=None):
1816+
if api_only:
1817+
username = None
1818+
password = None
1819+
elif not username or not password:
18181820
username, password = get_credentials()
18191821

18201822
try:
@@ -6296,6 +6298,8 @@ def multipod_modular_spine_bootscript_check(tversion, fabric_nodes, username, pa
62966298

62976299
def parse_args(args):
62986300
parser = ArgumentParser(description="ACI Pre-Upgrade Validation Script - %s" % SCRIPT_VERSION)
6301+
parser.add_argument("-u", "--username", action="store", type=str, help="Username used for SSH. If not provied it will prompt for")
6302+
parser.add_argument("-p", "--password", action="store", type=str, help="Password used for SSH. If not provied it will prompt for")
62996303
parser.add_argument("-t", "--tversion", action="store", type=str, help="Upgrade Target Version. Ex. 6.2(1a)")
63006304
parser.add_argument("-c", "--cversion", action="store", type=str, help="Override Current Version. Ex. 6.1(1a)")
63016305
parser.add_argument("-d", "--debug-function", action="store", type=str, help="Name of a single function to debug. Ex. 'apic_version_md5_check'")
@@ -6589,7 +6593,7 @@ def main(_args=None):
65896593
prints(' ==== %s%s, Script Version %s ====\n' % (ts, tz, SCRIPT_VERSION))
65906594
prints('!!!! Check https://github.com/datacenter/ACI-Pre-Upgrade-Validation-Script for Latest Release !!!!\n')
65916595

6592-
common_data = query_common_data(args.api_only, args.cversion, args.tversion)
6596+
common_data = query_common_data(args.api_only, args.cversion, args.tversion, args.username, args.password)
65936597
write_script_metadata(args.api_only, args.timeout, cm.total_checks, common_data)
65946598

65956599
cm.run_checks(common_data)

tests/test_common_data.py

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def _mock_get_target_version(arg_tversion):
324324
"attributes": {
325325
"dn": "topology/pod-1/node-1/sys/ctrlrfwstatuscont/ctrlrrunning",
326326
"type": "controller",
327-
"version": "3.2(7f)"
327+
"version": "3.2(7f)",
328328
}
329329
}
330330
}
@@ -336,7 +336,7 @@ def _mock_get_target_version(arg_tversion):
336336
"dn": "topology/pod-1/node-101/sys/fwstatuscont/running",
337337
"peVer": "3.1(2u)",
338338
"type": "switch",
339-
"version": "n9000-13.1(2u)"
339+
"version": "n9000-13.1(2u)",
340340
}
341341
}
342342
},
@@ -346,7 +346,7 @@ def _mock_get_target_version(arg_tversion):
346346
"dn": "topology/pod-1/node-102/sys/fwstatuscont/running",
347347
"peVer": "3.2(7f)",
348348
"type": "switch",
349-
"version": "n9000-13.2(7f)"
349+
"version": "n9000-13.2(7f)",
350350
}
351351
}
352352
},
@@ -356,7 +356,7 @@ def _mock_get_target_version(arg_tversion):
356356
"dn": "topology/pod-1/node-1001/sys/fwstatuscont/running",
357357
"peVer": "3.2(7f)",
358358
"type": "switch",
359-
"version": "n9000-13.2(7f)"
359+
"version": "n9000-13.2(7f)",
360360
}
361361
}
362362
},
@@ -366,7 +366,7 @@ def _mock_get_target_version(arg_tversion):
366366
"dn": "topology/pod-2/node-201/sys/fwstatuscont/running",
367367
"peVer": "3.2(7f)",
368368
"type": "switch",
369-
"version": "n9000-13.2(7f)"
369+
"version": "n9000-13.2(7f)",
370370
}
371371
}
372372
},
@@ -376,11 +376,10 @@ def _mock_get_target_version(arg_tversion):
376376
"dn": "topology/pod-2/node-2001/sys/fwstatuscont/running",
377377
"peVer": "3.2(7f)",
378378
"type": "switch",
379-
"version": "n9000-13.2(7f)"
379+
"version": "n9000-13.2(7f)",
380380
}
381381
}
382382
},
383-
384383
],
385384
}
386385

@@ -391,6 +390,8 @@ def fake_args(request):
391390
"api_only": False,
392391
"cversion": None,
393392
"tversion": None,
393+
"username": None,
394+
"password": None,
394395
}
395396
# update data contents when parametrize provides non-falsy values
396397
for key in data:
@@ -412,6 +413,8 @@ def fake_args(request):
412413
_icurl_outputs,
413414
{},
414415
{
416+
"username": "admin",
417+
"password": "mypassword",
415418
"cversion": AciVersion("6.1(1a)"),
416419
"sw_cversion": AciVersion("6.0(9d)"),
417420
"tversion": AciVersion("6.2(1a)"),
@@ -425,6 +428,8 @@ def fake_args(request):
425428
_icurl_outputs_old,
426429
{},
427430
{
431+
"username": "admin",
432+
"password": "mypassword",
428433
"cversion": AciVersion("3.2(7f)"),
429434
"sw_cversion": AciVersion("3.1(2u)"),
430435
"tversion": AciVersion("6.2(1a)"),
@@ -451,6 +456,34 @@ def fake_args(request):
451456
},
452457
id="api_only",
453458
),
459+
# `api_only` is True.
460+
# username and password are provide but ignored.
461+
pytest.param(
462+
_icurl_outputs,
463+
{
464+
"api_only": True,
465+
"username": "arg_admin",
466+
"password": "arg_password",
467+
},
468+
{
469+
"username": None,
470+
"password": None,
471+
},
472+
id="api_only_with_username_password",
473+
),
474+
# username and password are provided
475+
pytest.param(
476+
_icurl_outputs,
477+
{
478+
"username": "arg_admin",
479+
"password": "arg_password",
480+
},
481+
{
482+
"username": "arg_admin",
483+
"password": "arg_password",
484+
},
485+
id="username_password",
486+
),
454487
# `arg_tversion` is provided (i.e. -t 6.1(4a))
455488
pytest.param(
456489
_icurl_outputs,
@@ -537,7 +570,11 @@ def test_common_data(mock_icurl, fake_args, expected_common_data):
537570
"""test query_common_data and write_script_metadata"""
538571
# --- test for `query_common_data()`
539572
common_data = script.query_common_data(
540-
api_only=fake_args["api_only"], arg_cversion=fake_args["cversion"], arg_tversion=fake_args["tversion"]
573+
api_only=fake_args["api_only"],
574+
arg_cversion=fake_args["cversion"],
575+
arg_tversion=fake_args["tversion"],
576+
username=fake_args["username"],
577+
password=fake_args["password"],
541578
)
542579
for key in common_data:
543580
if isinstance(common_data[key], AciVersion):
@@ -603,7 +640,13 @@ def test_cversion_invald(capsys, mock_icurl):
603640
# `get_fabric_nodes()` failure
604641
(
605642
{
606-
"fabricNode.json": [{"error": {"attributes": {"code": "400", "text": "Request failed, unresolved class for dummyClass"}}}],
643+
"fabricNode.json": [
644+
{
645+
"error": {
646+
"attributes": {"code": "400", "text": "Request failed, unresolved class for dummyClass"}
647+
}
648+
}
649+
],
607650
"fabricNodePEp.json": _icurl_outputs["fabricNodePEp.json"],
608651
},
609652
"Gathering Node Information...\n\n",
@@ -612,7 +655,13 @@ def test_cversion_invald(capsys, mock_icurl):
612655
(
613656
{
614657
"fabricNode.json": _icurl_outputs["fabricNode.json"],
615-
"fabricNodePEp.json": [{"error": {"attributes": {"code": "400", "text": "Request failed, unresolved class for dummyClass"}}}],
658+
"fabricNodePEp.json": [
659+
{
660+
"error": {
661+
"attributes": {"code": "400", "text": "Request failed, unresolved class for dummyClass"}
662+
}
663+
}
664+
],
616665
},
617666
"Collecting VPC Node IDs...",
618667
),

tests/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# ----------------------------
1414
@pytest.fixture
1515
def mock_query_common_data(monkeypatch, expected_common_data):
16-
def _mock_query_common_data(api_only, args_cversion, args_tversion):
16+
def _mock_query_common_data(api_only, args_cversion, args_tversion, username, password):
1717
return expected_common_data
1818

1919
monkeypatch.setattr(script, "query_common_data", _mock_query_common_data)

tests/test_parse_args.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def test_no_args():
1818
assert args.api_only is False
1919
assert args.tversion is None
2020
assert args.cversion is None
21+
assert args.username is None
22+
assert args.password is None
2123
assert args.debug_function is None
2224
assert args.no_cleanup is False
2325
assert args.version is False
@@ -76,6 +78,32 @@ def test_cversion(args, expected_result):
7678
assert str(args.cversion) == str(expected_result)
7779

7880

81+
@pytest.mark.parametrize(
82+
"args, expected_result",
83+
[
84+
([], None),
85+
(["-u", "myusername"], "myusername"),
86+
(["--username", "myusername"], "myusername"),
87+
],
88+
)
89+
def test_username(args, expected_result):
90+
args = script.parse_args(args)
91+
assert args.username == expected_result
92+
93+
94+
@pytest.mark.parametrize(
95+
"args, expected_result",
96+
[
97+
([], None),
98+
(["-p", "mypassword"], "mypassword"),
99+
(["--password", "mypassword"], "mypassword"),
100+
],
101+
)
102+
def test_password(args, expected_result):
103+
args = script.parse_args(args)
104+
assert args.password == expected_result
105+
106+
79107
@pytest.mark.parametrize(
80108
"args, expected_result",
81109
[

0 commit comments

Comments
 (0)