How to use label_parameter_version method in localstack

Best Python code snippet using localstack_python

test_ssm_boto3.py

Source:test_ssm_boto3.py Github

copy

Full Screen

...338 )339 client.put_parameter(340 Name="test-2", Description="A test parameter", Value="value", Type="String"341 )342 client.label_parameter_version(343 Name="test-2", ParameterVersion=1, Labels=["test-label"]344 )345 response = client.get_parameter(Name="test-1:1", WithDecryption=False)346 response["Parameter"]["Name"].should.equal("test-1")347 response["Parameter"]["Value"].should.equal("value")348 response["Parameter"]["Type"].should.equal("String")349 response["Parameter"]["LastModifiedDate"].should.be.a(datetime.datetime)350 response["Parameter"]["ARN"].should.equal(351 "arn:aws:ssm:us-east-1:1234567890:parameter/test-1"352 )353 response = client.get_parameter(Name="test-2:1", WithDecryption=False)354 response["Parameter"]["Name"].should.equal("test-2")355 response["Parameter"]["Value"].should.equal("value")356 response["Parameter"]["Type"].should.equal("String")357 response["Parameter"]["LastModifiedDate"].should.be.a(datetime.datetime)358 response["Parameter"]["ARN"].should.equal(359 "arn:aws:ssm:us-east-1:1234567890:parameter/test-2"360 )361 response = client.get_parameter(Name="test-2:test-label", WithDecryption=False)362 response["Parameter"]["Name"].should.equal("test-2")363 response["Parameter"]["Value"].should.equal("value")364 response["Parameter"]["Type"].should.equal("String")365 response["Parameter"]["LastModifiedDate"].should.be.a(datetime.datetime)366 response["Parameter"]["ARN"].should.equal(367 "arn:aws:ssm:us-east-1:1234567890:parameter/test-2"368 )369 with pytest.raises(ClientError) as ex:370 client.get_parameter(Name="test-2:2:3", WithDecryption=False)371 ex.value.response["Error"]["Code"].should.equal("ParameterNotFound")372 ex.value.response["Error"]["Message"].should.equal(373 "Parameter test-2:2:3 not found."374 )375 with pytest.raises(ClientError) as ex:376 client.get_parameter(Name="test-2:2", WithDecryption=False)377 ex.value.response["Error"]["Code"].should.equal("ParameterNotFound")378 ex.value.response["Error"]["Message"].should.equal("Parameter test-2:2 not found.")379@mock_ssm380def test_get_parameters_errors():381 client = boto3.client("ssm", region_name="us-east-1")382 ssm_parameters = {name: "value" for name in string.ascii_lowercase[:11]}383 for name, value in ssm_parameters.items():384 client.put_parameter(Name=name, Value=value, Type="String")385 with pytest.raises(ClientError) as e:386 client.get_parameters(Names=list(ssm_parameters.keys()))387 ex = e.value388 ex.operation_name.should.equal("GetParameters")389 ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)390 ex.response["Error"]["Code"].should.contain("ValidationException")391 ex.response["Error"]["Message"].should.equal(392 "1 validation error detected: "393 "Value '[{}]' at 'names' failed to satisfy constraint: "394 "Member must have length less than or equal to 10.".format(395 ", ".join(ssm_parameters.keys())396 )397 )398@mock_ssm399def test_get_nonexistant_parameter():400 client = boto3.client("ssm", region_name="us-east-1")401 try:402 client.get_parameter(Name="test_noexist", WithDecryption=False)403 raise RuntimeError("Should of failed")404 except botocore.exceptions.ClientError as err:405 err.operation_name.should.equal("GetParameter")406 err.response["Error"]["Message"].should.equal(407 "Parameter test_noexist not found."408 )409@mock_ssm410def test_describe_parameters():411 client = boto3.client("ssm", region_name="us-east-1")412 client.put_parameter(413 Name="test",414 Description="A test parameter",415 Value="value",416 Type="String",417 AllowedPattern=r".*",418 )419 response = client.describe_parameters()420 parameters = response["Parameters"]421 parameters.should.have.length_of(1)422 parameters[0]["Name"].should.equal("test")423 parameters[0]["Type"].should.equal("String")424 parameters[0]["AllowedPattern"].should.equal(r".*")425@mock_ssm426def test_describe_parameters_paging():427 client = boto3.client("ssm", region_name="us-east-1")428 for i in range(50):429 client.put_parameter(Name="param-%d" % i, Value="value-%d" % i, Type="String")430 response = client.describe_parameters()431 response["Parameters"].should.have.length_of(10)432 response["NextToken"].should.equal("10")433 response = client.describe_parameters(NextToken=response["NextToken"])434 response["Parameters"].should.have.length_of(10)435 response["NextToken"].should.equal("20")436 response = client.describe_parameters(NextToken=response["NextToken"])437 response["Parameters"].should.have.length_of(10)438 response["NextToken"].should.equal("30")439 response = client.describe_parameters(NextToken=response["NextToken"])440 response["Parameters"].should.have.length_of(10)441 response["NextToken"].should.equal("40")442 response = client.describe_parameters(NextToken=response["NextToken"])443 response["Parameters"].should.have.length_of(10)444 response["NextToken"].should.equal("50")445 response = client.describe_parameters(NextToken=response["NextToken"])446 response["Parameters"].should.have.length_of(0)447 response.should_not.have.key("NextToken")448@mock_ssm449def test_describe_parameters_filter_names():450 client = boto3.client("ssm", region_name="us-east-1")451 for i in range(50):452 p = {"Name": "param-%d" % i, "Value": "value-%d" % i, "Type": "String"}453 if i % 5 == 0:454 p["Type"] = "SecureString"455 p["KeyId"] = "a key"456 client.put_parameter(**p)457 response = client.describe_parameters(458 Filters=[{"Key": "Name", "Values": ["param-22"]}]459 )460 parameters = response["Parameters"]461 parameters.should.have.length_of(1)462 parameters[0]["Name"].should.equal("param-22")463 parameters[0]["Type"].should.equal("String")464 response.should_not.have.key("NextToken")465@mock_ssm466def test_describe_parameters_filter_type():467 client = boto3.client("ssm", region_name="us-east-1")468 for i in range(50):469 p = {"Name": "param-%d" % i, "Value": "value-%d" % i, "Type": "String"}470 if i % 5 == 0:471 p["Type"] = "SecureString"472 p["KeyId"] = "a key"473 client.put_parameter(**p)474 response = client.describe_parameters(475 Filters=[{"Key": "Type", "Values": ["SecureString"]}]476 )477 parameters = response["Parameters"]478 parameters.should.have.length_of(10)479 parameters[0]["Type"].should.equal("SecureString")480 response.should.have.key("NextToken").which.should.equal("10")481@mock_ssm482def test_describe_parameters_filter_keyid():483 client = boto3.client("ssm", region_name="us-east-1")484 for i in range(50):485 p = {"Name": "param-%d" % i, "Value": "value-%d" % i, "Type": "String"}486 if i % 5 == 0:487 p["Type"] = "SecureString"488 p["KeyId"] = "key:%d" % i489 client.put_parameter(**p)490 response = client.describe_parameters(491 Filters=[{"Key": "KeyId", "Values": ["key:10"]}]492 )493 parameters = response["Parameters"]494 parameters.should.have.length_of(1)495 parameters[0]["Name"].should.equal("param-10")496 parameters[0]["Type"].should.equal("SecureString")497 response.should_not.have.key("NextToken")498@mock_ssm499def test_describe_parameters_with_parameter_filters_keyid():500 client = boto3.client("ssm", region_name="us-east-1")501 client.put_parameter(Name="secure-param", Value="secure-value", Type="SecureString")502 client.put_parameter(503 Name="custom-secure-param",504 Value="custom-secure-value",505 Type="SecureString",506 KeyId="alias/custom",507 )508 client.put_parameter(Name="param", Value="value", Type="String")509 response = client.describe_parameters(510 ParameterFilters=[{"Key": "KeyId", "Values": ["alias/aws/ssm"]}]511 )512 parameters = response["Parameters"]513 parameters.should.have.length_of(1)514 parameters[0]["Name"].should.equal("secure-param")515 parameters[0]["Type"].should.equal("SecureString")516 response.should_not.have.key("NextToken")517 response = client.describe_parameters(518 ParameterFilters=[{"Key": "KeyId", "Values": ["alias/custom"]}]519 )520 parameters = response["Parameters"]521 parameters.should.have.length_of(1)522 parameters[0]["Name"].should.equal("custom-secure-param")523 parameters[0]["Type"].should.equal("SecureString")524 response.should_not.have.key("NextToken")525 response = client.describe_parameters(526 ParameterFilters=[{"Key": "KeyId", "Option": "BeginsWith", "Values": ["alias"]}]527 )528 parameters = response["Parameters"]529 parameters.should.have.length_of(2)530 response.should_not.have.key("NextToken")531@mock_ssm532def test_describe_parameters_with_parameter_filters_name():533 client = boto3.client("ssm", region_name="us-east-1")534 client.put_parameter(Name="param", Value="value", Type="String")535 client.put_parameter(Name="/param-2", Value="value-2", Type="String")536 client.put_parameter(Name="/tangent-3", Value="value-3", Type="String")537 client.put_parameter(Name="tangram-4", Value="value-4", Type="String")538 client.put_parameter(Name="standby-5", Value="value-5", Type="String")539 response = client.describe_parameters(540 ParameterFilters=[{"Key": "Name", "Values": ["param"]}]541 )542 parameters = response["Parameters"]543 parameters.should.have.length_of(1)544 parameters[0]["Name"].should.equal("param")545 parameters[0]["Type"].should.equal("String")546 response.should_not.have.key("NextToken")547 response = client.describe_parameters(548 ParameterFilters=[{"Key": "Name", "Values": ["/param"]}]549 )550 parameters = response["Parameters"]551 parameters.should.have.length_of(1)552 parameters[0]["Name"].should.equal("param")553 parameters[0]["Type"].should.equal("String")554 response.should_not.have.key("NextToken")555 response = client.describe_parameters(556 ParameterFilters=[{"Key": "Name", "Values": ["param-2"]}]557 )558 parameters = response["Parameters"]559 parameters.should.have.length_of(1)560 parameters[0]["Name"].should.equal("/param-2")561 parameters[0]["Type"].should.equal("String")562 response.should_not.have.key("NextToken")563 response = client.describe_parameters(564 ParameterFilters=[{"Key": "Name", "Option": "BeginsWith", "Values": ["param"]}]565 )566 parameters = response["Parameters"]567 parameters.should.have.length_of(2)568 response.should_not.have.key("NextToken")569 response = client.describe_parameters(570 ParameterFilters=[{"Key": "Name", "Option": "Contains", "Values": ["ram"]}]571 )572 parameters = response["Parameters"]573 parameters.should.have.length_of(3)574 response.should_not.have.key("NextToken")575 response = client.describe_parameters(576 ParameterFilters=[{"Key": "Name", "Option": "Contains", "Values": ["/tan"]}]577 )578 parameters = response["Parameters"]579 parameters.should.have.length_of(2)580 response.should_not.have.key("NextToken")581@mock_ssm582def test_describe_parameters_with_parameter_filters_path():583 client = boto3.client("ssm", region_name="us-east-1")584 client.put_parameter(Name="/foo/name1", Value="value1", Type="String")585 client.put_parameter(Name="/foo/name2", Value="value2", Type="String")586 client.put_parameter(Name="/bar/name3", Value="value3", Type="String")587 client.put_parameter(Name="/bar/name3/name4", Value="value4", Type="String")588 client.put_parameter(Name="foo", Value="bar", Type="String")589 response = client.describe_parameters(590 ParameterFilters=[{"Key": "Path", "Values": ["/fo"]}]591 )592 parameters = response["Parameters"]593 parameters.should.have.length_of(0)594 response.should_not.have.key("NextToken")595 response = client.describe_parameters(596 ParameterFilters=[{"Key": "Path", "Values": ["/"]}]597 )598 parameters = response["Parameters"]599 parameters.should.have.length_of(1)600 parameters[0]["Name"].should.equal("foo")601 parameters[0]["Type"].should.equal("String")602 response.should_not.have.key("NextToken")603 response = client.describe_parameters(604 ParameterFilters=[{"Key": "Path", "Values": ["/", "/foo"]}]605 )606 parameters = response["Parameters"]607 parameters.should.have.length_of(3)608 {parameter["Name"] for parameter in response["Parameters"]}.should.equal(609 {"/foo/name1", "/foo/name2", "foo"}610 )611 response.should_not.have.key("NextToken")612 response = client.describe_parameters(613 ParameterFilters=[{"Key": "Path", "Values": ["/foo/"]}]614 )615 parameters = response["Parameters"]616 parameters.should.have.length_of(2)617 {parameter["Name"] for parameter in response["Parameters"]}.should.equal(618 {"/foo/name1", "/foo/name2"}619 )620 response.should_not.have.key("NextToken")621 response = client.describe_parameters(622 ParameterFilters=[623 {"Key": "Path", "Option": "OneLevel", "Values": ["/bar/name3"]}624 ]625 )626 parameters = response["Parameters"]627 parameters.should.have.length_of(1)628 parameters[0]["Name"].should.equal("/bar/name3/name4")629 parameters[0]["Type"].should.equal("String")630 response.should_not.have.key("NextToken")631 response = client.describe_parameters(632 ParameterFilters=[{"Key": "Path", "Option": "Recursive", "Values": ["/fo"]}]633 )634 parameters = response["Parameters"]635 parameters.should.have.length_of(0)636 response.should_not.have.key("NextToken")637 response = client.describe_parameters(638 ParameterFilters=[{"Key": "Path", "Option": "Recursive", "Values": ["/"]}]639 )640 parameters = response["Parameters"]641 parameters.should.have.length_of(5)642 response.should_not.have.key("NextToken")643 response = client.describe_parameters(644 ParameterFilters=[645 {"Key": "Path", "Option": "Recursive", "Values": ["/foo", "/bar"]}646 ]647 )648 parameters = response["Parameters"]649 parameters.should.have.length_of(4)650 {parameter["Name"] for parameter in response["Parameters"]}.should.equal(651 {"/foo/name1", "/foo/name2", "/bar/name3", "/bar/name3/name4"}652 )653 response.should_not.have.key("NextToken")654 response = client.describe_parameters(655 ParameterFilters=[{"Key": "Path", "Option": "Recursive", "Values": ["/foo/"]}]656 )657 parameters = response["Parameters"]658 parameters.should.have.length_of(2)659 {parameter["Name"] for parameter in response["Parameters"]}.should.equal(660 {"/foo/name1", "/foo/name2"}661 )662 response.should_not.have.key("NextToken")663 response = client.describe_parameters(664 ParameterFilters=[665 {"Key": "Path", "Option": "Recursive", "Values": ["/bar/name3"]}666 ]667 )668 parameters = response["Parameters"]669 parameters.should.have.length_of(1)670 parameters[0]["Name"].should.equal("/bar/name3/name4")671 parameters[0]["Type"].should.equal("String")672 response.should_not.have.key("NextToken")673@mock_ssm674def test_describe_parameters_needs_param():675 client = boto3.client("ssm", region_name="us-east-1")676 client.describe_parameters.when.called_with(677 Filters=[{"Key": "Name", "Values": ["test"]}],678 ParameterFilters=[{"Key": "Name", "Values": ["test"]}],679 ).should.throw(680 ClientError,681 "You can use either Filters or ParameterFilters in a single request.",682 )683@pytest.mark.parametrize(684 "filters,error_msg",685 [686 (687 [{"Key": "key"}],688 "Member must satisfy regular expression pattern: tag:.+|Name|Type|KeyId|Path|Label|Tier",689 ),690 (691 [{"Key": "tag:" + "t" * 129}],692 "Member must have length less than or equal to 132",693 ),694 (695 [{"Key": "Name", "Option": "over 10 chars"}],696 "Member must have length less than or equal to 10",697 ),698 (699 [{"Key": "Name", "Values": ["test"] * 51}],700 "Member must have length less than or equal to 50",701 ),702 (703 [{"Key": "Name", "Values": ["t" * 1025]}],704 "Member must have length less than or equal to 1024, Member must have length greater than or equal to 1",705 ),706 (707 [{"Key": "Name", "Option": "over 10 chars"}, {"Key": "key"}],708 "2 validation errors detected:",709 ),710 (711 [{"Key": "Label"}],712 "The following filter key is not valid: Label. Valid filter keys include: [Path, Name, Type, KeyId, Tier]",713 ),714 (715 [{"Key": "Name"}],716 "The following filter values are missing : null for filter key Name",717 ),718 (719 [720 {"Key": "Name", "Values": ["test"]},721 {"Key": "Name", "Values": ["test test"]},722 ],723 "The following filter is duplicated in the request: Name. A request can contain only one occurrence of a specific filter.",724 ),725 (726 [{"Key": "Path", "Values": ["/aws", "/ssm"]}],727 'Filters for common parameters can\'t be prefixed with "aws" or "ssm" (case-insensitive).',728 ),729 (730 [{"Key": "Path", "Option": "Equals", "Values": ["test"]}],731 "The following filter option is not valid: Equals. Valid options include: [Recursive, OneLevel]",732 ),733 (734 [{"Key": "Tier", "Values": ["test"]}],735 "The following filter value is not valid: test. Valid values include: [Standard, Advanced, Intelligent-Tiering]",736 ),737 (738 [{"Key": "Type", "Values": ["test"]}],739 "The following filter value is not valid: test. Valid values include: [String, StringList, SecureString]",740 ),741 (742 [{"Key": "Name", "Option": "option", "Values": ["test"]}],743 "The following filter option is not valid: option. Valid options include: [BeginsWith, Equals].",744 ),745 ],746)747@mock_ssm748def test_describe_parameters_invalid_parameter_filters(filters, error_msg):749 client = boto3.client("ssm", region_name="us-east-1")750 with pytest.raises(ClientError) as e:751 client.describe_parameters(ParameterFilters=filters)752 e.value.response["Error"]["Message"].should.contain(error_msg)753@pytest.mark.parametrize("value", ["/###", "//", "test"])754@mock_ssm755def test_describe_parameters_invalid_path(value):756 client = boto3.client("ssm", region_name="us-east-1")757 with pytest.raises(ClientError) as e:758 client.describe_parameters(759 ParameterFilters=[{"Key": "Path", "Values": [value]}]760 )761 msg = e.value.response["Error"]["Message"]762 msg.should.contain("The parameter doesn't meet the parameter name requirements")763 msg.should.contain('The parameter name must begin with a forward slash "/".')764 msg.should.contain('It can\'t be prefixed with "aws" or "ssm" (case-insensitive).')765 msg.should.contain(766 "It must use only letters, numbers, or the following symbols: . (period), - (hyphen), _ (underscore)."767 )768 msg.should.contain(769 'Special characters are not allowed. All sub-paths, if specified, must use the forward slash symbol "/".'770 )771 msg.should.contain("Valid example: /get/parameters2-/by1./path0_.")772@mock_ssm773def test_describe_parameters_attributes():774 client = boto3.client("ssm", region_name="us-east-1")775 client.put_parameter(776 Name="aa", Value="11", Type="String", Description="my description"777 )778 client.put_parameter(Name="bb", Value="22", Type="String")779 response = client.describe_parameters()780 parameters = response["Parameters"]781 parameters.should.have.length_of(2)782 parameters[0]["Description"].should.equal("my description")783 parameters[0]["Version"].should.equal(1)784 parameters[0]["LastModifiedDate"].should.be.a(datetime.date)785 parameters[0]["LastModifiedUser"].should.equal("N/A")786 parameters[1].should_not.have.key("Description")787 parameters[1]["Version"].should.equal(1)788@mock_ssm789def test_describe_parameters_tags():790 client = boto3.client("ssm", region_name="us-east-1")791 client.put_parameter(Name="/foo/bar", Value="spam", Type="String")792 client.put_parameter(793 Name="/spam/eggs",794 Value="eggs",795 Type="String",796 Tags=[{"Key": "spam", "Value": "eggs"}],797 )798 response = client.describe_parameters(799 ParameterFilters=[{"Key": "tag:spam", "Values": ["eggs"]}]800 )801 parameters = response["Parameters"]802 parameters.should.have.length_of(1)803 parameters[0]["Name"].should.equal("/spam/eggs")804@mock_ssm805def test_tags_in_list_tags_from_resource():806 client = boto3.client("ssm", region_name="us-east-1")807 client.put_parameter(808 Name="/spam/eggs",809 Value="eggs",810 Type="String",811 Tags=[{"Key": "spam", "Value": "eggs"}],812 )813 tags = client.list_tags_for_resource(814 ResourceId="/spam/eggs", ResourceType="Parameter"815 )816 assert tags.get("TagList") == [{"Key": "spam", "Value": "eggs"}]817@mock_ssm818def test_get_parameter_invalid():819 client = client = boto3.client("ssm", region_name="us-east-1")820 response = client.get_parameters(Names=["invalid"], WithDecryption=False)821 len(response["Parameters"]).should.equal(0)822 len(response["InvalidParameters"]).should.equal(1)823 response["InvalidParameters"][0].should.equal("invalid")824@mock_ssm825def test_put_parameter_secure_default_kms():826 client = boto3.client("ssm", region_name="us-east-1")827 client.put_parameter(828 Name="test", Description="A test parameter", Value="value", Type="SecureString"829 )830 response = client.get_parameters(Names=["test"], WithDecryption=False)831 len(response["Parameters"]).should.equal(1)832 response["Parameters"][0]["Name"].should.equal("test")833 response["Parameters"][0]["Value"].should.equal("kms:alias/aws/ssm:value")834 response["Parameters"][0]["Type"].should.equal("SecureString")835 response = client.get_parameters(Names=["test"], WithDecryption=True)836 len(response["Parameters"]).should.equal(1)837 response["Parameters"][0]["Name"].should.equal("test")838 response["Parameters"][0]["Value"].should.equal("value")839 response["Parameters"][0]["Type"].should.equal("SecureString")840@mock_ssm841def test_put_parameter_secure_custom_kms():842 client = boto3.client("ssm", region_name="us-east-1")843 client.put_parameter(844 Name="test",845 Description="A test parameter",846 Value="value",847 Type="SecureString",848 KeyId="foo",849 )850 response = client.get_parameters(Names=["test"], WithDecryption=False)851 len(response["Parameters"]).should.equal(1)852 response["Parameters"][0]["Name"].should.equal("test")853 response["Parameters"][0]["Value"].should.equal("kms:foo:value")854 response["Parameters"][0]["Type"].should.equal("SecureString")855 response = client.get_parameters(Names=["test"], WithDecryption=True)856 len(response["Parameters"]).should.equal(1)857 response["Parameters"][0]["Name"].should.equal("test")858 response["Parameters"][0]["Value"].should.equal("value")859 response["Parameters"][0]["Type"].should.equal("SecureString")860@mock_ssm861def test_get_parameter_history():862 client = boto3.client("ssm", region_name="us-east-1")863 test_parameter_name = "test"864 for i in range(3):865 client.put_parameter(866 Name=test_parameter_name,867 Description="A test parameter version %d" % i,868 Value="value-%d" % i,869 Type="String",870 Overwrite=True,871 )872 response = client.get_parameter_history(Name=test_parameter_name)873 parameters_response = response["Parameters"]874 for index, param in enumerate(parameters_response):875 param["Name"].should.equal(test_parameter_name)876 param["Type"].should.equal("String")877 param["Value"].should.equal("value-%d" % index)878 param["Version"].should.equal(index + 1)879 param["Description"].should.equal("A test parameter version %d" % index)880 param["Labels"].should.equal([])881 len(parameters_response).should.equal(3)882@mock_ssm883def test_get_parameter_history_with_secure_string():884 client = boto3.client("ssm", region_name="us-east-1")885 test_parameter_name = "test"886 for i in range(3):887 client.put_parameter(888 Name=test_parameter_name,889 Description="A test parameter version %d" % i,890 Value="value-%d" % i,891 Type="SecureString",892 Overwrite=True,893 )894 for with_decryption in [True, False]:895 response = client.get_parameter_history(896 Name=test_parameter_name, WithDecryption=with_decryption897 )898 parameters_response = response["Parameters"]899 for index, param in enumerate(parameters_response):900 param["Name"].should.equal(test_parameter_name)901 param["Type"].should.equal("SecureString")902 expected_plaintext_value = "value-%d" % index903 if with_decryption:904 param["Value"].should.equal(expected_plaintext_value)905 else:906 param["Value"].should.equal(907 "kms:alias/aws/ssm:%s" % expected_plaintext_value908 )909 param["Version"].should.equal(index + 1)910 param["Description"].should.equal("A test parameter version %d" % index)911 len(parameters_response).should.equal(3)912@mock_ssm913def test_label_parameter_version():914 client = boto3.client("ssm", region_name="us-east-1")915 test_parameter_name = "test"916 client.put_parameter(917 Name=test_parameter_name,918 Description="A test parameter",919 Value="value",920 Type="String",921 )922 response = client.label_parameter_version(923 Name=test_parameter_name, Labels=["test-label"]924 )925 response["InvalidLabels"].should.equal([])926 response["ParameterVersion"].should.equal(1)927@mock_ssm928def test_label_parameter_version_with_specific_version():929 client = boto3.client("ssm", region_name="us-east-1")930 test_parameter_name = "test"931 client.put_parameter(932 Name=test_parameter_name,933 Description="A test parameter",934 Value="value",935 Type="String",936 )937 response = client.label_parameter_version(938 Name=test_parameter_name, ParameterVersion=1, Labels=["test-label"]939 )940 response["InvalidLabels"].should.equal([])941 response["ParameterVersion"].should.equal(1)942@mock_ssm943def test_label_parameter_version_twice():944 client = boto3.client("ssm", region_name="us-east-1")945 test_parameter_name = "test"946 test_labels = ["test-label"]947 client.put_parameter(948 Name=test_parameter_name,949 Description="A test parameter",950 Value="value",951 Type="String",952 )953 response = client.label_parameter_version(954 Name=test_parameter_name, ParameterVersion=1, Labels=test_labels955 )956 response["InvalidLabels"].should.equal([])957 response["ParameterVersion"].should.equal(1)958 response = client.label_parameter_version(959 Name=test_parameter_name, ParameterVersion=1, Labels=test_labels960 )961 response["InvalidLabels"].should.equal([])962 response["ParameterVersion"].should.equal(1)963 response = client.get_parameter_history(Name=test_parameter_name)964 len(response["Parameters"]).should.equal(1)965 response["Parameters"][0]["Labels"].should.equal(test_labels)966@mock_ssm967def test_label_parameter_moving_versions():968 client = boto3.client("ssm", region_name="us-east-1")969 test_parameter_name = "test"970 test_labels = ["test-label"]971 for i in range(3):972 client.put_parameter(973 Name=test_parameter_name,974 Description="A test parameter version %d" % i,975 Value="value-%d" % i,976 Type="String",977 Overwrite=True,978 )979 response = client.label_parameter_version(980 Name=test_parameter_name, ParameterVersion=1, Labels=test_labels981 )982 response["InvalidLabels"].should.equal([])983 response["ParameterVersion"].should.equal(1)984 response = client.label_parameter_version(985 Name=test_parameter_name, ParameterVersion=2, Labels=test_labels986 )987 response["InvalidLabels"].should.equal([])988 response["ParameterVersion"].should.equal(2)989 response = client.get_parameter_history(Name=test_parameter_name)990 parameters_response = response["Parameters"]991 for index, param in enumerate(parameters_response):992 param["Name"].should.equal(test_parameter_name)993 param["Type"].should.equal("String")994 param["Value"].should.equal("value-%d" % index)995 param["Version"].should.equal(index + 1)996 param["Description"].should.equal("A test parameter version %d" % index)997 labels = test_labels if param["Version"] == 2 else []998 param["Labels"].should.equal(labels)999 len(parameters_response).should.equal(3)1000@mock_ssm1001def test_label_parameter_moving_versions_complex():1002 client = boto3.client("ssm", region_name="us-east-1")1003 test_parameter_name = "test"1004 for i in range(3):1005 client.put_parameter(1006 Name=test_parameter_name,1007 Description="A test parameter version %d" % i,1008 Value="value-%d" % i,1009 Type="String",1010 Overwrite=True,1011 )1012 response = client.label_parameter_version(1013 Name=test_parameter_name,1014 ParameterVersion=1,1015 Labels=["test-label1", "test-label2", "test-label3"],1016 )1017 response["InvalidLabels"].should.equal([])1018 response["ParameterVersion"].should.equal(1)1019 response = client.label_parameter_version(1020 Name=test_parameter_name,1021 ParameterVersion=2,1022 Labels=["test-label2", "test-label3"],1023 )1024 response["InvalidLabels"].should.equal([])1025 response["ParameterVersion"].should.equal(2)1026 response = client.get_parameter_history(Name=test_parameter_name)1027 parameters_response = response["Parameters"]1028 for index, param in enumerate(parameters_response):1029 param["Name"].should.equal(test_parameter_name)1030 param["Type"].should.equal("String")1031 param["Value"].should.equal("value-%d" % index)1032 param["Version"].should.equal(index + 1)1033 param["Description"].should.equal("A test parameter version %d" % index)1034 labels = (1035 ["test-label2", "test-label3"]1036 if param["Version"] == 21037 else (["test-label1"] if param["Version"] == 1 else [])1038 )1039 param["Labels"].should.equal(labels)1040 len(parameters_response).should.equal(3)1041@mock_ssm1042def test_label_parameter_version_exception_ten_labels_at_once():1043 client = boto3.client("ssm", region_name="us-east-1")1044 test_parameter_name = "test"1045 test_labels = [1046 "test-label1",1047 "test-label2",1048 "test-label3",1049 "test-label4",1050 "test-label5",1051 "test-label6",1052 "test-label7",1053 "test-label8",1054 "test-label9",1055 "test-label10",1056 "test-label11",1057 ]1058 client.put_parameter(1059 Name=test_parameter_name,1060 Description="A test parameter",1061 Value="value",1062 Type="String",1063 )1064 client.label_parameter_version.when.called_with(1065 Name="test", ParameterVersion=1, Labels=test_labels1066 ).should.throw(1067 ClientError,1068 "An error occurred (ParameterVersionLabelLimitExceeded) when calling the LabelParameterVersion operation: "1069 "A parameter version can have maximum 10 labels."1070 "Move one or more labels to another version and try again.",1071 )1072@mock_ssm1073def test_label_parameter_version_exception_ten_labels_over_multiple_calls():1074 client = boto3.client("ssm", region_name="us-east-1")1075 test_parameter_name = "test"1076 client.put_parameter(1077 Name=test_parameter_name,1078 Description="A test parameter",1079 Value="value",1080 Type="String",1081 )1082 client.label_parameter_version(1083 Name=test_parameter_name,1084 ParameterVersion=1,1085 Labels=[1086 "test-label1",1087 "test-label2",1088 "test-label3",1089 "test-label4",1090 "test-label5",1091 ],1092 )1093 client.label_parameter_version.when.called_with(1094 Name="test",1095 ParameterVersion=1,1096 Labels=[1097 "test-label6",1098 "test-label7",1099 "test-label8",1100 "test-label9",1101 "test-label10",1102 "test-label11",1103 ],1104 ).should.throw(1105 ClientError,1106 "An error occurred (ParameterVersionLabelLimitExceeded) when calling the LabelParameterVersion operation: "1107 "A parameter version can have maximum 10 labels."1108 "Move one or more labels to another version and try again.",1109 )1110@mock_ssm1111def test_label_parameter_version_invalid_name():1112 client = boto3.client("ssm", region_name="us-east-1")1113 test_parameter_name = "test"1114 response = client.label_parameter_version.when.called_with(1115 Name=test_parameter_name, Labels=["test-label"]1116 ).should.throw(1117 ClientError,1118 "An error occurred (ParameterNotFound) when calling the LabelParameterVersion operation: "1119 "Parameter test not found.",1120 )1121@mock_ssm1122def test_label_parameter_version_invalid_parameter_version():1123 client = boto3.client("ssm", region_name="us-east-1")1124 test_parameter_name = "test"1125 client.put_parameter(1126 Name=test_parameter_name,1127 Description="A test parameter",1128 Value="value",1129 Type="String",1130 )1131 response = client.label_parameter_version.when.called_with(1132 Name=test_parameter_name, Labels=["test-label"], ParameterVersion=51133 ).should.throw(1134 ClientError,1135 "An error occurred (ParameterVersionNotFound) when calling the LabelParameterVersion operation: "1136 "Systems Manager could not find version 5 of test. "1137 "Verify the version and try again.",1138 )1139@mock_ssm1140def test_label_parameter_version_invalid_label():1141 client = boto3.client("ssm", region_name="us-east-1")1142 test_parameter_name = "test"1143 client.put_parameter(1144 Name=test_parameter_name,1145 Description="A test parameter",1146 Value="value",1147 Type="String",1148 )1149 response = client.label_parameter_version(1150 Name=test_parameter_name, ParameterVersion=1, Labels=["awsabc"]1151 )1152 response["InvalidLabels"].should.equal(["awsabc"])1153 response = client.label_parameter_version(1154 Name=test_parameter_name, ParameterVersion=1, Labels=["ssmabc"]1155 )1156 response["InvalidLabels"].should.equal(["ssmabc"])1157 response = client.label_parameter_version(1158 Name=test_parameter_name, ParameterVersion=1, Labels=["9abc"]1159 )1160 response["InvalidLabels"].should.equal(["9abc"])1161 response = client.label_parameter_version(1162 Name=test_parameter_name, ParameterVersion=1, Labels=["abc/123"]1163 )1164 response["InvalidLabels"].should.equal(["abc/123"])1165 client.label_parameter_version.when.called_with(1166 Name=test_parameter_name, ParameterVersion=1, Labels=["a" * 101]1167 ).should.throw(1168 ClientError,1169 "1 validation error detected: "1170 "Value '[%s]' at 'labels' failed to satisfy constraint: "1171 "Member must satisfy constraint: "1172 "[Member must have length less than or equal to 100, Member must have length greater than or equal to 1]"1173 % ("a" * 101),1174 )1175@mock_ssm1176def test_get_parameter_history_with_label():1177 client = boto3.client("ssm", region_name="us-east-1")1178 test_parameter_name = "test"1179 test_labels = ["test-label"]1180 for i in range(3):1181 client.put_parameter(1182 Name=test_parameter_name,1183 Description="A test parameter version %d" % i,1184 Value="value-%d" % i,1185 Type="String",1186 Overwrite=True,1187 )1188 client.label_parameter_version(1189 Name=test_parameter_name, ParameterVersion=1, Labels=test_labels1190 )1191 response = client.get_parameter_history(Name=test_parameter_name)1192 parameters_response = response["Parameters"]1193 for index, param in enumerate(parameters_response):1194 param["Name"].should.equal(test_parameter_name)1195 param["Type"].should.equal("String")1196 param["Value"].should.equal("value-%d" % index)1197 param["Version"].should.equal(index + 1)1198 param["Description"].should.equal("A test parameter version %d" % index)1199 labels = test_labels if param["Version"] == 1 else []1200 param["Labels"].should.equal(labels)1201 len(parameters_response).should.equal(3)1202@mock_ssm1203def test_get_parameter_history_with_label_non_latest():1204 client = boto3.client("ssm", region_name="us-east-1")1205 test_parameter_name = "test"1206 test_labels = ["test-label"]1207 for i in range(3):1208 client.put_parameter(1209 Name=test_parameter_name,1210 Description="A test parameter version %d" % i,1211 Value="value-%d" % i,1212 Type="String",1213 Overwrite=True,1214 )1215 client.label_parameter_version(1216 Name=test_parameter_name, ParameterVersion=2, Labels=test_labels1217 )1218 response = client.get_parameter_history(Name=test_parameter_name)1219 parameters_response = response["Parameters"]1220 for index, param in enumerate(parameters_response):1221 param["Name"].should.equal(test_parameter_name)1222 param["Type"].should.equal("String")1223 param["Value"].should.equal("value-%d" % index)1224 param["Version"].should.equal(index + 1)1225 param["Description"].should.equal("A test parameter version %d" % index)1226 labels = test_labels if param["Version"] == 2 else []1227 param["Labels"].should.equal(labels)1228 len(parameters_response).should.equal(3)1229@mock_ssm1230def test_get_parameter_history_with_label_latest_assumed():1231 client = boto3.client("ssm", region_name="us-east-1")1232 test_parameter_name = "test"1233 test_labels = ["test-label"]1234 for i in range(3):1235 client.put_parameter(1236 Name=test_parameter_name,1237 Description="A test parameter version %d" % i,1238 Value="value-%d" % i,1239 Type="String",1240 Overwrite=True,1241 )1242 client.label_parameter_version(Name=test_parameter_name, Labels=test_labels)1243 response = client.get_parameter_history(Name=test_parameter_name)1244 parameters_response = response["Parameters"]1245 for index, param in enumerate(parameters_response):1246 param["Name"].should.equal(test_parameter_name)1247 param["Type"].should.equal("String")1248 param["Value"].should.equal("value-%d" % index)1249 param["Version"].should.equal(index + 1)1250 param["Description"].should.equal("A test parameter version %d" % index)1251 labels = test_labels if param["Version"] == 3 else []1252 param["Labels"].should.equal(labels)1253 len(parameters_response).should.equal(3)1254@mock_ssm1255def test_get_parameter_history_missing_parameter():1256 client = boto3.client("ssm", region_name="us-east-1")1257 try:1258 client.get_parameter_history(Name="test_noexist")1259 raise RuntimeError("Should have failed")1260 except botocore.exceptions.ClientError as err:1261 err.operation_name.should.equal("GetParameterHistory")1262 err.response["Error"]["Message"].should.equal(1263 "Parameter test_noexist not found."1264 )1265@mock_ssm1266def test_add_remove_list_tags_for_resource():1267 client = boto3.client("ssm", region_name="us-east-1")1268 client.add_tags_to_resource(1269 ResourceId="test",1270 ResourceType="Parameter",1271 Tags=[{"Key": "test-key", "Value": "test-value"}],1272 )1273 response = client.list_tags_for_resource(1274 ResourceId="test", ResourceType="Parameter"1275 )1276 len(response["TagList"]).should.equal(1)1277 response["TagList"][0]["Key"].should.equal("test-key")1278 response["TagList"][0]["Value"].should.equal("test-value")1279 client.remove_tags_from_resource(1280 ResourceId="test", ResourceType="Parameter", TagKeys=["test-key"]1281 )1282 response = client.list_tags_for_resource(1283 ResourceId="test", ResourceType="Parameter"1284 )1285 len(response["TagList"]).should.equal(0)1286@mock_ssm1287def test_send_command():1288 ssm_document = "AWS-RunShellScript"1289 params = {"commands": ["#!/bin/bash\necho 'hello world'"]}1290 client = boto3.client("ssm", region_name="us-east-1")1291 # note the timeout is determined server side, so this is a simpler check.1292 before = datetime.datetime.now()1293 response = client.send_command(1294 InstanceIds=["i-123456"],1295 DocumentName=ssm_document,1296 Parameters=params,1297 OutputS3Region="us-east-2",1298 OutputS3BucketName="the-bucket",1299 OutputS3KeyPrefix="pref",1300 )1301 cmd = response["Command"]1302 cmd["CommandId"].should_not.be(None)1303 cmd["DocumentName"].should.equal(ssm_document)1304 cmd["Parameters"].should.equal(params)1305 cmd["OutputS3Region"].should.equal("us-east-2")1306 cmd["OutputS3BucketName"].should.equal("the-bucket")1307 cmd["OutputS3KeyPrefix"].should.equal("pref")1308 cmd["ExpiresAfter"].should.be.greater_than(before)1309 # test sending a command without any optional parameters1310 response = client.send_command(DocumentName=ssm_document)1311 cmd = response["Command"]1312 cmd["CommandId"].should_not.be(None)1313 cmd["DocumentName"].should.equal(ssm_document)1314@mock_ssm1315def test_list_commands():1316 client = boto3.client("ssm", region_name="us-east-1")1317 ssm_document = "AWS-RunShellScript"1318 params = {"commands": ["#!/bin/bash\necho 'hello world'"]}1319 response = client.send_command(1320 InstanceIds=["i-123456"],1321 DocumentName=ssm_document,1322 Parameters=params,1323 OutputS3Region="us-east-2",1324 OutputS3BucketName="the-bucket",1325 OutputS3KeyPrefix="pref",1326 )1327 cmd = response["Command"]1328 cmd_id = cmd["CommandId"]1329 # get the command by id1330 response = client.list_commands(CommandId=cmd_id)1331 cmds = response["Commands"]1332 len(cmds).should.equal(1)1333 cmds[0]["CommandId"].should.equal(cmd_id)1334 # add another command with the same instance id to test listing by1335 # instance id1336 client.send_command(InstanceIds=["i-123456"], DocumentName=ssm_document)1337 response = client.list_commands(InstanceId="i-123456")1338 cmds = response["Commands"]1339 len(cmds).should.equal(2)1340 for cmd in cmds:1341 cmd["InstanceIds"].should.contain("i-123456")1342 # test the error case for an invalid command id1343 with pytest.raises(ClientError):1344 response = client.list_commands(CommandId=str(uuid.uuid4()))1345@mock_ssm1346def test_get_command_invocation():1347 client = boto3.client("ssm", region_name="us-east-1")1348 ssm_document = "AWS-RunShellScript"1349 params = {"commands": ["#!/bin/bash\necho 'hello world'"]}1350 response = client.send_command(1351 InstanceIds=["i-123456", "i-234567", "i-345678"],1352 DocumentName=ssm_document,1353 Parameters=params,1354 OutputS3Region="us-east-2",1355 OutputS3BucketName="the-bucket",1356 OutputS3KeyPrefix="pref",1357 )1358 cmd = response["Command"]1359 cmd_id = cmd["CommandId"]1360 instance_id = "i-345678"1361 invocation_response = client.get_command_invocation(1362 CommandId=cmd_id, InstanceId=instance_id, PluginName="aws:runShellScript"1363 )1364 invocation_response["CommandId"].should.equal(cmd_id)1365 invocation_response["InstanceId"].should.equal(instance_id)1366 # test the error case for an invalid instance id1367 with pytest.raises(ClientError):1368 invocation_response = client.get_command_invocation(1369 CommandId=cmd_id, InstanceId="i-FAKE"1370 )1371 # test the error case for an invalid plugin name1372 with pytest.raises(ClientError):1373 invocation_response = client.get_command_invocation(1374 CommandId=cmd_id, InstanceId=instance_id, PluginName="FAKE"1375 )1376@mock_ec21377@mock_ssm1378def test_get_command_invocations_by_instance_tag():1379 ec2 = boto3.client("ec2", region_name="us-east-1")1380 ssm = boto3.client("ssm", region_name="us-east-1")1381 tag_specifications = [1382 {"ResourceType": "instance", "Tags": [{"Key": "Name", "Value": "test-tag"}]}1383 ]1384 num_instances = 31385 resp = ec2.run_instances(1386 ImageId=EXAMPLE_AMI_ID,1387 MaxCount=num_instances,1388 MinCount=num_instances,1389 TagSpecifications=tag_specifications,1390 )1391 instance_ids = []1392 for instance in resp["Instances"]:1393 instance_ids.append(instance["InstanceId"])1394 instance_ids.should.have.length_of(num_instances)1395 command_id = ssm.send_command(1396 DocumentName="AWS-RunShellScript",1397 Targets=[{"Key": "tag:Name", "Values": ["test-tag"]}],1398 )["Command"]["CommandId"]1399 resp = ssm.list_commands(CommandId=command_id)1400 resp["Commands"][0]["TargetCount"].should.equal(num_instances)1401 for instance_id in instance_ids:1402 resp = ssm.get_command_invocation(CommandId=command_id, InstanceId=instance_id)1403 resp["Status"].should.equal("Success")1404@mock_ssm1405def test_parameter_version_limit():1406 client = boto3.client("ssm", region_name="us-east-1")1407 parameter_name = "test-param"1408 for i in range(PARAMETER_VERSION_LIMIT + 1):1409 client.put_parameter(1410 Name=parameter_name,1411 Value="value-%d" % (i + 1),1412 Type="String",1413 Overwrite=True,1414 )1415 paginator = client.get_paginator("get_parameter_history")1416 page_iterator = paginator.paginate(Name=parameter_name)1417 parameter_history = list(1418 item for page in page_iterator for item in page["Parameters"]1419 )1420 len(parameter_history).should.equal(PARAMETER_VERSION_LIMIT)1421 parameter_history[0]["Value"].should.equal("value-2")1422 latest_version_index = PARAMETER_VERSION_LIMIT - 11423 latest_version_value = "value-%d" % (PARAMETER_VERSION_LIMIT + 1)1424 parameter_history[latest_version_index]["Value"].should.equal(latest_version_value)1425@mock_ssm1426def test_parameter_overwrite_fails_when_limit_reached_and_oldest_version_has_label():1427 client = boto3.client("ssm", region_name="us-east-1")1428 parameter_name = "test-param"1429 for i in range(PARAMETER_VERSION_LIMIT):1430 client.put_parameter(1431 Name=parameter_name,1432 Value="value-%d" % (i + 1),1433 Type="String",1434 Overwrite=True,1435 )1436 client.label_parameter_version(1437 Name=parameter_name, ParameterVersion=1, Labels=["test-label"]1438 )1439 with pytest.raises(ClientError) as ex:1440 client.put_parameter(1441 Name=parameter_name, Value="new-value", Type="String", Overwrite=True,1442 )1443 error = ex.value.response["Error"]1444 error["Code"].should.equal("ParameterMaxVersionLimitExceeded")1445 error["Message"].should.contain(parameter_name)1446 error["Message"].should.contain("Version 1")1447 error["Message"].should.match(1448 r"the oldest version, can't be deleted because it has a label associated with it. Move the label to another version of the parameter, and try again."...

Full Screen

Full Screen

change.py

Source:change.py Github

copy

Full Screen

...13 # Set label - latest14 changed_parameter = client.get_parameter(15 Name=event['detail']['name'], WithDecryption=False16 )17 client.label_parameter_version(18 Name=event['detail']['name'],19 ParameterVersion=changed_parameter['Parameter']['Version'],20 Labels=[LABEL_LATEST]21 )...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful