File: test_datasync.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (438 lines) | stat: -rw-r--r-- 14,676 bytes parent folder | download | duplicates (2)
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
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws


def create_locations(client, create_smb=False, create_s3=False):
    """
    Convenience function for creating locations.
    Locations must exist before tasks can be created.
    """
    smb_arn = None
    s3_arn = None
    if create_smb:
        response = client.create_location_smb(
            ServerHostname="host",
            Subdirectory="somewhere",
            User="",
            Password="",
            AgentArns=["stuff"],
        )
        smb_arn = response["LocationArn"]
    if create_s3:
        response = client.create_location_s3(
            S3BucketArn="arn:aws:s3:::my_bucket",
            Subdirectory="dir",
            S3Config={"BucketAccessRoleArn": "role"},
        )
        s3_arn = response["LocationArn"]
    return {"smb_arn": smb_arn, "s3_arn": s3_arn}


@mock_aws
def test_create_location_smb():
    client = boto3.client("datasync", region_name="us-east-1")
    response = client.create_location_smb(
        ServerHostname="host",
        Subdirectory="somewhere",
        User="",
        Password="",
        AgentArns=["stuff"],
    )
    assert "LocationArn" in response


@mock_aws
def test_describe_location_smb():
    client = boto3.client("datasync", region_name="us-east-1")
    agent_arns = ["stuff"]
    user = "user"
    response = client.create_location_smb(
        ServerHostname="host",
        Subdirectory="somewhere",
        User=user,
        Password="",
        AgentArns=agent_arns,
    )
    response = client.describe_location_smb(LocationArn=response["LocationArn"])
    assert "LocationArn" in response
    assert "LocationUri" in response
    assert response["User"] == user
    assert response["AgentArns"] == agent_arns


@mock_aws
def test_create_location_s3():
    client = boto3.client("datasync", region_name="us-east-1")
    response = client.create_location_s3(
        S3BucketArn="arn:aws:s3:::my_bucket",
        Subdirectory="dir",
        S3Config={"BucketAccessRoleArn": "role"},
    )
    assert "LocationArn" in response


@mock_aws
def test_describe_location_s3():
    client = boto3.client("datasync", region_name="us-east-1")
    s3_config = {"BucketAccessRoleArn": "role"}
    response = client.create_location_s3(
        S3BucketArn="arn:aws:s3:::my_bucket", Subdirectory="dir", S3Config=s3_config
    )
    response = client.describe_location_s3(LocationArn=response["LocationArn"])
    assert "LocationArn" in response
    assert "LocationUri" in response
    assert response["S3Config"] == s3_config


@mock_aws
def test_describe_location_wrong():
    client = boto3.client("datasync", region_name="us-east-1")
    agent_arns = ["stuff"]
    user = "user"
    response = client.create_location_smb(
        ServerHostname="host",
        Subdirectory="somewhere",
        User=user,
        Password="",
        AgentArns=agent_arns,
    )
    with pytest.raises(ClientError) as e:
        client.describe_location_s3(LocationArn=response["LocationArn"])
    err = e.value.response["Error"]
    assert err["Code"] == "InvalidRequestException"
    assert err["Message"] == "Invalid Location type: SMB"


@mock_aws
def test_list_locations():
    client = boto3.client("datasync", region_name="us-east-1")
    response = client.list_locations()
    assert len(response["Locations"]) == 0

    create_locations(client, create_smb=True)
    response = client.list_locations()
    assert len(response["Locations"]) == 1
    assert response["Locations"][0]["LocationUri"] == "smb://host/somewhere"

    create_locations(client, create_s3=True)
    response = client.list_locations()
    assert len(response["Locations"]) == 2
    assert response["Locations"][1]["LocationUri"] == "s3://my_bucket/dir"

    create_locations(client, create_s3=True)
    response = client.list_locations()
    assert len(response["Locations"]) == 3
    assert response["Locations"][2]["LocationUri"] == "s3://my_bucket/dir"


@mock_aws
def test_delete_location():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_smb=True)
    response = client.list_locations()
    assert len(response["Locations"]) == 1
    location_arn = locations["smb_arn"]

    client.delete_location(LocationArn=location_arn)
    response = client.list_locations()
    assert len(response["Locations"]) == 0

    with pytest.raises(ClientError):
        client.delete_location(LocationArn=location_arn)


@mock_aws
def test_create_task():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_smb=True, create_s3=True)
    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
    )
    assert "TaskArn" in response


@mock_aws
def test_create_task_fail():
    """Test that Locations must exist before a Task can be created"""
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_smb=True, create_s3=True)
    with pytest.raises(ClientError) as e:
        client.create_task(
            SourceLocationArn="1", DestinationLocationArn=locations["s3_arn"]
        )
    err = e.value.response["Error"]
    assert err["Code"] == "InvalidRequestException"
    assert err["Message"] == "Location 1 not found."

    with pytest.raises(ClientError) as e:
        client.create_task(
            SourceLocationArn=locations["smb_arn"], DestinationLocationArn="2"
        )
    err = e.value.response["Error"]
    assert err["Code"] == "InvalidRequestException"
    assert err["Message"] == "Location 2 not found."


@mock_aws
def test_list_tasks():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
    )
    client.create_task(
        SourceLocationArn=locations["s3_arn"],
        DestinationLocationArn=locations["smb_arn"],
        Name="task_name",
    )
    response = client.list_tasks()
    tasks = response["Tasks"]
    assert len(tasks) == 2

    task = tasks[0]
    assert task["Status"] == "AVAILABLE"
    assert "Name" not in task

    task = tasks[1]
    assert task["Status"] == "AVAILABLE"
    assert task["Name"] == "task_name"


@mock_aws
def test_describe_task():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name="task_name",
    )
    task_arn = response["TaskArn"]

    response = client.describe_task(TaskArn=task_arn)

    assert "TaskArn" in response
    assert "Status" in response
    assert "SourceLocationArn" in response
    assert "DestinationLocationArn" in response


@mock_aws
def test_describe_task_not_exist():
    client = boto3.client("datasync", region_name="us-east-1")

    with pytest.raises(ClientError) as e:
        client.describe_task(TaskArn="abc")
    err = e.value.response["Error"]
    assert err["Code"] == "InvalidRequestException"
    assert err["Message"] == "The request is not valid."


@mock_aws
def test_update_task():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    initial_name = "Initial_Name"
    updated_name = "Updated_Name"
    initial_options = {
        "VerifyMode": "NONE",
        "Atime": "BEST_EFFORT",
        "Mtime": "PRESERVE",
    }
    updated_options = {
        "VerifyMode": "POINT_IN_TIME_CONSISTENT",
        "Atime": "BEST_EFFORT",
        "Mtime": "PRESERVE",
    }
    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name=initial_name,
        Options=initial_options,
    )
    task_arn = response["TaskArn"]
    response = client.describe_task(TaskArn=task_arn)
    assert response["TaskArn"] == task_arn
    assert response["Name"] == initial_name
    assert response["Options"] == initial_options

    client.update_task(TaskArn=task_arn, Name=updated_name, Options=updated_options)

    response = client.describe_task(TaskArn=task_arn)
    assert response["TaskArn"] == task_arn
    assert response["Name"] == updated_name
    assert response["Options"] == updated_options

    with pytest.raises(ClientError):
        client.update_task(TaskArn="doesnt_exist")


@mock_aws
def test_delete_task():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name="task_name",
    )

    response = client.list_tasks()
    assert len(response["Tasks"]) == 1
    task_arn = response["Tasks"][0]["TaskArn"]
    assert task_arn is not None

    client.delete_task(TaskArn=task_arn)
    response = client.list_tasks()
    assert len(response["Tasks"]) == 0

    with pytest.raises(ClientError):
        client.delete_task(TaskArn=task_arn)


@mock_aws
def test_start_task_execution():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name="task_name",
    )
    task_arn = response["TaskArn"]
    response = client.describe_task(TaskArn=task_arn)
    assert "CurrentTaskExecutionArn" not in response

    response = client.start_task_execution(TaskArn=task_arn)
    assert "TaskExecutionArn" in response
    task_execution_arn = response["TaskExecutionArn"]

    response = client.describe_task(TaskArn=task_arn)
    assert response["CurrentTaskExecutionArn"] == task_execution_arn


@mock_aws
def test_start_task_execution_twice():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name="task_name",
    )
    task_arn = response["TaskArn"]

    response = client.start_task_execution(TaskArn=task_arn)
    assert "TaskExecutionArn" in response

    with pytest.raises(ClientError) as exc:
        client.start_task_execution(TaskArn=task_arn)
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidRequestException"


@mock_aws
def test_describe_task_execution():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name="task_name",
    )
    task_arn = response["TaskArn"]
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "AVAILABLE"

    response = client.start_task_execution(TaskArn=task_arn)
    task_execution_arn = response["TaskExecutionArn"]

    # Each time task_execution is described the Status will increment
    # This is a simple way to simulate a task being executed
    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["TaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "INITIALIZING"
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "RUNNING"

    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["TaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "PREPARING"
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "RUNNING"

    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["TaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "TRANSFERRING"
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "RUNNING"

    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["TaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "VERIFYING"
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "RUNNING"

    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["TaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "SUCCESS"
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "AVAILABLE"

    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["TaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "SUCCESS"
    response = client.describe_task(TaskArn=task_arn)
    assert response["Status"] == "AVAILABLE"


@mock_aws
def test_describe_task_execution_not_exist():
    client = boto3.client("datasync", region_name="us-east-1")

    with pytest.raises(ClientError) as e:
        client.describe_task_execution(TaskExecutionArn="abc")
    err = e.value.response["Error"]
    assert err["Code"] == "InvalidRequestException"
    assert err["Message"] == "The request is not valid."


@mock_aws
def test_cancel_task_execution():
    client = boto3.client("datasync", region_name="us-east-1")
    locations = create_locations(client, create_s3=True, create_smb=True)

    response = client.create_task(
        SourceLocationArn=locations["smb_arn"],
        DestinationLocationArn=locations["s3_arn"],
        Name="task_name",
    )
    task_arn = response["TaskArn"]

    response = client.start_task_execution(TaskArn=task_arn)
    task_execution_arn = response["TaskExecutionArn"]

    response = client.describe_task(TaskArn=task_arn)
    assert response["CurrentTaskExecutionArn"] == task_execution_arn
    assert response["Status"] == "RUNNING"

    client.cancel_task_execution(TaskExecutionArn=task_execution_arn)

    response = client.describe_task(TaskArn=task_arn)
    assert "CurrentTaskExecutionArn" not in response
    assert response["Status"] == "AVAILABLE"

    response = client.describe_task_execution(TaskExecutionArn=task_execution_arn)
    assert response["Status"] == "ERROR"