File: test_storage.py

package info (click to toggle)
python-pylxd 2.2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 820 kB
  • sloc: python: 7,258; sh: 104; makefile: 21
file content (305 lines) | stat: -rw-r--r-- 11,308 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
# Copyright (c) 2016 Canonical Ltd
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
import json

try:
    from unittest import mock
except ImportError:
    import mock

from pylxd import exceptions
from pylxd import models
from pylxd.tests import testing


def add_api_extension_helper(obj, extensions):
    obj.add_rule({
        'text': json.dumps({
            'type': 'sync',
            'metadata': {'auth': 'trusted',
                         'environment': {
                             'certificate': 'an-pem-cert',
                             },
                         'api_extensions': extensions
                         }}),
        'method': 'GET',
        'url': r'^http://pylxd.test/1.0$',
    })
    # Update hostinfo
    obj.client.host_info = obj.client.api.get().json()['metadata']


class TestStoragePool(testing.PyLXDTestCase):
    """Tests for pylxd.models.StoragePool."""

    def test_all(self):
        """A list of all storage_pools are returned."""
        # first assert that the lxd storage requires 'storage' api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            models.StoragePool.all(self.client)
        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        storage_pools = models.StoragePool.all(self.client)
        self.assertEqual(1, len(storage_pools))

    def test_get(self):
        """Return a container."""
        name = 'lxd'

        # first assert that the lxd storage requires 'storage' api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            models.StoragePool.get(self.client, name)
        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        a_storage_pool = models.StoragePool.get(self.client, name)

        self.assertEqual(name, a_storage_pool.name)

    def test_partial(self):
        """A partial storage_pool is synced."""
        a_storage_pool = models.StoragePool(self.client, name='lxd')

        self.assertEqual('zfs', a_storage_pool.driver)

    def test_create(self):
        """A new storage pool is created."""
        config = {"config": {}, "driver": "zfs", "name": "lxd"}

        # first assert that the lxd storage requires 'storage' api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            models.StoragePool.create(self.client, config)
        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        a_storage_pool = models.StoragePool.create(self.client, config)

        self.assertEqual(config['name'], a_storage_pool.name)

    def test_exists(self):
        """A storage pool exists."""
        name = 'lxd'

        # first assert that the lxd storage requires 'storage' api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            models.StoragePool.exists(self.client, name)
        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        self.assertTrue(models.StoragePool.exists(self.client, name))

    def test_not_exists(self):
        """A storage pool exists."""
        def not_found(request, context):
            context.status_code = 404
            return json.dumps({
                'type': 'error',
                'error': 'Not found',
                'error_code': 404})
        self.add_rule({
            'text': not_found,
            'method': 'GET',
            'url': r'^http://pylxd.test/1.0/storage-pools/an-missing-storage-pool$',  # NOQA
        })

        name = 'an-missing-storage-pool'

        with mock.patch.object(self.client, 'assert_has_api_extension'):
            self.assertFalse(models.StoragePool.exists(self.client, name))

    def test_delete(self):
        """A storage pool can be deleted"""
        a_storage_pool = models.StoragePool(self.client, name='lxd')

        with mock.patch.object(self.client, 'assert_has_api_extension'):
            a_storage_pool.delete()

    def test_save(self):
        """A storage pool can be saved"""
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        a_storage_pool.config = {'some': 'value'}

        with mock.patch.object(self.client, 'assert_has_api_extension'):
            a_storage_pool.save()

    def test_put(self):
        """A storage pool can be PUT to"""
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        put_object = {'some': 'value'}

        with mock.patch.object(self.client, 'assert_has_api_extension'):
            a_storage_pool.put(put_object)

    def test_patch(self):
        """A storage pool can be PATCHed"""
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        patch_object = {'some': 'value'}

        with mock.patch.object(self.client, 'assert_has_api_extension'):
            a_storage_pool.patch(patch_object)


class TestStorageResources(testing.PyLXDTestCase):
    """Tests for pylxd.models.StorageResources."""

    def test_get(self):
        a_storage_pool = models.StoragePool(self.client, name='lxd')

        # first assert that the lxd storage resource requires 'resources'
        # api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            a_storage_pool.resources.get()

        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['resources'])
        resources = a_storage_pool.resources.get()

        self.assertEqual(resources.space['used'], 207111192576)
        self.assertEqual(resources.space['total'], 306027577344)
        self.assertEqual(resources.inodes['used'], 3275333)
        self.assertEqual(resources.inodes['total'], 18989056)


class TestStorageVolume(testing.PyLXDTestCase):
    """Tests for pylxd.models.StorageVolume."""

    def test_all(self):
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        # first assert that the lxd storage resource requires 'storage'
        # api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            a_storage_pool.volumes.all()

        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        volumes = a_storage_pool.volumes.all()

        # assert that we decoded stuff reasonably well.
        self.assertEqual(len(volumes), 6)
        self.assertEqual(volumes[0].type, 'container')
        self.assertEqual(volumes[0].name, 'c1')
        self.assertEqual(volumes[3].type, 'image')
        self.assertEqual(volumes[3].name, 'i1')
        self.assertEqual(volumes[5].type, 'custom')
        self.assertEqual(volumes[5].name, 'cu1')

    def test_get(self):
        a_storage_pool = models.StoragePool(self.client, name='lxd')

        # first assert that the lxd storage requires 'storage' api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            a_storage_pool.volumes.get('custom', 'cu1')
        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        # now do the proper get
        volume = a_storage_pool.volumes.get('custom', 'cu1')
        self.assertEqual(volume.type, 'custom')
        self.assertEqual(volume.name, 'cu1')
        config = {
            "block.filesystem": "ext4",
            "block.mount_options": "discard",
            "size": "10737418240"
        }
        self.assertEqual(volume.config, config)

    def test_create(self):
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        config = {
            "config": {},
            "pool": "lxd",
            "name": "cu1",
            "type": "custom"
        }

        # first assert that the lxd storage requires 'storage' api_extension
        with self.assertRaises(exceptions.LXDAPIExtensionNotAvailable):
            a_storage_pool.volumes.create(self.client, config)
        # now make sure that it's available without mocking it out.
        add_api_extension_helper(self, ['storage'])

        a_volume = a_storage_pool.volumes.create(self.client, config)

        self.assertEqual(config['name'], a_volume.name)

    def test_create_async(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='async-lxd')
        config = {
            "config": {},
            "pool": "async-lxd",
            "name": "cu1",
            "type": "custom"
        }
        a_storage_pool.volumes.create(self.client, config, wait=True)

    def test_rename(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        a_volume = a_storage_pool.volumes.get('custom', 'cu1')

        _input = {
            "name": "vol1",
            "pool": "pool3",
            "migration": True
        }
        result = a_volume.rename(_input)
        self.assertEqual(result['control'], 'secret1')
        self.assertEqual(result['fs'], 'secret2')

    def test_rename_async(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='async-lxd')
        a_volume = a_storage_pool.volumes.get('custom', 'cu1')

        _input = {
            "name": "vol1",
            "pool": "pool3",
            "migration": True
        }
        a_volume.rename(_input, wait=True)

    def test_put(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        a_volume = a_storage_pool.volumes.get('custom', 'cu1')
        put_object = {
            'config': {'size': 1}
        }
        a_volume.put(put_object)

    def test_patch(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        a_volume = a_storage_pool.volumes.get('custom', 'cu1')
        patch_object = {
            'config': {'size': 1}
        }
        with mock.patch.object(self.client, 'assert_has_api_extension'):
            a_volume.patch(patch_object)

    def test_save(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        a_volume = a_storage_pool.volumes.get('custom', 'cu1')
        a_volume.config = {'size': 2}
        a_volume.save()

    def test_delete(self):
        add_api_extension_helper(self, ['storage'])
        a_storage_pool = models.StoragePool(self.client, name='lxd')
        a_volume = a_storage_pool.volumes.get('custom', 'cu1')
        a_volume.delete()