File: test_ssm_docs.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 (787 lines) | stat: -rw-r--r-- 27,701 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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import copy
import datetime
import hashlib
import json
import pkgutil
from datetime import timezone

import boto3
import botocore.exceptions
import pytest
import yaml
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID


def _get_yaml_template():
    return pkgutil.get_data(__name__, "test_templates/good.yaml")


def _validate_document_description(
    doc_name,
    doc_description,
    json_doc,
    expected_document_version,
    expected_latest_version,
    expected_default_version,
    expected_format,
):
    if expected_format == "JSON":
        assert doc_description["Hash"] == (
            hashlib.sha256(json.dumps(json_doc).encode("utf-8")).hexdigest()
        )
    else:
        assert doc_description["Hash"] == (
            hashlib.sha256(yaml.dump(json_doc).encode("utf-8")).hexdigest()
        )

    assert doc_description["HashType"] == "Sha256"
    assert doc_description["Name"] == doc_name
    assert doc_description["Owner"] == ACCOUNT_ID

    difference = datetime.datetime.now(tz=timezone.utc) - doc_description["CreatedDate"]
    assert not difference.min > datetime.timedelta(minutes=1)

    assert doc_description["Status"] == "Active"
    assert doc_description["DocumentVersion"] == expected_document_version
    assert doc_description["Description"] == json_doc["description"]

    doc_description["Parameters"] = sorted(
        doc_description["Parameters"], key=lambda doc: doc["Name"]
    )

    assert doc_description["Parameters"][0]["Name"] == "Parameter1"
    assert doc_description["Parameters"][0]["Type"] == "Integer"
    assert doc_description["Parameters"][0]["Description"] == "Command Duration."
    assert doc_description["Parameters"][0]["DefaultValue"] == "3"

    assert doc_description["Parameters"][1]["Name"] == "Parameter2"
    assert doc_description["Parameters"][1]["Type"] == "String"
    assert doc_description["Parameters"][1]["DefaultValue"] == "def"

    assert doc_description["Parameters"][2]["Name"] == "Parameter3"
    assert doc_description["Parameters"][2]["Type"] == "Boolean"
    assert doc_description["Parameters"][2]["Description"] == "A boolean"
    assert doc_description["Parameters"][2]["DefaultValue"] == "False"

    assert doc_description["Parameters"][3]["Name"] == "Parameter4"
    assert doc_description["Parameters"][3]["Type"] == "StringList"
    assert doc_description["Parameters"][3]["Description"] == "A string list"
    assert doc_description["Parameters"][3]["DefaultValue"] == '["abc", "def"]'

    assert doc_description["Parameters"][4]["Name"] == "Parameter5"
    assert doc_description["Parameters"][4]["Type"] == "StringMap"

    assert doc_description["Parameters"][5]["Name"] == "Parameter6"
    assert doc_description["Parameters"][5]["Type"] == "MapList"

    if expected_format == "JSON":
        # We have to replace single quotes from the response to package it back up
        assert json.loads(doc_description["Parameters"][4]["DefaultValue"]) == {
            "NotificationArn": "$dependency.topicArn",
            "NotificationEvents": ["Failed"],
            "NotificationType": "Command",
        }

        assert json.loads(doc_description["Parameters"][5]["DefaultValue"]) == [
            {"DeviceName": "/dev/sda1", "Ebs": {"VolumeSize": "50"}},
            {"DeviceName": "/dev/sdm", "Ebs": {"VolumeSize": "100"}},
        ]
    else:
        assert yaml.safe_load(doc_description["Parameters"][4]["DefaultValue"]) == {
            "NotificationArn": "$dependency.topicArn",
            "NotificationEvents": ["Failed"],
            "NotificationType": "Command",
        }
        assert yaml.safe_load(doc_description["Parameters"][5]["DefaultValue"]) == [
            {"DeviceName": "/dev/sda1", "Ebs": {"VolumeSize": "50"}},
            {"DeviceName": "/dev/sdm", "Ebs": {"VolumeSize": "100"}},
        ]

    assert doc_description["DocumentType"] == "Command"
    assert doc_description["SchemaVersion"] == "2.2"
    assert doc_description["LatestVersion"] == expected_latest_version
    assert doc_description["DefaultVersion"] == expected_default_version
    assert doc_description["DocumentFormat"] == expected_format


def _get_doc_validator(
    response, version_name, doc_version, json_doc_content, document_format
):
    assert response["Name"] == "TestDocument3"
    if version_name:
        assert response["VersionName"] == version_name
    assert response["DocumentVersion"] == doc_version
    assert response["Status"] == "Active"
    if document_format == "JSON":
        assert json.loads(response["Content"]) == json_doc_content
    else:
        assert yaml.safe_load(response["Content"]) == json_doc_content
    assert response["DocumentType"] == "Command"
    assert response["DocumentFormat"] == document_format


@mock_aws
def test_create_document():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)

    client = boto3.client("ssm", region_name="us-east-1")

    response = client.create_document(
        Content=yaml.dump(json_doc),
        Name="TestDocument",
        DocumentType="Command",
        DocumentFormat="YAML",
    )
    doc_description = response["DocumentDescription"]
    _validate_document_description(
        "TestDocument", doc_description, json_doc, "1", "1", "1", "YAML"
    )

    response = client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument2",
        DocumentType="Command",
        DocumentFormat="JSON",
    )
    doc_description = response["DocumentDescription"]
    _validate_document_description(
        "TestDocument2", doc_description, json_doc, "1", "1", "1", "JSON"
    )

    response = client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument3",
        DocumentType="Command",
        DocumentFormat="JSON",
        VersionName="Base",
        TargetType="/AWS::EC2::Instance",
        Tags=[{"Key": "testing", "Value": "testingValue"}],
    )
    doc_description = response["DocumentDescription"]
    assert doc_description["VersionName"] == "Base"
    assert doc_description["TargetType"] == "/AWS::EC2::Instance"
    assert doc_description["Tags"] == [{"Key": "testing", "Value": "testingValue"}]

    _validate_document_description(
        "TestDocument3", doc_description, json_doc, "1", "1", "1", "JSON"
    )

    try:
        client.create_document(
            Content=json.dumps(json_doc),
            Name="TestDocument3",
            DocumentType="Command",
            DocumentFormat="JSON",
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "CreateDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document already exists."
        )

    try:
        client.create_document(
            Content=yaml.dump(json_doc),
            Name="TestDocument4",
            DocumentType="Command",
            DocumentFormat="JSON",
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "CreateDocument"
        assert (
            err.response["Error"]["Message"]
            == "The content for the document is not valid."
        )

    del json_doc["parameters"]
    response = client.create_document(
        Content=yaml.dump(json_doc),
        Name="EmptyParamDoc",
        DocumentType="Command",
        DocumentFormat="YAML",
    )
    doc_description = response["DocumentDescription"]

    assert doc_description["Hash"] == (
        hashlib.sha256(yaml.dump(json_doc).encode("utf-8")).hexdigest()
    )
    assert doc_description["HashType"] == "Sha256"
    assert doc_description["Name"] == "EmptyParamDoc"
    assert doc_description["Owner"] == ACCOUNT_ID

    difference = datetime.datetime.now(tz=timezone.utc) - doc_description["CreatedDate"]
    assert not difference.min > datetime.timedelta(minutes=1)

    assert doc_description["Status"] == "Active"
    assert doc_description["DocumentVersion"] == "1"
    assert doc_description["Description"] == json_doc["description"]
    assert doc_description["DocumentType"] == "Command"
    assert doc_description["SchemaVersion"] == "2.2"
    assert doc_description["LatestVersion"] == "1"
    assert doc_description["DefaultVersion"] == "1"
    assert doc_description["DocumentFormat"] == "YAML"


@mock_aws
def test_get_document():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)

    client = boto3.client("ssm", region_name="us-east-1")

    try:
        client.get_document(Name="DNE")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    client.create_document(
        Content=yaml.dump(json_doc),
        Name="TestDocument3",
        DocumentType="Command",
        DocumentFormat="YAML",
        VersionName="Base",
    )

    new_json_doc = copy.copy(json_doc)
    new_json_doc["description"] = "a new description"

    client.update_document(
        Content=json.dumps(new_json_doc),
        Name="TestDocument3",
        DocumentVersion="$LATEST",
        VersionName="NewBase",
    )

    response = client.get_document(Name="TestDocument3")
    _get_doc_validator(response, "Base", "1", json_doc, "JSON")

    response = client.get_document(Name="TestDocument3", DocumentFormat="YAML")
    _get_doc_validator(response, "Base", "1", json_doc, "YAML")

    response = client.get_document(Name="TestDocument3", DocumentFormat="JSON")
    _get_doc_validator(response, "Base", "1", json_doc, "JSON")

    response = client.get_document(Name="TestDocument3", VersionName="Base")
    _get_doc_validator(response, "Base", "1", json_doc, "JSON")

    response = client.get_document(Name="TestDocument3", DocumentVersion="1")
    _get_doc_validator(response, "Base", "1", json_doc, "JSON")

    response = client.get_document(Name="TestDocument3", DocumentVersion="2")
    _get_doc_validator(response, "NewBase", "2", new_json_doc, "JSON")

    response = client.get_document(Name="TestDocument3", VersionName="NewBase")
    _get_doc_validator(response, "NewBase", "2", new_json_doc, "JSON")

    response = client.get_document(
        Name="TestDocument3", VersionName="NewBase", DocumentVersion="2"
    )
    _get_doc_validator(response, "NewBase", "2", new_json_doc, "JSON")

    try:
        response = client.get_document(
            Name="TestDocument3", VersionName="BadName", DocumentVersion="2"
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    try:
        response = client.get_document(Name="TestDocument3", DocumentVersion="3")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    # Updating default should update normal get
    client.update_document_default_version(Name="TestDocument3", DocumentVersion="2")

    response = client.get_document(Name="TestDocument3", DocumentFormat="JSON")
    _get_doc_validator(response, "NewBase", "2", new_json_doc, "JSON")


@mock_aws
def test_delete_document():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)
    client = boto3.client("ssm", region_name="us-east-1")

    try:
        client.delete_document(Name="DNE")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "DeleteDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    # Test simple
    client.create_document(
        Content=yaml.dump(json_doc),
        Name="TestDocument3",
        DocumentType="Command",
        DocumentFormat="YAML",
        VersionName="Base",
        TargetType="/AWS::EC2::Instance",
    )
    client.delete_document(Name="TestDocument3")

    try:
        client.get_document(Name="TestDocument3")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    # Delete default version with other version is bad
    client.create_document(
        Content=yaml.dump(json_doc),
        Name="TestDocument3",
        DocumentType="Command",
        DocumentFormat="YAML",
        VersionName="Base",
        TargetType="/AWS::EC2::Instance",
    )

    new_json_doc = copy.copy(json_doc)
    new_json_doc["description"] = "a new description"

    client.update_document(
        Content=json.dumps(new_json_doc),
        Name="TestDocument3",
        DocumentVersion="$LATEST",
        VersionName="NewBase",
    )

    new_json_doc["description"] = "a new description2"
    client.update_document(
        Content=json.dumps(new_json_doc),
        Name="TestDocument3",
        DocumentVersion="$LATEST",
    )

    new_json_doc["description"] = "a new description3"
    client.update_document(
        Content=json.dumps(new_json_doc),
        Name="TestDocument3",
        DocumentVersion="$LATEST",
    )

    new_json_doc["description"] = "a new description4"
    client.update_document(
        Content=json.dumps(new_json_doc),
        Name="TestDocument3",
        DocumentVersion="$LATEST",
    )

    try:
        client.delete_document(Name="TestDocument3", DocumentVersion="1")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "DeleteDocument"
        assert err.response["Error"]["Message"] == (
            "Default version of the document can't be deleted."
        )

    try:
        client.delete_document(Name="TestDocument3", VersionName="Base")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "DeleteDocument"
        assert err.response["Error"]["Message"] == (
            "Default version of the document can't be deleted."
        )

    # Make sure no ill side effects
    response = client.get_document(Name="TestDocument3")
    _get_doc_validator(response, "Base", "1", json_doc, "JSON")

    client.delete_document(Name="TestDocument3", DocumentVersion="5")

    # Check that latest version is changed
    response = client.describe_document(Name="TestDocument3")
    assert response["Document"]["LatestVersion"] == "4"

    client.delete_document(Name="TestDocument3", VersionName="NewBase")

    # Make sure other versions okay
    client.get_document(Name="TestDocument3", DocumentVersion="1")
    client.get_document(Name="TestDocument3", DocumentVersion="3")
    client.get_document(Name="TestDocument3", DocumentVersion="4")

    client.delete_document(Name="TestDocument3")

    try:
        client.get_document(Name="TestDocument3", DocumentVersion="1")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    try:
        client.get_document(Name="TestDocument3", DocumentVersion="3")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    try:
        client.get_document(Name="TestDocument3", DocumentVersion="4")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "GetDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    response = client.list_documents()
    assert len(response["DocumentIdentifiers"]) == 0


@mock_aws
def test_update_document_default_version():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)
    client = boto3.client("ssm", region_name="us-east-1")

    try:
        client.update_document_default_version(Name="DNE", DocumentVersion="1")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "UpdateDocumentDefaultVersion"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentType="Command",
        VersionName="Base",
    )

    json_doc["description"] = "a new description"

    client.update_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentVersion="$LATEST",
        DocumentFormat="JSON",
    )

    json_doc["description"] = "a new description2"

    client.update_document(
        Content=json.dumps(json_doc), Name="TestDocument", DocumentVersion="$LATEST"
    )

    response = client.update_document_default_version(
        Name="TestDocument", DocumentVersion="2"
    )
    assert response["Description"]["Name"] == "TestDocument"
    assert response["Description"]["DefaultVersion"] == "2"

    json_doc["description"] = "a new description3"

    client.update_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentVersion="$LATEST",
        VersionName="NewBase",
    )

    response = client.update_document_default_version(
        Name="TestDocument", DocumentVersion="4"
    )
    assert response["Description"]["Name"] == "TestDocument"
    assert response["Description"]["DefaultVersion"] == "4"
    assert response["Description"]["DefaultVersionName"] == "NewBase"


@mock_aws
def test_update_document():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)

    client = boto3.client("ssm", region_name="us-east-1")

    try:
        client.update_document(
            Name="DNE",
            Content=json.dumps(json_doc),
            DocumentVersion="1",
            DocumentFormat="JSON",
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "UpdateDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentType="Command",
        DocumentFormat="JSON",
        VersionName="Base",
    )

    try:
        client.update_document(
            Name="TestDocument",
            Content=json.dumps(json_doc),
            DocumentVersion="2",
            DocumentFormat="JSON",
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "UpdateDocument"
        assert err.response["Error"]["Message"] == (
            "The document version is not valid or does not exist."
        )

    # Duplicate content throws an error
    try:
        client.update_document(
            Content=json.dumps(json_doc),
            Name="TestDocument",
            DocumentVersion="1",
            DocumentFormat="JSON",
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "UpdateDocument"
        assert err.response["Error"]["Message"] == (
            "The content of the association document matches another "
            "document. Change the content of the document and try again."
        )

    json_doc["description"] = "a new description"
    # Duplicate version name
    try:
        client.update_document(
            Content=json.dumps(json_doc),
            Name="TestDocument",
            DocumentVersion="1",
            DocumentFormat="JSON",
            VersionName="Base",
        )
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "UpdateDocument"
        assert err.response["Error"]["Message"] == (
            "The specified version name is a duplicate."
        )

    response = client.update_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        VersionName="Base2",
        DocumentVersion="1",
        DocumentFormat="JSON",
    )
    assert response["DocumentDescription"]["Description"] == "a new description"
    assert response["DocumentDescription"]["DocumentVersion"] == "2"
    assert response["DocumentDescription"]["LatestVersion"] == "2"
    assert response["DocumentDescription"]["DefaultVersion"] == "1"

    json_doc["description"] = "a new description2"

    response = client.update_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentVersion="$LATEST",
        DocumentFormat="JSON",
        VersionName="NewBase",
    )
    assert response["DocumentDescription"]["Description"] == "a new description2"
    assert response["DocumentDescription"]["DocumentVersion"] == "3"
    assert response["DocumentDescription"]["LatestVersion"] == "3"
    assert response["DocumentDescription"]["DefaultVersion"] == "1"
    assert response["DocumentDescription"]["VersionName"] == "NewBase"


@mock_aws
def test_describe_document():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)
    client = boto3.client("ssm", region_name="us-east-1")

    try:
        client.describe_document(Name="DNE")
        raise RuntimeError("Should fail")
    except botocore.exceptions.ClientError as err:
        assert err.operation_name == "DescribeDocument"
        assert (
            err.response["Error"]["Message"] == "The specified document does not exist."
        )

    client.create_document(
        Content=yaml.dump(json_doc),
        Name="TestDocument",
        DocumentType="Command",
        DocumentFormat="YAML",
        VersionName="Base",
        TargetType="/AWS::EC2::Instance",
        Tags=[{"Key": "testing", "Value": "testingValue"}],
    )
    response = client.describe_document(Name="TestDocument")
    doc_description = response["Document"]
    _validate_document_description(
        "TestDocument", doc_description, json_doc, "1", "1", "1", "YAML"
    )

    # Adding update to check for issues
    new_json_doc = copy.copy(json_doc)
    new_json_doc["description"] = "a new description2"

    client.update_document(
        Content=json.dumps(new_json_doc), Name="TestDocument", DocumentVersion="$LATEST"
    )
    response = client.describe_document(Name="TestDocument")
    doc_description = response["Document"]
    _validate_document_description(
        "TestDocument", doc_description, json_doc, "1", "2", "1", "YAML"
    )


@mock_aws
def test_list_documents():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)

    client = boto3.client("ssm", region_name="us-east-1")

    client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentType="Command",
        DocumentFormat="JSON",
    )
    client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument2",
        DocumentType="Command",
        DocumentFormat="JSON",
    )
    client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument3",
        DocumentType="Command",
        DocumentFormat="JSON",
        TargetType="/AWS::EC2::Instance",
    )

    response = client.list_documents()
    assert len(response["DocumentIdentifiers"]) == 3
    assert response["DocumentIdentifiers"][0]["Name"] == "TestDocument"
    assert response["DocumentIdentifiers"][1]["Name"] == "TestDocument2"
    assert response["DocumentIdentifiers"][2]["Name"] == "TestDocument3"
    assert response["NextToken"] == ""

    response = client.list_documents(MaxResults=1)
    assert len(response["DocumentIdentifiers"]) == 1
    assert response["DocumentIdentifiers"][0]["Name"] == "TestDocument"
    assert response["DocumentIdentifiers"][0]["DocumentVersion"] == "1"
    assert response["NextToken"] == "1"

    response = client.list_documents(MaxResults=1, NextToken=response["NextToken"])
    assert len(response["DocumentIdentifiers"]) == 1
    assert response["DocumentIdentifiers"][0]["Name"] == "TestDocument2"
    assert response["DocumentIdentifiers"][0]["DocumentVersion"] == "1"
    assert response["NextToken"] == "2"

    response = client.list_documents(MaxResults=1, NextToken=response["NextToken"])
    assert len(response["DocumentIdentifiers"]) == 1
    assert response["DocumentIdentifiers"][0]["Name"] == "TestDocument3"
    assert response["DocumentIdentifiers"][0]["DocumentVersion"] == "1"
    assert response["NextToken"] == ""

    # making sure no bad interactions with update
    json_doc["description"] = "a new description"
    client.update_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentVersion="$LATEST",
        DocumentFormat="JSON",
    )

    client.update_document(
        Content=json.dumps(json_doc),
        Name="TestDocument2",
        DocumentVersion="$LATEST",
        DocumentFormat="JSON",
    )

    client.update_document_default_version(Name="TestDocument", DocumentVersion="2")

    response = client.list_documents()
    assert len(response["DocumentIdentifiers"]) == 3
    assert response["DocumentIdentifiers"][0]["Name"] == "TestDocument"
    assert response["DocumentIdentifiers"][0]["DocumentVersion"] == "2"

    assert response["DocumentIdentifiers"][1]["Name"] == "TestDocument2"
    assert response["DocumentIdentifiers"][1]["DocumentVersion"] == "1"

    assert response["DocumentIdentifiers"][2]["Name"] == "TestDocument3"
    assert response["DocumentIdentifiers"][2]["DocumentVersion"] == "1"
    assert response["NextToken"] == ""

    response = client.list_documents(Filters=[{"Key": "Owner", "Values": ["Self"]}])
    assert len(response["DocumentIdentifiers"]) == 3

    response = client.list_documents(
        Filters=[{"Key": "TargetType", "Values": ["/AWS::EC2::Instance"]}]
    )
    assert len(response["DocumentIdentifiers"]) == 1


@mock_aws
def test_tags_in_list_tags_from_resource_document():
    template_file = _get_yaml_template()
    json_doc = yaml.safe_load(template_file)

    client = boto3.client("ssm", region_name="us-east-1")

    client.create_document(
        Content=json.dumps(json_doc),
        Name="TestDocument",
        DocumentType="Command",
        DocumentFormat="JSON",
        Tags=[{"Key": "spam", "Value": "ham"}],
    )

    tags = client.list_tags_for_resource(
        ResourceId="TestDocument", ResourceType="Document"
    )
    assert tags.get("TagList") == [{"Key": "spam", "Value": "ham"}]

    client.delete_document(Name="TestDocument")

    with pytest.raises(ClientError) as ex:
        client.list_tags_for_resource(
            ResourceType="Document", ResourceId="TestDocument"
        )
    assert ex.value.response["Error"]["Code"] == "InvalidResourceId"