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 (362 lines) | stat: -rw-r--r-- 12,834 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
from __future__ import annotations

import datetime
from datetime import timezone
from unittest import mock

import pytest

from hcloud.actions import BoundAction
from hcloud.images import BoundImage, Image, ImagesClient
from hcloud.servers import BoundServer


class TestBoundImage:
    @pytest.fixture()
    def bound_image(self, hetzner_client):
        return BoundImage(client=hetzner_client.images, data=dict(id=14))

    def test_bound_image_init(self, image_response):
        bound_image = BoundImage(client=mock.MagicMock(), data=image_response["image"])

        assert bound_image.id == 4711
        assert bound_image.type == "snapshot"
        assert bound_image.status == "available"
        assert bound_image.name == "ubuntu-20.04"
        assert bound_image.description == "Ubuntu 20.04 Standard 64 bit"
        assert bound_image.image_size == 2.3
        assert bound_image.disk_size == 10
        assert bound_image.created == datetime.datetime(
            2016, 1, 30, 23, 50, tzinfo=timezone.utc
        )
        assert bound_image.os_flavor == "ubuntu"
        assert bound_image.os_version == "16.04"
        assert bound_image.architecture == "x86"
        assert bound_image.rapid_deploy is False
        assert bound_image.deprecated == datetime.datetime(
            2018, 2, 28, 0, 0, tzinfo=timezone.utc
        )

        assert isinstance(bound_image.created_from, BoundServer)
        assert bound_image.created_from.id == 1
        assert bound_image.created_from.name == "Server"
        assert bound_image.created_from.complete is False

        assert isinstance(bound_image.bound_to, BoundServer)
        assert bound_image.bound_to.id == 1
        assert bound_image.bound_to.complete is False

    @pytest.mark.parametrize(
        "params", [{}, {"sort": ["status"], "page": 1, "per_page": 2}]
    )
    def test_get_actions_list(
        self, hetzner_client, bound_image, response_get_actions, params
    ):
        hetzner_client.request.return_value = response_get_actions
        result = bound_image.get_actions_list(**params)
        hetzner_client.request.assert_called_with(
            url="/images/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", [{}, {"sort": ["status"]}])
    def test_get_actions(
        self, hetzner_client, bound_image, response_get_actions, params
    ):
        hetzner_client.request.return_value = response_get_actions
        actions = bound_image.get_actions(**params)

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

        hetzner_client.request.assert_called_with(
            url="/images/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_image, response_update_image):
        hetzner_client.request.return_value = response_update_image
        image = bound_image.update(
            description="My new Image description", type="snapshot", labels={}
        )
        hetzner_client.request.assert_called_with(
            url="/images/14",
            method="PUT",
            json={
                "description": "My new Image description",
                "type": "snapshot",
                "labels": {},
            },
        )

        assert image.id == 4711
        assert image.description == "My new Image description"

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

        assert delete_success is True

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

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


class TestImagesClient:
    @pytest.fixture()
    def images_client(self):
        return ImagesClient(client=mock.MagicMock())

    def test_get_by_id(self, images_client, image_response):
        images_client._client.request.return_value = image_response
        image = images_client.get_by_id(1)
        images_client._client.request.assert_called_with(url="/images/1", method="GET")
        assert image._client is images_client
        assert image.id == 4711
        assert image.name == "ubuntu-20.04"

    @pytest.mark.parametrize(
        "params",
        [
            {
                "name": "ubuntu-20.04",
                "type": "system",
                "sort": "id",
                "bound_to": "1",
                "label_selector": "k==v",
                "page": 1,
                "per_page": 10,
            },
            {"name": ""},
            {"include_deprecated": True},
            {},
        ],
    )
    def test_get_list(self, images_client, two_images_response, params):
        images_client._client.request.return_value = two_images_response
        result = images_client.get_list(**params)
        images_client._client.request.assert_called_with(
            url="/images", method="GET", params=params
        )

        images = result.images
        assert result.meta is None

        assert len(images) == 2

        images1 = images[0]
        images2 = images[1]

        assert images1._client is images_client
        assert images1.id == 4711
        assert images1.name == "ubuntu-20.04"

        assert images2._client is images_client
        assert images2.id == 4712
        assert images2.name == "ubuntu-18.10"

    @pytest.mark.parametrize(
        "params",
        [
            {
                "name": "ubuntu-20.04",
                "type": "system",
                "sort": "id",
                "bound_to": "1",
                "label_selector": "k==v",
            },
            {"include_deprecated": True},
            {},
        ],
    )
    def test_get_all(self, images_client, two_images_response, params):
        images_client._client.request.return_value = two_images_response
        images = images_client.get_all(**params)

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

        images_client._client.request.assert_called_with(
            url="/images", method="GET", params=params
        )

        assert len(images) == 2

        images1 = images[0]
        images2 = images[1]

        assert images1._client is images_client
        assert images1.id == 4711
        assert images1.name == "ubuntu-20.04"

        assert images2._client is images_client
        assert images2.id == 4712
        assert images2.name == "ubuntu-18.10"

    def test_get_by_name(self, images_client, one_images_response):
        images_client._client.request.return_value = one_images_response
        image = images_client.get_by_name("ubuntu-20.04")

        params = {"name": "ubuntu-20.04"}

        images_client._client.request.assert_called_with(
            url="/images", method="GET", params=params
        )

        assert image._client is images_client
        assert image.id == 4711
        assert image.name == "ubuntu-20.04"

    def test_get_by_name_and_architecture(self, images_client, one_images_response):
        images_client._client.request.return_value = one_images_response
        image = images_client.get_by_name_and_architecture("ubuntu-20.04", "x86")

        params = {"name": "ubuntu-20.04", "architecture": ["x86"]}

        images_client._client.request.assert_called_with(
            url="/images", method="GET", params=params
        )

        assert image._client is images_client
        assert image.id == 4711
        assert image.name == "ubuntu-20.04"
        assert image.architecture == "x86"

    @pytest.mark.parametrize(
        "image", [Image(id=1), BoundImage(mock.MagicMock(), dict(id=1))]
    )
    def test_get_actions_list(self, images_client, image, response_get_actions):
        images_client._client.request.return_value = response_get_actions
        result = images_client.get_actions_list(image)
        images_client._client.request.assert_called_with(
            url="/images/1/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 == images_client._client.actions
        assert actions[0].id == 13
        assert actions[0].command == "change_protection"

    @pytest.mark.parametrize(
        "image", [Image(id=1), BoundImage(mock.MagicMock(), dict(id=1))]
    )
    def test_update(self, images_client, image, response_update_image):
        images_client._client.request.return_value = response_update_image
        image = images_client.update(
            image, description="My new Image description", type="snapshot", labels={}
        )
        images_client._client.request.assert_called_with(
            url="/images/1",
            method="PUT",
            json={
                "description": "My new Image description",
                "type": "snapshot",
                "labels": {},
            },
        )

        assert image.id == 4711
        assert image.description == "My new Image description"

    @pytest.mark.parametrize(
        "image", [Image(id=1), BoundImage(mock.MagicMock(), dict(id=1))]
    )
    def test_change_protection(self, images_client, image, generic_action):
        images_client._client.request.return_value = generic_action
        action = images_client.change_protection(image, True)
        images_client._client.request.assert_called_with(
            url="/images/1/actions/change_protection",
            method="POST",
            json={"delete": True},
        )

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

    @pytest.mark.parametrize(
        "image", [Image(id=1), BoundImage(mock.MagicMock(), dict(id=1))]
    )
    def test_delete(self, images_client, image, generic_action):
        images_client._client.request.return_value = generic_action
        delete_success = images_client.delete(image)
        images_client._client.request.assert_called_with(
            url="/images/1", method="DELETE"
        )

        assert delete_success is True

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

        images_client._client.request.assert_called_with(
            url="/images/actions/13", method="GET"
        )

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

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

        images_client._client.request.assert_called_with(
            url="/images/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 == images_client._client.actions
        assert actions[0].id == 13
        assert actions[0].command == "change_protection"

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

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

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