Skip to content

Commit

Permalink
feat: Add spec cups_browsed_conf (#4227)
Browse files Browse the repository at this point in the history
* Add spec cups_browsed_conf


Signed-off-by: jiazhang <jiazhang@redhat.com>

* enhance coverage to 100%

Signed-off-by: Xiangce Liu <xiangceliu@redhat.com>

rh-pre-commit.version: 2.3.1
rh-pre-commit.check-secrets: ENABLED

Signed-off-by: jiazhang <jiazhang@redhat.com>
Co-authored-by: Xiangce Liu <xiangceliu@redhat.com>
  • Loading branch information
wushiqinlou and xiangce authored Sep 27, 2024
1 parent 16695a5 commit 19e6afa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
36 changes: 36 additions & 0 deletions insights/parsers/cups_confs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
CupsdConf - file ``/etc/cups/cupsd.conf``
-----------------------------------------
CupsBrowsedConf - file ``/etc/cups/cups-browsed.conf``
------------------------------------------------------
CupsFilesConf - file ``/etc/cups/cups-files.conf``
--------------------------------------------------
"""
Expand Down Expand Up @@ -90,6 +92,40 @@ def parse_doc(self, content):
return Entry(children=result, src=self)


@parser(Specs.cups_browsed_conf)
class CupsBrowsedConf(Parser, dict):
"""
Class for parsing the file ``/etc/cups/cups-browsed.conf``
Sample file content::
BrowseRemoteProtocols dnssd cups
BrowseAllow cups.example.com
Examples:
>>> type(cups_browsed_conf)
<class 'insights.parsers.cups_confs.CupsBrowsedConf'>
>>> 'dnssd' in cups_browsed_conf['BrowseRemoteProtocols']
True
>>> 'cups.example.com' in cups_browsed_conf['BrowseAllow']
True
"""
def parse_content(self, content):
if not content:
raise ParseException('Empty Content')

for line in get_active_lines(content):
k, v = [i.strip() for i in line.split(None, 1)]
if k not in self:
self[k] = v if len(v.split()) == 1 else v.split()
else:
_v = self[k]
_v = [_v] if not isinstance(_v, list) else _v
if v not in _v:
_v.append(v)
self[k] = _v


@parser(Specs.cups_files_conf)
class CupsFilesConf(Parser, dict):
"""
Expand Down
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Specs(SpecSet):
crypto_policies_state_current = RegistryPoint(no_obfuscate=['hostname', 'ip'])
cryptsetup_luksDump = RegistryPoint(multi_output=True, no_obfuscate=['hostname', 'ip'])
cupsd_conf = RegistryPoint()
cups_browsed_conf = RegistryPoint(filterable=True)
cups_files_conf = RegistryPoint()
cups_ppd = RegistryPoint(multi_output=True, no_obfuscate=['hostname', 'ip'])
current_clocksource = RegistryPoint(no_obfuscate=['hostname', 'ip'])
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class DefaultSpecs(Specs):
crypto_policies_state_current = simple_file("/etc/crypto-policies/state/current")
cryptsetup_luksDump = luks_devices.luks_data_sources
cupsd_conf = simple_file("/etc/cups/cupsd.conf")
cups_browsed_conf = simple_file("/etc/cups/cups-browsed.conf")
cups_files_conf = simple_file("/etc/cups/cups-files.conf")
current_clocksource = simple_file("/sys/devices/system/clocksource/clocksource0/current_clocksource")
date = simple_command("/bin/date")
Expand Down
36 changes: 33 additions & 3 deletions insights/tests/parsers/test_cups_confs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from insights.core.exceptions import ParseException
from insights.parsers import cups_confs
from insights.parsers.cups_confs import CupsdConf, CupsFilesConf
from insights.parsers.cups_confs import CupsdConf, CupsFilesConf, CupsBrowsedConf
from insights.parsr.query import first, last
from insights.tests import context_wrap

Expand Down Expand Up @@ -127,6 +127,21 @@
""".strip()


CUPS_BROWSED_CONF = """
# Which protocols will we use to discover printers on the network?
# Can use DNSSD and/or CUPS and/or LDAP, or 'none' for neither.
BrowseRemoteProtocols dnssd cups
BrowseAllow 192.168.0.1
BrowseAllow 192.168.0.255
BrowseAllow cups.example.com
BrowseAllow 192.168.0.255
""".strip()

CUPS_BROWSED_CONF_CONF_EMPTY = """
""".strip()


def test_cupsd_conf():
context = context_wrap(CUPSD_CONF)
result = CupsdConf(context)
Expand All @@ -151,16 +166,31 @@ def test_cups_files_conf():
assert result['AccessLog'] == 'syslog'


def test_cups_files_conf_empyt():
def test_cups_files_conf_empty():
with pytest.raises(ParseException) as exc:
CupsFilesConf(context_wrap(CUPS_FILES_CONF_EMPTY))
assert str(exc.value) == "Empty Content"


def test_cups_browsed_files_conf():
result = CupsBrowsedConf(context_wrap(CUPS_BROWSED_CONF))
assert len(result) == 2
assert 'BrowseRemoteProtocols' in result
assert result['BrowseRemoteProtocols'] == ['dnssd', 'cups']
assert sorted(result['BrowseAllow']) == sorted(['192.168.0.1', '192.168.0.255', 'cups.example.com'])


def test_cups_browsed_conf_empty():
with pytest.raises(ParseException) as exc:
CupsBrowsedConf(context_wrap(CUPS_BROWSED_CONF_CONF_EMPTY))
assert str(exc.value) == "Empty Content"


def test_doc():
env = {
'cupsd_conf': CupsdConf(context_wrap(CUPSD_CONF_EXAMPLE)),
'cups_files_conf': CupsFilesConf(context_wrap(CUPS_FILES_CONF))
'cups_files_conf': CupsFilesConf(context_wrap(CUPS_FILES_CONF)),
'cups_browsed_conf': CupsBrowsedConf(context_wrap(CUPS_BROWSED_CONF))
}
failed, total = doctest.testmod(cups_confs, globs=env)
assert failed == 0

0 comments on commit 19e6afa

Please sign in to comment.