|
| 1 | +"""Asynchronous SDK smoke tests for Network Policy operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from runloop_api_client.sdk import AsyncRunloopSDK |
| 8 | +from tests.smoketests.utils import unique_name |
| 9 | + |
| 10 | +pytestmark = [pytest.mark.smoketest, pytest.mark.asyncio] |
| 11 | + |
| 12 | +THIRTY_SECOND_TIMEOUT = 30 |
| 13 | + |
| 14 | + |
| 15 | +class TestAsyncNetworkPolicyLifecycle: |
| 16 | + """Test basic async network policy lifecycle operations.""" |
| 17 | + |
| 18 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 19 | + async def test_network_policy_create(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 20 | + """Test creating a network policy.""" |
| 21 | + name = unique_name("sdk-async-network-policy") |
| 22 | + network_policy = await async_sdk_client.network_policy.create( |
| 23 | + name=name, |
| 24 | + description="SDK async smoke test network policy", |
| 25 | + allowed_hostnames=["github.com", "*.npmjs.org"], |
| 26 | + ) |
| 27 | + |
| 28 | + try: |
| 29 | + assert network_policy is not None |
| 30 | + assert network_policy.id is not None |
| 31 | + assert len(network_policy.id) > 0 |
| 32 | + finally: |
| 33 | + await network_policy.delete() |
| 34 | + |
| 35 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 36 | + async def test_network_policy_get_info(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 37 | + """Test retrieving network policy information.""" |
| 38 | + name = unique_name("sdk-async-network-policy-info") |
| 39 | + network_policy = await async_sdk_client.network_policy.create( |
| 40 | + name=name, |
| 41 | + description="Async test policy for get_info", |
| 42 | + allowed_hostnames=["example.com"], |
| 43 | + ) |
| 44 | + |
| 45 | + try: |
| 46 | + info = await network_policy.get_info() |
| 47 | + |
| 48 | + assert info.id == network_policy.id |
| 49 | + assert info.name == name |
| 50 | + assert info.description == "Async test policy for get_info" |
| 51 | + finally: |
| 52 | + await network_policy.delete() |
| 53 | + |
| 54 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 55 | + async def test_network_policy_update(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 56 | + """Test updating a network policy.""" |
| 57 | + network_policy = await async_sdk_client.network_policy.create( |
| 58 | + name=unique_name("sdk-async-network-policy-update"), |
| 59 | + description="Original async description", |
| 60 | + allowed_hostnames=["example.com"], |
| 61 | + ) |
| 62 | + |
| 63 | + try: |
| 64 | + # Update the policy |
| 65 | + updated_name = unique_name("sdk-async-network-policy-updated") |
| 66 | + result = await network_policy.update( |
| 67 | + name=updated_name, |
| 68 | + description="Updated async description", |
| 69 | + allowed_hostnames=["example.com", "api.example.com"], |
| 70 | + ) |
| 71 | + |
| 72 | + assert result is not None |
| 73 | + assert result.name == updated_name |
| 74 | + assert result.description == "Updated async description" |
| 75 | + |
| 76 | + # Verify update persisted |
| 77 | + info = await network_policy.get_info() |
| 78 | + assert info.name == updated_name |
| 79 | + assert info.description == "Updated async description" |
| 80 | + finally: |
| 81 | + await network_policy.delete() |
| 82 | + |
| 83 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 84 | + async def test_network_policy_delete(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 85 | + """Test deleting a network policy.""" |
| 86 | + network_policy = await async_sdk_client.network_policy.create( |
| 87 | + name=unique_name("sdk-async-network-policy-delete"), |
| 88 | + allowed_hostnames=["example.com"], |
| 89 | + ) |
| 90 | + |
| 91 | + policy_id = network_policy.id |
| 92 | + result = await network_policy.delete() |
| 93 | + |
| 94 | + assert result is not None |
| 95 | + # Verify it's deleted |
| 96 | + info = await async_sdk_client.api.network_policies.retrieve(policy_id) |
| 97 | + assert info.id == policy_id |
| 98 | + |
| 99 | + |
| 100 | +class TestAsyncNetworkPolicyCreationVariations: |
| 101 | + """Test different async network policy creation scenarios.""" |
| 102 | + |
| 103 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 104 | + async def test_network_policy_with_allow_all(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 105 | + """Test creating a network policy with allow_all enabled.""" |
| 106 | + network_policy = await async_sdk_client.network_policy.create( |
| 107 | + name=unique_name("sdk-async-network-policy-allow-all"), |
| 108 | + description="Allow all egress traffic", |
| 109 | + allow_all=True, |
| 110 | + ) |
| 111 | + |
| 112 | + try: |
| 113 | + info = await network_policy.get_info() |
| 114 | + assert info.egress is not None |
| 115 | + assert info.egress.allow_all is True |
| 116 | + finally: |
| 117 | + await network_policy.delete() |
| 118 | + |
| 119 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 120 | + async def test_network_policy_with_devbox_to_devbox(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 121 | + """Test creating a network policy with devbox-to-devbox traffic allowed.""" |
| 122 | + network_policy = await async_sdk_client.network_policy.create( |
| 123 | + name=unique_name("sdk-async-network-policy-d2d"), |
| 124 | + description="Allow devbox to devbox traffic", |
| 125 | + allow_devbox_to_devbox=True, |
| 126 | + allowed_hostnames=["internal.example.com"], |
| 127 | + ) |
| 128 | + |
| 129 | + try: |
| 130 | + info = await network_policy.get_info() |
| 131 | + assert info.egress is not None |
| 132 | + assert info.egress.allow_devbox_to_devbox is True |
| 133 | + finally: |
| 134 | + await network_policy.delete() |
| 135 | + |
| 136 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 137 | + async def test_network_policy_with_wildcard_hostnames(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 138 | + """Test creating a network policy with wildcard hostname patterns.""" |
| 139 | + network_policy = await async_sdk_client.network_policy.create( |
| 140 | + name=unique_name("sdk-async-network-policy-wildcards"), |
| 141 | + allowed_hostnames=[ |
| 142 | + "*.github.com", |
| 143 | + "*.githubusercontent.com", |
| 144 | + "registry.npmjs.org", |
| 145 | + "*.pypi.org", |
| 146 | + ], |
| 147 | + ) |
| 148 | + |
| 149 | + try: |
| 150 | + info = await network_policy.get_info() |
| 151 | + assert info.egress is not None |
| 152 | + assert info.egress.allowed_hostnames is not None |
| 153 | + assert len(info.egress.allowed_hostnames) == 4 |
| 154 | + finally: |
| 155 | + await network_policy.delete() |
| 156 | + |
| 157 | + |
| 158 | +class TestAsyncNetworkPolicyListing: |
| 159 | + """Test async network policy listing and retrieval operations.""" |
| 160 | + |
| 161 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 162 | + async def test_list_network_policies(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 163 | + """Test listing network policies.""" |
| 164 | + policies = await async_sdk_client.network_policy.list(limit=10) |
| 165 | + |
| 166 | + assert isinstance(policies, list) |
| 167 | + # List might be empty, that's okay |
| 168 | + assert len(policies) >= 0 |
| 169 | + |
| 170 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 171 | + async def test_get_network_policy_by_id(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 172 | + """Test retrieving network policy by ID.""" |
| 173 | + # Create a policy |
| 174 | + created = await async_sdk_client.network_policy.create( |
| 175 | + name=unique_name("sdk-async-network-policy-retrieve"), |
| 176 | + allowed_hostnames=["example.com"], |
| 177 | + ) |
| 178 | + |
| 179 | + try: |
| 180 | + # Retrieve it by ID |
| 181 | + retrieved = async_sdk_client.network_policy.from_id(created.id) |
| 182 | + assert retrieved.id == created.id |
| 183 | + |
| 184 | + # Verify it's the same policy |
| 185 | + info = await retrieved.get_info() |
| 186 | + assert info.id == created.id |
| 187 | + finally: |
| 188 | + await created.delete() |
| 189 | + |
| 190 | + @pytest.mark.timeout(THIRTY_SECOND_TIMEOUT) |
| 191 | + async def test_list_network_policies_with_limit(self, async_sdk_client: AsyncRunloopSDK) -> None: |
| 192 | + """Test listing network policies with a limit.""" |
| 193 | + # Create two policies |
| 194 | + policy1 = await async_sdk_client.network_policy.create( |
| 195 | + name=unique_name("sdk-async-network-policy-list-1"), |
| 196 | + allowed_hostnames=["example1.com"], |
| 197 | + ) |
| 198 | + policy2 = await async_sdk_client.network_policy.create( |
| 199 | + name=unique_name("sdk-async-network-policy-list-2"), |
| 200 | + allowed_hostnames=["example2.com"], |
| 201 | + ) |
| 202 | + |
| 203 | + try: |
| 204 | + # List with limit |
| 205 | + policies = await async_sdk_client.network_policy.list(limit=100) |
| 206 | + |
| 207 | + assert isinstance(policies, list) |
| 208 | + # Should find our policies |
| 209 | + policy_ids = [p.id for p in policies] |
| 210 | + assert policy1.id in policy_ids |
| 211 | + assert policy2.id in policy_ids |
| 212 | + finally: |
| 213 | + await policy1.delete() |
| 214 | + await policy2.delete() |
0 commit comments