|
| 1 | +from test.integration.conftest import get_region |
| 2 | +from test.integration.helpers import ( |
| 3 | + get_test_label, |
| 4 | + send_request_when_resource_available, |
| 5 | +) |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from linode_api4.objects import Lock, LockType |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="function") |
| 13 | +def linode_for_lock(test_linode_client, e2e_test_firewall): |
| 14 | + """ |
| 15 | + Create a Linode instance for testing locks. |
| 16 | + """ |
| 17 | + client = test_linode_client |
| 18 | + region = get_region(client, {"Linodes", "Cloud Firewall"}, site_type="core") |
| 19 | + label = get_test_label(length=8) |
| 20 | + |
| 21 | + linode_instance, _ = client.linode.instance_create( |
| 22 | + "g6-nanode-1", |
| 23 | + region, |
| 24 | + image="linode/debian12", |
| 25 | + label=label, |
| 26 | + firewall=e2e_test_firewall, |
| 27 | + ) |
| 28 | + |
| 29 | + yield linode_instance |
| 30 | + |
| 31 | + # Clean up any locks on the Linode before deleting it |
| 32 | + locks = client.locks() |
| 33 | + for lock in locks: |
| 34 | + if ( |
| 35 | + lock.entity.id == linode_instance.id |
| 36 | + and lock.entity.type == "linode" |
| 37 | + ): |
| 38 | + lock.delete() |
| 39 | + |
| 40 | + send_request_when_resource_available( |
| 41 | + timeout=100, func=linode_instance.delete |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(scope="function") |
| 46 | +def test_lock(test_linode_client, linode_for_lock): |
| 47 | + """ |
| 48 | + Create a lock for testing. |
| 49 | + """ |
| 50 | + lock = test_linode_client.locks.create( |
| 51 | + entity_type="linode", |
| 52 | + entity_id=linode_for_lock.id, |
| 53 | + lock_type=LockType.cannot_delete, |
| 54 | + ) |
| 55 | + |
| 56 | + yield lock |
| 57 | + |
| 58 | + # Clean up lock if it still exists |
| 59 | + try: |
| 60 | + lock.delete() |
| 61 | + except Exception: |
| 62 | + pass # Lock may have been deleted by the test |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.smoke |
| 66 | +def test_get_lock(test_linode_client, test_lock): |
| 67 | + """ |
| 68 | + Test that a lock can be retrieved by ID. |
| 69 | + """ |
| 70 | + lock = test_linode_client.load(Lock, test_lock.id) |
| 71 | + |
| 72 | + assert lock.id == test_lock.id |
| 73 | + assert lock.lock_type == "cannot_delete" |
| 74 | + assert lock.entity is not None |
| 75 | + assert lock.entity.type == "linode" |
| 76 | + |
| 77 | + |
| 78 | +def test_list_locks(test_linode_client, test_lock): |
| 79 | + """ |
| 80 | + Test that locks can be listed. |
| 81 | + """ |
| 82 | + locks = test_linode_client.locks() |
| 83 | + |
| 84 | + assert len(locks) > 0 |
| 85 | + |
| 86 | + # Verify our test lock is in the list |
| 87 | + lock_ids = [lock.id for lock in locks] |
| 88 | + assert test_lock.id in lock_ids |
| 89 | + |
| 90 | + |
| 91 | +def test_create_lock_cannot_delete(test_linode_client, linode_for_lock): |
| 92 | + """ |
| 93 | + Test creating a cannot_delete lock. |
| 94 | + """ |
| 95 | + lock = test_linode_client.locks.create( |
| 96 | + entity_type="linode", |
| 97 | + entity_id=linode_for_lock.id, |
| 98 | + lock_type=LockType.cannot_delete, |
| 99 | + ) |
| 100 | + |
| 101 | + assert lock.id is not None |
| 102 | + assert lock.lock_type == "cannot_delete" |
| 103 | + assert lock.entity.id == linode_for_lock.id |
| 104 | + assert lock.entity.type == "linode" |
| 105 | + assert lock.entity.label == linode_for_lock.label |
| 106 | + |
| 107 | + # Clean up |
| 108 | + lock.delete() |
| 109 | + |
| 110 | + |
| 111 | +def test_create_lock_cannot_delete_with_subresources( |
| 112 | + test_linode_client, linode_for_lock |
| 113 | +): |
| 114 | + """ |
| 115 | + Test creating a cannot_delete_with_subresources lock. |
| 116 | + """ |
| 117 | + lock = test_linode_client.locks.create( |
| 118 | + entity_type="linode", |
| 119 | + entity_id=linode_for_lock.id, |
| 120 | + lock_type=LockType.cannot_delete_with_subresources, |
| 121 | + ) |
| 122 | + |
| 123 | + assert lock.id is not None |
| 124 | + assert lock.lock_type == "cannot_delete_with_subresources" |
| 125 | + assert lock.entity.id == linode_for_lock.id |
| 126 | + assert lock.entity.type == "linode" |
| 127 | + |
| 128 | + # Clean up |
| 129 | + lock.delete() |
| 130 | + |
| 131 | + |
| 132 | +def test_delete_lock(test_linode_client, linode_for_lock): |
| 133 | + """ |
| 134 | + Test that a lock can be deleted. |
| 135 | + """ |
| 136 | + # Create a lock |
| 137 | + lock = test_linode_client.locks.create( |
| 138 | + entity_type="linode", |
| 139 | + entity_id=linode_for_lock.id, |
| 140 | + lock_type=LockType.cannot_delete, |
| 141 | + ) |
| 142 | + |
| 143 | + lock_id = lock.id |
| 144 | + |
| 145 | + # Delete the lock using the group method |
| 146 | + result = test_linode_client.locks.delete(lock) |
| 147 | + |
| 148 | + assert result is True |
| 149 | + |
| 150 | + # Verify the lock no longer exists |
| 151 | + locks = test_linode_client.locks() |
| 152 | + lock_ids = [l.id for l in locks] |
| 153 | + assert lock_id not in lock_ids |
| 154 | + |
| 155 | + |
| 156 | +def test_delete_lock_by_id(test_linode_client, linode_for_lock): |
| 157 | + """ |
| 158 | + Test that a lock can be deleted by ID. |
| 159 | + """ |
| 160 | + # Create a lock |
| 161 | + lock = test_linode_client.locks.create( |
| 162 | + entity_type="linode", |
| 163 | + entity_id=linode_for_lock.id, |
| 164 | + lock_type=LockType.cannot_delete, |
| 165 | + ) |
| 166 | + |
| 167 | + lock_id = lock.id |
| 168 | + |
| 169 | + # Delete the lock by ID |
| 170 | + result = test_linode_client.locks.delete(lock_id) |
| 171 | + |
| 172 | + assert result is True |
| 173 | + |
| 174 | + # Verify the lock no longer exists |
| 175 | + locks = test_linode_client.locks() |
| 176 | + lock_ids = [l.id for l in locks] |
| 177 | + assert lock_id not in lock_ids |
| 178 | + |
| 179 | + |
| 180 | +def test_lock_object_delete(test_linode_client, linode_for_lock): |
| 181 | + """ |
| 182 | + Test that a lock can be deleted using the Lock object's delete method. |
| 183 | + """ |
| 184 | + # Create a lock |
| 185 | + lock = test_linode_client.locks.create( |
| 186 | + entity_type="linode", |
| 187 | + entity_id=linode_for_lock.id, |
| 188 | + lock_type=LockType.cannot_delete, |
| 189 | + ) |
| 190 | + |
| 191 | + lock_id = lock.id |
| 192 | + |
| 193 | + # Delete the lock using the object method |
| 194 | + lock.delete() |
| 195 | + |
| 196 | + # Verify the lock no longer exists |
| 197 | + locks = test_linode_client.locks() |
| 198 | + lock_ids = [l.id for l in locks] |
| 199 | + assert lock_id not in lock_ids |
0 commit comments