Skip to content

Commit fbf65e7

Browse files
committed
ValueError when col and row len do not match + pytest + offending check fixes
1 parent d9efebb commit fbf65e7

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

aci-preupgrade-validation-script.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,14 @@ def craftData(column, rows):
951951
if not (isinstance(rows, list) and isinstance(column, list)):
952952
raise TypeError("Rows and column must be lists.")
953953
data = []
954-
for i in range(len(rows)):
954+
c_len = len(column)
955+
for row_entry in range(len(rows)):
956+
r_len = len(rows[row_entry])
957+
if r_len != c_len:
958+
raise ValueError("Row length ({}), data: {} does not match column length ({}).".format(r_len, rows[row_entry], c_len))
955959
entry = {}
956-
for j in range(len(column)):
957-
if j < len(rows[i]):
958-
entry[column[j]] = rows[i][j]
959-
else:
960-
entry[column[j]] = None
960+
for col_pos in range(c_len):
961+
entry[column[col_pos]] = rows[row_entry][col_pos]
961962
data.append(entry)
962963
return data
963964

@@ -1648,7 +1649,7 @@ def switch_group_guideline_check(**kwargs):
16481649
def switch_bootflash_usage_check(tversion, **kwargs):
16491650
result = FAIL_UF
16501651
msg = ''
1651-
headers = ["Pod-ID", "Node-ID", "Utilization", "Alert"]
1652+
headers = ["Pod-ID", "Node-ID", "Utilization"]
16521653
data = []
16531654
recommended_action = "Over 50% usage! Contact Cisco TAC for Support"
16541655
doc_url = "https://datacenter.github.io/ACI-Pre-Upgrade-Validation-Script/validations/#switch-node-bootflash-usage"
@@ -2860,7 +2861,7 @@ def apic_version_md5_check(tversion, username, password, **kwargs):
28602861
c.log = LOG_FILE
28612862
c.connect()
28622863
except Exception as e:
2863-
data.append([apic_name, '-', '-', str(e), '-'])
2864+
data.append([apic_name, '-', '-', str(e)])
28642865
print_result(node_title, ERROR)
28652866
has_error = True
28662867
continue
@@ -2870,7 +2871,7 @@ def apic_version_md5_check(tversion, username, password, **kwargs):
28702871
tversion.dot_version)
28712872
except Exception as e:
28722873
data.append([apic_name, '-', '-',
2873-
'ls command via ssh failed due to:{}'.format(str(e)), '-'])
2874+
'ls command via ssh failed due to:{}'.format(str(e))])
28742875
print_result(node_title, ERROR)
28752876
has_error = True
28762877
continue
@@ -2884,7 +2885,7 @@ def apic_version_md5_check(tversion, username, password, **kwargs):
28842885
tversion.dot_version)
28852886
except Exception as e:
28862887
data.append([apic_name, str(tversion), '-',
2887-
'failed to check md5sum via ssh due to:{}'.format(str(e)), '-'])
2888+
'failed to check md5sum via ssh due to:{}'.format(str(e))])
28882889
print_result(node_title, ERROR)
28892890
has_error = True
28902891
continue

tests/test_AciResult.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"critical",
145145
"failed"
146146
),
147-
# Check 9: FAIL_O
147+
# Check 9: FAIL_O unformatted only
148148
(
149149
"fake_func_name_FAIL_O_unformatted_only_test",
150150
"FAIL_O Unformatted only",
@@ -204,4 +204,19 @@ def test_invalid_headers_or_data(headers, data):
204204
synth.craftData(
205205
column=headers,
206206
rows=data,
207-
)
207+
)
208+
209+
@pytest.mark.parametrize(
210+
"headers, data",
211+
[
212+
(["col1", "col2"], [["row1"], ["row2"]]), # Rows are shorter
213+
(["col1"], [["row1", "row2"], ["row3", "row4"]]), # columns are shorter
214+
]
215+
)
216+
def test_mismatched_lengths(headers, data):
217+
with pytest.raises(ValueError):
218+
synth = script.AciResult("func_name", "Check Title", "A Description")
219+
synth.craftData(
220+
column=headers,
221+
rows=data,
222+
)

tests/test_run_checks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
script = importlib.import_module("aci-preupgrade-validation-script")
77
AciVersion = script.AciVersion
88
JSON_DIR = script.JSON_DIR
9-
ApicResult = script.AciResult
9+
AciResult = script.AciResult
1010
Result = script.Result
1111
check_wrapper = script.check_wrapper
1212

@@ -162,10 +162,10 @@ def test_run_checks(capsys, caplog):
162162
else:
163163
assert data["failureDetails"]["failType"] == ""
164164
# failureDetails.data
165-
assert data["failureDetails"]["data"] == ApicResult.craftData(
165+
assert data["failureDetails"]["data"] == AciResult.craftData(
166166
others.get("headers", []), others.get("data", [])
167167
)
168-
assert data["failureDetails"]["unformatted_data"] == ApicResult.craftData(
168+
assert data["failureDetails"]["unformatted_data"] == AciResult.craftData(
169169
others.get("unformatted_headers", []), others.get("unformatted_data", [])
170170
)
171171
# other fields

0 commit comments

Comments
 (0)