Best Python code snippet using localstack_python
test_ssm.py
Source:test_ssm.py
...7@mock_ssm8def test_list_no_arg():9 conn = boto3.client('ssm')10 # Create two parameters and check both are returned11 conn.put_parameter(Name='test', Value='testing123', Type='SecureString')12 conn.put_parameter(Name='test1', Value='testing123', Type='SecureString')13 # Call list without any parameters14 out = ssm.list_params([])15 names = [param['Name'] for param in out]16 assert len(out) == 217 assert 'test1' in names18 assert 'test' in names19@mock_ssm20def test_list_starts_with():21 conn = boto3.client('ssm')22 # Create two parameters and check both are returned23 conn.put_parameter(Name='test1', Value='testing123', Type='SecureString')24 conn.put_parameter(Name='anotherValue', Value='testing123', Type='SecureString')25 out = ssm.list_params(["test"])26 names = [param['Name'] for param in out]27 assert len(out) == 128 assert 'test1' in names29 assert 'anotherValue' not in names30@mock_ssm31def test_delete_correct():32 conn = boto3.client('ssm')33 # Create a parameter and check if it is deleted.34 conn.put_parameter(Name='test1', Value='testing123', Type='SecureString')35 out, err = ssm.delete_params(['test1'])36 assert len(err) == 037 assert len(out) == 138 assert 'test1' in out39 # Check if parameter is actually deleted40 out = ssm.list_params(['test1'])41 assert len(out) == 042@mock_ssm43def test_delete_invalid():44 # Delete an invalid parameter45 out, err = ssm.delete_params(['InvalidParam'])46 assert len(out) == 047 assert len(err) == 148 assert 'InvalidParam' in err49@mock_ssm50def test_get_param_correct():51 conn = boto3.client('ssm')52 # Put 2 parameters53 conn.put_parameter(Name='test1', Value='testing1', Type='SecureString')54 conn.put_parameter(Name='test1', Value='testing2', Type='SecureString')55 # get the parameter56 out, err = ssm.get_params(['test1'])57 # Check output58 assert len(out) == 159 assert len(err) == 060 assert out[0]['Name'] == 'test1'61 assert out[0]['Value'] == 'testing1'62@mock_ssm63def test_get_param_incorrect():64 conn = boto3.client('ssm')65 # Put 2 parameters66 conn.put_parameter(Name='test1', Value='testing1', Type='SecureString')67 conn.put_parameter(Name='test2', Value='testing2', Type='SecureString')68 # get the parameter69 out, err = ssm.get_params(['test1', 'test3'])70 # Check output71 assert len(out) == 172 assert len(err) == 173 assert out[0]['Name'] == 'test1'74 assert out[0]['Value'] == 'testing1'75 # Check error76 assert err[0] == 'test3'77@mock_ssm78def test_put_param():79 # Put 1 parameter80 ssm.put_param(name="test1", value="value1", description='my var', encrypt=True)81 # get the parameter82 out, err = ssm.get_params(['test1'])83 # Check parameter is returned84 assert len(out) == 185 assert len(err) == 086 assert out[0]['Name'] == 'test1'87 assert out[0]['Value'] == 'value1'88@mock_ssm89def test_cli_list_no_arg():90 conn = boto3.client('ssm')91 # Create two parameters and check both are returned92 conn.put_parameter(Name='test1', Value='testing1', Type='SecureString')93 conn.put_parameter(Name='test2', Value='testing2', Type='SecureString')94 # Call list without any parameters95 runner = CliRunner()96 result = runner.invoke(ssm.list)97 assert "No parameters found" not in result.output98 assert 'test1' in result.output99 assert 'test2' in result.output100@mock_ssm101def test_cli_list_starts_with():102 conn = boto3.client('ssm')103 # Create two parameters and check both are returned104 conn.put_parameter(Name='test1', Value='testing123', Type='SecureString')105 conn.put_parameter(Name='anotherValue', Value='testing123', Type='SecureString')106 runner = CliRunner()107 result = runner.invoke(ssm.list, ['--name', 'test'])108 assert 'test1' in result.output109 assert 'anotherValue' not in result.output110@mock_ssm111def test_cli_get_param():112 conn = boto3.client('ssm')113 # Create two parameters and check both are returned114 conn.put_parameter(Name='test1', Value='value1', Type='SecureString')115 conn.put_parameter(Name='test2', Value='value2', Type='SecureString')116 runner = CliRunner()117 result = runner.invoke(ssm.get, ['--name', 'test1'])118 assert 'Invalid Parameters' not in result.output119 assert 'value1' in result.output120 assert 'test1' in result.output121 assert 'test2' not in result.output122 assert 'value2' not in result.output123@mock_ssm124def test_cli_delete_correct():125 conn = boto3.client('ssm')126 # Create a parameter and check if it is deleted.127 conn.put_parameter(Name='test1', Value='testing123', Type='SecureString')128 runner = CliRunner()129 result = runner.invoke(ssm.delete, ['--name', 'test1'])130 assert 'Deleted Parameters' in result.output131 assert 'test1' in result.output132 # Check if parameter is actually deleted133 runner = CliRunner()134 result = runner.invoke(ssm.list, ['--name', 'test1'])135 assert 'No parameters found' in result.output136@mock_ssm137def test_cli_delete_invalid():138 # Delete an invalid parameter139 runner = CliRunner()140 result = runner.invoke(ssm.delete, ['--name', 'invalid_param'])141 assert 'Invalid Parameters' in result.output142 assert 'invalid_param' in result.output143 assert 'Deleted Parameters' not in result.output144@mock_ssm145def test_cli_put_param():146 # Create a parameter147 runner = CliRunner()148 result = runner.invoke(ssm.put, ['--name', 'test1', '--value', 'value1', '--encrypt'])149 assert 'Created Parameters' in result.output150 assert 'test' in result.output151@mock_ssm152def test_cli_file_list():153 # Create a temp file154 test_file = tempfile.NamedTemporaryFile(delete=False, mode='w')155 conn = boto3.client('ssm')156 conn.put_parameter(Name='test1', Value='val', Type='SecureString')157 conn.put_parameter(Name='test2', Value='val', Type='SecureString')158 test_file.write('''159list:160 - test''')161 test_file.close()162 runner = CliRunner()163 result = runner.invoke(ssm.from_file, ['--path', test_file.name])164 output_yaml = yaml.load(result.output)165 assert 'list' in output_yaml.keys()166 assert len(output_yaml['list']) == 2167 assert 'test1' in [param['Name'] for param in output_yaml['list']]168 assert 'test2' in [param['Name'] for param in output_yaml['list']]169@mock_ssm170def test_file_delete():171 # Create a temp file172 test_file = tempfile.NamedTemporaryFile(delete=False, mode='w')173 conn = boto3.client('ssm')174 conn.put_parameter(Name='test1', Value='val', Type='SecureString')175 test_file.write('''176delete:177 - test1178 - invalid''')179 test_file.close()180 runner = CliRunner()181 result = runner.invoke(ssm.from_file, ['--path', test_file.name])182 output_yaml = yaml.load(result.output)183 assert 'delete' in output_yaml.keys()184 assert len(output_yaml['delete']['DeletedParameters']) == 1185 assert output_yaml['delete']['DeletedParameters'][0] == 'test1'186 assert len(output_yaml['delete']['InvalidParameters']) == 1187 assert output_yaml['delete']['InvalidParameters'][0] == 'invalid'188@mock_ssm189def test_file_get():190 # Create a temp file191 test_file = tempfile.NamedTemporaryFile(delete=False, mode='w')192 conn = boto3.client('ssm')193 conn.put_parameter(Name='test1', Value='val', Type='SecureString')194 test_file.write('''195get:196 - test1197 - invalid''')198 test_file.close()199 runner = CliRunner()200 result = runner.invoke(ssm.from_file, ['--path', test_file.name])201 output_yaml = yaml.load(result.output)202 assert 'get' in output_yaml.keys()203 assert len(output_yaml['get']['GetParameters']) == 1204 assert output_yaml['get']['GetParameters'][0]['Name'] == 'test1'205 assert len(output_yaml['get']['InvalidParameters']) == 1206 assert output_yaml['get']['InvalidParameters'][0] == 'invalid'207@mock_ssm...
test_parameter_store.py
Source:test_parameter_store.py
...9# TODO: moto does not support pagination for get_parameters_by_path (similar test below by prefix). Maybe use stubber.10@mock_ssm11def test_get_values_by_path():12 client = boto3.client('ssm')13 client.put_parameter(Name='/service1/key1', Value='value1_1', Type='SecureString')14 client.put_parameter(Name='/service1/key2', Value='value1_2', Type='SecureString')15 client.put_parameter(Name='/service2/key1', Value='value2_1', Type='SecureString')16 values = ParameterStore().get_values_by_path('service1')17 assert len(values) is 218 _assert_key_value(values, '/service1/key1', 'value1_1')19 _assert_key_value(values, '/service1/key2', 'value1_2')20@mock_ssm21def test_get_values_by_path_with_empty_path():22 with raises(ParamValidationError, message='Invalid length for parameter Path'):23 ParameterStore().get_values_by_path(' ')24@mock_ssm25def test_get_values():26 client = boto3.client('ssm')27 client.put_parameter(Name='/service1/key1', Value='value1_1', Type='SecureString')28 client.put_parameter(Name='/service1/key2', Value='value1_2', Type='SecureString')29 client.put_parameter(Name='/service2/key1', Value='value2_1', Type='SecureString')30 values = ParameterStore().get_values('/service1/key1', '/service2/key1')31 assert len(values) is 232 _assert_key_value(values, '/service1/key1', 'value1_1')33 _assert_key_value(values, '/service2/key1', 'value2_1')34@mock_ssm35def test_get_value():36 client = boto3.client('ssm')37 client.put_parameter(Name='/service1/key1', Value='value1_1', Type='SecureString')38 client.put_parameter(Name='/service1/key2', Value='value1_2', Type='SecureString')39 client.put_parameter(Name='/service2/key1', Value='value2_1', Type='SecureString')40 value = ParameterStore().get_value('/service1/key2')41 assert value == 'value1_2'42@mock_ssm43def test_get_values_with_empty_key_list():44 values = ParameterStore().get_values()45 assert len(values) is 046@mock_ssm47def test_get_values_by_prefix():48 client = boto3.client('ssm')49 client.put_parameter(Name='service1_key1', Value='value1_1', Type='SecureString')50 client.put_parameter(Name='/service1/key2', Value='value1_2', Type='SecureString')51 client.put_parameter(Name='/service2/key1', Value='value2_1', Type='SecureString')52 values = ParameterStore().get_values_by_prefix('service1')53 assert len(values) is 154 _assert_key_value(values, 'service1_key1', 'value1_1')55@mock_ssm56def test_get_values_by_prefix_with_pagination():57 client = boto3.client('ssm')58 for i in range(1, 13):59 client.put_parameter(Name='service1_key{0}'.format(i), Value='value1_{0}'.format(i), Type='SecureString')60 values = ParameterStore().get_values_by_prefix('service1')61 assert len(values) is 1262 for i in range(1, 12):63 _assert_key_value(values, 'service1_key{0}'.format(i), 'value1_{0}'.format(i))64def _assert_key_value(dictionary, key, value):65 assert key in dictionary...
create_ssm_parameters.py
Source:create_ssm_parameters.py
...11 params = input_values()12 if not confirm():13 return14 ssm = boto3.client("ssm")15 put_parameter(16 name="/GithubPrNotification/GithubToken",17 value=params.github_token,18 is_secure=True,19 ssm=ssm,20 )21 put_parameter(22 name="/GithubPrNotification/WebhookUrl",23 value=params.slack_webhook_url,24 is_secure=False,25 ssm=ssm,26 )27 print('finish: set parameters ')28def input_values() -> Parameters:29 print("input parameters.\n")30 github_token = prompt("github token: ").strip()31 slack_webhook_url = prompt("slack webhook url: ").strip()32 return Parameters(github_token, slack_webhook_url)33def confirm() -> bool:34 value = prompt("\ncreate parameters? [y/N]: ")35 return value.lower() in {"y", "yes"}36def put_parameter(name: str, value: str, is_secure: bool, ssm: SSMClient):37 if is_secure:38 ssm.put_parameter(Name=name, Value=value, Type="SecureString", Overwrite=True)39 else:40 ssm.put_parameter(Name=name, Value=value, Type="String", Overwrite=True)41if __name__ == "__main__":...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!