Skip to content

Commit

Permalink
ConvertPamUserDB: implement db conversion
Browse files Browse the repository at this point in the history
Check the databases reported by ScanPamUserDB and convert them to GDBM
format. It also includes the unit-tests.

Signed-off-by: Iker Pedrosa <[email protected]>
  • Loading branch information
ikerexxe committed Sep 4, 2024
1 parent 3bbc9f0 commit 8ecacdf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from leapp.actors import Actor
from leapp.libraries.actor import convertpamuserdb
from leapp.models import PamUserDbLocation
from leapp.tags import IPUWorkflowTag, PreparationPhaseTag


class ConvertPamUserDb(Actor):
"""
Convert the pam_userdb databases to GDBM
"""

name = 'convert_pam_user_db'
consumes = (PamUserDbLocation,)
produces = ()
tags = (PreparationPhaseTag, IPUWorkflowTag)

def process(self):
convertpamuserdb.process()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from leapp.models import PamUserDbLocation
from leapp.exceptions import StopActorExecutionError
from leapp.libraries.stdlib import api, CalledProcessError, run


def _convert_db(db_path):
cmd = ['db_converter', '--src', f'{db_path}.db', '--dest', f'{db_path}.gdbm']
try:
run(cmd)
except (CalledProcessError, OSError) as e:
# As the db_converter does not remove the original DB after conversion or upon failure,
# interrupt the upgrade, keeping the original DBs.
# If all DBs are successfully converted, the leftover DBs are removed in the removeoldpamuserdb actor.
raise StopActorExecutionError(
'Cannot convert pam_userdb database.',
details={'details': '{}: {}'.format(str(e), e.stderr)}
)


def process():
msg = next(api.consume(PamUserDbLocation), None)
if not msg:
raise StopActorExecutionError('Expected PamUserDbLocation, but got None')

if msg.locations:
for location in msg.locations:
_convert_db(location)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest
import os

from leapp.exceptions import StopActorExecutionError
from leapp.libraries.actor import convertpamuserdb
from leapp.libraries.stdlib import api, CalledProcessError
from leapp.libraries.common.testutils import logger_mocked

CUR_DIR = os.path.dirname(os.path.abspath(__file__))


def test_convert_db_success(monkeypatch):
location = os.path.join(CUR_DIR, '/files/db1')

def run_mocked(cmd, **kwargs):
assert cmd == ['db_converter', '--src', f'{location}.db', '--dest', f'{location}.gdbm']

monkeypatch.setattr(api, 'current_logger', logger_mocked())
monkeypatch.setattr(convertpamuserdb, 'run', run_mocked)
convertpamuserdb._convert_db(location)
assert len(api.current_logger.errmsg) == 0


def test_convert_db_failure(monkeypatch):
location = os.path.join(CUR_DIR, '/files/db1')

def run_mocked(cmd, **kwargs):
raise CalledProcessError(
message='A Leapp Command Error occurred.',
command=cmd,
result={'exit_code': 1}
)

monkeypatch.setattr(api, 'current_logger', logger_mocked())
monkeypatch.setattr(convertpamuserdb, 'run', run_mocked)
with pytest.raises(StopActorExecutionError) as err:
convertpamuserdb._convert_db(location)
assert str(err.value) == 'Cannot convert pam_userdb database.'

0 comments on commit 8ecacdf

Please sign in to comment.