Skip to content

Commit

Permalink
Feat: New Parser for 'dotnet --version' Command for Containers (#3581)
Browse files Browse the repository at this point in the history
* Feat: New Parser for 'dotnet --version' Command

Signed-off-by: Xinting Li <[email protected]>

* Update spec
* Fix Parser Name
* Update Doc string
* Update doc title
* Add doctest

Signed-off-by: Xinting Li <[email protected]>
  • Loading branch information
TZ3070 authored Nov 9, 2022
1 parent 23f7bd9 commit ce50be2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
38 changes: 37 additions & 1 deletion insights/parsers/dotnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
DotNet- Comand ``/usr/bin/dotnet``
==================================
The parser for ``/usr/bin/dotnet --version`` is included in this module..
The parsers related ``/usr/bin/dotnet --version`` is included in this module.
DotNetVersion - command ``dotnet --version``
--------------------------------------------
ContainerDotNetVersion - command ``dotnet --version`` for containers
--------------------------------------------------------------------
"""

from insights import parser, CommandParser
from insights.parsers import SkipException, ParseException
from insights.specs import Specs
from insights import ContainerParser


@parser(Specs.dotnet_version)
Expand Down Expand Up @@ -44,3 +50,33 @@ def parse_content(self, content):

if self.major is None:
raise ParseException("Unrecognized version: {0}", self.raw)


@parser(Specs.container_dotnet_version)
class ContainerDotNetVersion(ContainerParser, DotNetVersion):
"""
Parses the output of the ``/usr/bin/dotnet --version`` command of the running
containers which are based on RHEL images.
Sample output::
3.1.108
Examples:
>>> type(con_dotnet_ver)
<class 'insights.parsers.dotnet.ContainerDotNetVersion'>
>>> con_dotnet_ver.container_id
'cc2883a1a369'
>>> con_dotnet_ver.image
'quay.io/rhel8'
>>> con_dotnet_ver.engine
'podman'
>>> con_dotnet_ver.major
3
>>> con_dotnet_ver.minor
1
>>> con_dotnet_ver.raw
'3.1.108'
"""

pass
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ class Specs(SpecSet):
zipl_conf = RegistryPoint()

# container_specs
container_dotnet_version = RegistryPoint(multi_output=True)
container_redhat_release = RegistryPoint(multi_output=True)
container_nginx_conf = RegistryPoint(multi_output=True)
container_installed_rpms = RegistryPoint(multi_output=True)
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ class DefaultSpecs(Specs):
zipl_conf = simple_file("/etc/zipl.conf")

# Container collection specs
container_dotnet_version = container_execute(running_rhel_containers, "/usr/bin/dotnet --version")
container_installed_rpms = container_execute(running_rhel_containers, "rpm -qa --qf '%s'" % _rpm_format, context=HostContext, signum=signal.SIGTERM)
container_nginx_conf = container_collect(container_nginx_conf_ds)
container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release")
Expand Down
42 changes: 40 additions & 2 deletions insights/tests/parsers/test_dotnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from insights.parsers import dotnet
from insights.core.plugins import ContentException
from insights.parsers import SkipException, ParseException
from insights.parsers.dotnet import DotNetVersion
from insights.parsers.dotnet import DotNetVersion, ContainerDotNetVersion
from insights.tests import context_wrap

dotnet_version_1 = "3.1.108"
Expand Down Expand Up @@ -45,9 +45,47 @@ def test_dotnet_version_ab():
assert ret is None


def test_container_dotnet_version():
ret = ContainerDotNetVersion(
context_wrap(
dotnet_version_1,
container_id='cc2883a1a369',
image='quay.io/rhel8',
engine='podman'
)
)
assert ret.major == 3
assert ret.minor == 1
assert ret.raw == dotnet_version_1
assert ret.image == "quay.io/rhel8"
assert ret.engine == "podman"
assert ret.container_id == "cc2883a1a369"


def test_container_dotnet_version_error():
with pytest.raises(ContentException):
ret = ContainerDotNetVersion(
context_wrap(
dotnet_version_3,
container_id='cc2883a1a369',
image='quay.io/rhel8',
engine='podman'
)
)
assert ret is None


def test_doc_examples():
env = {
'dotnet_ver': DotNetVersion(context_wrap(dotnet_version_1))
'dotnet_ver': DotNetVersion(context_wrap(dotnet_version_1)),
'con_dotnet_ver': ContainerDotNetVersion(
context_wrap(
dotnet_version_1,
container_id='cc2883a1a369',
image='quay.io/rhel8',
engine='podman'
)
)
}
failed, total = doctest.testmod(dotnet, globs=env)
assert failed == 0

0 comments on commit ce50be2

Please sign in to comment.