-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michal Hecko
committed
Aug 11, 2024
1 parent
b02f2da
commit 546280a
Showing
4 changed files
with
73 additions
and
13 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
25 changes: 25 additions & 0 deletions
25
repos/system_upgrade/common/actors/livemode/removeliveimage/libraries/remove_live_image.py
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,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) |
45 changes: 45 additions & 0 deletions
45
repos/system_upgrade/common/actors/livemode/removeliveimage/tests/test_remove_live_image.py
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,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() | ||
|
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