Skip to content

Commit

Permalink
remove_live_image: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Hecko committed Aug 11, 2024
1 parent b02f2da commit 546280a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os

from leapp.actors import Actor
from leapp.libraries.stdlib import api
from leapp.libraries.actor import remove_live_image as remove_live_image_lib
from leapp.models import LiveModeArtifacts, LiveModeConfig
from leapp.tags import ExperimentalTag, FirstBootPhaseTag, IPUWorkflowTag

Expand All @@ -17,12 +15,4 @@ class RemoveLiveImage(Actor):
tags = (ExperimentalTag, FirstBootPhaseTag, IPUWorkflowTag)

def process(self):
livemode = next(api.consume(LiveModeConfig), None)
if not livemode or not livemode.enabled:
return

artifacts = next(api.consume(LiveModeArtifacts), None)
try:
os.unlink(artifacts.squashfs)
except (FileNotFoundError, PermissionError):
api.current_logger().warning('Cannot remove %s', artifacts.squashfs)
remove_live_image_lib.remove_live_image()
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os

from leapp.libraries.stdlib import api
from leapp.models import LiveModeArtifacts, LiveModeConfig


def remove_live_image():
livemode = next(api.consume(LiveModeConfig), None)
if not livemode or not livemode.is_enabled:
return

artifacts = next(api.consume(LiveModeArtifacts), None)

if not artifacts:
# Livemode is enabled, but we have received no artifacts - this should not happen.
# Anyway, it is futile to sabotage the upgrade this late (after the upgrade transaction)
error_descr = ('Livemode is enabled, but there is no LiveModeArtifacts message. '
'Cannot delete squashfs image (location is unknown)')
api.current_logger().error(error_descr)
return

try:
os.unlink(artifacts.squashfs_path)
except OSError as error:
api.current_logger().warning('Failed to remove %s with error: %s', artifacts.squashfs, error)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import functools

from leapp.libraries.actor import remove_live_image as remove_live_image_lib
from leapp.libraries.stdlib import api
from leapp.libraries.common.testutils import CurrentActorMocked
from leapp.models import LiveModeArtifacts, LiveModeConfig

import pytest


_LiveModeConfig = functools.partial(LiveModeConfig, squashfs_fullpath='configured_path')

@pytest.mark.parametrize(
('livemode_config', 'squashfs_path', 'should_unlink_be_called'),
(
(_LiveModeConfig(is_enabled=True), '/squashfs', True),
(_LiveModeConfig(is_enabled=True), '/var/lib/leapp/upgrade.img', True),
(_LiveModeConfig(is_enabled=False), '/var/lib/leapp/upgrade.img', False),
(None, '/var/lib/leapp/upgrade.img', False),
(_LiveModeConfig(is_enabled=True), None, False),
)
)
def test_remove_live_image(monkeypatch, livemode_config, squashfs_path, should_unlink_be_called):
""" Test whether live-mode image (as found in LiveModeArtifacts) is removed. """

messages = []
if livemode_config:
messages.append(livemode_config)
if squashfs_path:
messages.append(LiveModeArtifacts(squashfs_path=squashfs_path))

monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(msgs=messages))

def unlink_mock(path):
if should_unlink_be_called:
assert path == squashfs_path
return
assert False # If we should not call unlink and we call it then fail the test
monkeypatch.setattr(os, 'unlink', unlink_mock)

monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(msgs=messages))

remove_live_image_lib.remove_live_image()

2 changes: 1 addition & 1 deletion repos/system_upgrade/common/models/livemode.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ class LiveModeArtifacts(Model):
"""
Artifacts created for the Live Mode
"""
squashfs = fields.String()
squashfs_path = fields.String()

0 comments on commit 546280a

Please sign in to comment.