Skip to content

Commit

Permalink
refactor: remove duplicated specs from get_dependency_specs (#3549)
Browse files Browse the repository at this point in the history
* refactor: remove duplicated specs from the result of get_dependency_specs

Signed-off-by: Xiangce Liu <[email protected]>

* update the conditions name in the test

Signed-off-by: Xiangce Liu <[email protected]>
(cherry picked from commit 730b663)
  • Loading branch information
xiangce committed Oct 13, 2022
1 parent bf9ff2a commit 31c4e9d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
10 changes: 6 additions & 4 deletions insights/core/dr.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,11 @@ def get_requires(comp):
req = list()
for cmp in get_delegate(comp).requires:
if get_name(cmp).startswith("insights.specs."):
req.append(get_simple_name(cmp))
cmpn = get_simple_name(cmp)
req.append(cmpn) if cmpn not in req else None
else:
req.extend(get_requires(cmp) + get_at_least_one(cmp))
union = get_requires(cmp) + get_at_least_one(cmp)
req.extend([ss for ss in union if ss not in req])
return req

def get_at_least_one(comp):
Expand All @@ -412,10 +414,10 @@ def get_at_least_one(comp):
ssalo = get_at_least_one(cmp)
if ssreq and ssalo:
# they are mixed and in the same `requires` level
salo.append([ss for ss in ssreq + ssalo])
salo.append([ss for ss in ssreq + ssalo if ss not in salo])
else:
# no mixed, just add them one by one
salo.extend(ssreq or ssalo)
salo.extend([ss for ss in ssreq or ssalo if ss not in salo])
alo.append(tuple(salo))
return alo

Expand Down
67 changes: 49 additions & 18 deletions insights/tests/test_get_dependency_specs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from insights import condition, rule, make_fail
from insights.parsers.installed_rpms import InstalledRpms
from insights.parsers.up2date import Up2Date
from insights.parsers.uname import Uname
from insights.parsers.redhat_release import RedhatRelease
from insights.parsers.messages import Messages
from insights.parsers.ps import PsAuxcww
from insights.parsers.virt_what import VirtWhat
Expand Down Expand Up @@ -29,69 +31,80 @@


@condition(InstalledRpms, [Up2Date, PsAuxcww], optional=[Messages])
def condition_1(*args):
def condition_01(*args):
return True


@condition(VirtWhat, Tags, RedHatRelease, optional=[Date, Uptime])
def condition_2(*args):
def condition_02(*args):
return True


@condition(LsVarLog, [LsBoot, LsEtc])
def condition_3(*args):
def condition_03(*args):
return True


@condition(LsVarWwwPerms, [LsDev, LsDisk], optional=[LsVarRun])
def condition_4(*args):
def condition_04(*args):
return True


@condition([LsPci, LsPciVmmkn])
def condition_5(*args):
def condition_05(*args):
return True


@condition([LvmConf, LvmConfig])
def condition_6(*args):
def condition_06(*args):
return True


@condition(Mount, [LsTmp, LsVarTmp], optional=[LsUsrBin])
def condition_7(*args):
def condition_07(*args):
return True


@rule(Lsof, condition_1, condition_2,
[LsCPU, condition_3], [condition_4, LsSCSI], [condition_5, condition_6],
optional=[LSBlock, condition_7])
def report(*args):
@rule(Lsof, condition_01, condition_02,
[LsCPU, condition_03], [condition_04, LsSCSI],
[condition_05, condition_06],
optional=[LSBlock, condition_07])
def report_01(*args):
return make_fail("HIT")


@condition(LsVarLog, [LsBoot, LsEtc])
def ABC(*args):
def condition_11(*args):
return True


@condition([LsPci, ABC])
def DEF(*args):
@condition([LsPci, condition_11])
def condition_12(*args):
return True


@condition(LsPci, LsBoot, LsDisk)
def XYZ(*args):
def report_11(*args):
return True


@condition(PsAuxcww, LsVarLog, [Uname, RedhatRelease], [LsPci, condition_11])
def condition_21(*args):
return True


@condition(RedHatRelease, LsVarLog, condition_12, condition_21)
def report_21(*args):
return True


def test_get_dependency_specs_1_level_requires_only():
specs = get_dependency_specs(XYZ)
specs = get_dependency_specs(report_11)
assert sorted(specs) == ['ls_boot', 'ls_disk', 'lspci']


def test_get_dependency_specs_2_level():
specs = get_dependency_specs(DEF)
specs = get_dependency_specs(condition_12)
# [
# ('lspci', [('ls_etc', 'ls_boot'), 'ls_var_log'])
# ]
Expand All @@ -111,7 +124,7 @@ def test_get_dependency_specs_2_level():


def test_get_dependency_specs_complex():
specs = get_dependency_specs(report)
specs = get_dependency_specs(report_01)
# [
# 'installed_rpms',
# 'tags',
Expand All @@ -129,3 +142,21 @@ def test_get_dependency_specs_complex():
# order due to python versions. Here we just check the number of the
# `at_least_one` specs
assert len([alo for alo in specs if isinstance(alo, tuple)]) == 5


def test_get_dependency_specs_duplicate():
specs = get_dependency_specs(report_21)
# [
# ('uname', 'redhat_release'),
# 'ps_auxcww',
# 'ls_var_log',
# ('ls_pci', ['ls_var_log', ('ls_boot', 'ls_etc')])
# ]
# There is only one such item in the result
assert ('uname', 'redhat_release') in specs
specs.remove(('uname', 'redhat_release'))
assert ('uname', 'redhat_release') not in specs

assert 'ls_var_log' in specs
specs.remove('ls_var_log')
assert 'ls_var_log' not in specs

0 comments on commit 31c4e9d

Please sign in to comment.