|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from common.allocation import Allocation, APIAllocation |
7 | | -from common.exceptions import InvalidBlockUUIDError |
| 7 | +from common.exceptions import InvalidBlockUUIDError, InvalidPSRDIndex |
8 | 8 | from common.fragment import APIFragment, Fragment |
9 | 9 | from .unit_test_common import create_test_block, create_test_pool_and_blocks |
10 | 10 |
|
@@ -102,11 +102,28 @@ def test_from_api_success(): |
102 | 102 |
|
103 | 103 | def test_from_api_bad_fragment(): |
104 | 104 | """ |
105 | | - Attempt to create an Allocation from a valid APIAllocation: one of the fragments has an invalid |
| 105 | + Attempt to create an Allocation from a bad APIAllocation: one of the fragments has an invalid |
106 | 106 | block UUID. |
107 | 107 | """ |
108 | 108 | pool, _blocks = create_test_pool_and_blocks([10]) |
109 | 109 | api_fragment = APIFragment(block_uuid="not-a-uuid", start=0, size=5) |
110 | 110 | _api_allocation = APIAllocation(fragments=[api_fragment]) |
111 | 111 | with pytest.raises(InvalidBlockUUIDError): |
112 | 112 | _allocation = Allocation.from_api(_api_allocation, pool) |
| 113 | + |
| 114 | + |
| 115 | +def test_from_api_second_fragment_is_bad(): |
| 116 | + """ |
| 117 | + Attempt to create an Allocation from bad APIAllocations. The first APIFragment is valid. The |
| 118 | + second has an invalid block size. Make sure the first fragment is properly given back to the |
| 119 | + pool (all-or-nothing behavior). |
| 120 | + """ |
| 121 | + # pylint: disable=protected-access |
| 122 | + pool, blocks = create_test_pool_and_blocks([10]) |
| 123 | + api_fragment_1 = APIFragment(block_uuid=str(blocks[0].uuid), start=0, size=5) |
| 124 | + api_fragment_2 = APIFragment(block_uuid=str(blocks[0].uuid), start=5, size=99999) |
| 125 | + _api_allocation = APIAllocation(fragments=[api_fragment_1, api_fragment_2]) |
| 126 | + with pytest.raises(InvalidPSRDIndex): |
| 127 | + _allocation = Allocation.from_api(_api_allocation, pool) |
| 128 | + assert pool.nr_used_bytes == 0 |
| 129 | + assert blocks[0]._data == bytes.fromhex("00010203040506070809") |
0 commit comments