Skip to content

Commit e6e0d45

Browse files
authored
test: Integration tests for ip share (#339)
## 📝 Description Make the tests case for `ip_addresses_share` to be integration test instead of using mocked data. ## ✔️ How to Test `pytest test/integration/models/test_networking.py`
1 parent e2edd71 commit e6e0d45

3 files changed

Lines changed: 81 additions & 32 deletions

File tree

test/fixtures/networking_ips_share.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/integration/helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def get_test_label():
1313
return label
1414

1515

16+
def get_rand_nanosec_test_label():
17+
unique_timestamp = str(time.time_ns())
18+
label = "IntTestSDK_" + unique_timestamp
19+
return label
20+
21+
1622
def delete_instance_with_test_kw(paginated_list: PaginatedList):
1723
for i in paginated_list:
1824
try:
Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from test.integration.helpers import get_rand_nanosec_test_label
2+
13
import pytest
24

3-
from linode_api4.objects import Firewall, IPAddress, IPv6Pool, IPv6Range
5+
from linode_api4.objects import Firewall
46

57

68
@pytest.mark.smoke
@@ -15,37 +17,79 @@ def test_get_networking_rules(get_client, create_firewall):
1517
assert "outbound_policy" in str(rules)
1618

1719

20+
def create_linode(get_client):
21+
client = get_client
22+
available_regions = client.regions()
23+
chosen_region = available_regions[0]
24+
label = get_rand_nanosec_test_label()
25+
26+
linode_instance, password = client.linode.instance_create(
27+
"g6-nanode-1",
28+
chosen_region,
29+
image="linode/debian12",
30+
label=label,
31+
)
32+
33+
return linode_instance
34+
35+
36+
@pytest.fixture
37+
def create_linode_for_ip_share(get_client):
38+
linode = create_linode(get_client)
39+
40+
yield linode
41+
42+
linode.delete()
43+
44+
45+
@pytest.fixture
46+
def create_linode_to_be_shared_with_ips(get_client):
47+
linode = create_linode(get_client)
48+
49+
yield linode
50+
51+
linode.delete()
52+
53+
1854
@pytest.mark.smoke
19-
def test_ip_addresses_share(self):
55+
def test_ip_addresses_share(
56+
get_client, create_linode_for_ip_share, create_linode_to_be_shared_with_ips
57+
):
2058
"""
2159
Test that you can share IP addresses with Linode.
2260
"""
23-
ip_share_url = "/networking/ips/share"
24-
ips = ["127.0.0.1"]
25-
linode_id = 12345
26-
with self.mock_post(ip_share_url) as m:
27-
result = self.client.networking.ip_addresses_share(ips, linode_id)
28-
29-
self.assertIsNotNone(result)
30-
self.assertEqual(m.call_url, ip_share_url)
31-
self.assertEqual(
32-
m.call_data,
33-
{
34-
"ips": ips,
35-
"linode": linode_id,
36-
},
37-
)
38-
39-
# Test that entering an empty IP array is allowed.
40-
with self.mock_post(ip_share_url) as m:
41-
result = self.client.networking.ip_addresses_share([], linode_id)
42-
43-
self.assertIsNotNone(result)
44-
self.assertEqual(m.call_url, ip_share_url)
45-
self.assertEqual(
46-
m.call_data,
47-
{
48-
"ips": [],
49-
"linode": linode_id,
50-
},
51-
)
61+
62+
# create two linode instances and share the ip of instance1 with instance2
63+
linode_instance1 = create_linode_for_ip_share
64+
linode_instance2 = create_linode_to_be_shared_with_ips
65+
66+
get_client.networking.ip_addresses_share(
67+
[linode_instance1.ips.ipv4.public[0]], linode_instance2.id
68+
)
69+
70+
assert (
71+
linode_instance1.ips.ipv4.public[0].address
72+
== linode_instance2.ips.ipv4.shared[0].address
73+
)
74+
75+
76+
@pytest.mark.smoke
77+
def test_ip_addresses_unshare(
78+
get_client, create_linode_for_ip_share, create_linode_to_be_shared_with_ips
79+
):
80+
"""
81+
Test that you can unshare IP addresses with Linode.
82+
"""
83+
84+
# create two linode instances and share the ip of instance1 with instance2
85+
linode_instance1 = create_linode_for_ip_share
86+
linode_instance2 = create_linode_to_be_shared_with_ips
87+
88+
get_client.networking.ip_addresses_share(
89+
[linode_instance1.ips.ipv4.public[0]], linode_instance2.id
90+
)
91+
92+
# unshared the ip with instance2
93+
get_client.networking.ip_addresses_share([], linode_instance2.id)
94+
95+
assert [] == linode_instance2.ips.ipv4.shared

0 commit comments

Comments
 (0)