Debian Codegraph @

ansible_collections/hetzner/hcloud/tests/unit/inventory/test_hcloud.py

raw
1 from __future__ import annotations
2
3 import json
4 from unittest.mock import MagicMock
5
6 from plugins.inventory.hcloud import InventoryModule, first_ipv6_address
7 from plugins.module_utils._vendor.hcloud.servers import BoundServer
8
9
10 def test_first_ipv6_address():
11 found = first_ipv6_address("2001:db8::/64")
12 assert isinstance(found, str)
13 assert found == "2001:db8::1"
14
15
16 def test_build_inventory_server():
17 client = MagicMock()
18 inventory = InventoryModule()
19 inventory.get_option = MagicMock()
20 inventory.get_option.return_value = None
21
22 server = BoundServer(
23 client,
24 {
25 "id": 45921624,
26 "name": "my-server",
27 "labels": {},
28 "status": "running",
29 "public_net": {
30 "ipv4": {
31 "id": 56583278,
32 "ip": "127.0.0.1",
33 "blocked": False,
34 "dns_ptr": "static.1.0.0.127.clients.your-server.de",
35 },
36 "ipv6": {
37 "id": 56583279,
38 "ip": "2001:db8::/64",
39 "blocked": False,
40 "dns_ptr": [],
41 },
42 "floating_ips": [],
43 "firewalls": [],
44 },
45 "private_net": [],
46 "server_type": {
47 "id": 109,
48 "name": "cpx22",
49 "architecture": "x86",
50 },
51 "location": {
52 "id": 3,
53 "name": "hel1",
54 },
55 "datacenter": {
56 "id": 3,
57 "name": "hel1-dc2",
58 "location": {
59 "id": 3,
60 "name": "hel1",
61 },
62 },
63 "image": {
64 "id": 114690387,
65 "name": "debian-12",
66 "os_flavor": "debian",
67 "os_version": "12",
68 },
69 },
70 )
71 # pylint: disable=protected-access
72 variables = inventory._build_inventory_server(server)
73
74 # Ensure the host_vars are json serializable
75 json.dumps(variables)
76
77 assert variables == {
78 "id": 45921624,
79 "name": "my-server",
80 "status": "running",
81 "type": "cpx22",
82 "server_type": "cpx22",
83 "architecture": "x86",
84 "location": "hel1",
85 "datacenter": "hel1-dc2",
86 "labels": {},
87 "ipv4": "127.0.0.1",
88 "ipv6": "2001:db8::1",
89 "ipv6_network": "2001:db8::",
90 "ipv6_network_mask": "64",
91 "private_networks": [],
92 "image_id": 114690387,
93 "image_name": "debian-12",
94 "image_os_flavor": "debian",
95 "ansible_host": None,
96 }