File: test_volume.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (354 lines) | stat: -rw-r--r-- 14,881 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
import time
import json
from azure.mgmt.resource import ResourceManagementClient
from devtools_testutils import AzureMgmtTestCase
from azure.mgmt.netapp.models import Volume, VolumePatch, ReplicationObject, VolumePropertiesDataProtection
from test_pool import create_pool, delete_pool
from test_account import delete_account
from setup import *
import azure.mgmt.netapp.models
import unittest

volumes = [TEST_VOL_1, TEST_VOL_2]

# to skip tests use
raise unittest.SkipTest("Skipping Volume test")


def create_volume_body(volume_name, location, rg=TEST_RG, vnet=VNET):
    default_protocol_type = { "NFSv3" }

    volume_body = Volume(
        location=location,
        usage_threshold = 100 * GIGABYTE,
        protocol_types = default_protocol_type,
        creation_token=volume_name,
        service_level=SERVICE_LEVEL,
        subnet_id = "/subscriptions/" + SUBSID + "/resourceGroups/" + rg + "/providers/Microsoft.Network/virtualNetworks/" + vnet + "/subnets/default"
    )

    return volume_body

def create_volume(client, rg=TEST_RG, account_name=TEST_ACC_1, pool_name=TEST_POOL_1, volume_name=TEST_VOL_1, location=LOCATION, vnet=VNET, volume_only=False, live=False):
    if not volume_only:
        pool = create_pool(
            client,
            rg,
            account_name,
            pool_name,
            location,
            False)
        if live:
            time.sleep(10)

    volume_body = create_volume_body(volume_name, location, rg, vnet)
    volume = client.volumes.create_or_update(
        volume_body,
        rg,
        account_name,
        pool_name,
        volume_name
    ).result()

    return volume

def create_dp_volume(client, source_volume, rg=TEST_REMOTE_RG, account_name=TEST_ACC_2, pool_name=TEST_POOL_2, volume_name=TEST_VOL_2, location=REMOTE_LOCATION, volume_only=False, live=False):
    if not volume_only:
        pool = create_pool(
            client,
            rg,
            account_name,
            pool_name,
            location,
            False)
        if live:
            time.sleep(10)

    # data protection and replication object
    replication = ReplicationObject(
        endpoint_type = "dst",
        remote_volume_resource_id = source_volume.id,
        replication_schedule = "_10minutely"
    )

    data_protection = VolumePropertiesDataProtection(
        replication = replication
    )

    default_protocol_type = { "NFSv3" }

    volume_body = Volume(
        location=location,
        usage_threshold = 100 * GIGABYTE,
        protocol_types = default_protocol_type,
        creation_token=volume_name,
        subnet_id = "/subscriptions/" + SUBSID + "/resourceGroups/" + rg + "/providers/Microsoft.Network/virtualNetworks/" + REMOTE_VNET + "/subnets/default",
        volume_type = "DataProtection",
        data_protection = data_protection
    )

    destination_volume = client.volumes.create_or_update(
        volume_body,
        rg,
        account_name,
        pool_name,
        volume_name
    ).result()

    return destination_volume

def get_volume(client, rg, account_name, pool_name, volume_name):
    volume = client.volumes.get(rg, account_name, pool_name, volume_name)
    return volume

def wait_for_no_volume(client, rg, account_name, pool_name, volume_name, live=False):
    # a workaround for the async nature of certain ARM processes
    co=0
    while co<10:
        co += 1
        if live:
            time.sleep(200)
        try:
            get_volume(client, rg, account_name, pool_name, volume_name)
        except:
            # not found is an exception case (status code 200 expected)
            # but is what we are waiting for
            break

def wait_for_volume(client, rg, account_name, pool_name, volume_name, live=False):
    # a work around for the async nature of certain ARM processes
    co=0
    while co<10:
        co += 1
        if live:
            time.sleep(10)
        try:
            get_volume(client, rg, account_name, pool_name, volume_name)
            break
        except:
            # not found is an exception case (status code 200 expected)
            # but is what we are waiting for
            pass

def delete_volume(client, rg, account_name, pool_name, volume_name, live=False):
    client.volumes.delete(rg, account_name, pool_name, volume_name).wait()
    wait_for_no_volume(client, rg, account_name, pool_name, volume_name, live)

def wait_for_replication_status(client, target_state):
    # python isn't good at do-while loops but loop until we get the target state
    while True:
        replication_status = client.volumes.replication_status_method(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
        if (replication_status.mirror_state == target_state):
            break
        time.sleep(1)

def wait_for_succeeded(client):
    # python isn't good at do-while loops but loop until we get volumes in succeeded state
    while True:
        source_volume = client.volumes.get(TEST_REPL_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1);
        dp_volume = client.volumes.get(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
        if ((source_volume.provisioning_state == "Succeeded") and (dp_volume.provisioning_state == "Succeeded")):
            break
        time.sleep(1)


class NetAppAccountTestCase(AzureMgmtTestCase):
    def setUp(self):
        super(NetAppAccountTestCase, self).setUp()
        self.client = self.create_mgmt_client(azure.mgmt.netapp.AzureNetAppFilesManagementClient)

    def test_create_delete_list_volume(self):
        volume = create_volume(
            self.client,
            TEST_RG,
            TEST_ACC_1,
            TEST_POOL_1,
            TEST_VOL_1,
            live=self.is_live
        )
        self.assertEqual(volume.name, TEST_ACC_1 + '/' + TEST_POOL_1 + '/' + TEST_VOL_1)
        # check default export policy and protocol
        self.assertTrue(volume.export_policy.rules[0].nfsv3),
        self.assertFalse(volume.export_policy.rules[0].nfsv41)
        self.assertEqual("0.0.0.0/0", volume.export_policy.rules[0].allowed_clients)
        self.assertEqual(volume.protocol_types[0], "NFSv3")

        volume_list = self.client.volumes.list(TEST_RG, TEST_ACC_1, TEST_POOL_1)
        self.assertEqual(len(list(volume_list)), 1)

        self.client.volumes.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1).wait()
        volume_list = self.client.volumes.list(TEST_RG, TEST_ACC_1, TEST_POOL_1)
        self.assertEqual(len(list(volume_list)), 0)

        wait_for_no_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1)
        delete_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_list_volumes(self):
        volume = create_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, LOCATION, live=self.is_live)
        volume = create_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_2, LOCATION, volume_only=True, live=self.is_live)

        volume_list = self.client.volumes.list(TEST_RG, TEST_ACC_1, TEST_POOL_1)
        self.assertEqual(len(list(volume_list)), 2)
        idx = 0
        for volume in volume_list:
            self.assertEqual(volume.name, volumes[idx])
            idx += 1

        self.client.volumes.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1).wait()
        self.client.volumes.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_2).wait()
        for volume in volumes:
            wait_for_no_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, volumes[idx], live=self.is_live)
        delete_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_volume_replication(self):
        source_volume = create_volume(
            self.client,
            TEST_REPL_RG,
            TEST_ACC_1,
            TEST_POOL_1,
            TEST_VOL_1,
            vnet=REPL_VNET,
            live=self.is_live)

        if self.is_live:
            time.sleep(5)

        sourceVolume = self.client.volumes.get(
            TEST_REPL_RG,
            TEST_ACC_1,
            TEST_POOL_1,
            TEST_VOL_1);

        dp_volume = create_dp_volume(
            self.client,
            source_volume,
            TEST_REMOTE_RG,
            TEST_ACC_2,
            TEST_POOL_2,
            TEST_VOL_2,
            live=self.is_live)

        if self.is_live:
            time.sleep(30)

        # sync replication
        self.client.volumes.authorize_replication(TEST_REPL_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, dp_volume.id)
        wait_for_succeeded(self.client);
        wait_for_replication_status(self.client, "Mirrored");

        # break replication
        self.client.volumes.break_replication(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
        wait_for_replication_status(self.client, "Broken");
        if self.is_live:
            time.sleep(30)
        wait_for_succeeded(self.client);

        # resync
        self.client.volumes.resync_replication(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
        wait_for_replication_status(self.client, "Mirrored");
        if self.is_live:
            time.sleep(30)

        # break again
        self.client.volumes.break_replication(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
        wait_for_replication_status(self.client, "Broken");
        if self.is_live:
            time.sleep(30)

        # delete the data protection object
        #  - initiate delete replication on destination, this then releases on source, both resulting in object deletion
        self.client.volumes.delete_replication(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);

        replication_found = True; # because it was previously present
        while replication_found:
            try:
                replication_status = self.client.volumes.replication_status_method(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
            except:
                # an exception means the replication was not found
                # i.e. it has been deleted
                # ok without checking it could have been for another reason
                # but then the delete below will fail
                replication_found = False;

            time.sleep(1)

        # seems the volumes are not always in a terminal state here so check again
        # and ensure the replication objects are removed
        # python isn't good at do-while loops but loop until we get volumes in succeeded state
        while True:
            source_volume = self.client.volumes.get(TEST_REPL_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1);
            dp_volume = self.client.volumes.get(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2);
            if ((source_volume.provisioning_state == "Succeeded") and (dp_volume.provisioning_state == "Succeeded") and (source_volume.data_protection.replication is None) and (dp_volume.data_protection.replication is None)):
                break
            time.sleep(1)

        # now proceed with the delete of the volumes and tidy up resources

        # delete destination volume
        self.client.volumes.delete(TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2).wait()
        wait_for_no_volume(self.client, TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, TEST_VOL_2, live=self.is_live)
        delete_pool(self.client, TEST_REMOTE_RG, TEST_ACC_2, TEST_POOL_2, live=self.is_live)
        delete_account(self.client, TEST_REMOTE_RG, TEST_ACC_2, live=self.is_live)

        # delete source volume
        self.client.volumes.delete(TEST_REPL_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1).wait()
        wait_for_no_volume(self.client, TEST_REPL_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, live=self.is_live)
        delete_pool(self.client, TEST_REPL_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_REPL_RG, TEST_ACC_1, live=self.is_live)


    def test_get_volume_by_name(self):
        volume = create_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, LOCATION, live=self.is_live)

        volume = self.client.volumes.get(TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1)
        self.assertEqual(volume.name, TEST_ACC_1 + '/' + TEST_POOL_1 + '/' + TEST_VOL_1)

        self.client.volumes.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1).wait()
        wait_for_no_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, live=self.is_live)
        delete_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_update_volume(self):
        volume = create_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, live=self.is_live)
        self.assertEqual("Premium", volume.service_level)
        self.assertEqual(100 * GIGABYTE, volume.usage_threshold)

        volume_body = Volume(
            usage_threshold = 200 * GIGABYTE,
            creation_token=TEST_VOL_1,
            service_level="Premium", # cannot differ from pool
            location=LOCATION,
            subnet_id = "/subscriptions/" + SUBSID + "/resourceGroups/" + TEST_RG + "/providers/Microsoft.Network/virtualNetworks/" + VNET + "/subnets/default"
        )

        volume = self.client.volumes.create_or_update(
            volume_body,
            TEST_RG,
            TEST_ACC_1,
            TEST_POOL_1,
            TEST_VOL_1
        ).result()
        self.assertEqual("Premium", volume.service_level);  # unchanged
        self.assertEqual(200 * GIGABYTE, volume.usage_threshold)

        delete_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, live=self.is_live)
        delete_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_patch_volume(self):
        volume = create_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, live=self.is_live)
        self.assertEqual("Premium", volume.service_level);
        self.assertEqual(100 * GIGABYTE, volume.usage_threshold);

        volume_patch = VolumePatch(usage_threshold = 200 * GIGABYTE)
        volume = self.client.volumes.update(volume_patch, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1).result()
        self.assertEqual("Premium", volume.service_level);  # unchanged
        self.assertEqual(200 * GIGABYTE, volume.usage_threshold);

        self.client.volumes.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1).wait()
        wait_for_no_volume(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, TEST_VOL_1, live=self.is_live)
        delete_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)