-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New spec and parser for
authselect current
(#3490)
Add a new spec for authselect current and its parser Signed-off-by: Xiangce Liu <[email protected]>
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.. automodule:: insights.parsers.authselect | ||
:members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
AuthSelectCurrent - command ``authselect current`` | ||
================================================== | ||
""" | ||
from insights import parser | ||
from insights.core import CommandParser | ||
from insights.parsers import SkipException | ||
from insights.specs import Specs | ||
|
||
|
||
@parser(Specs.authselect_current) | ||
class AuthSelectCurrent(CommandParser): | ||
""" | ||
Class to parse the output of command "authselect current". | ||
Sample output of command ``authselect current``:: | ||
Profile ID: sssd | ||
Enabled features: | ||
- with-sudo | ||
- with-mkhomedir | ||
- with-smartcard | ||
Attributes: | ||
profile_id (str): The enabled profile ID | ||
enabled_features (list): List of enabled features | ||
Examples: | ||
>>> asc.profile_id | ||
'sssd' | ||
>>> len(asc.enabled_features) | ||
3 | ||
>>> 'with-sudo' in asc.enabled_features | ||
True | ||
""" | ||
def parse_content(self, content): | ||
feature_flag = False | ||
self.profile_id = None | ||
self.enabled_features = [] | ||
for line in content: | ||
if 'No existing configuration detected.' in line: | ||
raise SkipException | ||
line_sp = line.split() | ||
if line.startswith('Profile ID:'): | ||
self.profile_id = line_sp[-1] | ||
elif line.endswith('Enabled features:'): | ||
# Enabled features: | ||
# - ... | ||
feature_flag = True | ||
elif line.startswith('Enabled features:') and len(line_sp) > 2: | ||
# Enabled features: None | ||
feature_flag = True | ||
if line_sp[-1] != 'None': | ||
self.enabled_features.append(line_sp[-1]) | ||
elif line.startswith('-') and feature_flag: | ||
self.enabled_features.append(line_sp[-1]) | ||
if self.profile_id is None: | ||
raise SkipException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
import doctest | ||
from insights.parsers.authselect import AuthSelectCurrent | ||
from insights.parsers import SkipException, authselect | ||
from insights.tests import context_wrap | ||
|
||
AUTHSELECT_CURRENT_1 = """ | ||
Profile ID: sssd | ||
Enabled features: | ||
- with-sudo | ||
- with-mkhomedir | ||
- with-smartcard | ||
""".strip() | ||
|
||
AUTHSELECT_CURRENT_2 = """ | ||
Profile ID: custom/password-policy | ||
Enabled features: None | ||
""".strip() | ||
|
||
AUTHSELECT_CURRENT_EMPTY = "" | ||
|
||
AUTHSELECT_CURRENT_NG = """ | ||
No existing configuration detected. | ||
""".strip() | ||
|
||
|
||
def test_authselect_current(): | ||
asc = AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_1)) | ||
assert asc.profile_id == 'sssd' | ||
assert asc.enabled_features == ['with-sudo', 'with-mkhomedir', 'with-smartcard'] | ||
|
||
asc = AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_2)) | ||
assert asc.profile_id == 'custom/password-policy' | ||
assert asc.enabled_features == [] | ||
|
||
|
||
def test_authselect_current_exp(): | ||
with pytest.raises(SkipException): | ||
AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_EMPTY)) | ||
|
||
with pytest.raises(SkipException): | ||
AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_NG)) | ||
|
||
|
||
def test_doc_examples(): | ||
env = { | ||
'asc': AuthSelectCurrent(context_wrap(AUTHSELECT_CURRENT_1)) | ||
} | ||
failed, total = doctest.testmod(authselect, globs=env) | ||
assert failed == 0 |