File: test_client.py

package info (click to toggle)
hcloud-python 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,396 kB
  • sloc: python: 15,311; makefile: 43; javascript: 3
file content (594 lines) | stat: -rw-r--r-- 22,039 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
from __future__ import annotations

from unittest import mock

import pytest

from hcloud.actions import BoundAction
from hcloud.load_balancer_types import LoadBalancerType
from hcloud.load_balancers import (
    BoundLoadBalancer,
    LoadBalancer,
    LoadBalancerAlgorithm,
    LoadBalancerHealthCheck,
    LoadBalancersClient,
    LoadBalancerService,
    LoadBalancerTarget,
    LoadBalancerTargetIP,
    LoadBalancerTargetLabelSelector,
)
from hcloud.locations import Location
from hcloud.networks import Network
from hcloud.servers import Server


class TestBoundLoadBalancer:
    @pytest.fixture()
    def bound_load_balancer(self, hetzner_client):
        return BoundLoadBalancer(client=hetzner_client.load_balancers, data=dict(id=14))

    def test_bound_load_balancer_init(self, response_load_balancer):
        bound_load_balancer = BoundLoadBalancer(
            client=mock.MagicMock(), data=response_load_balancer["load_balancer"]
        )

        assert bound_load_balancer.id == 4711
        assert bound_load_balancer.name == "Web Frontend"

    @pytest.mark.parametrize("params", [{"page": 1, "per_page": 10}, {}])
    def test_get_actions_list(
        self, hetzner_client, bound_load_balancer, response_get_actions, params
    ):
        hetzner_client.request.return_value = response_get_actions
        result = bound_load_balancer.get_actions_list(**params)
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions", method="GET", params=params
        )

        actions = result.actions
        assert result.meta is None

        assert len(actions) == 1
        assert isinstance(actions[0], BoundAction)
        assert actions[0]._client == hetzner_client.actions
        assert actions[0].id == 13
        assert actions[0].command == "change_protection"

    @pytest.mark.parametrize("params", [{}])
    def test_get_actions(
        self, hetzner_client, bound_load_balancer, response_get_actions, params
    ):
        hetzner_client.request.return_value = response_get_actions
        actions = bound_load_balancer.get_actions(**params)

        params.update({"page": 1, "per_page": 50})

        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions", method="GET", params=params
        )

        assert len(actions) == 1
        assert isinstance(actions[0], BoundAction)
        assert actions[0]._client == hetzner_client.actions
        assert actions[0].id == 13
        assert actions[0].command == "change_protection"

    def test_update(
        self, hetzner_client, bound_load_balancer, response_update_load_balancer
    ):
        hetzner_client.request.return_value = response_update_load_balancer
        load_balancer = bound_load_balancer.update(name="new-name", labels={})
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14",
            method="PUT",
            json={"name": "new-name", "labels": {}},
        )

        assert load_balancer.id == 4711
        assert load_balancer.name == "new-name"

    def test_delete(self, hetzner_client, generic_action, bound_load_balancer):
        hetzner_client.request.return_value = generic_action
        delete_success = bound_load_balancer.delete()
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14", method="DELETE"
        )

        assert delete_success is True

    def test_get_metrics(
        self,
        hetzner_client,
        response_get_metrics,
        bound_load_balancer: BoundLoadBalancer,
    ):
        hetzner_client.request.return_value = response_get_metrics
        response = bound_load_balancer.get_metrics(
            type=["requests_per_second"],
            start="2023-12-14T16:55:32+01:00",
            end="2023-12-14T16:55:32+01:00",
        )
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/metrics",
            method="GET",
            params={
                "type": "requests_per_second",
                "start": "2023-12-14T16:55:32+01:00",
                "end": "2023-12-14T16:55:32+01:00",
            },
        )
        assert "requests_per_second" in response.metrics.time_series
        assert len(response.metrics.time_series["requests_per_second"]["values"]) == 3

    def test_add_service(
        self, hetzner_client, response_add_service, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_add_service
        service = LoadBalancerService(listen_port=80, protocol="http")
        action = bound_load_balancer.add_service(service)
        hetzner_client.request.assert_called_with(
            json={"protocol": "http", "listen_port": 80},
            url="/load_balancers/14/actions/add_service",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "add_service"

    def test_delete_service(
        self, hetzner_client, response_delete_service, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_delete_service
        service = LoadBalancerService(listen_port=12)
        action = bound_load_balancer.delete_service(service)
        hetzner_client.request.assert_called_with(
            json={"listen_port": 12},
            url="/load_balancers/14/actions/delete_service",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "delete_service"

    @pytest.mark.parametrize(
        "target,params",
        [
            (
                LoadBalancerTarget(
                    type="server", server=Server(id=1), use_private_ip=True
                ),
                {"server": {"id": 1}, "use_private_ip": True},
            ),
            (
                LoadBalancerTarget(type="ip", ip=LoadBalancerTargetIP(ip="127.0.0.1")),
                {"ip": {"ip": "127.0.0.1"}},
            ),
            (
                LoadBalancerTarget(
                    type="label_selector",
                    label_selector=LoadBalancerTargetLabelSelector(selector="abc=def"),
                ),
                {"label_selector": {"selector": "abc=def"}},
            ),
        ],
    )
    def test_add_target(
        self, hetzner_client, response_add_target, bound_load_balancer, target, params
    ):
        hetzner_client.request.return_value = response_add_target
        action = bound_load_balancer.add_target(target)
        params.update({"type": target.type})
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions/add_target", method="POST", json=params
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "add_target"

    @pytest.mark.parametrize(
        "target,params",
        [
            (
                LoadBalancerTarget(
                    type="server", server=Server(id=1), use_private_ip=True
                ),
                {"server": {"id": 1}},
            ),
            (
                LoadBalancerTarget(type="ip", ip=LoadBalancerTargetIP(ip="127.0.0.1")),
                {"ip": {"ip": "127.0.0.1"}},
            ),
            (
                LoadBalancerTarget(
                    type="label_selector",
                    label_selector=LoadBalancerTargetLabelSelector(selector="abc=def"),
                ),
                {"label_selector": {"selector": "abc=def"}},
            ),
        ],
    )
    def test_remove_target(
        self,
        hetzner_client,
        response_remove_target,
        bound_load_balancer,
        target,
        params,
    ):
        hetzner_client.request.return_value = response_remove_target
        action = bound_load_balancer.remove_target(target)
        params.update({"type": target.type})
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions/remove_target", method="POST", json=params
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "remove_target"

    def test_update_service(
        self, hetzner_client, response_update_service, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_update_service
        new_health_check = LoadBalancerHealthCheck(
            protocol="http", port=13, interval=1, timeout=1, retries=1
        )
        service = LoadBalancerService(listen_port=12, health_check=new_health_check)

        action = bound_load_balancer.update_service(service)
        hetzner_client.request.assert_called_with(
            json={
                "listen_port": 12,
                "health_check": {
                    "protocol": "http",
                    "port": 13,
                    "interval": 1,
                    "timeout": 1,
                    "retries": 1,
                },
            },
            url="/load_balancers/14/actions/update_service",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "update_service"

    def test_change_algorithm(
        self, hetzner_client, response_change_algorithm, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_change_algorithm
        algorithm = LoadBalancerAlgorithm(type="round_robin")
        action = bound_load_balancer.change_algorithm(algorithm)
        hetzner_client.request.assert_called_with(
            json={"type": "round_robin"},
            url="/load_balancers/14/actions/change_algorithm",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "change_algorithm"

    def test_change_dns_ptr(
        self, hetzner_client, response_change_reverse_dns_entry, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_change_reverse_dns_entry
        action = bound_load_balancer.change_dns_ptr(
            ip="1.2.3.4", dns_ptr="lb1.example.com"
        )
        hetzner_client.request.assert_called_with(
            json={"dns_ptr": "lb1.example.com", "ip": "1.2.3.4"},
            url="/load_balancers/14/actions/change_dns_ptr",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "change_dns_ptr"

    def test_change_protection(
        self, hetzner_client, response_change_protection, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_change_protection
        action = bound_load_balancer.change_protection(delete=True)
        hetzner_client.request.assert_called_with(
            json={"delete": True},
            url="/load_balancers/14/actions/change_protection",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "change_protection"

    def test_enable_public_interface(
        self, response_enable_public_interface, hetzner_client, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_enable_public_interface
        action = bound_load_balancer.enable_public_interface()
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions/enable_public_interface", method="POST"
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "enable_public_interface"

    def test_disable_public_interface(
        self, response_disable_public_interface, hetzner_client, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_disable_public_interface
        action = bound_load_balancer.disable_public_interface()
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions/disable_public_interface", method="POST"
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "disable_public_interface"

    def test_attach_to_network(
        self,
        response_attach_load_balancer_to_network,
        hetzner_client,
        bound_load_balancer,
    ):
        hetzner_client.request.return_value = response_attach_load_balancer_to_network
        action = bound_load_balancer.attach_to_network(Network(id=1))
        hetzner_client.request.assert_called_with(
            json={"network": 1},
            url="/load_balancers/14/actions/attach_to_network",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "attach_to_network"

    def test_detach_from_network(
        self, response_detach_from_network, hetzner_client, bound_load_balancer
    ):
        hetzner_client.request.return_value = response_detach_from_network
        action = bound_load_balancer.detach_from_network(Network(id=1))
        hetzner_client.request.assert_called_with(
            json={"network": 1},
            url="/load_balancers/14/actions/detach_from_network",
            method="POST",
        )

        assert action.id == 13
        assert action.progress == 100
        assert action.command == "detach_from_network"

    def test_change_type(self, hetzner_client, bound_load_balancer, generic_action):
        hetzner_client.request.return_value = generic_action
        action = bound_load_balancer.change_type(LoadBalancerType(name="lb21"))
        hetzner_client.request.assert_called_with(
            url="/load_balancers/14/actions/change_type",
            method="POST",
            json={"load_balancer_type": "lb21"},
        )

        assert action.id == 1
        assert action.progress == 0


class TestLoadBalancerslient:
    @pytest.fixture()
    def load_balancers_client(self):
        return LoadBalancersClient(client=mock.MagicMock())

    def test_get_by_id(self, load_balancers_client, response_load_balancer):
        load_balancers_client._client.request.return_value = response_load_balancer
        bound_load_balancer = load_balancers_client.get_by_id(1)
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/1", method="GET"
        )
        assert bound_load_balancer._client is load_balancers_client
        assert bound_load_balancer.id == 4711
        assert bound_load_balancer.name == "Web Frontend"
        assert bound_load_balancer.outgoing_traffic == 123456
        assert bound_load_balancer.ingoing_traffic == 123456
        assert bound_load_balancer.included_traffic == 654321

    @pytest.mark.parametrize(
        "params",
        [
            {
                "name": "load_balancer1",
                "label_selector": "label1",
                "page": 1,
                "per_page": 10,
            },
            {"name": ""},
            {},
        ],
    )
    def test_get_list(
        self, load_balancers_client, response_simple_load_balancers, params
    ):
        load_balancers_client._client.request.return_value = (
            response_simple_load_balancers
        )
        result = load_balancers_client.get_list(**params)
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers", method="GET", params=params
        )

        bound_load_balancers = result.load_balancers
        assert result.meta is None

        assert len(bound_load_balancers) == 2

        bound_load_balancer1 = bound_load_balancers[0]
        bound_load_balancer2 = bound_load_balancers[1]

        assert bound_load_balancer1._client is load_balancers_client
        assert bound_load_balancer1.id == 4711
        assert bound_load_balancer1.name == "Web Frontend"

        assert bound_load_balancer2._client is load_balancers_client
        assert bound_load_balancer2.id == 4712
        assert bound_load_balancer2.name == "Web Frontend2"

    @pytest.mark.parametrize(
        "params", [{"name": "loadbalancer1", "label_selector": "label1"}, {}]
    )
    def test_get_all(
        self, load_balancers_client, response_simple_load_balancers, params
    ):
        load_balancers_client._client.request.return_value = (
            response_simple_load_balancers
        )
        bound_load_balancers = load_balancers_client.get_all(**params)

        params.update({"page": 1, "per_page": 50})

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers", method="GET", params=params
        )

        assert len(bound_load_balancers) == 2

        bound_load_balancer1 = bound_load_balancers[0]
        bound_load_balancer2 = bound_load_balancers[1]

        assert bound_load_balancer1._client is load_balancers_client
        assert bound_load_balancer1.id == 4711
        assert bound_load_balancer1.name == "Web Frontend"

        assert bound_load_balancer2._client is load_balancers_client
        assert bound_load_balancer2.id == 4712
        assert bound_load_balancer2.name == "Web Frontend2"

    def test_get_by_name(self, load_balancers_client, response_simple_load_balancers):
        load_balancers_client._client.request.return_value = (
            response_simple_load_balancers
        )
        bound_load_balancer = load_balancers_client.get_by_name("Web Frontend")

        params = {"name": "Web Frontend"}

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers", method="GET", params=params
        )

        assert bound_load_balancer._client is load_balancers_client
        assert bound_load_balancer.id == 4711
        assert bound_load_balancer.name == "Web Frontend"

    def test_create(self, load_balancers_client, response_create_load_balancer):
        load_balancers_client._client.request.return_value = (
            response_create_load_balancer
        )
        response = load_balancers_client.create(
            "my-balancer",
            load_balancer_type=LoadBalancerType(name="lb11"),
            location=Location(id=1),
        )
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers",
            method="POST",
            json={"name": "my-balancer", "load_balancer_type": "lb11", "location": 1},
        )

        bound_load_balancer = response.load_balancer

        assert bound_load_balancer._client is load_balancers_client
        assert bound_load_balancer.id == 1
        assert bound_load_balancer.name == "my-balancer"

    @pytest.mark.parametrize(
        "load_balancer",
        [LoadBalancer(id=1), BoundLoadBalancer(mock.MagicMock(), dict(id=1))],
    )
    def test_change_type_with_load_balancer_type_name(
        self, load_balancers_client, load_balancer, generic_action
    ):
        load_balancers_client._client.request.return_value = generic_action
        action = load_balancers_client.change_type(
            load_balancer, LoadBalancerType(name="lb11")
        )
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/1/actions/change_type",
            method="POST",
            json={"load_balancer_type": "lb11"},
        )

        assert action.id == 1
        assert action.progress == 0

    @pytest.mark.parametrize(
        "load_balancer",
        [LoadBalancer(id=1), BoundLoadBalancer(mock.MagicMock(), dict(id=1))],
    )
    def test_change_type_with_load_balancer_type_id(
        self, load_balancers_client, load_balancer, generic_action
    ):
        load_balancers_client._client.request.return_value = generic_action
        action = load_balancers_client.change_type(
            load_balancer, LoadBalancerType(id=1)
        )
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/1/actions/change_type",
            method="POST",
            json={"load_balancer_type": 1},
        )

        assert action.id == 1
        assert action.progress == 0

    def test_actions_get_by_id(self, load_balancers_client, response_get_actions):
        load_balancers_client._client.request.return_value = {
            "action": response_get_actions["actions"][0]
        }
        action = load_balancers_client.actions.get_by_id(13)

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/actions/13", method="GET"
        )

        assert isinstance(action, BoundAction)
        assert action._client == load_balancers_client._client.actions
        assert action.id == 13
        assert action.command == "change_protection"

    def test_actions_get_list(self, load_balancers_client, response_get_actions):
        load_balancers_client._client.request.return_value = response_get_actions
        result = load_balancers_client.actions.get_list()

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/actions",
            method="GET",
            params={},
        )

        actions = result.actions
        assert result.meta is None

        assert len(actions) == 1
        assert isinstance(actions[0], BoundAction)
        assert actions[0]._client == load_balancers_client._client.actions
        assert actions[0].id == 13
        assert actions[0].command == "change_protection"

    def test_actions_get_all(self, load_balancers_client, response_get_actions):
        load_balancers_client._client.request.return_value = response_get_actions
        actions = load_balancers_client.actions.get_all()

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/actions",
            method="GET",
            params={"page": 1, "per_page": 50},
        )

        assert len(actions) == 1
        assert isinstance(actions[0], BoundAction)
        assert actions[0]._client == load_balancers_client._client.actions
        assert actions[0].id == 13
        assert actions[0].command == "change_protection"