debian-forge/test/mod/test_mixins.py
Michael Vogt f5d6d11f1d osbuild: error when {Device,Mount} is modified after creation
This is a drive-by change after spending some quality time with the
mount code. The `id` field of `Mount` is calculated only once and
only when creating a `Mount`. This seems slightly dangerous as
any change to an attribute after creation will not update the
id. This means two options:
1. dynamically update the `id` on changes
2. forbid changes after the `id` is calculcated

I went with (2) but happy to discuss of course but it seems more
the spirit of the class.

It also does the same change for "devices.Device"
2024-01-19 02:54:26 +01:00

32 lines
932 B
Python

from unittest.mock import Mock
import pytest
from osbuild.devices import Device
from osbuild.meta import ModuleInfo
from osbuild.mounts import Mount
def test_mount_immutable_mixin():
info = Mock(spec=ModuleInfo)
info.name = "some-name"
device = Mock(spec=ModuleInfo)
device.id = "some-id"
partition = 1
target = "/"
opts = {"opt1": 1}
mnt = Mount("name", info, device, partition, target, opts)
with pytest.raises(ValueError) as e:
mnt.name = "new-name"
assert str(e.value) == "cannot set 'name': Mount cannot be changed after creation"
def test_device_immutable_mixins():
info = Mock(spec=ModuleInfo)
info.name = "some-name"
parent = None
opts = {"opt1": 1}
dev = Device("name", info, parent, opts)
with pytest.raises(ValueError) as e:
dev.name = "new-name"
assert str(e.value) == "cannot set 'name': Device cannot be changed after creation"